/*
* ResultXML.java
*
* Created on 25 May 2007, 12:14
*
*Author: Mark Holliman
*/
package paperscope;
import java.lang.reflect.*;
import java.util.Vector;
import prefuse.data.Graph;
import prefuse.data.Table;
import prefuse.data.Tuple;
import prefuse.data.tuple.TupleSet;
import prefuse.data.Schema;
import prefuse.data.io.GraphMLWriter;
import prefuse.data.io.GraphMLReader;
import java.io.*;
public class ResultXML
{
Graph graphML;
//==== Object for opening and saving GraphML files
public ResultXML()
{
}
//################################################################################################
// Generate the XML file from the graph
public boolean writeFile(Graph graph, String fileName)
{
GraphMLWriter graphWriter = new GraphMLWriter();
try
{
//=== See if the file exists, if so overwrite it, if not create a new file and write to it
File newFile = new File(fileName);
if (newFile.exists())
{
graphWriter.writeGraph(graph, newFile);
}
else
{
newFile.createNewFile();
graphWriter.writeGraph(graph, newFile);
}
return true;
}
catch(Exception e){
System.out.println("Exception "+ e);
return false;
}
}
//################################################################################################
// Open the XML file and generate the graph
public boolean openFile(File openMe)
{
//==== If the file exists, open it and save the graph to this.graphML, if it doesn't return false
if(openMe.exists())
{
try
{
GraphMLReader graphReader = new GraphMLReader();
this.graphML = graphReader.readGraph(openMe);
return true;
}
catch (Exception e){
System.out.println("Exception "+ e);
return false;
}
}
else
return false;
}
public Graph getGraph()
{
return this.graphML;
} //end getXML
//#################################################################################################
//==== Generate XML manually using a Graph as the base
public String writeManualXML(Graph g)
{
//==== Get the tables and schemas from the graph
Table nodes = g.getNodeTable();
Table edges = g.getEdgeTable();
Schema nodeSchema = nodes.getSchema();
Schema edgeSchema = edges.getSchema();
//==== Generate the XML header, and data schema for visualization
String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!-- A paper and all its citations and references -->\n" +
"<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">\n" +
"<graph edgedefault=\"directed\">\n\n";
//======================================
//==== Generate the Nodes table XML
//======================================
String nodesTableXML = "<!-- nodes -->\n";
//==== Go through the nodes table, writing a line of XML for each row
for (int j=0; j<nodes.getRowCount(); j++)
{
String row = "";
for (int k=0; k<nodeSchema.getColumnCount(); k++)
{
//=== first column is the node key, start a new node element
if(k==0)
{
row = "<node id=\"" + nodes.get(j,k) + "\">\n";
}
//=== the remaining columns are nested data for the node
else
{
row = row + " <data key=\"" + nodeSchema.getColumnName(k) + "\">" + nodes.get(j,k) + "</data>\n";
}
} // end for, loop through columns
//=== close off the node, and add it to the nodeTableXML
row = row + "</node>\n";
nodesTableXML = nodesTableXML + row;
} //end for, loop through rows
//======================================
//==== Generate the Edges table XML
//======================================
String edgesTableXML = "<!-- edges -->\n";
//==== Go through the edges table, writing a line of XML for each row
//==== We know that the edge table has only two columns, source and target
for (int j=0; j<edges.getRowCount(); j++)
{
String row = "<edge source=\"" + edges.get(j,0) + "\" " +
"target=\"" + edges.get(j, 1) + "\"></edge>\n";
edgesTableXML = edgesTableXML + row;
} //end for, loop through rows
//==== Generate XML footer
String footer = "</graph>\n</graphml>";
return (header + nodesTableXML + edgesTableXML + footer);
} //end default constructor
} //end ResultXML class