KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*--
2
3  $Id: XML4JDOMAdapter.java,v 1.17 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 import org.xml.sax.*;
66
67 /**
68  * An adapter for the IBM XML4J DOM parser.
69  *
70  * @version $Revision: 1.17 $, $Date: 2004/02/06 09:28:31 $
71  * @author Brett McLaughlin
72  * @author Jason Hunter
73  */

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

90     public Document JavaDoc getDocument(InputStream in, boolean validate)
91         throws IOException, JDOMException {
92
93         try {
94             /*
95              * IBM XML4J actually uses the Xerces parser, so this is
96              * all Xerces specific code.
97              */

98
99             // Load the parser class
100
Class JavaDoc parserClass = Class.forName("org.apache.xerces.parsers.DOMParser");
101             Object JavaDoc parser = parserClass.newInstance();
102
103             // Set validation
104
Method setFeature =
105                 parserClass.getMethod("setFeature",
106                                       new Class JavaDoc[] {java.lang.String JavaDoc.class,
107                                                    boolean.class});
108             setFeature.invoke(parser, new Object JavaDoc[] {"http://xml.org/sax/features/validation",
109                                                     new Boolean JavaDoc(validate)});
110
111             // Set namespaces
112
setFeature.invoke(parser, new Object JavaDoc[] {"http://xml.org/sax/features/namespaces",
113                                                     new Boolean JavaDoc(false)});
114
115             // Set the error handler
116
if (validate) {
117                 Method setErrorHandler =
118                     parserClass.getMethod("setErrorHandler",
119                         new Class JavaDoc[] {ErrorHandler.class});
120                 setErrorHandler.invoke(parser, new Object JavaDoc[] {new BuilderErrorHandler()});
121             }
122
123             // Parse the document
124
Method parse =
125                 parserClass.getMethod("parse",
126                                       new Class JavaDoc[] {org.xml.sax.InputSource JavaDoc.class});
127             parse.invoke(parser, new Object JavaDoc[]{new InputSource(in)});
128
129             // Get the Document object
130
Method getDocument = parserClass.getMethod("getDocument", null);
131             Document JavaDoc doc = (Document JavaDoc)getDocument.invoke(parser, null);
132
133             return doc;
134         } catch (InvocationTargetException e) {
135             Throwable JavaDoc targetException = e.getTargetException();
136             if (targetException instanceof org.xml.sax.SAXParseException JavaDoc) {
137                 SAXParseException parseException = (SAXParseException)targetException;
138                 throw new JDOMException("Error on line " + parseException.getLineNumber() +
139                                       " of XML document: " + parseException.getMessage(), parseException);
140             } else if (targetException instanceof IOException) {
141                 IOException ioException = (IOException) targetException;
142                 throw ioException;
143             } else {
144                 throw new JDOMException(targetException.getMessage(), targetException);
145             }
146         } catch (Exception JavaDoc e) {
147             throw new JDOMException(e.getClass().getName() + ": " +
148                                   e.getMessage(), e);
149         }
150     }
151
152     /**
153      * This creates an empty <code>Document</code> object based
154      * on a specific parser implementation.
155      *
156      * @return <code>Document</code> - created DOM Document.
157      * @throws JDOMException when errors occur.
158      */

159     public Document JavaDoc createDocument() throws JDOMException {
160         try {
161             return
162                 (Document JavaDoc)Class.forName(
163                     "org.apache.xerces.dom.DocumentImpl")
164                     .newInstance();
165
166         } catch (Exception JavaDoc e) {
167             throw new JDOMException(e.getClass().getName() + ": " +
168                                   e.getMessage() + " while creating document", e);
169         }
170     }
171 }
172
Popular Tags