KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.io;
2
3 import java.io.InputStream JavaDoc;
4 import java.util.Date JavaDoc;
5
6 import javax.xml.parsers.SAXParser JavaDoc;
7 import javax.xml.parsers.SAXParserFactory JavaDoc;
8
9 import org.xml.sax.Attributes JavaDoc;
10 import org.xml.sax.helpers.DefaultHandler JavaDoc;
11
12 import prefuse.data.Graph;
13 import prefuse.data.Node;
14 import prefuse.data.Table;
15 import prefuse.data.Tree;
16 import prefuse.data.parser.DataParseException;
17 import prefuse.data.parser.DataParser;
18 import prefuse.data.parser.ParserFactory;
19
20
21 /**
22  * GraphReader instance that reads in tree-structured data in the
23  * XML-based TreeML format. TreeML is an XML format originally created for
24  * the 2003 InfoVis conference contest. A DTD (Document Type Definition) for
25  * TreeML is
26  * <a HREF="http://www.nomencurator.org/InfoVis2003/download/treeml.dtd">
27  * available online</a>.
28  *
29  * @author <a HREF="http://jheer.org">jeffrey heer</a>
30  */

31 public class TreeMLReader extends AbstractGraphReader {
32
33     private ParserFactory JavaDoc m_pf = ParserFactory.getDefaultFactory();
34
35     /**
36      * @see prefuse.data.io.GraphReader#readGraph(java.io.InputStream)
37      */

38     public Graph readGraph(InputStream JavaDoc is) throws DataIOException {
39         try {
40             TreeMLHandler handler = new TreeMLHandler();
41             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
42             SAXParser JavaDoc saxParser = factory.newSAXParser();
43             saxParser.parse(is, handler);
44             return handler.getTree();
45         } catch ( Exception JavaDoc e ) {
46             throw new DataIOException(e);
47         }
48     }
49
50     /**
51      * String tokens used in the TreeML format.
52      */

53     public static interface Tokens {
54         public static final String JavaDoc TREE = "tree";
55         public static final String JavaDoc BRANCH = "branch";
56         public static final String JavaDoc LEAF = "leaf";
57         public static final String JavaDoc ATTR = "attribute";
58         public static final String JavaDoc NAME = "name";
59         public static final String JavaDoc VALUE = "value";
60         public static final String JavaDoc TYPE = "type";
61         
62         public static final String JavaDoc DECLS = "declarations";
63         public static final String JavaDoc DECL = "attributeDecl";
64         
65         public static final String JavaDoc INT = "Int";
66         public static final String JavaDoc INTEGER = "Integer";
67         public static final String JavaDoc LONG = "Long";
68         public static final String JavaDoc FLOAT = "Float";
69         public static final String JavaDoc REAL = "Real";
70         public static final String JavaDoc STRING = "String";
71         public static final String JavaDoc DATE = "Date";
72         public static final String JavaDoc CATEGORY = "Category";
73         
74         // prefuse-specific allowed types
75
public static final String JavaDoc BOOLEAN = "Boolean";
76         public static final String JavaDoc DOUBLE = "Double";
77     }
78     
79     /**
80      * A SAX Parser for TreeML data files.
81      */

82     public class TreeMLHandler extends DefaultHandler JavaDoc implements Tokens {
83         
84         private Table m_nodes = null;
85         private Tree m_tree = null;
86         
87         private Node m_activeNode = null;
88         private boolean m_inSchema = true;
89         
90         public void startDocument() {
91             m_tree = new Tree();
92             m_nodes = m_tree.getNodeTable();
93         }
94         
95         private void schemaCheck() {
96             if ( m_inSchema ) {
97                 m_inSchema = false;
98             }
99         }
100         
101         public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) {
102             if ( qName.equals(BRANCH) || qName.equals(LEAF) ) {
103                 m_activeNode = m_activeNode.getParent();
104             }
105         }
106         
107         public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
108                                  String JavaDoc qName, Attributes JavaDoc atts) {
109             if ( qName.equals(DECL) ) {
110                 if ( !m_inSchema ) {
111                     throw new RuntimeException JavaDoc("All declarations must be done "
112                             + "before nodes begin");
113                 }
114                 String JavaDoc name = atts.getValue(NAME);
115                 String JavaDoc type = atts.getValue(TYPE);
116                 Class JavaDoc t = parseType(type);
117                 m_nodes.addColumn(name, t);
118             }
119             else if ( qName.equals(BRANCH) || qName.equals(LEAF) ) {
120                 schemaCheck();
121                 
122                 // parse a node element
123
Node n;
124                 if ( m_activeNode == null ) {
125                     n = m_tree.addRoot();
126                 } else {
127                     n = m_tree.addChild(m_activeNode);
128                 }
129                 m_activeNode = n;
130             }
131             else if ( qName.equals(ATTR) ) {
132                 // parse an attribute
133
parseAttribute(atts);
134             }
135         }
136         
137         protected void parseAttribute(Attributes JavaDoc atts) {
138             String JavaDoc alName, name = null, value = null;
139             for ( int i = 0; i < atts.getLength(); i++ ) {
140                 alName = atts.getQName(i);
141                 if ( alName.equals(NAME) ) {
142                     name = atts.getValue(i);
143                 } else if ( alName.equals(VALUE) ) {
144                     value = atts.getValue(i);
145                 }
146             }
147             if ( name == null || value == null ) {
148                 System.err.println("Attribute under-specified");
149                 return;
150             }
151
152             try {
153                 Object JavaDoc val = parse(value, m_nodes.getColumnType(name));
154                 m_activeNode.set(name, val);
155             } catch ( Exception JavaDoc e ) {
156                 throw new RuntimeException JavaDoc(e);
157             }
158         }
159         
160         protected Object JavaDoc parse(String JavaDoc s, Class JavaDoc type)
161             throws DataParseException
162         {
163             DataParser dp = m_pf.getParser(type);
164             return dp.parse(s);
165         }
166         
167         protected Class JavaDoc parseType(String JavaDoc type) {
168             type = Character.toUpperCase(type.charAt(0)) +
169                    type.substring(1).toLowerCase();
170             if ( type.equals(INT) || type.equals(INTEGER) ) {
171                 return int.class;
172             } else if ( type.equals(LONG) ) {
173                 return long.class;
174             } else if ( type.equals(FLOAT) ) {
175                 return float.class;
176             } else if ( type.equals(DOUBLE) || type.equals(REAL)) {
177                 return double.class;
178             } else if ( type.equals(BOOLEAN) ) {
179                 return boolean.class;
180             } else if ( type.equals(STRING) ) {
181                 return String JavaDoc.class;
182             } else if ( type.equals(DATE) ) {
183                 return Date JavaDoc.class;
184             } else {
185                 throw new RuntimeException JavaDoc("Unrecognized data type: "+type);
186             }
187         }
188         
189         public Tree getTree() {
190             return m_tree;
191         }
192         
193     } // end of inner class TreeMLHandler
194

195 } // end of class TreeMLTReeReader
196
Popular Tags