~ubuntu-branches/ubuntu/karmic/libxerces2-java/karmic

« back to all changes in this revision

Viewing changes to src/org/apache/xerces/util/XMLCatalogResolver.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-12-04 17:37:55 UTC
  • mfrom: (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20061204173755-hb6ybrrrk097zhx7
Tags: 2.8.1-1ubuntu1
* Merge with Debian unstable; remaining changes:
  - Build -gcj package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * The Apache Software License, Version 1.1
3
 
 *
4
 
 *
5
 
 * Copyright (c) 2004 The Apache Software Foundation.  All rights 
6
 
 * 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 following disclaimer in
17
 
 *    the documentation and/or other materials provided with the
18
 
 *    distribution.
19
 
 *
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.
26
 
 *
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.
31
 
 *
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.
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 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
47
 
 * SUCH DAMAGE.
48
 
 * ====================================================================
49
 
 *
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.
 
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.
56
15
 */
57
16
 
58
17
package org.apache.xerces.util;
59
18
 
60
19
import java.io.IOException;
61
20
 
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;
 
24
 
 
25
import org.w3c.dom.ls.LSInput;
 
26
import org.w3c.dom.ls.LSResourceResolver;
65
27
 
66
28
import javax.xml.parsers.SAXParserFactory;
67
29
 
 
30
import org.apache.xerces.dom.DOMInputImpl;
68
31
import org.apache.xerces.jaxp.SAXParserFactoryImpl;
69
32
 
70
33
import org.apache.xerces.xni.XNIException;
86
49
 * OASIS XML Catalogs Specification</a>. It encapsulates the 
87
50
 * <a href="http://xml.apache.org/commons/">XML Commons</a> resolver. 
88
51
 * An instance of this class may be registered on the parser 
89
 
 * as a SAX entity resolver or as an XNI entity resolver by setting  
90
 
 * the property (http://apache.org/xml/properties/internal/entity-resolver).</p>
 
52
 * as a SAX entity resolver, as a DOM LSResourceResolver or 
 
53
 * as an XNI entity resolver by setting the property
 
54
 * (http://apache.org/xml/properties/internal/entity-resolver).</p>
91
55
 * 
92
56
 * <p>It is intended that this class may be used standalone to perform 
93
57
 * catalog resolution outside of a parsing context. It may be shared
95
59
 *
96
60
 * @author Michael Glavassevich, IBM
97
61
 *
98
 
 * @version $Id: XMLCatalogResolver.java,v 1.2 2004/01/23 15:07:51 mrglavas Exp $
 
62
 * @version $Id: XMLCatalogResolver.java 320293 2005-05-02 21:44:37Z mrglavas $
99
63
 */
100
64
public class XMLCatalogResolver 
101
 
    implements XMLEntityResolver, EntityResolver {
 
65
    implements XMLEntityResolver, EntityResolver2, LSResourceResolver {
102
66
    
103
67
    /** Internal catalog manager for Apache catalogs. **/
104
68
    private CatalogManager fResolverCatalogManager = null;
268
232
     * @throws IOException thrown if some i/o error occurs
269
233
     */
270
234
    public InputSource resolveEntity(String publicId, String systemId)
271
 
        throws SAXException, IOException {
 
235
         throws SAXException, IOException {
272
236
        
273
237
        String resolvedId = null;
274
238
        if (publicId != null && systemId != null) {
286
250
        return null;
287
251
    }
288
252
    
 
253
     /**
 
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>
 
259
      * 
 
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
 
264
      * 
 
265
      * @throws SAXException any SAX exception, possibly wrapping another exception
 
266
      * @throws IOException thrown if some i/o error occurs
 
267
      */
 
268
     public InputSource resolveEntity(String name, String publicId, 
 
269
         String baseURI, String systemId) throws SAXException, IOException { 
 
270
        
 
271
         String resolvedId = null;
 
272
         
 
273
         if (!getUseLiteralSystemId() && baseURI != null) {
 
274
             // Attempt to resolve the system identifier against the base URI.
 
275
             try {
 
276
                 URI uri = new URI(new URI(baseURI), systemId);
 
277
                 systemId = uri.toString();
 
278
             }
 
279
             // Ignore the exception. Fallback to the literal system identifier.
 
280
             catch (URI.MalformedURIException ex) {}
 
281
         }
 
282
         
 
283
         if (publicId != null && systemId != null) {
 
284
             resolvedId = resolvePublic(publicId, systemId);
 
285
         }
 
286
         else if (systemId != null) {
 
287
             resolvedId = resolveSystem(systemId);
 
288
         }
 
289
        
 
290
         if (resolvedId != null) {
 
291
             InputSource source = new InputSource(resolvedId);
 
292
             source.setPublicId(publicId);
 
293
             return source;
 
294
         }  
 
295
         return null;
 
296
    }
 
297
 
 
298
     /**
 
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>
 
302
      * 
 
303
      * @param name the identifier of the document root element 
 
304
      * @param baseURI the document's base URI
 
305
      * 
 
306
      * @throws SAXException any SAX exception, possibly wrapping another exception
 
307
      * @throws IOException thrown if some i/o error occurs
 
308
      */
 
309
     public InputSource getExternalSubset(String name, String baseURI)
 
310
         throws SAXException, IOException {
 
311
         return null;
 
312
     }
 
313
 
 
314
    /** 
 
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>
 
319
     * 
 
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
 
329
     */
 
330
    public LSInput resolveResource(String type, String namespaceURI,
 
331
        String publicId, String systemId, String baseURI) {
 
332
 
 
333
        String resolvedId = null;
 
334
        
 
335
        try {
 
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);
 
341
            }
 
342
            
 
343
            if (!getUseLiteralSystemId() && baseURI != null) {
 
344
                // Attempt to resolve the system identifier against the base URI.
 
345
                try {
 
346
                    URI uri = new URI(new URI(baseURI), systemId);
 
347
                    systemId = uri.toString();
 
348
                }
 
349
                // Ignore the exception. Fallback to the literal system identifier.
 
350
                catch (URI.MalformedURIException ex) {}
 
351
            }
 
352
        
 
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);
 
361
                }
 
362
                else if (systemId != null) {
 
363
                    resolvedId = resolveSystem(systemId);
 
364
                }
 
365
            }
 
366
        }
 
367
        // Ignore IOException. It cannot be thrown from this method.
 
368
        catch (IOException ex) {}
 
369
        
 
370
        if (resolvedId != null) {
 
371
            return new DOMInputImpl(publicId, resolvedId, baseURI);
 
372
        }  
 
373
        return null;
 
374
    }
 
375
    
289
376
    
290
377
    /**
291
378
     * <p>Resolves an external entity. If the entity cannot be