annotate NGSrich_0.5.5/src/org/jdom/adapters/JAXPDOMAdapter.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: JAXPDOMAdapter.java,v 1.13 2007/11/10 05:28:59 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.adapters;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
58
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
59 import java.io.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
60 import java.lang.reflect.*;
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.input.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
64 import org.w3c.dom.Document;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
65
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
66 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
67 * An adapter for any parser supporting the Sun JAXP APIs.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
68 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
69 * @version $Revision: 1.13 $, $Date: 2007/11/10 05:28:59 $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
70 * @author Jason Hunter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
71 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
72 public class JAXPDOMAdapter extends AbstractDOMAdapter {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
73
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
74 private static final String CVS_ID =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
75 "@(#) $RCSfile: JAXPDOMAdapter.java,v $ $Revision: 1.13 $ $Date: 2007/11/10 05:28:59 $ $Name: jdom_1_1_1 $";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
76
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
77 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
78 * This creates a new <code>{@link Document}</code> from an
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
79 * existing <code>InputStream</code> by letting a JAXP
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
80 * parser handle parsing using the supplied stream.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
81 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
82 * @param in <code>InputStream</code> to parse.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
83 * @param validate <code>boolean</code> to indicate if validation
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
84 * should occur.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
85 * @return <code>Document</code> - instance ready for use.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
86 * @throws IOException when I/O error occurs.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
87 * @throws JDOMException when errors occur in parsing.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
88 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
89 public Document getDocument(InputStream in, boolean validate)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
90 throws IOException, JDOMException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
91
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
92 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
93 // Try using JAXP...
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
94 // Note we need DOM Level 2 and thus JAXP 1.1.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
95 Class.forName("javax.xml.transform.Transformer");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
96
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
97 // Try JAXP 1.1 calls to build the document
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
98 Class factoryClass =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
99 Class.forName("javax.xml.parsers.DocumentBuilderFactory");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
100
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
101 // factory = DocumentBuilderFactory.newInstance();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
102 Method newParserInstance =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
103 factoryClass.getMethod("newInstance", null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
104 Object factory = newParserInstance.invoke(null, null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
105
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
106 // factory.setValidating(validate);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
107 Method setValidating =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
108 factoryClass.getMethod("setValidating",
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
109 new Class[]{boolean.class});
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
110 setValidating.invoke(factory,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
111 new Object[]{new Boolean(validate)});
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
112
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
113 // factory.setNamespaceAware(true);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
114 Method setNamespaceAware =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
115 factoryClass.getMethod("setNamespaceAware",
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
116 new Class[]{boolean.class});
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
117 setNamespaceAware.invoke(factory,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
118 new Object[]{Boolean.TRUE});
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
119
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
120 // jaxpParser = factory.newDocumentBuilder();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
121 Method newDocBuilder =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
122 factoryClass.getMethod("newDocumentBuilder", null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
123 Object jaxpParser = newDocBuilder.invoke(factory, null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
124
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
125 // jaxpParser.setErrorHandler(null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
126 Class parserClass = jaxpParser.getClass();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
127 Method setErrorHandler =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
128 parserClass.getMethod("setErrorHandler",
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
129 new Class[]{org.xml.sax.ErrorHandler.class});
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
130 setErrorHandler.invoke(jaxpParser,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
131 new Object[]{new BuilderErrorHandler()});
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
132
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
133 // domDoc = jaxpParser.parse(in);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
134 Method parse = parserClass.getMethod(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
135 "parse", new Class[]{InputStream.class});
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
136 org.w3c.dom.Document domDoc = (org.w3c.dom.Document)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
137 parse.invoke(jaxpParser, new Object[]{in});
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
138
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
139 return domDoc;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
140 } catch (InvocationTargetException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
141 Throwable targetException = e.getTargetException();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
142 if (targetException instanceof IOException) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
143 throw (IOException) targetException;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
144 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
145 throw new JDOMException(targetException.getMessage(), targetException);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
146 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
147 } catch (Exception e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
148 throw new JDOMException("Reflection failed while parsing a document with JAXP", e);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
149 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
150
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
151 // Allow all exceptions to pass through
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
152 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
153
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
154 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
155 * This creates an empty <code>Document</code> object based
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
156 * on a specific parser implementation.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
157 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
158 * @return <code>Document</code> - created DOM Document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
159 * @throws JDOMException when errors occur in parsing.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
160 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
161 public Document createDocument()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
162 throws JDOMException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
163
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
164 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
165 // We need DOM Level 2 and thus JAXP 1.1.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
166 // If JAXP 1.0 is all that's available then we error out.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
167 Class.forName("javax.xml.transform.Transformer");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
168
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
169 // Try JAXP 1.1 calls to build the document
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
170 Class factoryClass =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
171 Class.forName("javax.xml.parsers.DocumentBuilderFactory");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
172
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
173 // factory = DocumentBuilderFactory.newInstance();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
174 Method newParserInstance =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
175 factoryClass.getMethod("newInstance", null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
176 Object factory = newParserInstance.invoke(null, null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
177
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
178 // jaxpParser = factory.newDocumentBuilder();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
179 Method newDocBuilder =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
180 factoryClass.getMethod("newDocumentBuilder", null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
181 Object jaxpParser = newDocBuilder.invoke(factory, null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
182
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
183 // domDoc = jaxpParser.newDocument();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
184 Class parserClass = jaxpParser.getClass();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
185 Method newDoc = parserClass.getMethod("newDocument", null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
186 org.w3c.dom.Document domDoc =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
187 (org.w3c.dom.Document) newDoc.invoke(jaxpParser, null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
188
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
189 return domDoc;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
190 } catch (Exception e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
191 throw new JDOMException("Reflection failed while creating new JAXP document", e);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
192 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
193
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
194 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
195 }