KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > trax > SAX2DOM


1 /*
2  * Copyright 2001-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  * $Id: SAX2DOM.java,v 1.18 2004/02/16 22:57:21 minchau Exp $
18  */

19
20
21 package org.apache.xalan.xsltc.trax;
22
23 import java.util.Stack JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28
29 import org.apache.xalan.xsltc.runtime.Constants;
30
31 import org.w3c.dom.Comment JavaDoc;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.ProcessingInstruction JavaDoc;
36 import org.xml.sax.Attributes JavaDoc;
37 import org.xml.sax.ContentHandler JavaDoc;
38 import org.xml.sax.Locator JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.ext.LexicalHandler JavaDoc;
41
42 /**
43  * @author G. Todd Miller
44  */

45 public class SAX2DOM implements ContentHandler JavaDoc, LexicalHandler JavaDoc, Constants {
46
47     private Node JavaDoc _root = null;
48     private Document JavaDoc _document = null;
49     private Stack JavaDoc _nodeStk = new Stack JavaDoc();
50     private Vector JavaDoc _namespaceDecls = null;
51
52     public SAX2DOM() throws ParserConfigurationException JavaDoc {
53     final DocumentBuilderFactory JavaDoc factory =
54         DocumentBuilderFactory.newInstance();
55     _document = factory.newDocumentBuilder().newDocument();
56     _root = _document;
57     }
58
59     public SAX2DOM(Node JavaDoc root) throws ParserConfigurationException JavaDoc {
60     _root = root;
61     if (root instanceof Document JavaDoc) {
62       _document = (Document JavaDoc)root;
63     }
64     else if (root != null) {
65       _document = root.getOwnerDocument();
66     }
67     else {
68       final DocumentBuilderFactory JavaDoc factory =
69         DocumentBuilderFactory.newInstance();
70       _document = factory.newDocumentBuilder().newDocument();
71       _root = _document;
72     }
73     }
74
75     public Node JavaDoc getDOM() {
76     return _root;
77     }
78
79     public void characters(char[] ch, int start, int length) {
80     final Node JavaDoc last = (Node JavaDoc)_nodeStk.peek();
81
82     // No text nodes can be children of root (DOM006 exception)
83
if (last != _document) {
84         final String JavaDoc text = new String JavaDoc(ch, start, length);
85         last.appendChild(_document.createTextNode(text));
86     }
87     }
88
89     public void startDocument() {
90     _nodeStk.push(_root);
91     }
92
93     public void endDocument() {
94         _nodeStk.pop();
95     }
96
97     public void startElement(String JavaDoc namespace, String JavaDoc localName, String JavaDoc qName,
98     Attributes JavaDoc attrs)
99     {
100     final Element JavaDoc tmp = (Element JavaDoc)_document.createElementNS(namespace, qName);
101
102     // Add namespace declarations first
103
if (_namespaceDecls != null) {
104         final int nDecls = _namespaceDecls.size();
105         for (int i = 0; i < nDecls; i++) {
106         final String JavaDoc prefix = (String JavaDoc) _namespaceDecls.elementAt(i++);
107
108         if (prefix == null || prefix.equals(EMPTYSTRING)) {
109             tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX,
110             (String JavaDoc) _namespaceDecls.elementAt(i));
111         }
112         else {
113             tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix,
114             (String JavaDoc) _namespaceDecls.elementAt(i));
115         }
116         }
117         _namespaceDecls.clear();
118     }
119
120     // Add attributes to element
121
final int nattrs = attrs.getLength();
122     for (int i = 0; i < nattrs; i++) {
123         if (attrs.getLocalName(i) == null) {
124         tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
125         }
126         else {
127         tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i),
128             attrs.getValue(i));
129         }
130     }
131
132     // Append this new node onto current stack node
133
Node JavaDoc last = (Node JavaDoc)_nodeStk.peek();
134     last.appendChild(tmp);
135
136     // Push this node onto stack
137
_nodeStk.push(tmp);
138     }
139
140     public void endElement(String JavaDoc namespace, String JavaDoc localName, String JavaDoc qName) {
141     _nodeStk.pop();
142     }
143
144     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
145     if (_namespaceDecls == null) {
146         _namespaceDecls = new Vector JavaDoc(2);
147     }
148     _namespaceDecls.addElement(prefix);
149     _namespaceDecls.addElement(uri);
150     }
151
152     public void endPrefixMapping(String JavaDoc prefix) {
153     // do nothing
154
}
155
156     /**
157      * This class is only used internally so this method should never
158      * be called.
159      */

160     public void ignorableWhitespace(char[] ch, int start, int length) {
161     }
162
163     /**
164      * adds processing instruction node to DOM.
165      */

166     public void processingInstruction(String JavaDoc target, String JavaDoc data) {
167     final Node JavaDoc last = (Node JavaDoc)_nodeStk.peek();
168     ProcessingInstruction JavaDoc pi = _document.createProcessingInstruction(
169         target, data);
170     if (pi != null) last.appendChild(pi);
171     }
172
173     /**
174      * This class is only used internally so this method should never
175      * be called.
176      */

177     public void setDocumentLocator(Locator JavaDoc locator) {
178     }
179
180     /**
181      * This class is only used internally so this method should never
182      * be called.
183      */

184     public void skippedEntity(String JavaDoc name) {
185     }
186
187
188     /**
189      * Lexical Handler method to create comment node in DOM tree.
190      */

191     public void comment(char[] ch, int start, int length) {
192     final Node JavaDoc last = (Node JavaDoc)_nodeStk.peek();
193     Comment JavaDoc comment = _document.createComment(new String JavaDoc(ch,start,length));
194     if (comment != null) last.appendChild(comment);
195     }
196
197     // Lexical Handler methods- not implemented
198
public void startCDATA() { }
199     public void endCDATA() { }
200     public void startEntity(java.lang.String JavaDoc name) { }
201     public void endDTD() { }
202     public void endEntity(String JavaDoc name) { }
203     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
204         throws SAXException JavaDoc { }
205
206 }
207
Popular Tags