view test/SessionXMLEncoder.java @ 39:c873ee0318f8

*** empty log message ***
author pin
date Sat, 10 Nov 2007 21:06:18 +0900
parents
children
line wrap: on
line source

package test;

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() {
		// TODO Auto-generated constructor stub
	}

	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();
			}
}