~ubuntu-branches/ubuntu/gutsy/libjaxp1.3-java/gutsy

« back to all changes in this revision

Viewing changes to javax/xml/parsers/DocumentBuilder.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2006-08-03 10:30:58 UTC
  • Revision ID: james.westby@ubuntu.com-20060803103058-7jwwiqv9g8w9094d
Tags: upstream-1.3.03
ImportĀ upstreamĀ versionĀ 1.3.03

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2003-2005 The Apache Software Foundation.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
// $Id: DocumentBuilder.java 226208 2005-06-03 18:28:57Z mrglavas $
 
18
 
 
19
package javax.xml.parsers;
 
20
 
 
21
import java.io.File;
 
22
import java.io.IOException;
 
23
import java.io.InputStream;
 
24
 
 
25
import javax.xml.validation.Schema;
 
26
 
 
27
import org.w3c.dom.Document;
 
28
import org.w3c.dom.DOMImplementation;
 
29
 
 
30
import org.xml.sax.EntityResolver;
 
31
import org.xml.sax.ErrorHandler;
 
32
import org.xml.sax.InputSource;
 
33
import org.xml.sax.SAXException;
 
34
 
 
35
/**
 
36
 * Defines the API to obtain DOM Document instances from an XML
 
37
 * document. Using this class, an application programmer can obtain a
 
38
 * {@link Document} from XML.<p>
 
39
 *
 
40
 * An instance of this class can be obtained from the
 
41
 * {@link DocumentBuilderFactory#newDocumentBuilder()} method. Once
 
42
 * an instance of this class is obtained, XML can be parsed from a
 
43
 * variety of input sources. These input sources are InputStreams,
 
44
 * Files, URLs, and SAX InputSources.<p>
 
45
 *
 
46
 * Note that this class reuses several classes from the SAX API. This
 
47
 * does not require that the implementor of the underlying DOM
 
48
 * implementation use a SAX parser to parse XML document into a
 
49
 * <code>Document</code>. It merely requires that the implementation
 
50
 * communicate with the application using these existing APIs.
 
51
 *
 
52
 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
 
53
 * @version $Revision: 226208 $, $Date: 2005-06-03 14:28:57 -0400 (Fri, 03 Jun 2005) $
 
54
 */
 
