KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > parser > DOMBuilder


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.xs.parser;
18
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
22
23 import org.w3c.dom.Document JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.EntityReference JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.ProcessingInstruction JavaDoc;
28 import org.w3c.dom.Text JavaDoc;
29 import org.xml.sax.Attributes JavaDoc;
30 import org.xml.sax.ContentHandler JavaDoc;
31 import org.xml.sax.Locator JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33
34
35 /** <p>Converts a stream of SAX events into a DOM node.</p>
36  *
37  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
38  * @version $Id: DOMBuilder.java,v 1.2 2004/02/16 23:39:59 jochen Exp $
39  */

40 public class DOMBuilder implements ContentHandler JavaDoc {
41   private Node JavaDoc currentNode;
42   private Element JavaDoc result;
43   private Locator JavaDoc locator;
44   private Document JavaDoc factory;
45
46   /** <p>Creates a new instance of DOMBuilder, which creates
47    * a new document. The document is available via
48    * {@link #getDocument()}.</p>
49    */

50   public DOMBuilder() throws ParserConfigurationException JavaDoc {
51     DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
52     dbf.setValidating(false);
53     dbf.setNamespaceAware(true);
54     DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
55     currentNode = factory = db.newDocument();
56   }
57
58   /** <p>Creates a new instance of DOMBuilder, which creates
59    * a new element node in the given node. The created node
60    * is available via {@link #getResult()}.</p>
61    */

62   public DOMBuilder(Node JavaDoc pTarget) {
63     if (pTarget == null) {
64       throw new NullPointerException JavaDoc("Target node must not be null.");
65     }
66     factory = pTarget.getOwnerDocument();
67     currentNode = pTarget;
68   }
69
70   /** <p>Returns the document being used as object factory. In the case
71    * of the empty constructor, this is a new document. Otherwise it is
72    * the target nodes owner document.</p>
73    */

74   public Document JavaDoc getDocument() {
75     return factory;
76   }
77
78   /** <p>Returns the result element. In the case of the default constructor,
79    * this is the document element. Otherwise it is added to the target node
80    * via {@link org.w3c.dom.Node#appendChild(org.w3c.dom.Node)}.<(p>
81    */

82   public Element JavaDoc getResult() {
83     return result;
84   }
85
86   /** <p>Sets the Locator.</p>
87    */

88   public void setDocumentLocator(Locator JavaDoc pLocator) {
89     locator = pLocator;
90   }
91
92   /** <p>Returns the Locator.</p>
93    */

94   public Locator JavaDoc getDocumentLocator() {
95     return locator;
96   }
97
98   /**
99    * @see org.xml.sax.ContentHandler#startDocument()
100    */

101   public void startDocument() throws SAXException JavaDoc {
102   }
103
104   /**
105    * @see org.xml.sax.ContentHandler#endDocument()
106    */

107   public void endDocument() throws SAXException JavaDoc {
108   }
109
110   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
111     throws SAXException JavaDoc {
112   }
113
114   public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
115   }
116
117   public void startElement(String JavaDoc pNamespaceURI, String JavaDoc pLocalName,
118                             String JavaDoc pQName, Attributes JavaDoc pAttr) throws SAXException JavaDoc {
119     Document JavaDoc doc = getDocument();
120     Element JavaDoc element;
121     if (pNamespaceURI == null || pNamespaceURI.length() == 0) {
122       element = doc.createElement(pQName);
123     } else {
124       element = doc.createElementNS(pNamespaceURI, pQName);
125     }
126     if (pAttr != null) {
127       for (int i = 0; i < pAttr.getLength(); i++) {
128         String JavaDoc uri = pAttr.getURI(i);
129         String JavaDoc qName = pAttr.getQName(i);
130         String JavaDoc value = pAttr.getValue(i);
131         if (uri == null || uri.length() == 0) {
132           element.setAttribute(qName, value);
133         } else {
134           element.setAttributeNS(uri, qName, value);
135         }
136       }
137     }
138
139     currentNode.appendChild(element);
140     currentNode = element;
141     if (result == null) {
142       result = element;
143     }
144   }
145
146   public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
147       throws SAXException JavaDoc {
148     currentNode = currentNode.getParentNode();
149   }
150
151   public void characters(char[] ch, int start, int length)
152       throws SAXException JavaDoc {
153     Node JavaDoc node = currentNode.getLastChild();
154     String JavaDoc s = new String JavaDoc(ch, start, length);
155     if (node != null && node.getNodeType() == Node.TEXT_NODE) {
156       ((Text JavaDoc) node).appendData(s);
157     } else {
158       Text JavaDoc text = getDocument().createTextNode(s);
159       currentNode.appendChild(text);
160     }
161   }
162
163   public void ignorableWhitespace(char[] ch, int start, int length)
164       throws SAXException JavaDoc {
165     characters(ch, start, length);
166   }
167
168   public void processingInstruction(String JavaDoc pTarget, String JavaDoc pData)
169       throws SAXException JavaDoc {
170     ProcessingInstruction JavaDoc pi = getDocument().createProcessingInstruction(pTarget, pData);
171     currentNode.appendChild(pi);
172   }
173
174   public void skippedEntity(String JavaDoc pName) throws SAXException JavaDoc {
175     EntityReference JavaDoc entity = getDocument().createEntityReference(pName);
176     currentNode.appendChild(entity);
177   }
178 }
179
Popular Tags