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
|
77
|
51 //sessionlist = createSessionList(root);
|
|
52 sessionlist = generateSessionList(root);
|
|
53 sessionlist.setHost(getSessionManagerHost(root));
|
56
|
54
|
|
55 } catch (Exception e) {
|
|
56 e.printStackTrace();
|
|
57 }
|
|
58 return sessionlist;
|
|
59 }
|
74
|
60
|
77
|
61 private String getSessionManagerHost(Element root) {
|
|
62 NodeList nodelist = root.getChildNodes();
|
|
63 String host = null;
|
|
64 for(int i = 0; i < nodelist.getLength(); i++){
|
|
65 if(nodelist.item(i).getNodeName().equals("host")){
|
|
66 host = nodelist.item(i).getTextContent();
|
|
67 }
|
|
68 }
|
|
69 return host;
|
|
70 }
|
|
71
|
|
72 private SessionList generateSessionList(Element element){
|
|
73 SessionList sessionlist = new SessionList();
|
|
74 NodeList nodelistSession = element.getElementsByTagName("Session");
|
|
75
|
|
76 for(int i = 0; i < nodelistSession.getLength(); i++){
|
|
77 Element elementSession = (Element) nodelistSession.item(i);
|
|
78 NodeList nodelistEditor = elementSession.getElementsByTagName("Editor");
|
|
79
|
|
80 Session session = null;
|
|
81 for(int j = 0; j < nodelistEditor.getLength(); j++){
|
|
82 String eid = ((Element)nodelistEditor.item(j)).getAttribute("eid");
|
|
83
|
|
84 Element elementEditor = (Element) nodelistEditor.item(j);
|
|
85 NodeList nodelistEditorHost = elementEditor.getElementsByTagName("host");
|
|
86 Element elementHost = (Element) nodelistEditorHost.item(0);
|
|
87 String host = elementHost.getFirstChild().getNodeValue();
|
|
88
|
|
89 NodeList nodelistEditorPort = elementEditor.getElementsByTagName("port");
|
|
90 Element elementPort = (Element) nodelistEditorPort.item(0);
|
|
91 String port = elementPort.getFirstChild().getNodeValue();
|
|
92
|
|
93 if(elementEditor.getChildNodes().getLength() > 2){
|
|
94 NodeList nodelistEditorFile = elementEditor.getElementsByTagName("file");
|
|
95 Element elementFile = (Element) nodelistEditorFile.item(0);
|
|
96 String file = elementFile.getFirstChild().getNodeValue();
|
|
97
|
|
98 Editor editor = new Editor();
|
|
99 editor.setHost(host); editor.setPort(port); editor.setName(file); editor.setEID(Integer.parseInt(eid));
|
|
100 session = new Session(editor);
|
|
101 session.addEditor(editor);
|
|
102 sessionlist.addSession(session);
|
|
103
|
|
104 }else {
|
|
105 Editor editor = new Editor();
|
|
106 editor.setHost(host); editor.setPort(port); editor.setName(null); editor.setEID(Integer.parseInt(eid));
|
|
107 if(session != null){
|
|
108 session.addEditor(editor);
|
|
109 //sessionlist.addSession(session);
|
|
110 }
|
|
111 }
|
|
112 }
|
|
113 }
|
|
114 return sessionlist;
|
|
115 }
|
|
116
|
|
117 private SessionList createSessionList(Element root){
|
74
|
118 NodeList nodelistSession = root.getChildNodes();
|
|
119 SessionList sessionlist = new SessionList();
|
|
120
|
|
121 for(int i = 0; i < nodelistSession.getLength(); i++){
|
|
122 Node nodeSession = nodelistSession.item(i);
|
|
123 NodeList nodelistEditor = nodeSession.getChildNodes();
|
|
124 Session session = null;
|
|
125
|
|
126 for(int j = 0; j < nodelistEditor.getLength(); j++){
|
|
127 Node nodeEditor = nodelistEditor.item(j);
|
|
128 NodeList nodelistEditorInfo = nodeEditor.getChildNodes();
|
|
129
|
|
130 Editor editor = new Editor();
|
|
131 String host = nodelistEditorInfo.item(0).getTextContent();
|
|
132 editor.setHost(host);
|
|
133 String port = nodelistEditorInfo.item(1).getTextContent();
|
|
134 editor.setPort(port);
|
|
135 if(nodelistEditorInfo.getLength() == 3){
|
|
136 String name = nodelistEditorInfo.item(2).getTextContent();
|
|
137 editor.setName(name);
|
|
138 session = new Session(editor);
|
|
139 }
|
|
140 //System.out.println(editor.toString());
|
|
141 session.addEditor(editor);
|
|
142
|
|
143 }
|
|
144 sessionlist.addSession(session);
|
56
|
145
|
74
|
146 }
|
|
147
|
|
148 return sessionlist;
|
|
149 }
|
|
150
|
|
151 /*
|
56
|
152 private void seachNode(NodeList list) {
|
|
153 String[] host_port_name = new String[3];
|
|
154 for(int i = 0; i < list.getLength(); i++){
|
|
155 Element element = null;
|
|
156 if(list.item(i) instanceof Element) {
|
|
157 element = (Element) list.item(i);
|
|
158 System.out.println(element.getNodeName());
|
|
159 }else{
|
|
160 String string = list.item(i).getNodeValue();
|
|
161 sessionListSize += string;
|
|
162 host_port_name[i] = string;
|
|
163 System.out.println(" " + i + ":" + string);
|
|
164 }
|
|
165 if(element != null) seachNode(element.getChildNodes());
|
|
166 }
|
|
167 }
|
74
|
168 */
|
41
|
169
|
40
|
170 }
|