12005
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 2001-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: SAX2DOM.java,v 1.8.2.1 2006/12/04 18:45:41 spericas Exp $
|
|
22 |
*/
|
|
23 |
|
|
24 |
|
|
25 |
package com.sun.org.apache.xalan.internal.xsltc.trax;
|
|
26 |
|
|
27 |
import java.util.Stack;
|
|
28 |
import java.util.Vector;
|
|
29 |
import javax.xml.parsers.DocumentBuilder;
|
|
30 |
|
|
31 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
32 |
import javax.xml.parsers.ParserConfigurationException;
|
|
33 |
|
|
34 |
import com.sun.org.apache.xalan.internal.xsltc.runtime.Constants;
|
|
35 |
|
|
36 |
import org.w3c.dom.Comment;
|
|
37 |
import org.w3c.dom.Document;
|
|
38 |
import org.w3c.dom.Element;
|
|
39 |
import org.w3c.dom.Node;
|
|
40 |
import org.w3c.dom.Text;
|
|
41 |
import org.w3c.dom.ProcessingInstruction;
|
|
42 |
import org.xml.sax.Attributes;
|
|
43 |
import org.xml.sax.ContentHandler;
|
|
44 |
import org.xml.sax.Locator;
|
|
45 |
import org.xml.sax.SAXException;
|
|
46 |
import org.xml.sax.ext.LexicalHandler;
|
|
47 |
import org.xml.sax.ext.Locator2;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* @author G. Todd Miller
|
|
51 |
* @author Sunitha Reddy
|
|
52 |
*/
|
|
53 |
public class SAX2DOM implements ContentHandler, LexicalHandler, Constants {
|
|
54 |
|
|
55 |
private Node _root = null;
|
|
56 |
private Document _document = null;
|
|
57 |
private Node _nextSibling = null;
|
|
58 |
private Stack _nodeStk = new Stack();
|
|
59 |
private Vector _namespaceDecls = null;
|
|
60 |
private Node _lastSibling = null;
|
|
61 |
private Locator locator = null;
|
|
62 |
private boolean needToSetDocumentInfo = true;
|
|
63 |
|
|
64 |
//Replace StringBuffer with StringBuilder now that we no long support jdk1.4
|
|
65 |
private StringBuilder _textBuffer = new StringBuilder();
|
|
66 |
private Node _nextSiblingCache = null;
|
|
67 |
/**
|
|
68 |
* JAXP document builder factory. Create a single instance and use
|
|
69 |
* synchronization because the Javadoc is not explicit about
|
|
70 |
* thread safety.
|
|
71 |
*/
|
|
72 |
static final DocumentBuilderFactory _factory =
|
|
73 |
DocumentBuilderFactory.newInstance();
|
|
74 |
static final DocumentBuilder _internalBuilder;
|
|
75 |
static {
|
|
76 |
DocumentBuilder tmpBuilder = null;
|
|
77 |
try {
|
|
78 |
if (_factory instanceof com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl) {
|
|
79 |
tmpBuilder = _factory.newDocumentBuilder();
|
|
80 |
}
|
|
81 |
} catch(Exception e) {
|
|
82 |
// It's OK. Will create DocumentBuilder every time
|
|
83 |
}
|
|
84 |
_internalBuilder = tmpBuilder;
|
|
85 |
}
|
|
86 |
|
|
87 |
public SAX2DOM() throws ParserConfigurationException {
|
|
88 |
_document = createDocument();
|
|
89 |
_root = _document;
|
|
90 |
}
|
|
91 |
|
|
92 |
public SAX2DOM(Node root, Node nextSibling) throws ParserConfigurationException {
|
|
93 |
_root = root;
|
|
94 |
if (root instanceof Document) {
|
|
95 |
_document = (Document)root;
|
|
96 |
}
|
|
97 |
else if (root != null) {
|
|
98 |
_document = root.getOwnerDocument();
|
|
99 |
}
|
|
100 |
else {
|
|
101 |
_document = createDocument();
|
|
102 |
_root = _document;
|
|
103 |
}
|
|
104 |
|
|
105 |
_nextSibling = nextSibling;
|
|
106 |
}
|
|
107 |
|
|
108 |
public SAX2DOM(Node root) throws ParserConfigurationException {
|
|
109 |
this(root, null);
|
|
110 |
}
|
|
111 |
|
|
112 |
public Node getDOM() {
|
|
113 |
return _root;
|
|
114 |
}
|
|
115 |
|
|
116 |
public void characters(char[] ch, int start, int length) {
|
|
117 |
// Ignore text nodes of length 0
|
|
118 |
if (length == 0) {
|
|
119 |
return;
|
|
120 |
}
|
|
121 |
|
|
122 |
final Node last = (Node)_nodeStk.peek();
|
|
123 |
|
|
124 |
// No text nodes can be children of root (DOM006 exception)
|
|
125 |
if (last != _document) {
|
|
126 |
_nextSiblingCache = _nextSibling;
|
|
127 |
_textBuffer.append(ch, start, length);
|
|
128 |
}
|
|
129 |
}
|
|
130 |
private void appendTextNode() {
|
|
131 |
if (_textBuffer.length() > 0) {
|
|
132 |
final Node last = (Node)_nodeStk.peek();
|
|
133 |
if (last == _root && _nextSiblingCache != null) {
|
|
134 |
_lastSibling = last.insertBefore(_document.createTextNode(_textBuffer.toString()), _nextSiblingCache);
|
|
135 |
}
|
|
136 |
else {
|
|
137 |
_lastSibling = last.appendChild(_document.createTextNode(_textBuffer.toString()));
|
|
138 |
}
|
|
139 |
_textBuffer.setLength(0);
|
|
140 |
}
|
|
141 |
}
|
|
142 |
public void startDocument() {
|
|
143 |
_nodeStk.push(_root);
|
|
144 |
}
|
|
145 |
|
|
146 |
public void endDocument() {
|
|
147 |
_nodeStk.pop();
|
|
148 |
}
|
|
149 |
|
|
150 |
private void setDocumentInfo() {
|
|
151 |
//try to set document version
|
|
152 |
if (locator == null) return;
|
|
153 |
try{
|
|
154 |
_document.setXmlVersion(((Locator2)locator).getXMLVersion());
|
|
155 |
}catch(ClassCastException e){}
|
|
156 |
|
|
157 |
}
|
|
158 |
|
|
159 |
public void startElement(String namespace, String localName, String qName,
|
|
160 |
Attributes attrs)
|
|
161 |
{
|
|
162 |
appendTextNode();
|
|
163 |
if (needToSetDocumentInfo) {
|
|
164 |
setDocumentInfo();
|
|
165 |
needToSetDocumentInfo = false;
|
|
166 |
}
|
|
167 |
|
|
168 |
final Element tmp = (Element)_document.createElementNS(namespace, qName);
|
|
169 |
|
|
170 |
// Add namespace declarations first
|
|
171 |
if (_namespaceDecls != null) {
|
|
172 |
final int nDecls = _namespaceDecls.size();
|
|
173 |
for (int i = 0; i < nDecls; i++) {
|
|
174 |
final String prefix = (String) _namespaceDecls.elementAt(i++);
|
|
175 |
|
|
176 |
if (prefix == null || prefix.equals(EMPTYSTRING)) {
|
|
177 |
tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX,
|
|
178 |
(String) _namespaceDecls.elementAt(i));
|
|
179 |
}
|
|
180 |
else {
|
|
181 |
tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix,
|
|
182 |
(String) _namespaceDecls.elementAt(i));
|
|
183 |
}
|
|
184 |
}
|
|
185 |
_namespaceDecls.clear();
|
|
186 |
}
|
|
187 |
|
|
188 |
// Add attributes to element
|
|
189 |
/* final int nattrs = attrs.getLength();
|
|
190 |
for (int i = 0; i < nattrs; i++) {
|
|
191 |
if (attrs.getLocalName(i) == null) {
|
|
192 |
tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
|
|
193 |
}
|
|
194 |
else {
|
|
195 |
tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i),
|
|
196 |
attrs.getValue(i));
|
|
197 |
}
|
|
198 |
} */
|
|
199 |
|
|
200 |
|
|
201 |
// Add attributes to element
|
|
202 |
final int nattrs = attrs.getLength();
|
|
203 |
for (int i = 0; i < nattrs; i++) {
|
|
204 |
// checking if Namespace processing is being done
|
|
205 |
String attQName = attrs.getQName(i);
|
|
206 |
String attURI = attrs.getURI(i);
|
|
207 |
if (attrs.getLocalName(i).equals("")) {
|
|
208 |
tmp.setAttribute(attQName, attrs.getValue(i));
|
|
209 |
if (attrs.getType(i).equals("ID")) {
|
|
210 |
tmp.setIdAttribute(attQName, true);
|
|
211 |
}
|
|
212 |
} else {
|
|
213 |
tmp.setAttributeNS(attURI, attQName, attrs.getValue(i));
|
|
214 |
if (attrs.getType(i).equals("ID")) {
|
|
215 |
tmp.setIdAttributeNS(attURI, attrs.getLocalName(i), true);
|
|
216 |
}
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
|
|
221 |
// Append this new node onto current stack node
|
|
222 |
Node last = (Node)_nodeStk.peek();
|
|
223 |
|
|
224 |
// If the SAX2DOM is created with a non-null next sibling node,
|
|
225 |
// insert the result nodes before the next sibling under the root.
|
|
226 |
if (last == _root && _nextSibling != null)
|
|
227 |
last.insertBefore(tmp, _nextSibling);
|
|
228 |
else
|
|
229 |
last.appendChild(tmp);
|
|
230 |
|
|
231 |
// Push this node onto stack
|
|
232 |
_nodeStk.push(tmp);
|
|
233 |
_lastSibling = null;
|
|
234 |
}
|
|
235 |
|
|
236 |
public void endElement(String namespace, String localName, String qName) {
|
|
237 |
appendTextNode();
|
|
238 |
_nodeStk.pop();
|
|
239 |
_lastSibling = null;
|
|
240 |
}
|
|
241 |
|
|
242 |
public void startPrefixMapping(String prefix, String uri) {
|
|
243 |
if (_namespaceDecls == null) {
|
|
244 |
_namespaceDecls = new Vector(2);
|
|
245 |
}
|
|
246 |
_namespaceDecls.addElement(prefix);
|
|
247 |
_namespaceDecls.addElement(uri);
|
|
248 |
}
|
|
249 |
|
|
250 |
public void endPrefixMapping(String prefix) {
|
|
251 |
// do nothing
|
|
252 |
}
|
|
253 |
|
|
254 |
/**
|
|
255 |
* This class is only used internally so this method should never
|
|
256 |
* be called.
|
|
257 |
*/
|
|
258 |
public void ignorableWhitespace(char[] ch, int start, int length) {
|
|
259 |
}
|
|
260 |
|
|
261 |
/**
|
|
262 |
* adds processing instruction node to DOM.
|
|
263 |
*/
|
|
264 |
public void processingInstruction(String target, String data) {
|
|
265 |
appendTextNode();
|
|
266 |
final Node last = (Node)_nodeStk.peek();
|
|
267 |
ProcessingInstruction pi = _document.createProcessingInstruction(
|
|
268 |
target, data);
|
|
269 |
if (pi != null){
|
|
270 |
if (last == _root && _nextSibling != null)
|
|
271 |
last.insertBefore(pi, _nextSibling);
|
|
272 |
else
|
|
273 |
last.appendChild(pi);
|
|
274 |
|
|
275 |
_lastSibling = pi;
|
|
276 |
}
|
|
277 |
}
|
|
278 |
|
|
279 |
/**
|
|
280 |
* This class is only used internally so this method should never
|
|
281 |
* be called.
|
|
282 |
*/
|
|
283 |
public void setDocumentLocator(Locator locator) {
|
|
284 |
this.locator = locator;
|
|
285 |
}
|
|
286 |
|
|
287 |
/**
|
|
288 |
* This class is only used internally so this method should never
|
|
289 |
* be called.
|
|
290 |
*/
|
|
291 |
public void skippedEntity(String name) {
|
|
292 |
}
|
|
293 |
|
|
294 |
|
|
295 |
/**
|
|
296 |
* Lexical Handler method to create comment node in DOM tree.
|
|
297 |
*/
|
|
298 |
public void comment(char[] ch, int start, int length) {
|
|
299 |
appendTextNode();
|
|
300 |
final Node last = (Node)_nodeStk.peek();
|
|
301 |
Comment comment = _document.createComment(new String(ch,start,length));
|
|
302 |
if (comment != null){
|
|
303 |
if (last == _root && _nextSibling != null)
|
|
304 |
last.insertBefore(comment, _nextSibling);
|
|
305 |
else
|
|
306 |
last.appendChild(comment);
|
|
307 |
|
|
308 |
_lastSibling = comment;
|
|
309 |
}
|
|
310 |
}
|
|
311 |
|
|
312 |
// Lexical Handler methods- not implemented
|
|
313 |
public void startCDATA() { }
|
|
314 |
public void endCDATA() { }
|
|
315 |
public void startEntity(java.lang.String name) { }
|
|
316 |
public void endDTD() { }
|
|
317 |
public void endEntity(String name) { }
|
|
318 |
public void startDTD(String name, String publicId, String systemId)
|
|
319 |
throws SAXException {}
|
|
320 |
|
|
321 |
private static Document createDocument() throws ParserConfigurationException {
|
|
322 |
Document doc;
|
|
323 |
if (_internalBuilder != null) {
|
|
324 |
//default implementation is thread safe
|
|
325 |
doc = _internalBuilder.newDocument();
|
|
326 |
} else {
|
|
327 |
synchronized(SAX2DOM.class) {
|
|
328 |
doc = _factory.newDocumentBuilder().newDocument();
|
|
329 |
}
|
|
330 |
}
|
|
331 |
return doc;
|
|
332 |
}
|
|
333 |
|
|
334 |
}
|