1 4 package org.objectweb.carol.cmi.compiler; 5 6 import java.io.InputStream ; 7 import java.io.StringWriter ; 8 9 import org.xml.sax.Attributes ; 10 import org.xml.sax.InputSource ; 11 import org.xml.sax.SAXException ; 12 import org.xml.sax.SAXParseException ; 13 import org.xml.sax.XMLReader ; 14 import org.xml.sax.helpers.DefaultHandler ; 15 16 public class XMLTree extends DefaultHandler { 17 18 private static final String DEFAULT_PARSER_NAME = 19 "org.apache.xerces.parsers.SAXParser"; 20 private XMLElement root = new XMLElement(null, null, null); 21 private XMLElement cur = root; 22 private boolean chars = false; 23 private StringWriter str = new StringWriter (); 24 25 29 private void flushChars() { 30 if (!chars) 31 return; 32 cur.add(str.toString()); 33 str.getBuffer().setLength(0); 34 chars = false; 35 } 36 37 public void startDocument() { 38 flushChars(); 39 } 40 41 public void endDocument() throws SAXException { 42 flushChars(); 43 if (cur != root) 44 throw new SAXException ("Unexpected end of document"); 45 } 46 47 public void startElement( 48 String uri, 49 String name, 50 String qName, 51 Attributes attrs) 52 throws SAXException { 53 flushChars(); 54 XMLElement e = new XMLElement(cur, name, attrs); 55 cur.add(e); 56 cur = e; 57 } 58 59 public void endElement(String uri, String name, String qName) 60 throws SAXException { 61 flushChars(); 62 if (!name.equals(cur.name)) 63 throw new SAXException ("endElement \"" + name + "\" not corresponding with the startElement \"" + cur.name + "\""); 64 cur = cur.parent; 65 if (cur == null) 66 throw new SAXException ("endElement \"" + name + "\" with no corresponding startElement"); 67 } 68 69 public void characters(char ch[], int start, int length) 70 throws SAXException { 71 str.write(ch, start, length); 72 chars = true; 73 } 74 75 public void ignorableWhitespace(char ch[], int start, int length) { 76 flushChars(); 77 } 78 79 83 public void warning(SAXParseException ex) { 84 System.err.println( 85 "[Warning] " + getLocationString(ex) + ": " + ex.getMessage()); 86 } 87 88 public void error(SAXParseException ex) { 89 System.err.println( 90 "[Error] " + getLocationString(ex) + ": " + ex.getMessage()); 91 } 92 93 public void fatalError(SAXParseException ex) throws SAXException { 94 System.err.println( 95 "[Fatal Error] " + getLocationString(ex) + ": " + ex.getMessage()); 96 throw ex; 97 } 98 99 public static String getLocationString(SAXParseException ex) { 100 StringBuffer str = new StringBuffer (); 101 String systemId = ex.getSystemId(); 102 if (systemId != null) { 103 int index = systemId.lastIndexOf('/'); 104 if (index != -1) 105 systemId = systemId.substring(index + 1); 106 str.append(systemId); 107 } 108 str.append(':'); 109 str.append(ex.getLineNumber()); 110 str.append(':'); 111 str.append(ex.getColumnNumber()); 112 113 return str.toString(); 114 } 115 116 public InputSource resolveEntity(String publicId, String systemId) { 117 try { 118 int limit = systemId.lastIndexOf('/'); 119 if (limit < 0) { 120 return null; 121 } 122 String base = systemId.substring(limit + 1); 123 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 124 InputStream in = 125 cl.getResourceAsStream("org/objectweb/carol/cmi/compiler/dtd/" + base); 126 return new InputSource (in); 127 } catch (Throwable t) { 128 return null; 129 } 130 } 131 132 public static XMLElement read(String uri) throws Exception { 133 XMLTree t = new XMLTree(); 134 XMLReader parser = 135 (XMLReader ) Class.forName(DEFAULT_PARSER_NAME).newInstance(); 136 parser.setContentHandler(t); 137 parser.setErrorHandler(t); 138 parser.setEntityResolver(t); 139 parser.setFeature("http://xml.org/sax/features/validation", true); 140 parser.parse(uri); 141 return t.root; 142 } 143 } 144 | Popular Tags |