KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > compiler > XMLTree


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  */

4 package org.objectweb.carol.cmi.compiler;
5
6 import java.io.InputStream JavaDoc;
7 import java.io.StringWriter JavaDoc;
8
9 import org.xml.sax.Attributes JavaDoc;
10 import org.xml.sax.InputSource JavaDoc;
11 import org.xml.sax.SAXException JavaDoc;
12 import org.xml.sax.SAXParseException JavaDoc;
13 import org.xml.sax.XMLReader JavaDoc;
14 import org.xml.sax.helpers.DefaultHandler JavaDoc;
15
16 public class XMLTree extends DefaultHandler JavaDoc {
17     /** Default parser name. */
18     private static final String JavaDoc 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 JavaDoc str = new StringWriter JavaDoc();
24
25     //
26
// DocumentHandler methods
27
//
28

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 JavaDoc {
42         flushChars();
43         if (cur != root)
44             throw new SAXException JavaDoc("Unexpected end of document");
45     }
46
47     public void startElement(
48         String JavaDoc uri,
49         String JavaDoc name,
50         String JavaDoc qName,
51         Attributes JavaDoc attrs)
52         throws SAXException JavaDoc {
53         flushChars();
54         XMLElement e = new XMLElement(cur, name, attrs);
55         cur.add(e);
56         cur = e;
57     }
58
59     public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc qName)
60         throws SAXException JavaDoc {
61         flushChars();
62         if (!name.equals(cur.name))
63             throw new SAXException JavaDoc("endElement \"" + name + "\" not corresponding with the startElement \"" + cur.name + "\"");
64         cur = cur.parent;
65         if (cur == null)
66             throw new SAXException JavaDoc("endElement \"" + name + "\" with no corresponding startElement");
67     }
68
69     public void characters(char ch[], int start, int length)
70         throws SAXException JavaDoc {
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     //
80
// ErrorHandler methods
81
//
82

83     public void warning(SAXParseException JavaDoc ex) {
84         System.err.println(
85             "[Warning] " + getLocationString(ex) + ": " + ex.getMessage());
86     }
87
88     public void error(SAXParseException JavaDoc ex) {
89         System.err.println(
90             "[Error] " + getLocationString(ex) + ": " + ex.getMessage());
91     }
92
93     public void fatalError(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
94         System.err.println(
95             "[Fatal Error] " + getLocationString(ex) + ": " + ex.getMessage());
96         throw ex;
97     }
98
99     public static String JavaDoc getLocationString(SAXParseException JavaDoc ex) {
100         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
101         String JavaDoc 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 JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) {
117         try {
118             int limit = systemId.lastIndexOf('/');
119             if (limit < 0) {
120                 return null;
121             }
122             String JavaDoc base = systemId.substring(limit + 1);
123             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
124             InputStream JavaDoc in =
125                 cl.getResourceAsStream("org/objectweb/carol/cmi/compiler/dtd/" + base);
126             return new InputSource JavaDoc(in);
127         } catch (Throwable JavaDoc t) {
128             return null;
129         }
130     }
131
132     public static XMLElement read(String JavaDoc uri) throws Exception JavaDoc {
133         XMLTree t = new XMLTree();
134         XMLReader JavaDoc parser =
135             (XMLReader JavaDoc) 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