annotate NGSrich_0.5.5/src/org/jdom/input/DOMBuilder.java @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
1 /*--
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
2
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
3 $Id: DOMBuilder.java,v 1.60 2007/11/10 05:29:00 jhunter Exp $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
4
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
5 Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
6 All rights reserved.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
7
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
8 Redistribution and use in source and binary forms, with or without
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
9 modification, are permitted provided that the following conditions
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
10 are met:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
11
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
12 1. Redistributions of source code must retain the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
13 notice, this list of conditions, and the following disclaimer.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
14
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
15 2. Redistributions in binary form must reproduce the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
16 notice, this list of conditions, and the disclaimer that follows
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
17 these conditions in the documentation and/or other materials
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
18 provided with the distribution.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
19
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
20 3. The name "JDOM" must not be used to endorse or promote products
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
21 derived from this software without prior written permission. For
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
22 written permission, please contact <request_AT_jdom_DOT_org>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
23
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
24 4. Products derived from this software may not be called "JDOM", nor
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
25 may "JDOM" appear in their name, without prior written permission
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
26 from the JDOM Project Management <request_AT_jdom_DOT_org>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
27
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
28 In addition, we request (but do not require) that you include in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
29 end-user documentation provided with the redistribution and/or in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
30 software itself an acknowledgement equivalent to the following:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
31 "This product includes software developed by the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
32 JDOM Project (http://www.jdom.org/)."
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
33 Alternatively, the acknowledgment may be graphical using the logos
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
34 available at http://www.jdom.org/images/logos.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
35
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
36 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
37 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
39 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
40 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
41 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
42 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
43 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
44 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
45 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
46 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
47 SUCH DAMAGE.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
48
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
49 This software consists of voluntary contributions made by many
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
50 individuals on behalf of the JDOM Project and was originally
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
51 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
52 Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
53 on the JDOM Project, please see <http://www.jdom.org/>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
54
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
55 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
56
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
57 package org.jdom.input;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
58
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
59 import org.jdom.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
60 import org.jdom.Document;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
61 import org.jdom.Element;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
62 import org.w3c.dom.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
63
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
64 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
65 * Builds a JDOM {@link org.jdom.Document org.jdom.Document} from a pre-existing
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
66 * DOM {@link org.w3c.dom.Document org.w3c.dom.Document}. Also handy for testing
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
67 * builds from files to sanity check {@link SAXBuilder}.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
68 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
69 * @version $Revision: 1.60 $, $Date: 2007/11/10 05:29:00 $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
70 * @author Brett McLaughlin
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
71 * @author Jason Hunter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
72 * @author Philip Nelson
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
73 * @author Kevin Regan
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
74 * @author Yusuf Goolamabbas
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
75 * @author Dan Schaffer
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
76 * @author Bradley S. Huffman
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
77 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
78 public class DOMBuilder {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
79
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
80 private static final String CVS_ID =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
81 "@(#) $RCSfile: DOMBuilder.java,v $ $Revision: 1.60 $ $Date: 2007/11/10 05:29:00 $ $Name: jdom_1_1_1 $";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
82
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
83 /** Adapter class to use */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
84 private String adapterClass;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
85
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
86 /** The factory for creating new JDOM objects */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
87 private JDOMFactory factory = new DefaultJDOMFactory();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
88
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
89 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
90 * This creates a new DOMBuilder which will attempt to first locate
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
91 * a parser via JAXP, then will try to use a set of default parsers.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
92 * The underlying parser will not validate.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
93 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
94 public DOMBuilder() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
95 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
96
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
97 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
98 * This creates a new DOMBuilder using the specified DOMAdapter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
99 * implementation as a way to choose the underlying parser.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
100 * The underlying parser will not validate.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
101 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
102 * @param adapterClass <code>String</code> name of class
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
103 * to use for DOM building.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
104 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
105 public DOMBuilder(String adapterClass) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
106 this.adapterClass = adapterClass;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
107 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
108
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
109 /*
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
110 * This sets a custom JDOMFactory for the builder. Use this to build
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
111 * the tree with your own subclasses of the JDOM classes.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
112 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
113 * @param factory <code>JDOMFactory</code> to use
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
114 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
115 public void setFactory(JDOMFactory factory) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
116 this.factory = factory;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
117 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
118
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
119 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
120 * Returns the current {@link org.jdom.JDOMFactory} in use.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
121 * @return the factory in use
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
122 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
123 public JDOMFactory getFactory() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
124 return factory;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
125 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
126
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
127 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
128 * This will build a JDOM tree from an existing DOM tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
129 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
130 * @param domDocument <code>org.w3c.dom.Document</code> object
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
131 * @return <code>Document</code> - JDOM document object.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
132 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
133 public Document build(org.w3c.dom.Document domDocument) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
134 Document doc = factory.document(null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
135 buildTree(domDocument, doc, null, true);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
136 return doc;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
137 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
138
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
139 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
140 * This will build a JDOM Element from an existing DOM Element
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
141 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
142 * @param domElement <code> org.w3c.dom.Element</code> object
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
143 * @return <code>Element</code> - JDOM Element object
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
144 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
145 public org.jdom.Element build(org.w3c.dom.Element domElement) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
146 Document doc = factory.document(null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
147 buildTree(domElement, doc, null, true);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
148 return doc.getRootElement();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
149 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
150
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
151 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
152 * This takes a DOM <code>Node</code> and builds up
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
153 * a JDOM tree, recursing until the DOM tree is exhausted
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
154 * and the JDOM tree results.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
155 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
156 * @param node <code>Code</node> to examine.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
157 * @param doc JDOM <code>Document</code> being built.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
158 * @param current <code>Element</code> that is current parent.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
159 * @param atRoot <code>boolean</code> indicating whether at root level.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
160 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
161 private void buildTree(Node node,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
162 Document doc,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
163 Element current,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
164 boolean atRoot) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
165 // Recurse through the tree
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
166 switch (node.getNodeType()) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
167 case Node.DOCUMENT_NODE:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
168 NodeList nodes = node.getChildNodes();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
169 for (int i=0, size=nodes.getLength(); i<size; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
170 buildTree(nodes.item(i), doc, current, true);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
171 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
172 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
173
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
174 case Node.ELEMENT_NODE:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
175 String nodeName = node.getNodeName();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
176 String prefix = "";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
177 String localName = nodeName;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
178 int colon = nodeName.indexOf(':');
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
179 if (colon >= 0) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
180 prefix = nodeName.substring(0, colon);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
181 localName = nodeName.substring(colon + 1);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
182 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
183
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
184 // Get element's namespace
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
185 Namespace ns = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
186 String uri = node.getNamespaceURI();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
187 if (uri == null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
188 ns = (current == null) ? Namespace.NO_NAMESPACE
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
189 : current.getNamespace(prefix);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
190 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
191 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
192 ns = Namespace.getNamespace(prefix, uri);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
193 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
194
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
195 Element element = factory.element(localName, ns);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
196
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
197 if (atRoot) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
198 // If at root, set as document root
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
199 doc.setRootElement(element); // XXX should we use a factory call?
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
200 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
201 // else add to parent element
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
202 factory.addContent(current, element);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
203 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
204
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
205 // Add namespaces
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
206 NamedNodeMap attributeList = node.getAttributes();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
207 int attsize = attributeList.getLength();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
208
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
209 for (int i = 0; i < attsize; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
210 Attr att = (Attr) attributeList.item(i);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
211
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
212 String attname = att.getName();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
213 if (attname.startsWith("xmlns")) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
214 String attPrefix = "";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
215 colon = attname.indexOf(':');
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
216 if (colon >= 0) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
217 attPrefix = attname.substring(colon + 1);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
218 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
219
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
220 String attvalue = att.getValue();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
221
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
222 Namespace declaredNS =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
223 Namespace.getNamespace(attPrefix, attvalue);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
224
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
225 // Add as additional namespaces if it's different
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
226 // than this element's namespace (perhaps we should
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
227 // also have logic not to mark them as additional if
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
228 // it's been done already, but it probably doesn't
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
229 // matter)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
230 if (prefix.equals(attPrefix)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
231 element.setNamespace(declaredNS);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
232 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
233 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
234 factory.addNamespaceDeclaration(element, declaredNS);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
235 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
236 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
237 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
238
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
239 // Add attributes
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
240 for (int i = 0; i < attsize; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
241 Attr att = (Attr) attributeList.item(i);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
242
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
243 String attname = att.getName();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
244
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
245 if ( !attname.startsWith("xmlns")) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
246 String attPrefix = "";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
247 String attLocalName = attname;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
248 colon = attname.indexOf(':');
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
249 if (colon >= 0) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
250 attPrefix = attname.substring(0, colon);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
251 attLocalName = attname.substring(colon + 1);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
252 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
253
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
254 String attvalue = att.getValue();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
255
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
256 // Get attribute's namespace
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
257 Namespace attns = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
258 if ("".equals(attPrefix)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
259 attns = Namespace.NO_NAMESPACE;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
260 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
261 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
262 attns = element.getNamespace(attPrefix);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
263 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
264
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
265 Attribute attribute =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
266 factory.attribute(attLocalName, attvalue, attns);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
267 factory.setAttribute(element, attribute);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
268 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
269 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
270
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
271 // Recurse on child nodes
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
272 // The list should never be null nor should it ever contain
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
273 // null nodes, but some DOM impls are broken
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
274 NodeList children = node.getChildNodes();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
275 if (children != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
276 int size = children.getLength();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
277 for (int i = 0; i < size; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
278 Node item = children.item(i);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
279 if (item != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
280 buildTree(item, doc, element, false);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
281 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
282 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
283 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
284 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
285
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
286 case Node.TEXT_NODE:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
287 String data = node.getNodeValue();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
288 factory.addContent(current, factory.text(data));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
289 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
290
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
291 case Node.CDATA_SECTION_NODE:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
292 String cdata = node.getNodeValue();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
293 factory.addContent(current, factory.cdata(cdata));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
294 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
295
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
296
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
297 case Node.PROCESSING_INSTRUCTION_NODE:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
298 if (atRoot) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
299 factory.addContent(doc,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
300 factory.processingInstruction(node.getNodeName(),
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
301 node.getNodeValue()));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
302 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
303 factory.addContent(current,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
304 factory.processingInstruction(node.getNodeName(),
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
305 node.getNodeValue()));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
306 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
307 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
308
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
309 case Node.COMMENT_NODE:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
310 if (atRoot) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
311 factory.addContent(doc, factory.comment(node.getNodeValue()));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
312 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
313 factory.addContent(current, factory.comment(node.getNodeValue()));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
314 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
315 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
316
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
317 case Node.ENTITY_REFERENCE_NODE:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
318 EntityRef entity = factory.entityRef(node.getNodeName());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
319 factory.addContent(current, entity);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
320 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
321
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
322 case Node.ENTITY_NODE:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
323 // ??
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
324 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
325
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
326 case Node.DOCUMENT_TYPE_NODE:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
327 DocumentType domDocType = (DocumentType)node;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
328 String publicID = domDocType.getPublicId();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
329 String systemID = domDocType.getSystemId();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
330 String internalDTD = domDocType.getInternalSubset();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
331
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
332 DocType docType = factory.docType(domDocType.getName());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
333 docType.setPublicID(publicID);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
334 docType.setSystemID(systemID);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
335 docType.setInternalSubset(internalDTD);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
336
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
337 factory.addContent(doc, docType);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
338 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
339 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
340 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
341 }