view rep/xml/SessionXMLDecoder.java @ 359:fa041bae35f1

all code written for distributed session except gather.
author kono
date Sun, 19 Oct 2008 19:24:38 +0900
parents 034acadc0cdc
children 4b87f89b3afd
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.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

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

public class SessionXMLDecoder {

	DocumentBuilderFactory factory;
	DocumentBuilder builder;

	public SessionXMLDecoder(String string) throws SAXException, IOException {
		decode(string);
	}

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

	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++){
			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.put(sid,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;
	}
	
}