/** * 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.Date; import java.util.HashMap; import prefuse.data.Graph; import prefuse.data.Node; import prefuse.data.Schema; import prefuse.util.io.XMLWriter; /** * GraphWriter instance that writes a tree file formatted using the * TreeML file format. TreeML is an XML format originally created for * the 2003 InfoVis conference contest. A DTD (Document Type Definition) for * TreeML is * * available online. * *

The GraphML spec only supports the data types Int, * Long, Float, Real (double), * Boolean, String, and Date. * An exception will be thrown if a data type outside these allowed * types is encountered.

* * @author jeffrey heer */ public class TreeMLWriter extends AbstractGraphWriter { /** * String tokens used in the TreeML format. */ public interface Tokens extends TreeMLReader.Tokens {} /** * Map containing legal data types and their names in the GraphML spec */ private static final HashMap TYPES = new HashMap(); static { TYPES.put(int.class, Tokens.INT); TYPES.put(long.class, Tokens.LONG); TYPES.put(float.class, Tokens.FLOAT); TYPES.put(double.class, Tokens.REAL); TYPES.put(boolean.class, Tokens.BOOLEAN); TYPES.put(String.class, Tokens.STRING); TYPES.put(Date.class, Tokens.DATE); } /** * @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(); checkTreeMLSchema(ns); XMLWriter xml = new XMLWriter(new PrintWriter(os)); xml.begin(); xml.comment("prefuse TreeML Writer | " + new Date(System.currentTimeMillis())); // print the tree contents xml.start(Tokens.TREE); // print the tree node schema xml.start(Tokens.DECLS); String[] attr = new String[] {Tokens.NAME, Tokens.TYPE }; String[] vals = new String[2]; for ( int i=0; i 0 ) { for ( int i=0; iint, * long, float, double, * boolean, string, and date. * @param s the Schema to check */ private void checkTreeMLSchema(Schema s) throws DataIOException { for ( int i=0; i