Mercurial > repos > pfrommolt > ngsrich
comparison NGSrich_0.5.5/src/org/jdom/adapters/XercesDOMAdapter.java @ 0:89ad0a9cca52 default tip
Uploaded
author | pfrommolt |
---|---|
date | Mon, 21 Nov 2011 08:12:19 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:89ad0a9cca52 |
---|---|
1 /*-- | |
2 | |
3 $Id: XercesDOMAdapter.java,v 1.19 2007/11/10 05:28:59 jhunter Exp $ | |
4 | |
5 Copyright (C) 2000-2007 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; | |
65 import org.xml.sax.*; | |
66 | |
67 /** | |
68 * An adapter for the Apache Xerces DOM parser. | |
69 * | |
70 * @version $Revision: 1.19 $, $Date: 2007/11/10 05:28:59 $ | |
71 * @author Brett McLaughlin | |
72 * @author Jason Hunter | |
73 */ | |
74 public class XercesDOMAdapter extends AbstractDOMAdapter { | |
75 | |
76 private static final String CVS_ID = | |
77 "@(#) $RCSfile: XercesDOMAdapter.java,v $ $Revision: 1.19 $ $Date: 2007/11/10 05:28:59 $ $Name: jdom_1_1_1 $"; | |
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 | |
86 * should occur. | |
87 * @return <code>Document</code> - instance ready for use. | |
88 * @throws IOException when I/O error occurs. | |
89 * @throws JDOMException when errors occur in parsing. | |
90 */ | |
91 public Document getDocument(InputStream in, boolean validate) | |
92 throws IOException, JDOMException { | |
93 | |
94 try { | |
95 // Load the parser class | |
96 Class parserClass = | |
97 Class.forName("org.apache.xerces.parsers.DOMParser"); | |
98 Object parser = parserClass.newInstance(); | |
99 | |
100 // Set validation | |
101 Method setFeature = parserClass.getMethod( | |
102 "setFeature", | |
103 new Class[] {java.lang.String.class, boolean.class}); | |
104 setFeature.invoke(parser, | |
105 new Object[] {"http://xml.org/sax/features/validation", | |
106 new Boolean(validate)}); | |
107 | |
108 // Set namespaces true | |
109 setFeature.invoke(parser, | |
110 new Object[] {"http://xml.org/sax/features/namespaces", | |
111 new Boolean(true)}); | |
112 | |
113 // Set the error handler | |
114 if (validate) { | |
115 Method setErrorHandler = parserClass.getMethod( | |
116 "setErrorHandler", | |
117 new Class[] {ErrorHandler.class}); | |
118 setErrorHandler.invoke(parser, | |
119 new Object[] {new BuilderErrorHandler()}); | |
120 } | |
121 | |
122 // Parse the document | |
123 Method parse = parserClass.getMethod( | |
124 "parse", | |
125 new Class[] {org.xml.sax.InputSource.class}); | |
126 parse.invoke(parser, new Object[]{new InputSource(in)}); | |
127 | |
128 // Get the Document object | |
129 Method getDocument = parserClass.getMethod("getDocument", null); | |
130 Document doc = (Document)getDocument.invoke(parser, null); | |
131 | |
132 return doc; | |
133 } catch (InvocationTargetException e) { | |
134 Throwable targetException = e.getTargetException(); | |
135 if (targetException instanceof org.xml.sax.SAXParseException) { | |
136 SAXParseException parseException = | |
137 (SAXParseException)targetException; | |
138 throw new JDOMException("Error on line " + | |
139 parseException.getLineNumber() + | |
140 " of XML document: " + | |
141 parseException.getMessage(), e); | |
142 } else if (targetException instanceof IOException) { | |
143 IOException ioException = (IOException) targetException; | |
144 throw ioException; | |
145 } else { | |
146 throw new JDOMException(targetException.getMessage(), e); | |
147 } | |
148 } catch (Exception e) { | |
149 throw new JDOMException(e.getClass().getName() + ": " + | |
150 e.getMessage(), e); | |
151 } | |
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. | |
160 */ | |
161 public Document createDocument() throws JDOMException { | |
162 try { | |
163 return (Document)Class.forName( | |
164 "org.apache.xerces.dom.DocumentImpl").newInstance(); | |
165 } catch (Exception e) { | |
166 throw new JDOMException(e.getClass().getName() + ": " + | |
167 e.getMessage() + " when creating document", e); | |
168 } | |
169 } | |
170 } |