package com.dev.swms.server.utils;

import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.dev.swms.server.model.interfaceLog.ResponseCode;


/**
 * @author  dev
 * @version $Id: XMLUtils.java,v 1.2 2010/06/01 09:14:16 peng.lei Exp $
 */

public class XMLUtils {
	
	/**
	 * 当前编号
	 */
	public static long seq = 1;
	/**
	 * 最大流水号
	 */
	private static int maxIndex = 999999;

	/**
	  * 去除回车�?
	  * @return - 返回处理后字符串  
	  */
	public static String formatXML(String in)
	{
		if(in == null || "".equals(in.trim())) {
			return "";
		}
		StringBuffer out = new StringBuffer();
		in = in.trim();
		int len=in.length();
		char[] ch;
		ch=new char[len+1];
		ch=in.toCharArray() ;
		for(int i=0;i<ch.length ;i++)
		{
			if(ch[i]!='\r' && ch[i]!='\t' && ch[i]!='\n')
			out.append(ch[i]);
		}
		return out.toString();
	}
	
	/**
	 * 去除回车符
	 * 
	 * @return - 返回处理后字符串
	 */
	public static String del_r(String in) {
		String out = "";
		in = in.trim();
		int len = in.length();
		char[] ch;
		ch = new char[len + 1];
		ch = in.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			if (ch[i] != '\r' && ch[i] != '\t' && ch[i] != '\n')
				out = out + ch[i];
		}
		return out;
	}
	
	/**
	 * 去除字符串左边连续的0字符
	 */
	public static String trimLeftZero(String s) {
		int index = 0;
		while (index < s.length()) {
			if (!(s.charAt(index) == '0')) {
				break;
			}
			index++;
		}
		return s.substring(index);
	}

	public static String genSeq() {
		if(seq > maxIndex) {
			seq = 1;
		}
		int bits = String.valueOf(maxIndex).length();
		StringBuffer sb = new StringBuffer();
		String temp = String.valueOf(seq);
		while(bits > temp.length()){
			sb.append("0");
			bits--;
		}
		sb.append(temp);
		seq++;
		return sb.toString();
	}
	
	public static String formatXml(Document doc) throws Exception {
		StringWriter writer = new StringWriter();
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter xmlWriter = new XMLWriter(writer, format);

		xmlWriter.write(doc);

		return writer.toString();
	}
	
	public static String genSuccessResultHelper(String responseText) {
		return genResultHelper(true, responseText);
	}
	
	public static String genFailureResultHelper(String responseText) {
		return genResultHelper(false, responseText);
	}
	
	public static String genResultHelper(boolean success, String responseText) {
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String response = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		response += "<XML>";
		if(success) {
			response += "<MSGCODE>" + ResponseCode.SUCCESS + "</MSGCODE>";
		} else {
			response += "<MSGCODE>" + ResponseCode.FAILURE + "</MSGCODE>";
		}
		response += "<MSGCONTENT>" + responseText + "</MSGCONTENT>";
		response += "<MSGNAME>RETURN" + df.format(new Date()) + genSeq() + "</MSGNAME>";
		response += "</XML>";
		return response;
	}

	public static String getWMSCode() {
		return "SWMS";
	}
	
	public static String getTMSCode() {
		return "STMS";
	}
	
}

