40
|
1 package rep.xml;
|
|
2
|
56
|
3 import java.io.File;
|
|
4 import java.io.InputStream;
|
|
5 import java.io.OutputStream;
|
|
6 import java.io.StringReader;
|
|
7 import java.io.StringWriter;
|
74
|
8 import java.nio.channels.SocketChannel;
|
|
9 import java.util.LinkedList;
|
56
|
10
|
|
11 import javax.xml.parsers.DocumentBuilder;
|
|
12 import javax.xml.parsers.DocumentBuilderFactory;
|
|
13 import javax.xml.parsers.ParserConfigurationException;
|
|
14 import javax.xml.transform.Transformer;
|
|
15 import javax.xml.transform.TransformerFactory;
|
|
16 import javax.xml.transform.dom.DOMSource;
|
|
17 import javax.xml.transform.stream.StreamResult;
|
|
18
|
|
19 import org.w3c.dom.Document;
|
|
20 import org.w3c.dom.Element;
|
|
21 import org.w3c.dom.Node;
|
|
22 import org.w3c.dom.NodeList;
|
|
23 import org.xml.sax.InputSource;
|
|
24
|
74
|
25 import rep.Editor;
|
|
26 import rep.Session;
|
56
|
27 import rep.SessionList;
|
|
28
|
40
|
29 public class SessionXMLDecoder {
|
56
|
30
|
74
|
31 private SocketChannel channel;
|
56
|
32
|
|
33 public SessionXMLDecoder(String string) {
|
|
34 decode(string);
|
|
35 }
|
|
36
|
|
37 public SessionXMLDecoder() {
|
|
38 }
|
|
39
|
|
40 public SessionList decode(String string) {
|
|
41 SessionList sessionlist = null;
|
|
42 System.out.println("");
|
|
43 try {
|
|
44 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
45 DocumentBuilder builder = factory.newDocumentBuilder();
|
|
46 InputSource source = new InputSource(new StringReader(string));
|
|
47 source.setEncoding("UTF-8");
|
|
48 Document doc = builder.parse(source);
|
|
49 Element root = doc.getDocumentElement();
|
|
50
|
74
|
51 //System.out.println(root.getTagName());
|
56
|
52
|
74
|
53 sessionlist = createSessionList(root);
|
56
|
54
|
|
55
|
|
56 } catch (Exception e) {
|
|
57 e.printStackTrace();
|
|
58 }
|
|
59 return sessionlist;
|
|
60 }
|
74
|
61
|
|
62 SessionList createSessionList(Element root){
|
|
63 NodeList nodelistSession = root.getChildNodes();
|
|
64 SessionList sessionlist = new SessionList();
|
|
65
|
|
66 for(int i = 0; i < nodelistSession.getLength(); i++){
|
|
67 Node nodeSession = nodelistSession.item(i);
|
|
68 NodeList nodelistEditor = nodeSession.getChildNodes();
|
|
69 Session session = null;
|
|
70
|
|
71 for(int j = 0; j < nodelistEditor.getLength(); j++){
|
|
72 Node nodeEditor = nodelistEditor.item(j);
|
|
73 NodeList nodelistEditorInfo = nodeEditor.getChildNodes();
|
|
74
|
|
75 Editor editor = new Editor();
|
|
76 String host = nodelistEditorInfo.item(0).getTextContent();
|
|
77 editor.setHost(host);
|
|
78 String port = nodelistEditorInfo.item(1).getTextContent();
|
|
79 editor.setPort(port);
|
|
80 if(nodelistEditorInfo.getLength() == 3){
|
|
81 String name = nodelistEditorInfo.item(2).getTextContent();
|
|
82 editor.setName(name);
|
|
83 session = new Session(editor);
|
|
84 }
|
|
85 //System.out.println(editor.toString());
|
|
86 session.addEditor(editor);
|
|
87
|
|
88 }
|
|
89 sessionlist.addSession(session);
|
56
|
90
|
74
|
91 }
|
|
92
|
|
93 return sessionlist;
|
|
94 }
|
|
95
|
|
96 /*
|
56
|
97 private void seachNode(NodeList list) {
|
|
98 String[] host_port_name = new String[3];
|
|
99 for(int i = 0; i < list.getLength(); i++){
|
|
100 Element element = null;
|
|
101 if(list.item(i) instanceof Element) {
|
|
102 element = (Element) list.item(i);
|
|
103 System.out.println(element.getNodeName());
|
|
104 }else{
|
|
105 String string = list.item(i).getNodeValue();
|
|
106 sessionListSize += string;
|
|
107 host_port_name[i] = string;
|
|
108 System.out.println(" " + i + ":" + string);
|
|
109 }
|
|
110 if(element != null) seachNode(element.getChildNodes());
|
|
111 }
|
|
112 }
|
74
|
113 */
|
41
|
114
|
40
|
115 }
|