KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > io > TreeMLWriter


1 /**
2  * Copyright (c) 2004-2006 Regents of the University of California.
3  * See "license-prefuse.txt" for licensing terms.
4  */

5 package prefuse.data.io;
6
7 import java.io.OutputStream JavaDoc;
8 import java.io.PrintWriter JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.HashMap JavaDoc;
11
12 import prefuse.data.Graph;
13 import prefuse.data.Node;
14 import prefuse.data.Schema;
15 import prefuse.util.io.XMLWriter;
16
17 /**
18  * GraphWriter instance that writes a tree file formatted using the
19  * TreeML file format. TreeML is an XML format originally created for
20  * the 2003 InfoVis conference contest. A DTD (Document Type Definition) for
21  * TreeML is
22  * <a HREF="http://www.nomencurator.org/InfoVis2003/download/treeml.dtd">
23  * available online</a>.
24  *
25  * <p>The GraphML spec only supports the data types <code>Int</code>,
26  * <code>Long</code>, <code>Float</code>, <code>Real</code> (double),
27  * <code>Boolean</code>, <code>String</code>, and <code>Date</code>.
28  * An exception will be thrown if a data type outside these allowed
29  * types is encountered.</p>
30  *
31  * @author <a HREF="http://jheer.org">jeffrey heer</a>
32  */

33 public class TreeMLWriter extends AbstractGraphWriter {
34
35     /**
36      * String tokens used in the TreeML format.
37      */

38     public interface Tokens extends TreeMLReader.Tokens {}
39     
40     /**
41      * Map containing legal data types and their names in the GraphML spec
42      */

43     private static final HashMap JavaDoc TYPES = new HashMap JavaDoc();
44     static {
45         TYPES.put(int.class, Tokens.INT);
46         TYPES.put(long.class, Tokens.LONG);
47         TYPES.put(float.class, Tokens.FLOAT);
48         TYPES.put(double.class, Tokens.REAL);
49         TYPES.put(boolean.class, Tokens.BOOLEAN);
50         TYPES.put(String JavaDoc.class, Tokens.STRING);
51         TYPES.put(Date JavaDoc.class, Tokens.DATE);
52     }
53     
54     /**
55      * @see prefuse.data.io.GraphWriter#writeGraph(prefuse.data.Graph, java.io.OutputStream)
56      */

57     public void writeGraph(Graph graph, OutputStream JavaDoc os) throws DataIOException
58     {
59         // first, check the schemas to ensure GraphML compatibility
60
Schema ns = graph.getNodeTable().getSchema();
61         checkTreeMLSchema(ns);
62         
63         XMLWriter xml = new XMLWriter(new PrintWriter JavaDoc(os));
64         xml.begin();
65         
66         xml.comment("prefuse TreeML Writer | "
67                 + new Date JavaDoc(System.currentTimeMillis()));
68                 
69         // print the tree contents
70
xml.start(Tokens.TREE);
71         
72         // print the tree node schema
73
xml.start(Tokens.DECLS);
74         String JavaDoc[] attr = new String JavaDoc[] {Tokens.NAME, Tokens.TYPE };
75         String JavaDoc[] vals = new String JavaDoc[2];
76
77         for ( int i=0; i<ns.getColumnCount(); ++i ) {
78             vals[0] = ns.getColumnName(i);
79             vals[1] = (String JavaDoc)TYPES.get(ns.getColumnType(i));
80             xml.tag(Tokens.DECL, attr, vals, 2);
81         }
82         xml.end();
83         xml.println();
84         
85         
86         // print the tree nodes
87
attr[0] = Tokens.NAME;
88         attr[1] = Tokens.VALUE;
89         
90         Node n = graph.getSpanningTree().getRoot();
91         while ( n != null ) {
92             boolean leaf = (n.getChildCount() == 0);
93             
94             if ( leaf ) {
95                 xml.start(Tokens.LEAF);
96             } else {
97                 xml.start(Tokens.BRANCH);
98             }
99             
100             if ( ns.getColumnCount() > 0 ) {
101                 for ( int i=0; i<ns.getColumnCount(); ++i ) {
102                     vals[0] = ns.getColumnName(i);
103                     vals[1] = n.getString(vals[0]);
104                     xml.tag(Tokens.ATTR, attr, vals, 2);
105                 }
106             }
107             n = nextNode(n, xml);
108         }
109         
110         // finish writing file
111
xml.end();
112         xml.finish();
113     }
114     
115     /**
116      * Find the next node in the depth first iteration, closing off open
117      * branch tags as needed.
118      * @param x the current node
119      * @param xml the XMLWriter
120      * @return the next node
121      */

122     private Node nextNode(Node x, XMLWriter xml) {
123         Node n, c;
124         if ( (c=x.getChild(0)) != null ) {
125             // do nothing
126
} else if ( (c=x.getNextSibling()) != null ) {
127             xml.end();
128         } else {
129             c = x.getParent();
130             xml.end();
131             while ( c != null ) {
132                 if ( (n=c.getNextSibling()) != null ) {
133                     c = n;
134                     xml.end();
135                     break;
136                 }
137                 c = c.getParent();
138                 xml.end();
139             }
140         }
141         return c;
142     }
143     
144     /**
145      * Checks if all Schema types are compatible with the TreeML specification.
146      * The TreeML spec only allows the types <code>int</code>,
147      * <code>long</code>, <code>float</code>, <code>double</code>,
148      * <code>boolean</code>, <code>string</code>, and <code>date</code>.
149      * @param s the Schema to check
150      */

151     private void checkTreeMLSchema(Schema s) throws DataIOException {
152         for ( int i=0; i<s.getColumnCount(); ++i ) {
153             Class JavaDoc type = s.getColumnType(i);
154             if ( TYPES.get(type) == null ) {
155                 throw new DataIOException("Data type unsupported by the "
156                     + "TreeML format: " + type.getName());
157             }
158         }
159     }
160     
161 } // end of class GraphMLWriter
Popular Tags