1 package prefuse.data.io; 2 3 import java.io.InputStream ; 4 import java.util.Date ; 5 6 import javax.xml.parsers.SAXParser ; 7 import javax.xml.parsers.SAXParserFactory ; 8 9 import org.xml.sax.Attributes ; 10 import org.xml.sax.helpers.DefaultHandler ; 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 31 public class TreeMLReader extends AbstractGraphReader { 32 33 private ParserFactory m_pf = ParserFactory.getDefaultFactory(); 34 35 38 public Graph readGraph(InputStream is) throws DataIOException { 39 try { 40 TreeMLHandler handler = new TreeMLHandler(); 41 SAXParserFactory factory = SAXParserFactory.newInstance(); 42 SAXParser saxParser = factory.newSAXParser(); 43 saxParser.parse(is, handler); 44 return handler.getTree(); 45 } catch ( Exception e ) { 46 throw new DataIOException(e); 47 } 48 } 49 50 53 public static interface Tokens { 54 public static final String TREE = "tree"; 55 public static final String BRANCH = "branch"; 56 public static final String LEAF = "leaf"; 57 public static final String ATTR = "attribute"; 58 public static final String NAME = "name"; 59 public static final String VALUE = "value"; 60 public static final String TYPE = "type"; 61 62 public static final String DECLS = "declarations"; 63 public static final String DECL = "attributeDecl"; 64 65 public static final String INT = "Int"; 66 public static final String INTEGER = "Integer"; 67 public static final String LONG = "Long"; 68 public static final String FLOAT = "Float"; 69 public static final String REAL = "Real"; 70 public static final String STRING = "String"; 71 public static final String DATE = "Date"; 72 public static final String CATEGORY = "Category"; 73 74 public static final String BOOLEAN = "Boolean"; 76 public static final String DOUBLE = "Double"; 77 } 78 79 82 public class TreeMLHandler extends DefaultHandler 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 namespaceURI, String localName, String qName) { 102 if ( qName.equals(BRANCH) || qName.equals(LEAF) ) { 103 m_activeNode = m_activeNode.getParent(); 104 } 105 } 106 107 public void startElement(String namespaceURI, String localName, 108 String qName, Attributes atts) { 109 if ( qName.equals(DECL) ) { 110 if ( !m_inSchema ) { 111 throw new RuntimeException ("All declarations must be done " 112 + "before nodes begin"); 113 } 114 String name = atts.getValue(NAME); 115 String type = atts.getValue(TYPE); 116 Class t = parseType(type); 117 m_nodes.addColumn(name, t); 118 } 119 else if ( qName.equals(BRANCH) || qName.equals(LEAF) ) { 120 schemaCheck(); 121 122 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 parseAttribute(atts); 134 } 135 } 136 137 protected void parseAttribute(Attributes atts) { 138 String 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 val = parse(value, m_nodes.getColumnType(name)); 154 m_activeNode.set(name, val); 155 } catch ( Exception e ) { 156 throw new RuntimeException (e); 157 } 158 } 159 160 protected Object parse(String s, Class type) 161 throws DataParseException 162 { 163 DataParser dp = m_pf.getParser(type); 164 return dp.parse(s); 165 } 166 167 protected Class parseType(String 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 .class; 182 } else if ( type.equals(DATE) ) { 183 return Date .class; 184 } else { 185 throw new RuntimeException ("Unrecognized data type: "+type); 186 } 187 } 188 189 public Tree getTree() { 190 return m_tree; 191 } 192 193 } 195 } | Popular Tags |