annotate rep/xml/SessionXMLDecoder.java @ 74:391d44c94799

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