package org.cytoscape.keggscape.internal.read.kgml;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.cytoscape.io.read.AbstractCyNetworkReader;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNetworkFactory;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.model.subnetwork.CyRootNetworkManager;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.CyNetworkViewFactory;
import org.cytoscape.work.TaskMonitor;
import org.cytoscape.data.reader.kgml.generated.*;

public class KeggscapeNetworkReader extends AbstractCyNetworkReader {
	
	private static final String PACKAGE_NAME = "org.cytoscape.data.reader.kgml.generated";
	private Pathway pathway;
	private CyNetwork network = null;
	private final InputStream is;
	private KGMLMapper mapper;
	
	public KeggscapeNetworkReader(InputStream is, CyNetworkViewFactory cyNetworkViewFactory,
			CyNetworkFactory cyNetworkFactory, CyNetworkManager cyNetworkManager,
			CyRootNetworkManager cyRootNetworkManager) {
		super(is, cyNetworkViewFactory, cyNetworkFactory, cyNetworkManager, cyRootNetworkManager);
		
		if (is == null) {
			throw new NullPointerException("Input Stream cannot be null.");
		}
		
		this.is = is;
	}
	
	@Override
	public CyNetworkView buildCyNetworkView(CyNetwork network) {
		final CyNetworkView view = cyNetworkViewFactory.createNetworkView(network);

		// TODO Apply (X,Y) to the nodes
		
		return view;
	}

	@Override
	public void run(TaskMonitor taskMonitor) throws Exception {
		InputStream is = null;
		pathway = null;
		
		try {
			final JAXBContext jaxbContext = JAXBContext.newInstance(PACKAGE_NAME, this.getClass().getClassLoader());
			final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
			pathway = (Pathway) unmarshaller.unmarshal(is);
		} catch (Exception e) {
			e.printStackTrace();
			throw new IOException("Could not unmarshall KGML file");
		} finally {
			if (is != null) {
				is.close();
			}
		}
		
		// TODO Auto-generated method stub
		mapper = new KGMLMapper(pathway);

	}

}

@@ -1316,32 +1316,168 @@
 %09%7D%0A%09%0A%09@Override%0A
+%09public CyNetwork%5B%5D getNetworks() %7B%0A%09%09%0A%09%09CyNetwork%5B%5D result = new CyNetwork%5B1%5D;%0A%09%09result%5B0%5D = network;%0A%09%09return result;%0A%09%7D%0A%09%0A%09@Override%0A
 %09public CyNetwor
@@ -1522,16 +1522,16 @@
 work) %7B%0A
-
 %09%09final 
@@ -1734,33 +1734,8 @@
 n %7B%0A
-%09%09InputStream is = null;%0A
 %09%09pa
@@ -2039,19 +2039,21 @@
 race();%0A
-
 %09%09%09
+//
 throw ne

add getNetworks()