
prefuse.data.io.GraphMLWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prefuse-vienna Show documentation
Show all versions of prefuse-vienna Show documentation
Prefuse is a set of software tools for creating rich interactive data visualizations in the Java programming language.
The newest version!
/** * Copyright (c) 2004-2006 Regents of the University of California. * See "license-prefuse.txt" for licensing terms. */ package prefuse.data.io; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import prefuse.data.Edge; import prefuse.data.Graph; import prefuse.data.Node; import prefuse.data.Schema; import prefuse.util.io.XMLWriter; /** * GraphWriter instance that writes a graph file formatted using the * GraphML file format. GraphML is an XML format supporting graph * structure and typed data schemas for both nodes and edges. For more * information about the format, please see the * GraphML home page. * *
, *The GraphML spec only supports the data types
* * @author jeffrey heer */ public class GraphMLWriter extends AbstractGraphWriter { /** * String tokens used in the GraphML format. */ public interface Tokens extends GraphMLReader.Tokens { public static final String GRAPHML = "graphml"; public static final String GRAPHML_HEADER = "int
, *long
,float
,double
, *boolean
, andstring
. An exception will * be thrown if a data type outside these allowed types is * encountered.\n\n"; } /** * Map containing legal data types and their names in the GraphML spec */ public static final java.util.Map TYPES; static { HashMap types = new HashMap(); types.put(int.class, Tokens.INT); types.put(long.class, Tokens.LONG); types.put(float.class, Tokens.FLOAT); types.put(double.class, Tokens.DOUBLE); types.put(boolean.class, Tokens.BOOLEAN); types.put(String.class, Tokens.STRING); TYPES = Collections.unmodifiableMap(types); } /** * @see prefuse.data.io.GraphWriter#writeGraph(prefuse.data.Graph, java.io.OutputStream) */ public void writeGraph(Graph graph, OutputStream os) throws DataIOException { // first, check the schemas to ensure GraphML compatibility Schema ns = graph.getNodeTable().getSchema(); Schema es = graph.getEdgeTable().getSchema(); checkGraphMLSchema(ns); checkGraphMLSchema(es); XMLWriter xml = new XMLWriter(new PrintWriter(os)); xml.begin(Tokens.GRAPHML_HEADER, 2); xml.comment("prefuse GraphML Writer | " + new Date(System.currentTimeMillis())); // print the graph schema printSchema(xml, Tokens.NODE, ns, null); printSchema(xml, Tokens.EDGE, es, new String[] { graph.getEdgeSourceField(), graph.getEdgeTargetField() }); xml.println(); // print graph contents xml.start(Tokens.GRAPH, Tokens.EDGEDEF, graph.isDirected() ? Tokens.DIRECTED : Tokens.UNDIRECTED); // print the nodes xml.comment("nodes"); Iterator nodes = graph.nodes(); while ( nodes.hasNext() ) { Node n = (Node)nodes.next(); if ( ns.getColumnCount() > 0 ) { xml.start(Tokens.NODE, Tokens.ID, String.valueOf(n.getRow())); for ( int i=0; i 2 ) { xml.start(Tokens.EDGE, attr, vals, 3); for ( int i=0; i \n"); } /** * Print a table schema to a GraphML file * @param xml the XMLWriter to write to * @param group the data group (node or edge) for the schema * @param s the schema */ private void printSchema(XMLWriter xml, String group, Schema s, String[] ignore) { String[] attr = new String[] {Tokens.ID, Tokens.FOR, Tokens.ATTRNAME, Tokens.ATTRTYPE }; String[] vals = new String[4]; OUTER: for ( int i=0; i int long
,float
,double
, *boolean
, andstring
. * @param s the Schema to check */ public static void checkGraphMLSchema(Schema s) throws DataIOException { for ( int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy