Mercurial > hg > RemoteEditor > REPSessionManager
changeset 40:5c0cd6c01ba8
new file SessionXMLEncoder and SessionXMLDecoder
author | pin |
---|---|
date | Sat, 10 Nov 2007 21:12:01 +0900 |
parents | c873ee0318f8 |
children | 86a1553028ad |
files | rep/xml/SessionXMLDecoder.java rep/xml/SessionXMLEncoder.java |
diffstat | 2 files changed, 112 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/xml/SessionXMLDecoder.java Sat Nov 10 21:12:01 2007 +0900 @@ -0,0 +1,5 @@ +package rep.xml; + +public class SessionXMLDecoder { + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/xml/SessionXMLEncoder.java Sat Nov 10 21:12:01 2007 +0900 @@ -0,0 +1,107 @@ +package rep.xml; + +import java.io.StringWriter; +import java.util.LinkedList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +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 rep.Editor; +import rep.Session; +import rep.SessionList; + +public class SessionXMLEncoder { + private LinkedList<Session> list; + + public SessionXMLEncoder(SessionList list) { + } + + public SessionXMLEncoder(LinkedList<Session> session) { + this.list = session; + } + + public SessionXMLEncoder() { + } + + public String sessionListToXML() { + /* SessionListをXMLに書き出す。ときの形式 + * <Session> + * <editor> + * <sid/> + * <host/> + * <port/> + * <filename/> + * </editor> + * <editor> + * <sid/> + * <host/> + * <port/> + * </editor> + * </Session> + * <SessionManager> + * <host/> + * <port/> + * </SessionManager> + * <Session> + * </Session> + */ + StringWriter str = null; + try { + str = new StringWriter(); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.newDocument(); + Element root = doc.getDocumentElement(); + root = doc.createElement("SessionList"); + for(Session session : list){ + Element elementSession = doc.createElement("Session"); + root.appendChild(elementSession); +// Element elementeditor = doc.createElement("editor"); +// elementSession.appendChild(elementeditor); +// Editor editor = session.getMaster(); +// Element element3 = doc.createElement(editor.getHost()); +// Element element4 = doc.createElement(editor.getPort()); +// Element element5 = doc.createElement(editor.getName()); + //element5 = doc.createElement(session.getName()); +// elementeditor.appendChild(element3); +// elementeditor.appendChild(element4); +// elementeditor.appendChild(element5); + LinkedList <Editor> editorlist = session.getEditorList(); + for(Editor editor : editorlist){ + Element elementEditor = doc.createElement("Editor"); + Element elementHost = doc.createElement(editor.getHost()); + Element elementPort = doc.createElement(editor.getPort()); + Element elementName = null; + if(editor.getName() != null) { + elementName = doc.createElement(editor.getName()); + elementEditor.appendChild(elementName); + } + elementEditor.appendChild(elementHost); + elementEditor.appendChild(elementPort); +// elementEditor.appendChild(elementName); + elementSession.appendChild(elementEditor); + } + + } + doc.appendChild(root); + + TransformerFactory tfactory = TransformerFactory.newInstance(); + Transformer transformer = tfactory.newTransformer(); + StreamResult result = new StreamResult(str); + //transformer.transform(new DOMSource(doc), new StreamResult(System.out)); + transformer.transform(new DOMSource(doc), result); + System.out.println(str); + + }catch (Exception e){ + e.printStackTrace(); + } + return str.toString(); + } +}