001: /* StdXMLBuilder.java NanoXML/Java
002: *
003: * $Revision: 1.17 $
004: * $Date: 2005/01/05 17:20:04 $
005: * $Name: $
006: *
007: * This file is part of NanoXML 2 for Java.
008: * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
009: *
010: * This software is provided 'as-is', without any express or implied warranty.
011: * In no event will the authors be held liable for any damages arising from the
012: * use of this software.
013: *
014: * Permission is granted to anyone to use this software for any purpose,
015: * including commercial applications, and to alter it and redistribute it
016: * freely, subject to the following restrictions:
017: *
018: * 1. The origin of this software must not be misrepresented; you must not
019: * claim that you wrote the original software. If you use this software in
020: * a product, an acknowledgment in the product documentation would be
021: * appreciated but is not required.
022: *
023: * 2. Altered source versions must be plainly marked as such, and must not be
024: * misrepresented as being the original software.
025: *
026: * 3. This notice may not be removed or altered from any source distribution.
027: */
028:
029: package net.n3.nanoxml;
030:
031: import java.io.IOException;
032: import java.io.Reader;
033: import java.util.Stack;
034:
035: /**
036: * StdXMLBuilder is a concrete implementation of IXMLBuilder which creates a
037: * tree of IXMLElement from an XML data source.
038: *
039: * @see net.n3.nanoxml.XMLElement
040: *
041: * @author Marc De Scheemaecker
042: * @version $Name: $, $Revision: 1.17 $
043: */
044: public class StdXMLBuilder implements IXMLBuilder {
045:
046: /**
047: * This stack contains the current element and its parents.
048: */
049: private Stack stack;
050:
051: /**
052: * The root element of the parsed XML tree.
053: */
054: private IXMLElement root;
055:
056: /**
057: * Prototype element for creating the tree.
058: */
059: private IXMLElement prototype;
060:
061: /**
062: * Creates the builder.
063: */
064: public StdXMLBuilder() {
065: this (new XMLElement());
066: }
067:
068: /**
069: * Creates the builder.
070: *
071: * @param prototype the prototype to use when building the tree.
072: */
073: public StdXMLBuilder(IXMLElement prototype) {
074: this .stack = null;
075: this .root = null;
076: this .prototype = prototype;
077: }
078:
079: /**
080: * Cleans up the object when it's destroyed.
081: */
082: protected void finalize() throws Throwable {
083: this .prototype = null;
084: this .root = null;
085: this .stack.removeAllElements();
086: this .stack = null;
087: super .finalize();
088: }
089:
090: /**
091: * This method is called before the parser starts processing its input.
092: *
093: * @param systemID the system ID of the XML data source.
094: * @param lineNr the line on which the parsing starts.
095: */
096: public void startBuilding(String systemID, int lineNr) {
097: this .stack = new Stack();
098: this .root = null;
099: }
100:
101: /**
102: * This method is called when a processing instruction is encountered.
103: * PIs with target "xml" are handled by the parser.
104: *
105: * @param target the PI target.
106: * @param reader to read the data from the PI.
107: */
108: public void newProcessingInstruction(String target, Reader reader) {
109: // nothing to do
110: }
111:
112: /**
113: * This method is called when a new XML element is encountered.
114: *
115: * @see #endElement
116: *
117: * @param name the name of the element.
118: * @param nsPrefix the prefix used to identify the namespace. If no
119: * namespace has been specified, this parameter is null.
120: * @param nsURI the URI associated with the namespace. If no
121: * namespace has been specified, or no URI is
122: * associated with nsPrefix, this parameter is null.
123: * @param systemID the system ID of the XML data source.
124: * @param lineNr the line in the source where the element starts.
125: */
126: public void startElement(String name, String nsPrefix,
127: String nsURI, String systemID, int lineNr) {
128: String fullName = name;
129:
130: if (nsPrefix != null) {
131: fullName = nsPrefix + ':' + name;
132: }
133:
134: IXMLElement elt = this .prototype.createElement(fullName, nsURI,
135: systemID, lineNr);
136:
137: if (this .stack.empty()) {
138: this .root = elt;
139: } else {
140: IXMLElement top = (IXMLElement) this .stack.peek();
141: top.addChild(elt);
142: }
143:
144: this .stack.push(elt);
145: }
146:
147: /**
148: * This method is called when the attributes of an XML element have been
149: * processed.
150: *
151: * @see #startElement
152: * @see #addAttribute
153: *
154: * @param name the name of the element.
155: * @param nsPrefix the prefix used to identify the namespace. If no
156: * namespace has been specified, this parameter is null.
157: * @param nsURI the URI associated with the namespace. If no
158: * namespace has been specified, or no URI is
159: * associated with nsPrefix, this parameter is null.
160: */
161: public void elementAttributesProcessed(String name,
162: String nsPrefix, String nsURI) {
163: // nothing to do
164: }
165:
166: /**
167: * This method is called when the end of an XML elemnt is encountered.
168: *
169: * @see #startElement
170: *
171: * @param name the name of the element.
172: * @param nsPrefix the prefix used to identify the namespace. If no
173: * namespace has been specified, this parameter is null.
174: * @param nsURI the URI associated with the namespace. If no
175: * namespace has been specified, or no URI is
176: * associated with nsPrefix, this parameter is null.
177: */
178: public void endElement(String name, String nsPrefix, String nsURI) {
179: IXMLElement elt = (IXMLElement) this .stack.pop();
180:
181: if (elt.getChildrenCount() == 1) {
182: IXMLElement child = elt.getChildAtIndex(0);
183:
184: if (child.getName() == null) {
185: elt.setContent(child.getContent());
186: elt.removeChildAtIndex(0);
187: }
188: }
189: }
190:
191: /**
192: * This method is called when a new attribute of an XML element is
193: * encountered.
194: *
195: * @param key the key (name) of the attribute.
196: * @param nsPrefix the prefix used to identify the namespace. If no
197: * namespace has been specified, this parameter is null.
198: * @param nsURI the URI associated with the namespace. If no
199: * namespace has been specified, or no URI is
200: * associated with nsPrefix, this parameter is null.
201: * @param value the value of the attribute.
202: * @param type the type of the attribute. If no type is known,
203: * "CDATA" is returned.
204: *
205: * @throws java.lang.Exception
206: * If an exception occurred while processing the event.
207: */
208: public void addAttribute(String key, String nsPrefix, String nsURI,
209: String value, String type) throws Exception {
210: String fullName = key;
211:
212: if (nsPrefix != null) {
213: fullName = nsPrefix + ':' + key;
214: }
215:
216: IXMLElement top = (IXMLElement) this .stack.peek();
217:
218: if (top.hasAttribute(fullName)) {
219: throw new XMLParseException(top.getSystemID(), top
220: .getLineNr(), "Duplicate attribute: " + key);
221: }
222:
223: if (nsPrefix != null) {
224: top.setAttribute(fullName, nsURI, value);
225: } else {
226: top.setAttribute(fullName, value);
227: }
228: }
229:
230: /**
231: * This method is called when a PCDATA element is encountered. A Java
232: * reader is supplied from which you can read the data. The reader will
233: * only read the data of the element. You don't need to check for
234: * boundaries. If you don't read the full element, the rest of the data
235: * is skipped. You also don't have to care about entities; they are
236: * resolved by the parser.
237: *
238: * @param reader the Java reader from which you can retrieve the data.
239: * @param systemID the system ID of the XML data source.
240: * @param lineNr the line in the source where the element starts.
241: */
242: public void addPCData(Reader reader, String systemID, int lineNr) {
243: int bufSize = 2048;
244: int sizeRead = 0;
245: StringBuffer str = new StringBuffer(bufSize);
246: char[] buf = new char[bufSize];
247:
248: for (;;) {
249: if (sizeRead >= bufSize) {
250: bufSize *= 2;
251: str.ensureCapacity(bufSize);
252: }
253:
254: int size;
255:
256: try {
257: size = reader.read(buf);
258: } catch (IOException e) {
259: break;
260: }
261:
262: if (size < 0) {
263: break;
264: }
265:
266: str.append(buf, 0, size);
267: sizeRead += size;
268: }
269:
270: IXMLElement elt = this .prototype.createElement(null, systemID,
271: lineNr);
272: elt.setContent(str.toString());
273:
274: if (!this .stack.empty()) {
275: IXMLElement top = (IXMLElement) this .stack.peek();
276: top.addChild(elt);
277: }
278: }
279:
280: /**
281: * Returns the result of the building process. This method is called just
282: * before the <I>parse</I> method of IXMLParser returns.
283: *
284: * @see net.n3.nanoxml.IXMLParser#parse
285: *
286: * @return the result of the building process.
287: */
288: public Object getResult() {
289: return this.root;
290: }
291:
292: }
|