comparison rep/xml/SessionXMLDecoder.java @ 386:bba62c4ac323

sync-option
author one@firefly.cr.ie.u-ryukyu.ac.jp
date Mon, 10 Nov 2008 22:19:34 +0900
parents 1fca50ce3508
children 6f356d160e58
comparison
equal deleted inserted replaced
385:1fca50ce3508 386:bba62c4ac323
7 import javax.xml.parsers.DocumentBuilderFactory; 7 import javax.xml.parsers.DocumentBuilderFactory;
8 import javax.xml.parsers.ParserConfigurationException; 8 import javax.xml.parsers.ParserConfigurationException;
9 9
10 import org.w3c.dom.Document; 10 import org.w3c.dom.Document;
11 import org.w3c.dom.Element; 11 import org.w3c.dom.Element;
12 import org.w3c.dom.NamedNodeMap;
13 import org.w3c.dom.Node;
12 import org.w3c.dom.NodeList; 14 import org.w3c.dom.NodeList;
13 import org.xml.sax.InputSource; 15 import org.xml.sax.InputSource;
14 import org.xml.sax.SAXException; 16 import org.xml.sax.SAXException;
15 17
16 import rep.Session; 18 import rep.Session;
20 public class SessionXMLDecoder { 22 public class SessionXMLDecoder {
21 23
22 DocumentBuilderFactory factory; 24 DocumentBuilderFactory factory;
23 DocumentBuilder builder; 25 DocumentBuilder builder;
24 26
25 public SessionXMLDecoder(String string) throws SAXException, IOException {
26 decode(string);
27 }
28 27
29 public SessionXMLDecoder() { 28 public SessionXMLDecoder() {
30 factory = DocumentBuilderFactory.newInstance(); 29 factory = DocumentBuilderFactory.newInstance();
31 try { 30 try {
32 factory.newDocumentBuilder(); 31 builder = factory.newDocumentBuilder();
33 } catch (ParserConfigurationException e) { 32 } catch (ParserConfigurationException e) {
33 assert false;
34 } 34 }
35 } 35 }
36 36
37 public SessionList decode(String string) throws SAXException, IOException { 37 public SessionList decode(String string) throws SAXException, IOException {
38 SessionList sessionlist = null; 38 SessionList sessionlist = null;
63 63
64 private SessionList generateSessionList(Element element){ 64 private SessionList generateSessionList(Element element){
65 SessionList sessionlist = new SessionList(); 65 SessionList sessionlist = new SessionList();
66 NodeList nodelistSession = element.getElementsByTagName("Session"); 66 NodeList nodelistSession = element.getElementsByTagName("Session");
67 for(int i = 0; i < nodelistSession.getLength(); i++){ 67 for(int i = 0; i < nodelistSession.getLength(); i++){
68 Element elementSession = (Element) nodelistSession.item(i); 68 Node elementSession = nodelistSession.item(i);
69 int sid = Integer.parseInt(elementSession.getAttribute("sid")); 69 int sid = getIntValue(elementSession,"sid");
70 NodeList nodelistEditor = elementSession.getElementsByTagName("Editor"); 70 NodeList nodelistEditor = ((Element)elementSession).getElementsByTagName("Editor");
71 71
72 Session session = null; 72 Session session = null;
73 for(int j = 0; j < nodelistEditor.getLength(); j++){ 73 for(int j = 0; j < nodelistEditor.getLength(); j++){
74 String eid = ((Element)nodelistEditor.item(j)).getAttribute("eid"); 74 String eid = ((Element)nodelistEditor.item(j)).getAttribute("eid");
75 75
76 Element elementEditor = (Element) nodelistEditor.item(j); 76 Element elementEditor = (Element) nodelistEditor.item(j);
77 NodeList nodelistEditorHost = elementEditor.getElementsByTagName("host"); 77 NodeList nodelistEditorHost = elementEditor.getElementsByTagName("host");
78 Element elementHost = (Element) nodelistEditorHost.item(0); 78 Element elementHost = (Element) nodelistEditorHost.item(0);
79 String host = elementHost.getFirstChild().getNodeValue(); 79 Node hostValue = elementHost.getFirstChild();
80 String host = hostValue==null?"":hostValue.getNodeValue();
80 81
81 if(elementEditor.getChildNodes().getLength() > 2){ 82 if(elementEditor.getChildNodes().getLength() > 2){
82 NodeList nodelistEditorFile = elementEditor.getElementsByTagName("file"); 83 NodeList nodelistEditorFile = elementEditor.getElementsByTagName("file");
83 Element elementFile = (Element) nodelistEditorFile.item(0); 84 Element elementFile = (Element) nodelistEditorFile.item(0);
84 String file = elementFile.getFirstChild().getNodeValue(); 85 String file = elementFile.getFirstChild().getNodeValue();
98 } 99 }
99 } 100 }
100 } 101 }
101 return sessionlist; 102 return sessionlist;
102 } 103 }
104
105 private int getIntValue(Node elementSession, String attrName) {
106 NamedNodeMap attr = elementSession.getAttributes();
107 Node sidNode = attr.getNamedItem(attrName);
108 int sid = Integer.parseInt(sidNode.getNodeValue());
109 return sid;
110 }
111 //
112 // private String getString(Node elementSession, String attrName) {
113 // NamedNodeMap attr = elementSession.getAttributes();
114 // Node sidNode = attr.getNamedItem(attrName);
115 // return sidNode.getNodeValue();
116 // }
103 117
104 } 118 }