40
|
1 package rep.xml;
|
|
2
|
347
|
3 import java.io.IOException;
|
56
|
4 import java.io.StringReader;
|
|
5
|
|
6 import javax.xml.parsers.DocumentBuilder;
|
|
7 import javax.xml.parsers.DocumentBuilderFactory;
|
347
|
8 import javax.xml.parsers.ParserConfigurationException;
|
56
|
9
|
|
10 import org.w3c.dom.Document;
|
|
11 import org.w3c.dom.Element;
|
|
12 import org.w3c.dom.NodeList;
|
|
13 import org.xml.sax.InputSource;
|
347
|
14 import org.xml.sax.SAXException;
|
56
|
15
|
74
|
16 import rep.Editor;
|
|
17 import rep.Session;
|
56
|
18 import rep.SessionList;
|
|
19
|
40
|
20 public class SessionXMLDecoder {
|
56
|
21
|
347
|
22 DocumentBuilderFactory factory;
|
|
23 DocumentBuilder builder;
|
|
24
|
|
25 public SessionXMLDecoder(String string) throws SAXException, IOException {
|
56
|
26 decode(string);
|
|
27 }
|
|
28
|
|
29 public SessionXMLDecoder() {
|
347
|
30 factory = DocumentBuilderFactory.newInstance();
|
|
31 try {
|
|
32 factory.newDocumentBuilder();
|
|
33 } catch (ParserConfigurationException e) {
|
|
34 }
|
56
|
35 }
|
|
36
|
347
|
37 public SessionList decode(String string) throws SAXException, IOException {
|
56
|
38 SessionList sessionlist = null;
|
322
|
39 //System.out.println("");
|
347
|
40 InputSource source = new InputSource(new StringReader(string));
|
|
41 //source.setEncoding("UTF-8");
|
|
42 Document doc = builder.parse(source);
|
|
43 Element root = doc.getDocumentElement();
|
|
44
|
|
45 //sessionlist = createSessionList(root);
|
|
46 sessionlist = generateSessionList(root);
|
358
|
47 //sessionlist.setMaxHost(getSessionManagerHost(root));
|
347
|
48
|
|
49
|
56
|
50 return sessionlist;
|
|
51 }
|
347
|
52
|
358
|
53 // private String getSessionManagerHost(Element root) {
|
|
54 // NodeList nodelist = root.getChildNodes();
|
|
55 // String host = null;
|
|
56 // for(int i = 0; i < nodelist.getLength(); i++){
|
|
57 // if(nodelist.item(i).getNodeName().equals("host")){
|
|
58 // host = nodelist.item(i).getTextContent();
|
|
59 // }
|
|
60 // }
|
|
61 // return host;
|
|
62 // }
|
77
|
63
|
|
64 private SessionList generateSessionList(Element element){
|
|
65 SessionList sessionlist = new SessionList();
|
|
66 NodeList nodelistSession = element.getElementsByTagName("Session");
|
|
67 for(int i = 0; i < nodelistSession.getLength(); i++){
|
|
68 Element elementSession = (Element) nodelistSession.item(i);
|
322
|
69 int sid = Integer.parseInt(elementSession.getAttribute("sid"));
|
77
|
70 NodeList nodelistEditor = elementSession.getElementsByTagName("Editor");
|
|
71
|
|
72 Session session = null;
|
|
73 for(int j = 0; j < nodelistEditor.getLength(); j++){
|
|
74 String eid = ((Element)nodelistEditor.item(j)).getAttribute("eid");
|
|
75
|
|
76 Element elementEditor = (Element) nodelistEditor.item(j);
|
|
77 NodeList nodelistEditorHost = elementEditor.getElementsByTagName("host");
|
|
78 Element elementHost = (Element) nodelistEditorHost.item(0);
|
|
79 String host = elementHost.getFirstChild().getNodeValue();
|
|
80
|
|
81 if(elementEditor.getChildNodes().getLength() > 2){
|
|
82 NodeList nodelistEditorFile = elementEditor.getElementsByTagName("file");
|
|
83 Element elementFile = (Element) nodelistEditorFile.item(0);
|
|
84 String file = elementFile.getFirstChild().getNodeValue();
|
|
85
|
317
|
86 Editor editor = new Editor(null, false, 0);
|
179
|
87 editor.setHost(host);/* editor.setPort(port)*/; editor.setName(file); editor.setEID(Integer.parseInt(eid));
|
322
|
88 session = new Session(sid, editor);
|
77
|
89 session.addEditor(editor);
|
|
90 sessionlist.addSession(session);
|
|
91
|
|
92 }else {
|
317
|
93 Editor editor = new Editor(null, false, 0);
|
179
|
94 editor.setHost(host);/* editor.setPort(port)*/; editor.setName(null); editor.setEID(Integer.parseInt(eid));
|
77
|
95 if(session != null){
|
|
96 session.addEditor(editor);
|
|
97 }
|
|
98 }
|
|
99 }
|
|
100 }
|
|
101 return sessionlist;
|
|
102 }
|
41
|
103
|
40
|
104 }
|