Mercurial > repos > pfrommolt > ngsrich
comparison NGSrich_0.5.5/src/org/jdom/adapters/AbstractDOMAdapter.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: AbstractDOMAdapter.java,v 1.21 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.w3c.dom.*; | |
64 import org.w3c.dom.Document; | |
65 | |
66 /** | |
67 * A DOMAdapter utility abstract base class. | |
68 * | |
69 * @version $Revision: 1.21 $, $Date: 2007/11/10 05:28:59 $ | |
70 * @author Brett McLaughlin | |
71 * @author Jason Hunter | |
72 */ | |
73 public abstract class AbstractDOMAdapter implements DOMAdapter { | |
74 | |
75 private static final String CVS_ID = | |
76 "@(#) $RCSfile: AbstractDOMAdapter.java,v $ $Revision: 1.21 $ $Date: 2007/11/10 05:28:59 $ $Name: jdom_1_1_1 $"; | |
77 | |
78 /** | |
79 * This creates a new <code>{@link Document}</code> from an | |
80 * existing <code>InputStream</code> by letting a DOM | |
81 * parser handle parsing using the supplied stream. | |
82 * | |
83 * @param filename file to parse. | |
84 * @param validate <code>boolean</code> to indicate if validation 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 getDocument(File filename, boolean validate) | |
90 throws IOException, JDOMException { | |
91 | |
92 return getDocument(new FileInputStream(filename), validate); | |
93 } | |
94 | |
95 /** | |
96 * This creates a new <code>{@link Document}</code> from an | |
97 * existing <code>InputStream</code> by letting a DOM | |
98 * parser handle parsing using the supplied stream. | |
99 * | |
100 * @param in <code>InputStream</code> to parse. | |
101 * @param validate <code>boolean</code> to indicate if validation should occur. | |
102 * @return <code>Document</code> - instance ready for use. | |
103 * @throws IOException when I/O error occurs. | |
104 * @throws JDOMException when errors occur in parsing. | |
105 */ | |
106 public abstract Document getDocument(InputStream in, boolean validate) | |
107 throws IOException, JDOMException; | |
108 | |
109 /** | |
110 * This creates an empty <code>Document</code> object based | |
111 * on a specific parser implementation. | |
112 * | |
113 * @return <code>Document</code> - created DOM Document. | |
114 * @throws JDOMException when errors occur. | |
115 */ | |
116 public abstract Document createDocument() throws JDOMException; | |
117 | |
118 /** | |
119 * This creates an empty <code>Document</code> object based | |
120 * on a specific parser implementation with the given DOCTYPE. | |
121 * If the doctype parameter is null, the behavior is the same as | |
122 * calling <code>createDocument()</code>. | |
123 * | |
124 * @param doctype Initial <code>DocType</code> of the document. | |
125 * @return <code>Document</code> - created DOM Document. | |
126 * @throws JDOMException when errors occur. | |
127 */ | |
128 public Document createDocument(DocType doctype) throws JDOMException { | |
129 if (doctype == null) { | |
130 return createDocument(); | |
131 } | |
132 | |
133 DOMImplementation domImpl = createDocument().getImplementation(); | |
134 DocumentType domDocType = domImpl.createDocumentType( | |
135 doctype.getElementName(), | |
136 doctype.getPublicID(), | |
137 doctype.getSystemID()); | |
138 | |
139 // Set the internal subset if possible | |
140 setInternalSubset(domDocType, doctype.getInternalSubset()); | |
141 | |
142 return domImpl.createDocument("http://temporary", | |
143 doctype.getElementName(), | |
144 domDocType); | |
145 } | |
146 | |
147 /** | |
148 * This attempts to change the DocumentType to have the given internal DTD | |
149 * subset value. This is not a standard ability in DOM, so it's only | |
150 * available with some parsers. Subclasses can alter the mechanism by | |
151 * which the attempt is made to set the value. | |
152 * | |
153 * @param dt DocumentType to be altered | |
154 * @param s String to use as the internal DTD subset | |
155 */ | |
156 protected void setInternalSubset(DocumentType dt, String s) { | |
157 if (dt == null || s == null) return; | |
158 | |
159 // Default behavior is to attempt a setInternalSubset() call using | |
160 // reflection. This method is not part of the DOM spec, but it's | |
161 // available on Xerces 1.4.4+. It's not currently in Crimson. | |
162 try { | |
163 Class dtclass = dt.getClass(); | |
164 Method setInternalSubset = dtclass.getMethod( | |
165 "setInternalSubset", new Class[] {java.lang.String.class}); | |
166 setInternalSubset.invoke(dt, new Object[] {s}); | |
167 } | |
168 catch (Exception e) { | |
169 // ignore | |
170 } | |
171 } | |
172 } |