2
* The Apache Software License, Version 1.1
5
* Copyright (c) 2004 The Apache Software Foundation. All rights
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
12
* 1. Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in
17
* the documentation and/or other materials provided with the
20
* 3. The end-user documentation included with the redistribution,
21
* if any, must include the following acknowledgment:
22
* "This product includes software developed by the
23
* Apache Software Foundation (http://www.apache.org/)."
24
* Alternately, this acknowledgment may appear in the software itself,
25
* if and wherever such third-party acknowledgments normally appear.
27
* 4. The names "Xerces" and "Apache Software Foundation" must
28
* not be used to endorse or promote products derived from this
29
* software without prior written permission. For written
30
* permission, please contact apache@apache.org.
32
* 5. Products derived from this software may not be called "Apache",
33
* nor may "Apache" appear in their name, without prior written
34
* permission of the Apache Software Foundation.
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 APACHE SOFTWARE FOUNDATION OR
40
* ITS 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
48
* ====================================================================
50
* This software consists of voluntary contributions made by many
51
* individuals on behalf of the Apache Software Foundation and was
52
* originally based on software copyright (c) 1999, International
53
* Business Machines, Inc., http://www.apache.org. For more
54
* information on the Apache Software Foundation, please see
55
* <http://www.apache.org/>.
2
* Copyright 2004,2005 The Apache Software Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
58
17
package org.apache.xerces.util;
60
19
import java.io.IOException;
62
import org.xml.sax.EntityResolver;
63
21
import org.xml.sax.InputSource;
64
22
import org.xml.sax.SAXException;
23
import org.xml.sax.ext.EntityResolver2;
25
import org.w3c.dom.ls.LSInput;
26
import org.w3c.dom.ls.LSResourceResolver;
66
28
import javax.xml.parsers.SAXParserFactory;
30
import org.apache.xerces.dom.DOMInputImpl;
68
31
import org.apache.xerces.jaxp.SAXParserFactoryImpl;
70
33
import org.apache.xerces.xni.XNIException;
254
* <p>Resolves an external entity. If the entity cannot be
255
* resolved, this method should return <code>null</code>. This
256
* method returns an input source if an entry was found in the
257
* catalog for the given external identifier. It should be
258
* overrided if other behaviour is required.</p>
260
* @param name the identifier of the external entity
261
* @param publicId the public identifier, or <code>null</code> if none was supplied
262
* @param baseURI the URI with respect to which relative systemIDs are interpreted.
263
* @param systemId the system identifier
265
* @throws SAXException any SAX exception, possibly wrapping another exception
266
* @throws IOException thrown if some i/o error occurs
268
public InputSource resolveEntity(String name, String publicId,
269
String baseURI, String systemId) throws SAXException, IOException {
271
String resolvedId = null;
273
if (!getUseLiteralSystemId() && baseURI != null) {
274
// Attempt to resolve the system identifier against the base URI.
276
URI uri = new URI(new URI(baseURI), systemId);
277
systemId = uri.toString();
279
// Ignore the exception. Fallback to the literal system identifier.
280
catch (URI.MalformedURIException ex) {}
283
if (publicId != null && systemId != null) {
284
resolvedId = resolvePublic(publicId, systemId);
286
else if (systemId != null) {
287
resolvedId = resolveSystem(systemId);
290
if (resolvedId != null) {
291
InputSource source = new InputSource(resolvedId);
292
source.setPublicId(publicId);
299
* <p>Locates an external subset for documents which do not explicitly
300
* provide one. This method always returns <code>null</code>. It
301
* should be overrided if other behaviour is required.</p>
303
* @param name the identifier of the document root element
304
* @param baseURI the document's base URI
306
* @throws SAXException any SAX exception, possibly wrapping another exception
307
* @throws IOException thrown if some i/o error occurs
309
public InputSource getExternalSubset(String name, String baseURI)
310
throws SAXException, IOException {
315
* <p>Resolves a resource using the catalog. This method interprets that
316
* the namespace URI corresponds to uri entries in the catalog.
317
* Where both a namespace and an external identifier exist, the namespace
318
* takes precedence.</p>
320
* @param type the type of the resource being resolved
321
* @param namespaceURI the namespace of the resource being resolved,
322
* or <code>null</code> if none was supplied
323
* @param publicId the public identifier of the resource being resolved,
324
* or <code>null</code> if none was supplied
325
* @param systemId the system identifier of the resource being resolved,
326
* or <code>null</code> if none was supplied
327
* @param baseURI the absolute base URI of the resource being parsed,
328
* or <code>null</code> if there is no base URI
330
public LSInput resolveResource(String type, String namespaceURI,
331
String publicId, String systemId, String baseURI) {
333
String resolvedId = null;
336
// The namespace is useful for resolving namespace aware
337
// grammars such as XML schema. Let it take precedence over
338
// the external identifier if one exists.
339
if (namespaceURI != null) {
340
resolvedId = resolveURI(namespaceURI);
343
if (!getUseLiteralSystemId() && baseURI != null) {
344
// Attempt to resolve the system identifier against the base URI.
346
URI uri = new URI(new URI(baseURI), systemId);
347
systemId = uri.toString();
349
// Ignore the exception. Fallback to the literal system identifier.
350
catch (URI.MalformedURIException ex) {}
353
// Resolve against an external identifier if one exists. This
354
// is useful for resolving DTD external subsets and other
355
// external entities. For XML schemas if there was no namespace
356
// mapping we might be able to resolve a system identifier
357
// specified as a location hint.
358
if (resolvedId == null) {
359
if (publicId != null && systemId != null) {
360
resolvedId = resolvePublic(publicId, systemId);
362
else if (systemId != null) {
363
resolvedId = resolveSystem(systemId);
367
// Ignore IOException. It cannot be thrown from this method.
368
catch (IOException ex) {}
370
if (resolvedId != null) {
371
return new DOMInputImpl(publicId, resolvedId, baseURI);
291
378
* <p>Resolves an external entity. If the entity cannot be