KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > 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.1.2.3 2006/09/27 18:40:59 spericas Exp $
18  */

19
20
21 package com.sun.org.apache.xalan.internal.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 com.sun.org.apache.xalan.internal.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.Text JavaDoc;
36 import org.w3c.dom.ProcessingInstruction JavaDoc;
37 import org.xml.sax.Attributes JavaDoc;
38 import org.xml.sax.ContentHandler JavaDoc;
39 import org.xml.sax.Locator JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.ext.LexicalHandler JavaDoc;
42
43 /**
44  * @author G. Todd Miller
45  */

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

178     public void ignorableWhitespace(char[] ch, int start, int length) {
179     }
180
181     /**
182      * adds processing instruction node to DOM.
183      */

184     public void processingInstruction(String JavaDoc target, String JavaDoc data) {
185     final Node JavaDoc last = (Node JavaDoc)_nodeStk.peek();
186     ProcessingInstruction JavaDoc pi = _document.createProcessingInstruction(
187         target, data);
188     if (pi != null){
189           last.appendChild(pi);
190           _lastSibling = pi;
191         }
192     }
193
194     /**
195      * This class is only used internally so this method should never
196      * be called.
197      */

198     public void setDocumentLocator(Locator JavaDoc locator) {
199     }
200
201     /**
202      * This class is only used internally so this method should never
203      * be called.
204      */

205     public void skippedEntity(String JavaDoc name) {
206     }
207
208
209     /**
210      * Lexical Handler method to create comment node in DOM tree.
211      */

212     public void comment(char[] ch, int start, int length) {
213     final Node JavaDoc last = (Node JavaDoc)_nodeStk.peek();
214     Comment JavaDoc comment = _document.createComment(new String JavaDoc(ch,start,length));
215     if (comment != null){
216           last.appendChild(comment);
217           _lastSibling = comment;
218         }
219     }
220
221     // Lexical Handler methods- not implemented
222
public void startCDATA() { }
223     public void endCDATA() { }
224     public void startEntity(java.lang.String JavaDoc name) { }
225     public void endDTD() { }
226     public void endEntity(String JavaDoc name) { }
227     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
228         throws SAXException JavaDoc { }
229
230 }
231
Popular Tags