55
 
 
56
public abstract class DocumentBuilder {
 
57
    
 
58
    private static final boolean DEBUG = false;
 
59
    
 
60
    /** Protected constructor */
 
61
    protected DocumentBuilder () {
 
62
    }
 
63
 
 
64
        /**
 
65
          * <p>Reset this <code>DocumentBuilder</code> to its original configuration.</p>
 
66
          * 
 
67
          * <p><code>DocumentBuilder</code> is reset to the same state as when it was created with
 
68
          * {@link DocumentBuilderFactory#newDocumentBuilder()}.
 
69
          * <code>reset()</code> is designed to allow the reuse of existing <code>DocumentBuilder</code>s
 
70
          * thus saving resources associated with the creation of new <code>DocumentBuilder</code>s.</p>
 
71
          * 
 
72
          * <p>The reset <code>DocumentBuilder</code> is not guaranteed to have the same {@link EntityResolver} or {@link ErrorHandler}
 
73
          * <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.  It is guaranteed to have a functionally equal
 
74
          * <code>EntityResolver</code> and <code>ErrorHandler</code>.</p>
 
75
          * 
 
76
          * @since 1.5
 
77
          */
 
78
        public void reset() {
 
79
        
 
80
                // implementors should override this method
 
81
                throw new UnsupportedOperationException(
 
82
                        "This DocumentBuilder, \"" + this.getClass().getName() + "\", does not support the reset functionality."
 
83
                        + "  Specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\""
 
84
                        + " version \"" + this.getClass().getPackage().getSpecificationVersion() + "\""
 
85
                        );
 
86
        }
 
87
 
 
88
    /**
 
89
     * Parse the content of the given <code>InputStream</code> as an XML
 
90
     * document and return a new DOM {@link Document} object.
 
91
     * An <code>IllegalArgumentException</code> is thrown if the
 
92
     * <code>InputStream</code> is null.
 
93
     *
 
94
     * @param is InputStream containing the content to be parsed.
 
95
     * @return <code>Document</code> result of parsing the
 
96
     *  <code>InputStream</code>
 
97
     * @exception IOException If any IO errors occur.
 
98
     * @exception SAXException If any parse errors occur.
 
99
     * @see org.xml.sax.DocumentHandler
 
100
     */
 
101
    
 
102
    public Document parse(InputStream is)
 
103
        throws SAXException, IOException {
 
104
        if (is == null) {
 
105
            throw new IllegalArgumentException("InputStream cannot be null");
 
106
        }
 
107
        
 
108
        InputSource in = new InputSource(is);
 
109
        return parse(in);
 
110
    }
 
111
 
 
112
    /**
 
113
     * Parse the content of the given <code>InputStream</code> as an
 
114
     * XML document and return a new DOM {@link Document} object.
 
115
     * An <code>IllegalArgumentException</code> is thrown if the
 
116
     * <code>InputStream</code> is null.
 
117
     *
 
118
     * @param is InputStream containing the content to be parsed.
 
119
     * @param systemId Provide a base for resolving relative URIs.
 
120
     * @return A new DOM Document object.
 
121
     * @exception IOException If any IO errors occur.
 
122
     * @exception SAXException If any parse errors occur.
 
123
     * @see org.xml.sax.DocumentHandler
 
124
     */
 
125
    
 
126
    public Document parse(InputStream is, String systemId)
 
127
        throws SAXException, IOException {
 
128
        if (is == null) {
 
129
            throw new IllegalArgumentException("InputStream cannot be null");
 
130
        }
 
131
        
 
132
        InputSource in = new InputSource(is);
 
133
        in.setSystemId(systemId);
 
134
        return parse(in);
 
135
    }
 
136
 
 
137
    /**
 
138
     * Parse the content of the given URI as an XML document
 
139
     * and return a new DOM {@link Document} object.
 
140
     * An <code>IllegalArgumentException</code> is thrown if the
 
141
     * URI is <code>null</code> null.
 
142
     *
 
143
     * @param uri The location of the content to be parsed.
 
144
     * @return A new DOM Document object.
 
145
     * @exception IOException If any IO errors occur.
 
146
     * @exception SAXException If any parse errors occur.
 
147
     * @see org.xml.sax.DocumentHandler
 
148
     */
 
149
    
 
150
    public Document parse(String uri)
 
151
        throws SAXException, IOException {
 
152
        if (uri == null) {
 
153
            throw new IllegalArgumentException("URI cannot be null");
 
154
        }
 
155
        
 
156
        InputSource in = new InputSource(uri);
 
157
        return parse(in);
 
158
    }
 
159
 
 
160
    /**
 
161
     * Parse the content of the given file as an XML document
 
162
     * and return a new DOM {@link Document} object.
 
163
     * An <code>IllegalArgumentException</code> is thrown if the
 
164
     * <code>File</code> is <code>null</code> null.
 
165
     *
 
166
     * @param f The file containing the XML to parse.
 
167
     * @exception IOException If any IO errors occur.
 
168
     * @exception SAXException If any parse errors occur.
 
169
     * @see org.xml.sax.DocumentHandler
 
170
     * @return A new DOM Document object.
 
171
     */
 
172
    
 
173
    public Document parse(File f) throws SAXException, IOException {
 
174
        if (f == null) {
 
175
            throw new IllegalArgumentException("File cannot be null");
 
176
        }
 
177
        
 
178
        String escapedURI = FilePathToURI.filepath2URI(f.getAbsolutePath());
 
179
        
 
180
        if (DEBUG) {
 
181
            System.out.println("Escaped URI = " + escapedURI);
 
182
        }
 
183
 
 
184
        InputSource in = new InputSource(escapedURI);
 
185
        return parse(in);
 
186
    }
 
187
 
 
188
    /**
 
189
     * Parse the content of the given input source as an XML document
 
190
     * and return a new DOM {@link Document} object.
 
191
     * An <code>IllegalArgumentException</code> is thrown if the
 
192
     * <code>InputSource</code> is <code>null</code> null.
 
193
     *
 
194
     * @param is InputSource containing the content to be parsed.
 
195
     * @exception IOException If any IO errors occur.
 
196
     * @exception SAXException If any parse errors occur.
 
197
     * @see org.xml.sax.DocumentHandler
 
198
     * @return A new DOM Document object.
 
199
     */
 
200
    
 
201
    public abstract Document parse(InputSource is)
 
202
        throws  SAXException, IOException;
 
203
 
 
204
    
 
205
    /**
 
206
     * Indicates whether or not this parser is configured to
 
207
     * understand namespaces.
 
208
     *
 
209
     * @return true if this parser is configured to understand
 
210
     *         namespaces; false otherwise.
 
211
     */
 
212
 
 
213
    public abstract boolean isNamespaceAware();
 
214
 
 
215
    /**
 
216
     * Indicates whether or not this parser is configured to
 
217
     * validate XML documents.
 
218
     *
 
219
     * @return true if this parser is configured to validate
 
220
     *         XML documents; false otherwise.
 
221
     */
 
222
    
 
223
    public abstract boolean isValidating();
 
224
 
 
225
    /**
 
226
     * Specify the {@link EntityResolver} to be used to resolve
 
227
     * entities present in the XML document to be parsed. Setting
 
228
     * this to <code>null</code> will result in the underlying
 
229
     * implementation using it's own default implementation and
 
230
     * behavior.
 
231
     *
 
232
     * @param er The <code>EntityResolver</code> to be used to resolve entities
 
233
     *           present in the XML document to be parsed.
 
234
     */
 
235
 
 
236
    public abstract void setEntityResolver(EntityResolver er);
 
237
 
 
238
    /**
 
239
     * Specify the {@link ErrorHandler} to be used by the parser.
 
240
     * Setting this to <code>null</code> will result in the underlying
 
241
     * implementation using it's own default implementation and
 
242
     * behavior.
 
243
     *
 
244
     * @param eh The <code>ErrorHandler</code> to be used by the parser.
 
245
     */
 
246
 
 
247
    public abstract void setErrorHandler(ErrorHandler eh);
 
248
 
 
249
    /**
 
250
     * Obtain a new instance of a DOM {@link Document} object
 
251
     * to build a DOM tree with.
 
252
     *
 
253
     * @return A new instance of a DOM Document object.
 
254
     */
 
255
    
 
256
    public abstract Document newDocument();
 
257
 
 
258
    /**
 
259
     * Obtain an instance of a {@link DOMImplementation} object.
 
260
     *
 
261
     * @return A new instance of a <code>DOMImplementation</code>.
 
262
     */
 
263
 
 
264
    public abstract DOMImplementation getDOMImplementation();
 
265
    
 
266
    /** <p>Get current state of canonicalization.</p>
 
267
     *
 
268
     * @return current state canonicalization control
 
269
     */
 
270
    /*
 
271
    public boolean getCanonicalization() {
 
272
        return canonicalState;
 
273
    }
 
274
    */
 
275
    
 
276
    /** <p>Get a reference to the the {@link Schema} being used by
 
277
     * the XML processor.</p>
 
278
     *
 
279
     * <p>If no schema is being used, <code>null</code> is returned.</p>
 
280
     *
 
281
     * @return {@link Schema} being used or <code>null</code>
 
282
     *  if none in use
 
283
     * 
 
284
     * @throws UnsupportedOperationException
 
285
     *      For backward compatibility, when implementations for
 
286
     *      earlier versions of JAXP is used, this exception will be
 
287
     *      thrown.
 
288
     * 
 
289
     * @since 1.5
 
290
     */
 
291
    public Schema getSchema() {
 
292
        throw new UnsupportedOperationException(
 
293
            "This parser does not support specification \""
 
294
            + this.getClass().getPackage().getSpecificationTitle()
 
295
            + "\" version \""
 
296
            + this.getClass().getPackage().getSpecificationVersion()
 
297
            + "\""
 
298
            );
 
299
    }
 
300
    
 
301
    
 
302
    /**
 
303
     * <p>Get the XInclude processing mode for this parser.</p>
 
304
     * 
 
305
     * @return
 
306
     *      the return value of
 
307
     *      the {@link DocumentBuilderFactory#isXIncludeAware()}
 
308
     *      when this parser was created from factory.
 
309
     * 
 
310
     * @throws UnsupportedOperationException
 
311
     *      For backward compatibility, when implementations for
 
312
     *      earlier versions of JAXP is used, this exception will be
 
313
     *      thrown.
 
314
     * 
 
315
     * @since 1.5
 
316
     * 
 
317
     * @see DocumentBuilderFactory#setXIncludeAware(boolean)
 
318
     */
 
319
    public boolean isXIncludeAware() {
 
320
        throw new UnsupportedOperationException(
 
321
            "This parser does not support specification \""
 
322
            + this.getClass().getPackage().getSpecificationTitle()
 
323
            + "\" version \""
 
324
            + this.getClass().getPackage().getSpecificationVersion()
 
325
            + "\""
 
326
            );
 
327
    }
 
328
}