package cust.com.rac.integration;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;

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

public class test {

	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub

//		String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"+
//				"<result>"+
//				    "<errmsg>失败返回错误描述；成功返回空</errmsg>"+
//				    "<sn>单号</sn>"+
//				    "<taskid>流程ID</taskid>"+
//				"</result>";
//		String errMsg = XMLDocAPI.readXmlErrorMsg(xml);
//		System.out.println(errMsg);
		
		//createProjectPlanXML();
		String str = "鏃犳晥鐨勬枃浠";
	
		//			System.out.println(str.getBytes("UTF-8"));
		//			str = URLEncoder.encode(str, "GBK");
		//			System.out.println(str);
		//			System.out.println(URLEncoder.encode(str, "UTF-8"));
		//			ByteArrayInputStream in=new ByteArrayInputStream(str.getBytes()); 
					OutputStream os = System.out;  
					try {
						os.write(str.getBytes());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}  
					try {
						String dc  = Charset.defaultCharset().name(); 
						InputStream is = new ByteArrayInputStream(str.getBytes());  
						UnicodeInputStream uin = new UnicodeInputStream(is,dc);  
				        BufferedReader br = new BufferedReader(new InputStreamReader(uin));    
				        String line = br.readLine();    
				        while(line != null)    
				        {    
				            System.out.println("line="+line);    
				            line = br.readLine();    
				        }    
				        
						
						OutputStreamWriter osw = new OutputStreamWriter(os, dc);
						osw.write("111="+str);
						osw.close();
						os.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
					
//					System.out.println(str);
//		System.out.println(getUnicode(str));
	}

	public static String getUnicode(String source) {
        String result = "";
         
        for (int i = 0; i < source.length(); i++) {
            result += "\\u"+Integer.toHexString((int) source.charAt(i));
        }
 
        return result;
    }
	
	public static void createProjectPlanXML(){
		Document document = DocumentHelper.createDocument();
		Element FormData = document.addElement("FormData"); 
		Element pm_project_schedule = FormData.addElement("pm_project_schedule");
		pm_project_schedule.addElement("code").addText("中国");
		
		XMLWriter xmlWriter = null;
        StringWriter stringWrite = new StringWriter();
        
        OutputFormat format = OutputFormat.createPrettyPrint();  //转换字符串格式
        format.setEncoding("UTF-8");
        format.setNewlines(true);
        
      //输出字符串
        xmlWriter = new XMLWriter(stringWrite, format);
        try {
			xmlWriter.write(document);
			 xmlWriter.flush();
		        xmlWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        System.out.println(stringWrite.toString()) ;
	}
}
