annotate NGSrich_0.5.5/src/org/jdom/output/DOMOutputter.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: DOMOutputter.java,v 1.43 2007/11/10 05:29:01 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
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
58 package org.jdom.output;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
59
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
60 import java.util.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
61
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
62 import org.jdom.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
63 import org.jdom.adapters.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
64
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
65
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
66 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
67 * Outputs a JDOM {@link org.jdom.Document org.jdom.Document} as a DOM {@link
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
68 * org.w3c.dom.Document org.w3c.dom.Document}.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
69 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
70 * @version $Revision: 1.43 $, $Date: 2007/11/10 05:29:01 $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
71 * @author Brett McLaughlin
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
72 * @author Jason Hunter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
73 * @author Matthew Merlo
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
74 * @author Dan Schaffer
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
75 * @author Yusuf Goolamabbas
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 DOMOutputter {
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: DOMOutputter.java,v $ $Revision: 1.43 $ $Date: 2007/11/10 05:29:01 $ $Name: jdom_1_1_1 $";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
82
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
83 /** Default adapter class */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
84 private static final String DEFAULT_ADAPTER_CLASS =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
85 "org.jdom.adapters.XercesDOMAdapter";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
86
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
87 /** Adapter to use for interfacing with the DOM implementation */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
88 private String adapterClass;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
89
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
90 /** Output a DOM with namespaces but just the empty namespace */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
91 private boolean forceNamespaceAware;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
92
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
93 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
94 * This creates a new DOMOutputter which will attempt to first locate
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
95 * a DOM implementation to use via JAXP, and if JAXP does not exist or
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
96 * there's a problem, will fall back to the default parser.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
97 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
98 public DOMOutputter() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
99 // nothing
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
100 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
101
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
102 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
103 * This creates a new DOMOutputter using the specified DOMAdapter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
104 * implementation as a way to choose the underlying parser.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
105 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
106 * @param adapterClass <code>String</code> name of class
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
107 * to use for DOM output
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
108 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
109 public DOMOutputter(String adapterClass) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
110 this.adapterClass = adapterClass;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
111 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
112
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
113 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
114 * Controls how NO_NAMESPACE nodes are handeled. If true the outputter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
115 * always creates a namespace aware DOM.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
116 * @param flag
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
117 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
118 public void setForceNamespaceAware(boolean flag) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
119 this.forceNamespaceAware = flag;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
120 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
121
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
122 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
123 * Returns whether DOMs will be constructed with namespaces even when
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
124 * the source document has elements all in the empty namespace.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
125 * @return the forceNamespaceAware flag value
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
126 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
127 public boolean getForceNamespaceAware() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
128 return forceNamespaceAware;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
129 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
130
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
131 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
132 * This converts the JDOM <code>Document</code> parameter to a
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
133 * DOM Document, returning the DOM version. The DOM implementation
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
134 * is the one chosen in the constructor.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
135 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
136 * @param document <code>Document</code> to output.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
137 * @return an <code>org.w3c.dom.Document</code> version
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
138 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
139 public org.w3c.dom.Document output(Document document)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
140 throws JDOMException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
141 NamespaceStack namespaces = new NamespaceStack();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
142
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
143 org.w3c.dom.Document domDoc = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
144 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
145 // Assign DOCTYPE during construction
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
146 DocType dt = document.getDocType();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
147 domDoc = createDOMDocument(dt);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
148
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
149 // Add content
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
150 Iterator itr = document.getContent().iterator();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
151 while (itr.hasNext()) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
152 Object node = itr.next();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
153
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
154 if (node instanceof Element) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
155 Element element = (Element) node;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
156 org.w3c.dom.Element domElement =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
157 output(element, domDoc, namespaces);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
158 // Add the root element, first check for existing root
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
159 org.w3c.dom.Element root = domDoc.getDocumentElement();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
160 if (root == null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
161 // Normal case
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
162 domDoc.appendChild(domElement); // normal case
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
163 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
164 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
165 // Xerces 1.3 creates new docs with a <root />
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
166 // XXX: Need to address DOCTYPE mismatch still
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
167 domDoc.replaceChild(domElement, root);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
168 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
169 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
170 else if (node instanceof Comment) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
171 Comment comment = (Comment) node;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
172 org.w3c.dom.Comment domComment =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
173 domDoc.createComment(comment.getText());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
174 domDoc.appendChild(domComment);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
175 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
176 else if (node instanceof ProcessingInstruction) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
177 ProcessingInstruction pi =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
178 (ProcessingInstruction) node;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
179 org.w3c.dom.ProcessingInstruction domPI =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
180 domDoc.createProcessingInstruction(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
181 pi.getTarget(), pi.getData());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
182 domDoc.appendChild(domPI);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
183 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
184 else if (node instanceof DocType) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
185 // We already dealt with the DocType above
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
186 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
187 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
188 throw new JDOMException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
189 "Document contained top-level content with type:" +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
190 node.getClass().getName());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
191 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
192 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
193 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
194 catch (Throwable e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
195 throw new JDOMException("Exception outputting Document", e);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
196 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
197
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
198 return domDoc;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
199 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
200
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
201 private org.w3c.dom.Document createDOMDocument(DocType dt)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
202 throws JDOMException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
203 if (adapterClass != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
204 // The user knows that they want to use a particular impl
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
205 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
206 DOMAdapter adapter =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
207 (DOMAdapter)Class.forName(adapterClass).newInstance();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
208 // System.out.println("using specific " + adapterClass);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
209 return adapter.createDocument(dt);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
210 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
211 catch (ClassNotFoundException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
212 // e.printStackTrace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
213 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
214 catch (IllegalAccessException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
215 // e.printStackTrace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
216 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
217 catch (InstantiationException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
218 // e.printStackTrace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
219 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
220 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
221 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
222 // Try using JAXP...
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
223 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
224 DOMAdapter adapter =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
225 (DOMAdapter)Class.forName(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
226 "org.jdom.adapters.JAXPDOMAdapter").newInstance();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
227 // System.out.println("using JAXP");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
228 return adapter.createDocument(dt);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
229 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
230 catch (ClassNotFoundException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
231 // e.printStackTrace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
232 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
233 catch (IllegalAccessException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
234 // e.printStackTrace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
235 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
236 catch (InstantiationException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
237 // e.printStackTrace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
238 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
239 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
240
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
241 // If no DOM doc yet, try to use a hard coded default
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
242 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
243 DOMAdapter adapter = (DOMAdapter)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
244 Class.forName(DEFAULT_ADAPTER_CLASS).newInstance();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
245 return adapter.createDocument(dt);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
246 // System.out.println("Using default " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
247 // DEFAULT_ADAPTER_CLASS);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
248 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
249 catch (ClassNotFoundException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
250 // e.printStackTrace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
251 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
252 catch (IllegalAccessException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
253 // e.printStackTrace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
254 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
255 catch (InstantiationException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
256 // e.printStackTrace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
257 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
258
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
259 throw new JDOMException("No JAXP or default parser available");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
260
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
261 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
262
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
263 private org.w3c.dom.Element output(Element element,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
264 org.w3c.dom.Document domDoc,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
265 NamespaceStack namespaces)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
266 throws JDOMException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
267 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
268 int previouslyDeclaredNamespaces = namespaces.size();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
269
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
270 org.w3c.dom.Element domElement = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
271 if (element.getNamespace() == Namespace.NO_NAMESPACE) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
272 // No namespace, use createElement
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
273 domElement = forceNamespaceAware ?
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
274 domDoc.createElementNS(null, element.getQualifiedName())
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
275 : domDoc.createElement(element.getQualifiedName()); }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
276 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
277 domElement = domDoc.createElementNS(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
278 element.getNamespaceURI(),
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
279 element.getQualifiedName());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
280 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
281
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
282 // Add namespace attributes, beginning with the element's own
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
283 // Do this only if it's not the XML namespace and it's
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
284 // not the NO_NAMESPACE with the prefix "" not yet mapped
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
285 // (we do output xmlns="" if the "" prefix was already used
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
286 // and we need to reclaim it for the NO_NAMESPACE)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
287 Namespace ns = element.getNamespace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
288 if (ns != Namespace.XML_NAMESPACE &&
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
289 !(ns == Namespace.NO_NAMESPACE &&
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
290 namespaces.getURI("") == null)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
291 String prefix = ns.getPrefix();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
292 String uri = namespaces.getURI(prefix);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
293 if (!ns.getURI().equals(uri)) { // output a new namespace decl
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
294 namespaces.push(ns);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
295 String attrName = getXmlnsTagFor(ns);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
296 domElement.setAttribute(attrName, ns.getURI());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
297 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
298 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
299
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
300 // Add additional namespaces also
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
301 Iterator itr = element.getAdditionalNamespaces().iterator();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
302 while (itr.hasNext()) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
303 Namespace additional = (Namespace)itr.next();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
304 String prefix = additional.getPrefix();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
305 String uri = namespaces.getURI(prefix);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
306 if (!additional.getURI().equals(uri)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
307 String attrName = getXmlnsTagFor(additional);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
308 domElement.setAttribute(attrName, additional.getURI());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
309 namespaces.push(additional);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
310 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
311 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
312
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
313 // Add attributes to the DOM element
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
314 itr = element.getAttributes().iterator();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
315 while (itr.hasNext()) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
316 Attribute attribute = (Attribute) itr.next();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
317 domElement.setAttributeNode(output(attribute, domDoc));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
318 Namespace ns1 = attribute.getNamespace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
319 if ((ns1 != Namespace.NO_NAMESPACE) &&
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
320 (ns1 != Namespace.XML_NAMESPACE)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
321 String prefix = ns1.getPrefix();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
322 String uri = namespaces.getURI(prefix);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
323 if (!ns1.getURI().equals(uri)) { // output a new decl
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
324 String attrName = getXmlnsTagFor(ns1);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
325 domElement.setAttribute(attrName, ns1.getURI());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
326 namespaces.push(ns1);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
327 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
328 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
329 // Crimson doesn't like setAttributeNS() for non-NS attribs
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
330 if (attribute.getNamespace() == Namespace.NO_NAMESPACE) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
331 // No namespace, use setAttribute
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
332 if (forceNamespaceAware) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
333 domElement.setAttributeNS(null,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
334 attribute.getQualifiedName(),
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
335 attribute.getValue());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
336 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
337 domElement.setAttribute(attribute.getQualifiedName(),
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
338 attribute.getValue());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
339 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
340 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
341 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
342 domElement.setAttributeNS(attribute.getNamespaceURI(),
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
343 attribute.getQualifiedName(),
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
344 attribute.getValue());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
345 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
346 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
347
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
348 // Add content to the DOM element
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
349 itr = element.getContent().iterator();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
350 while (itr.hasNext()) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
351 Object node = itr.next();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
352
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
353 if (node instanceof Element) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
354 Element e = (Element) node;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
355 org.w3c.dom.Element domElt = output(e, domDoc, namespaces);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
356 domElement.appendChild(domElt);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
357 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
358 else if (node instanceof String) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
359 String str = (String) node;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
360 org.w3c.dom.Text domText = domDoc.createTextNode(str);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
361 domElement.appendChild(domText);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
362 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
363 else if (node instanceof CDATA) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
364 CDATA cdata = (CDATA) node;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
365 org.w3c.dom.CDATASection domCdata =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
366 domDoc.createCDATASection(cdata.getText());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
367 domElement.appendChild(domCdata);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
368 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
369 else if (node instanceof Text) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
370 Text text = (Text) node;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
371 org.w3c.dom.Text domText =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
372 domDoc.createTextNode(text.getText());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
373 domElement.appendChild(domText);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
374 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
375 else if (node instanceof Comment) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
376 Comment comment = (Comment) node;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
377 org.w3c.dom.Comment domComment =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
378 domDoc.createComment(comment.getText());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
379 domElement.appendChild(domComment);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
380 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
381 else if (node instanceof ProcessingInstruction) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
382 ProcessingInstruction pi =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
383 (ProcessingInstruction) node;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
384 org.w3c.dom.ProcessingInstruction domPI =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
385 domDoc.createProcessingInstruction(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
386 pi.getTarget(), pi.getData());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
387 domElement.appendChild(domPI);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
388 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
389 else if (node instanceof EntityRef) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
390 EntityRef entity = (EntityRef) node;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
391 org.w3c.dom.EntityReference domEntity =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
392 domDoc.createEntityReference(entity.getName());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
393 domElement.appendChild(domEntity);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
394 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
395 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
396 throw new JDOMException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
397 "Element contained content with type:" +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
398 node.getClass().getName());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
399 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
400 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
401
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
402 // Remove declared namespaces from stack
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
403 while (namespaces.size() > previouslyDeclaredNamespaces) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
404 namespaces.pop();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
405 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
406
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
407 return domElement;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
408 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
409 catch (Exception e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
410 throw new JDOMException("Exception outputting Element " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
411 element.getQualifiedName(), e);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
412 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
413 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
414
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
415 private org.w3c.dom.Attr output(Attribute attribute,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
416 org.w3c.dom.Document domDoc)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
417 throws JDOMException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
418 org.w3c.dom.Attr domAttr = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
419 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
420 if (attribute.getNamespace() == Namespace.NO_NAMESPACE) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
421 // No namespace, use createAttribute
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
422 if (forceNamespaceAware) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
423 domAttr = domDoc.createAttributeNS(null, attribute.getQualifiedName());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
424 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
425 domAttr = domDoc.createAttribute(attribute.getQualifiedName());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
426 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
427 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
428 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
429 domAttr = domDoc.createAttributeNS(attribute.getNamespaceURI(),
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
430 attribute.getQualifiedName());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
431 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
432 domAttr.setValue(attribute.getValue());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
433 } catch (Exception e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
434 throw new JDOMException("Exception outputting Attribute " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
435 attribute.getQualifiedName(), e);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
436 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
437 return domAttr;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
438 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
439
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
440 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
441 * This will handle adding any <code>{@link Namespace}</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
442 * attributes to the DOM tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
443 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
444 * @param ns <code>Namespace</code> to add definition of
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
445 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
446 private static String getXmlnsTagFor(Namespace ns) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
447 String attrName = "xmlns";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
448 if (!ns.getPrefix().equals("")) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
449 attrName += ":";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
450 attrName += ns.getPrefix();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
451 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
452 return attrName;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
453 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
454 }