Mercurial > hg > RemoteEditor > REPSessionManager
view rep/xml/SessionXMLEncoder.java @ 382:4b87f89b3afd
REP Session Manager (Java version)
new structure
author | one@firefly.cr.ie.u-ryukyu.ac.jp |
---|---|
date | Mon, 10 Nov 2008 22:07:45 +0900 |
parents | fa041bae35f1 |
children |
line wrap: on
line source
package rep.xml; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; 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 rep.Session; import rep.SessionList; import rep.handler.REPNode; public class SessionXMLEncoder { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); TransformerFactory tfactory = TransformerFactory.newInstance(); public String sessionListToXML(SessionList list) { /* SessionListをXMLに書き出す。ときの形式 * <?xml version="1.0" encoding="UTF-8"?> * <SessionList><host>naha.ie.u-ryukyu.ac.jp</host> * <Session sid="1"> * <Editor eid="1"> * <host>firefly.cr.ie.u-ryukyu.ac.jp</host> * <port/> * <file>Test.java</file> * </Editor> * <Editor eid="1"> * <host>firefly.cr.ie.u-ryukyu.ac.jp</host> * <port/> * <file>Test.java</file> * </Editor> * <Editor eid="2"> * <host>teto.cr.ie.u-ryukyu.ac.jp</host> * <port/> * </Editor> * </Session> * </SessionList> */ StringWriter str = new StringWriter(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { return ""; } Document doc = builder.newDocument(); Element root = doc.getDocumentElement(); root = doc.createElement("SessionList"); // // Element elementSeMaHost = doc.createElement("host"); // if(sessionmanagerHost != null) elementSeMaHost.setTextContent(sessionmanagerHost); // root.appendChild(elementSeMaHost); for(Session session : list.values()){ Element elementSession = doc.createElement("Session"); root.appendChild(elementSession); elementSession.setAttribute("sid", Integer.toString(session.getSID())); for(REPNode editor : session.getEditorList()){ Element elementEditor = doc.createElement("Editor"); elementEditor.setAttribute("eid", Integer.toString(editor.getEID())); elementSession.appendChild(elementEditor); Element elementHost = doc.createElement("host"); elementEditor.appendChild(elementHost); elementHost.setTextContent(editor.getHost()); Element elementPort = doc.createElement("port"); elementEditor.appendChild(elementPort); //elementPort.setTextContent(editor.getPort()); if(editor.getName() != null){ Element elementName = doc.createElement("file"); elementEditor.appendChild(elementName); elementName.setTextContent(editor.getName()); //System.out.println(editor.getName()); } } } doc.appendChild(root); Transformer transformer; StreamResult result = new StreamResult(str); try { transformer = tfactory.newTransformer(); transformer.transform(new DOMSource(doc), result); } catch (TransformerConfigurationException e1) { return ""; } catch (TransformerException e) { return ""; } //System.out.println(str.toString()); return str.toString(); } }