KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdom > adapters > JAXPDOMAdapter


1 /*--
2
3  $Id: JAXPDOMAdapter.java,v 1.12 2004/02/06 09:28:31 jhunter Exp $
4
5  Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
6  All rights reserved.
7  
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11  
12  1. Redistributions of source code must retain the above copyright
13     notice, this list of conditions, and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright
16     notice, this list of conditions, and the disclaimer that follows
17     these conditions in the documentation and/or other materials
18     provided with the distribution.
19
20  3. The name "JDOM" must not be used to endorse or promote products
21     derived from this software without prior written permission. For
22     written permission, please contact <request_AT_jdom_DOT_org>.
23  
24  4. Products derived from this software may not be called "JDOM", nor
25     may "JDOM" appear in their name, without prior written permission
26     from the JDOM Project Management <request_AT_jdom_DOT_org>.
27  
28  In addition, we request (but do not require) that you include in the
29  end-user documentation provided with the redistribution and/or in the
30  software itself an acknowledgement equivalent to the following:
31      "This product includes software developed by the
32       JDOM Project (http://www.jdom.org/)."
33  Alternatively, the acknowledgment may be graphical using the logos
34  available at http://www.jdom.org/images/logos.
35
36  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
40  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  SUCH DAMAGE.
48
49  This software consists of voluntary contributions made by many
50  individuals on behalf of the JDOM Project and was originally
51  created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
52  Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
53  on the JDOM Project, please see <http://www.jdom.org/>.
54  
55  */

56
57 package org.jdom.adapters;
58
59 import java.io.*;
60 import java.lang.reflect.*;
61
62 import org.jdom.*;
63 import org.jdom.input.*;
64 import org.w3c.dom.Document JavaDoc;
65
66 /**
67  * An adapter for any parser supporting the Sun JAXP APIs.
68  *
69  * @version $Revision: 1.12 $, $Date: 2004/02/06 09:28:31 $
70  * @author Jason Hunter
71  */

72 public class JAXPDOMAdapter extends AbstractDOMAdapter {
73
74     private static final String JavaDoc CVS_ID =
75       "@(#) $RCSfile: JAXPDOMAdapter.java,v $ $Revision: 1.12 $ $Date: 2004/02/06 09:28:31 $ $Name: $";
76
77     /**
78      * This creates a new <code>{@link Document}</code> from an
79      * existing <code>InputStream</code> by letting a JAXP
80      * parser handle parsing using the supplied stream.
81      *
82      * @param in <code>InputStream</code> to parse.
83      * @param validate <code>boolean</code> to indicate if validation
84      * should occur.
85      * @return <code>Document</code> - instance ready for use.
86      * @throws IOException when I/O error occurs.
87      * @throws JDOMException when errors occur in parsing.
88       */

89     public Document JavaDoc getDocument(InputStream in, boolean validate)
90         throws IOException, JDOMException {
91
92         try {
93             // Try using JAXP...
94
// Note we need DOM Level 2 and thus JAXP 1.1.
95
Class.forName("javax.xml.transform.Transformer");
96
97             // Try JAXP 1.1 calls to build the document
98
Class JavaDoc factoryClass =
99                 Class.forName("javax.xml.parsers.DocumentBuilderFactory");
100
101             // factory = DocumentBuilderFactory.newInstance();
102
Method newParserInstance =
103                 factoryClass.getMethod("newInstance", null);
104             Object JavaDoc factory = newParserInstance.invoke(null, null);
105
106             // factory.setValidating(validate);
107
Method setValidating =
108                 factoryClass.getMethod("setValidating",
109                                    new Class JavaDoc[]{boolean.class});
110             setValidating.invoke(factory,
111                                  new Object JavaDoc[]{new Boolean JavaDoc(validate)});
112
113             // factory.setNamespaceAware(true);
114
Method setNamespaceAware =
115                 factoryClass.getMethod("setNamespaceAware",
116                                        new Class JavaDoc[]{boolean.class});
117             setNamespaceAware.invoke(factory,
118                                  new Object JavaDoc[]{Boolean.TRUE});
119     
120             // jaxpParser = factory.newDocumentBuilder();
121
Method newDocBuilder =
122                 factoryClass.getMethod("newDocumentBuilder", null);
123             Object JavaDoc jaxpParser = newDocBuilder.invoke(factory, null);
124
125             // jaxpParser.setErrorHandler(null);
126
Class JavaDoc parserClass = jaxpParser.getClass();
127             Method setErrorHandler =
128                 parserClass.getMethod("setErrorHandler",
129                                  new Class JavaDoc[]{org.xml.sax.ErrorHandler JavaDoc.class});
130             setErrorHandler.invoke(jaxpParser,
131                                  new Object JavaDoc[]{new BuilderErrorHandler()});
132
133             // domDoc = jaxpParser.parse(in);
134
Method parse = parserClass.getMethod(
135                 "parse", new Class JavaDoc[]{InputStream.class});
136             org.w3c.dom.Document JavaDoc domDoc = (org.w3c.dom.Document JavaDoc)
137                 parse.invoke(jaxpParser, new Object JavaDoc[]{in});
138
139             return domDoc;
140         } catch (InvocationTargetException e) {
141             Throwable JavaDoc targetException = e.getTargetException();
142             if (targetException instanceof IOException) {
143                 throw (IOException) targetException;
144             } else {
145                 throw new JDOMException(targetException.getMessage(), targetException);
146             }
147         } catch (Exception JavaDoc e) {
148             throw new JDOMException("Reflection failed while parsing a document with JAXP", e);
149         }
150
151         // Allow all exceptions to pass through
152
}
153
154     /**
155      * This creates an empty <code>Document</code> object based
156      * on a specific parser implementation.
157      *
158      * @return <code>Document</code> - created DOM Document.
159      * @throws JDOMException when errors occur in parsing.
160       */

161     public Document JavaDoc createDocument()
162         throws JDOMException {
163
164         try {
165             // We need DOM Level 2 and thus JAXP 1.1.
166
// If JAXP 1.0 is all that's available then we error out.
167
Class.forName("javax.xml.transform.Transformer");
168
169             // Try JAXP 1.1 calls to build the document
170
Class JavaDoc factoryClass =
171                 Class.forName("javax.xml.parsers.DocumentBuilderFactory");
172
173             // factory = DocumentBuilderFactory.newInstance();
174
Method newParserInstance =
175                 factoryClass.getMethod("newInstance", null);
176             Object JavaDoc factory = newParserInstance.invoke(null, null);
177
178             // jaxpParser = factory.newDocumentBuilder();
179
Method newDocBuilder =
180                 factoryClass.getMethod("newDocumentBuilder", null);
181             Object JavaDoc jaxpParser = newDocBuilder.invoke(factory, null);
182
183             // domDoc = jaxpParser.newDocument();
184
Class JavaDoc parserClass = jaxpParser.getClass();
185             Method newDoc = parserClass.getMethod("newDocument", null);
186             org.w3c.dom.Document JavaDoc domDoc =
187                 (org.w3c.dom.Document JavaDoc) newDoc.invoke(jaxpParser, null);
188
189             return domDoc;
190         } catch (Exception JavaDoc e) {
191             throw new JDOMException("Reflection failed while creating new JAXP document", e);
192         }
193
194     }
195 }
196
Popular Tags