view rep/xml/SessionXMLDecoder.java @ 502:49b689b17d06 default tip

merged TestEditor to REPEditor
author suika6039
date Tue, 21 Dec 2010 18:01:15 +0900
parents 6f356d160e58
children
line wrap: on
line source

package rep.xml;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import rep.Session;
import rep.SessionList;
import rep.handler.Editor;

public class SessionXMLDecoder {

	DocumentBuilderFactory factory;
	DocumentBuilder builder;


	public SessionXMLDecoder() {
		factory = DocumentBuilderFactory.newInstance();
		try {
			builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			assert false;
		}
	}

	public SessionList decode(String string) throws SAXException, IOException {
		SessionList sessionlist = null;
		//System.out.println("");
		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));


		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++){
			Node elementSession = nodelistSession.item(i);
			int sid = getIntValue(elementSession,"sid");
			NodeList nodelistEditor = ((Element)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); 
				Node hostValue = elementHost.getFirstChild();
				String host = hostValue==null?"":hostValue.getNodeValue();
				
				if(elementEditor.getChildNodes().getLength() > 2){
					NodeList nodelistEditorFile = elementEditor.getElementsByTagName("file");
					Element elementFile = (Element) nodelistEditorFile.item(0); 
					String file = elementFile.getFirstChild().getNodeValue();
					
					int id = Integer.parseInt(eid); 
					Editor editor = new Editor(null, id);
					editor.setHost(host);/* editor.setPort(port)*/; editor.setName(file); 
					session = new Session(sid, editor);
					session.addEditor(editor);
					sessionlist.put(sid,session);
					
				}else {
					int id = Integer.parseInt(eid);
					Editor editor = new Editor(null, id);
					editor.setHost(host);/* editor.setPort(port)*/; editor.setName(null);
					if(session != null){
						session.addEditor(editor);
					}
				}
			}
		}
		return sessionlist;
	}

	private int getIntValue(Node elementSession, String attrName) {
		NamedNodeMap attr = elementSession.getAttributes();
		Node sidNode = attr.getNamedItem(attrName);
		int sid = Integer.parseInt(sidNode.getNodeValue());
		return sid;
	}
//	
//	private String getString(Node elementSession, String attrName) {
//		NamedNodeMap attr = elementSession.getAttributes();
//		Node sidNode = attr.getNamedItem(attrName);
//		return sidNode.getNodeValue();
//	}
	
}