KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > platform > xml > SAXDocumentBuilder


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.platform.xml;
23
24 import java.util.Stack JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.Locator JavaDoc;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.ContentHandler JavaDoc;
29 import org.w3c.dom.Attr JavaDoc;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.ProcessingInstruction JavaDoc;
34 import org.w3c.dom.Text JavaDoc;
35
36 public class SAXDocumentBuilder implements ContentHandler JavaDoc {
37     private Document JavaDoc document;
38     private Stack JavaDoc nodes = new Stack JavaDoc();
39     private XMLPlatform xmlPlatform;
40
41     public SAXDocumentBuilder() {
42         super();
43         nodes = new Stack JavaDoc();
44         xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform();
45     }
46
47     public Document JavaDoc getDocument() {
48         return document;
49     }
50
51     public Document JavaDoc getInitializedDocument() throws SAXException JavaDoc {
52         if (document == null) {
53             try {
54                 document = xmlPlatform.createDocument();
55                 nodes.push(document);
56             } catch (Exception JavaDoc e) {
57                 throw new SAXException JavaDoc(e);
58             }
59         }
60         return document;
61     }
62
63     public void setDocumentLocator(Locator JavaDoc locator) {
64     }
65
66     public void startDocument() throws SAXException JavaDoc {
67         try {
68             document = xmlPlatform.createDocument();
69             nodes.push(document);
70         } catch (Exception JavaDoc e) {
71             throw new SAXException JavaDoc(e);
72         }
73     }
74
75     public void endDocument() throws SAXException JavaDoc {
76         nodes.pop();
77     }
78
79     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
80     }
81
82     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
83     }
84
85     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
86         if ((null != namespaceURI) && ("".equals(namespaceURI))) {
87             namespaceURI = null;
88         }
89         Element JavaDoc element = getInitializedDocument().createElementNS(namespaceURI, qName);
90         Node JavaDoc parentNode = (Node JavaDoc)nodes.peek();
91         parentNode.appendChild(element);
92         nodes.push(element);
93
94         int numberOfAttributes = atts.getLength();
95         Attr JavaDoc attribute;
96         for (int x = 0; x < numberOfAttributes; x++) {
97             element.setAttributeNS(atts.getURI(x), atts.getQName(x), atts.getValue(x));
98         }
99     }
100
101     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
102         nodes.pop();
103     }
104
105     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
106         String JavaDoc characters = new String JavaDoc(ch, start, length);
107         if (characters.trim().length() == 0) {
108             return;
109         }
110         Text JavaDoc text = getInitializedDocument().createTextNode(characters);
111         Node JavaDoc parentNode = (Node JavaDoc)nodes.peek();
112         parentNode.appendChild(text);
113     }
114
115     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException JavaDoc {
116     }
117
118     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
119         ProcessingInstruction JavaDoc pi = getInitializedDocument().createProcessingInstruction(target, data);
120         Node JavaDoc parentNode = (Node JavaDoc)nodes.peek();
121         parentNode.appendChild(pi);
122     }
123
124     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
125     }
126 }
127
Popular Tags