Mercurial > hg > RemoteEditor > REPSessionManager
view rep/xml/SessionXMLDecoder.java @ 262:a187180e0106
*** empty log message ***
author | pin |
---|---|
date | Tue, 09 Sep 2008 18:33:48 +0900 |
parents | 763aad2da6b3 |
children | 4768984ea4bc |
line wrap: on
line source
package rep.xml; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import rep.Editor; import rep.Session; import rep.SessionList; public class SessionXMLDecoder { 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(); //sessionlist = createSessionList(root); sessionlist = generateSessionList(root); sessionlist.setMaxHost(getSessionManagerHost(root)); } catch (Exception e) { e.printStackTrace(); } return sessionlist; } private String getSessionManagerHost(Element root) { NodeList nodelist = root.getChildNodes(); String host = null; for(int i = 0; i < nodelist.getLength(); i++){ if(nodelist.item(i).getNodeName().equals("host")){ host = nodelist.item(i).getTextContent(); } } return host; } private SessionList generateSessionList(Element element){ SessionList sessionlist = new SessionList(); NodeList nodelistSession = element.getElementsByTagName("Session"); for(int i = 0; i < nodelistSession.getLength(); i++){ Element elementSession = (Element) nodelistSession.item(i); NodeList nodelistEditor = elementSession.getElementsByTagName("Editor"); Session session = null; for(int j = 0; j < nodelistEditor.getLength(); j++){ String eid = ((Element)nodelistEditor.item(j)).getAttribute("eid"); Element elementEditor = (Element) nodelistEditor.item(j); NodeList nodelistEditorHost = elementEditor.getElementsByTagName("host"); Element elementHost = (Element) nodelistEditorHost.item(0); String host = elementHost.getFirstChild().getNodeValue(); if(elementEditor.getChildNodes().getLength() > 2){ NodeList nodelistEditorFile = elementEditor.getElementsByTagName("file"); Element elementFile = (Element) nodelistEditorFile.item(0); String file = elementFile.getFirstChild().getNodeValue(); Editor editor = new Editor(); editor.setHost(host);/* editor.setPort(port)*/; editor.setName(file); editor.setEID(Integer.parseInt(eid)); session = new Session(editor); session.addEditor(editor); sessionlist.addSession(session); }else { Editor editor = new Editor(); editor.setHost(host);/* editor.setPort(port)*/; editor.setName(null); editor.setEID(Integer.parseInt(eid)); if(session != null){ session.addEditor(editor); } } } } return sessionlist; } }