Mercurial > hg > RemoteEditor > REPSessionManager
view rep/xml/SessionXMLEncoder.java @ 217:4deaaaa6354e
*** empty log message ***
author | kono |
---|---|
date | Sun, 31 Aug 2008 00:26:35 +0900 |
parents | 763aad2da6b3 |
children | dfed28488274 |
line wrap: on
line source
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 = new LinkedList<Session>(); private String sessionmanagerHost; public SessionXMLEncoder(SessionList sessionlist) { sessionmanagerHost = sessionlist.getMaxHost(); this.list = sessionlist.getList(); } public SessionXMLEncoder(LinkedList<Session> sessionList) { this.list = sessionList; } public SessionXMLEncoder() { } public SessionXMLEncoder(Session session) { list.add(session); } public String sessionListToXML() { /* SessionListをXMLに書き出す。ときの形式 * <Session> * <editor> * <sid> 1 </sid> * <host> firefly </host> * <port> 5555 </port> * <filename> text.java </filename> * </editor> * <editor> * <sid> 2 </sid> * <host> asbel </host> * <port> 6666 </port> * </editor> * </Session> * <SessionManager> * <host> karateka </host> * <port> 7777 </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"); Element elementSeMaHost = doc.createElement("host"); if(sessionmanagerHost != null) elementSeMaHost.setTextContent(sessionmanagerHost); root.appendChild(elementSeMaHost); for(Session session : list){ Element elementSession = doc.createElement("Session"); root.appendChild(elementSession); LinkedList <Editor> editorlist = session.getEditorList(); for(Editor editor : editorlist){ 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); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer transformer = tfactory.newTransformer(); StreamResult result = new StreamResult(str); transformer.transform(new DOMSource(doc), result); }catch (Exception e){ e.printStackTrace(); } System.out.println(str.toString()); return str.toString(); } }