view rep/xml/SessionXMLDecoder.java @ 322:5893fd8c0f50

*** empty log message ***
author kono
date Fri, 10 Oct 2008 16:18:03 +0900
parents c83a3faec487
children 86935b872385
line wrap: on
line source

package rep.xml;

import java.io.StringReader;

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

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

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

public class SessionXMLDecoder {

	public SessionXMLDecoder(String string) {
		decode(string);
	}

	public SessionXMLDecoder() {
	}

	public SessionList decode(String string) {
		SessionList sessionlist = null;
		//System.out.println("");
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			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));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		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++){
			Element elementSession = (Element) nodelistSession.item(i);
			int sid = Integer.parseInt(elementSession.getAttribute("sid"));
			NodeList nodelistEditor = 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); 
				String host = elementHost.getFirstChild().getNodeValue();
				
				if(elementEditor.getChildNodes().getLength() > 2){
					NodeList nodelistEditorFile = elementEditor.getElementsByTagName("file");
					Element elementFile = (Element) nodelistEditorFile.item(0); 
					String file = elementFile.getFirstChild().getNodeValue();
					
					Editor editor = new Editor(null, false, 0);
					editor.setHost(host);/* editor.setPort(port)*/; editor.setName(file); editor.setEID(Integer.parseInt(eid)); 
					session = new Session(sid, editor);
					session.addEditor(editor);
					sessionlist.addSession(session);
					
				}else {
					Editor editor = new Editor(null, false, 0);
					editor.setHost(host);/* editor.setPort(port)*/; editor.setName(null); editor.setEID(Integer.parseInt(eid));
					if(session != null){
						session.addEditor(editor);
					}
				}
			}
		}
		return sessionlist;
	}
	
}