Mercurial > hg > RemoteEditor > REPSessionManager
view rep/xml/SessionXMLDecoder.java @ 74:391d44c94799
*** empty log message ***
author | pin |
---|---|
date | Sun, 25 Nov 2007 02:26:23 +0900 |
parents | 6ead43b2475e |
children | eb78312ea831 |
line wrap: on
line source
package rep.xml; import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.io.StringReader; import java.io.StringWriter; import java.nio.channels.SocketChannel; import java.util.LinkedList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import rep.Editor; import rep.Session; import rep.SessionList; public class SessionXMLDecoder { private SocketChannel channel; public SessionXMLDecoder(String string) { decode(string); } public SessionXMLDecoder() { } public SessionList decode(String string) { SessionList sessionlist = null; System.out.println(""); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource source = new InputSource(new StringReader(string)); source.setEncoding("UTF-8"); Document doc = builder.parse(source); Element root = doc.getDocumentElement(); //System.out.println(root.getTagName()); sessionlist = createSessionList(root); } catch (Exception e) { e.printStackTrace(); } return sessionlist; } SessionList createSessionList(Element root){ NodeList nodelistSession = root.getChildNodes(); SessionList sessionlist = new SessionList(); for(int i = 0; i < nodelistSession.getLength(); i++){ Node nodeSession = nodelistSession.item(i); NodeList nodelistEditor = nodeSession.getChildNodes(); Session session = null; for(int j = 0; j < nodelistEditor.getLength(); j++){ Node nodeEditor = nodelistEditor.item(j); NodeList nodelistEditorInfo = nodeEditor.getChildNodes(); Editor editor = new Editor(); String host = nodelistEditorInfo.item(0).getTextContent(); editor.setHost(host); String port = nodelistEditorInfo.item(1).getTextContent(); editor.setPort(port); if(nodelistEditorInfo.getLength() == 3){ String name = nodelistEditorInfo.item(2).getTextContent(); editor.setName(name); session = new Session(editor); } //System.out.println(editor.toString()); session.addEditor(editor); } sessionlist.addSession(session); } return sessionlist; } /* private void seachNode(NodeList list) { String[] host_port_name = new String[3]; for(int i = 0; i < list.getLength(); i++){ Element element = null; if(list.item(i) instanceof Element) { element = (Element) list.item(i); System.out.println(element.getNodeName()); }else{ String string = list.item(i).getNodeValue(); sessionListSize += string; host_port_name[i] = string; System.out.println(" " + i + ":" + string); } if(element != null) seachNode(element.getChildNodes()); } } */ }