view rep/xml/SessionXMLDecoder.java @ 179:763aad2da6b3

*** empty log message ***
author pin
date Thu, 28 Aug 2008 22:53:05 +0900
parents 617a47cb0150
children a187180e0106
line wrap: on
line source

package rep.xml;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.channels.SocketChannel;
import java.util.LinkedList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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 org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import rep.Editor;
import rep.Session;
import rep.SessionList;
import rep.channel.REPSocketChannel;

public class SessionXMLDecoder {

	private REPSocketChannel channel;

	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);
			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();
				
				NodeList nodelistEditorPort = elementEditor.getElementsByTagName("port");
				Element elementPort = (Element) nodelistEditorPort.item(0);
				String port = elementPort.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();
					editor.setHost(host);/* editor.setPort(port)*/; editor.setName(file); editor.setEID(Integer.parseInt(eid)); 
					session = new Session(editor);
					session.addEditor(editor);
					sessionlist.addSession(session);
					
				}else {
					Editor editor = new Editor();
					editor.setHost(host);/* editor.setPort(port)*/; editor.setName(null); editor.setEID(Integer.parseInt(eid));
					if(session != null){
						session.addEditor(editor);
						//sessionlist.addSession(session);
					}
				}
			}
		}
		return sessionlist;
	}

	private SessionList createSessionList(Element root){
		NodeList nodelistSession = root.getChildNodes();
		SessionList sessionlist = new SessionList();
		
		for(int i = 0; i < nodelistSession.getLength(); i++){
			Node nodeSession = nodelistSession.item(i);
			NodeList nodelistEditor = nodeSession.getChildNodes();
			Session session = null;
			
			for(int j = 0; j < nodelistEditor.getLength(); j++){
				Node nodeEditor = nodelistEditor.item(j);
				NodeList nodelistEditorInfo = nodeEditor.getChildNodes();
				
				Editor editor = new Editor();
				String host = nodelistEditorInfo.item(0).getTextContent();
				editor.setHost(host);
				String port = nodelistEditorInfo.item(1).getTextContent();
//				editor.setPort(port);
				if(nodelistEditorInfo.getLength() == 3){
					String name = nodelistEditorInfo.item(2).getTextContent();
					editor.setName(name);
					session = new Session(editor);
				}
				//System.out.println(editor.toString());
				session.addEditor(editor);
				
			}
			sessionlist.addSession(session);

		}
		
		return sessionlist;
	}

	/*
	private void seachNode(NodeList list) {
		String[] host_port_name = new String[3];
		for(int i = 0; i < list.getLength(); i++){
			Element element = null;
			if(list.item(i) instanceof Element) {
				element = (Element) list.item(i);
				System.out.println(element.getNodeName());
			}else{
				String string = list.item(i).getNodeValue();
				sessionListSize += string;
				host_port_name[i] = string;
				System.out.println("    " + i + ":" + string);
			}
			if(element != null) seachNode(element.getChildNodes());
		}
	}
	*/
	
}