jaxp/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/util/XMLCatalogResolver.java
author joehw
Wed, 14 Sep 2016 13:18:17 -0700
changeset 41021 444f2b1bcc8c
parent 39080 57563e513b11
child 44797 8b3b3b911b8a
permissions -rw-r--r--
8165784: Deprecate the internal Catalog API in JDK 9 Reviewed-by: dfuchs, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 2004,2005 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
package com.sun.org.apache.xerces.internal.util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import org.xml.sax.InputSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import org.xml.sax.ext.EntityResolver2;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import org.w3c.dom.ls.LSInput;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import org.w3c.dom.ls.LSResourceResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import javax.xml.parsers.SAXParserFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xerces.internal.dom.DOMInputImpl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xerces.internal.xni.XNIException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import com.sun.org.apache.xml.internal.resolver.Catalog;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
import com.sun.org.apache.xml.internal.resolver.CatalogManager;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
import com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
import com.sun.org.apache.xml.internal.resolver.readers.SAXCatalogReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * <p>The catalog resolver handles the resolution of external
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * identifiers and URI references through XML catalogs. This
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * component supports XML catalogs defined by the
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * <a href="http://www.oasis-open.org/committees/entity/spec.html">
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 * OASIS XML Catalogs Specification</a>. It encapsulates the
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 * <a href="http://xml.apache.org/commons/">XML Commons</a> resolver.
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * An instance of this class may be registered on the parser
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 * as a SAX entity resolver, as a DOM LSResourceResolver or
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * as an XNI entity resolver by setting the property
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 * (http://apache.org/xml/properties/internal/entity-resolver).</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
 * <p>It is intended that this class may be used standalone to perform
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
 * catalog resolution outside of a parsing context. It may be shared
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
 * between several parsers and the application.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
 *
41021
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 39080
diff changeset
    64
 * @deprecated This class and the JDK internal Catalog API in package
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 39080
diff changeset
    65
 * {@code com.sun.org.apache.xml.internal.resolver}
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 39080
diff changeset
    66
 * is encapsulated in JDK 9. The entire implementation under the package is now
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 39080
diff changeset
    67
 * deprecated and subject to removal in a future release. Users of the API should
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 39080
diff changeset
    68
 * migrate to the {@linkplain javax.xml.catalog new public API}.
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 39080
diff changeset
    69
 * <p>
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 39080
diff changeset
    70
 * The new Catalog API is supported throughout the JDK XML Processors, which allows
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 39080
diff changeset
    71
 * the use of Catalog by simply setting a path to a Catalog file as a property.
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 39080
diff changeset
    72
 *
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
 * @author Michael Glavassevich, IBM
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
 */
41021
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 39080
diff changeset
    76
@Deprecated(since="9", forRemoval=true)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
public class XMLCatalogResolver
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    implements XMLEntityResolver, EntityResolver2, LSResourceResolver {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    /** Internal catalog manager for Apache catalogs. **/
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    private CatalogManager fResolverCatalogManager = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    /** Internal catalog structure. **/
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    private Catalog fCatalog = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    /** An array of catalog URIs. **/
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    private String [] fCatalogsList = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
     * Indicates whether the list of catalogs has
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
     * changed since it was processed.
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    private boolean fCatalogsChanged = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    /** Application specified prefer public setting. **/
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    private boolean fPreferPublic = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
     * Indicates whether the application desires that
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
     * the parser or some other component performing catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
     * resolution should use the literal system identifier
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
     * instead of the expanded system identifier.
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    private boolean fUseLiteralSystemId = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
     * <p>Constructs a catalog resolver with a default configuration.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    public XMLCatalogResolver () {
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
        this(null, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
     * <p>Constructs a catalog resolver with the given
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
     * list of entry files.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
     * @param catalogs an ordered array list of absolute URIs
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    public XMLCatalogResolver (String [] catalogs) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        this(catalogs, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
     * <p>Constructs a catalog resolver with the given
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
     * list of entry files and the preference for whether
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
     * system or public matches are preferred.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
     * @param catalogs an ordered array list of absolute URIs
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
     * @param preferPublic the prefer public setting
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    public XMLCatalogResolver (String [] catalogs, boolean preferPublic) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        init(catalogs, preferPublic);
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
     * <p>Returns the initial list of catalog entry files.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
     * @return the initial list of catalog entry files
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    public final synchronized String [] getCatalogList () {
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
        return (fCatalogsList != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
            ? (String[]) fCatalogsList.clone() : null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
     * <p>Sets the initial list of catalog entry files.
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
     * If there were any catalog mappings cached from
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
     * the previous list they will be replaced by catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
     * mappings from the new list the next time the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
     * is queried.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
     * @param catalogs an ordered array list of absolute URIs
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    public final synchronized void setCatalogList (String [] catalogs) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
        fCatalogsChanged = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        fCatalogsList = (catalogs != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
            ? (String[]) catalogs.clone() : null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
     * <p>Forces the cache of catalog mappings to be cleared.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    public final synchronized void clear () {
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
        fCatalog = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
     * <p>Returns the preference for whether system or public
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
     * matches are preferred. This is used in the absence
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
     * of any occurence of the <code>prefer</code> attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
     * on the <code>catalog</code> entry of a catalog. If this
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
     * property has not yet been explicitly set its value is
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
     * <code>true</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     * @return the prefer public setting
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
    public final boolean getPreferPublic () {
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
        return fPreferPublic;
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
     * <p>Sets the preference for whether system or public
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
     * matches are preferred. This is used in the absence
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
     * of any occurence of the <code>prefer</code> attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
     * on the <code>catalog</code> entry of a catalog.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
     * @param preferPublic the prefer public setting
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
    public final void setPreferPublic (boolean preferPublic) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
        fPreferPublic = preferPublic;
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
        fResolverCatalogManager.setPreferPublic(preferPublic);
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
     * <p>Returns the preference for whether the literal system
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
     * identifier should be used when resolving system
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
     * identifiers when both it and the expanded system
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
     * identifier are available. If this property has not yet
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
     * been explicitly set its value is <code>true</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
     * @return the preference for using literal system identifers
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
     * for catalog resolution
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
     * @see #setUseLiteralSystemId
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
    public final boolean getUseLiteralSystemId () {
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
        return fUseLiteralSystemId;
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
     * <p>Sets the preference for whether the literal system
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
     * identifier should be used when resolving system
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
     * identifiers when both it and the expanded system
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
     * identifier are available.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
     * <p>The literal system identifier is the URI as it was
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
     * provided before absolutization. It may be embedded within
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
     * an entity. It may be provided externally or it may be the
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
     * result of redirection. For example, redirection may
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
     * have come from the protocol level through HTTP or from
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
     * an application's entity resolver.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
     * <p>The expanded system identifier is an absolute URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
     * which is the result of resolving the literal system
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
     * identifier against a base URI.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
     * @param useLiteralSystemId the preference for using
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
     * literal system identifers for catalog resolution
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    public final void setUseLiteralSystemId (boolean useLiteralSystemId) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
        fUseLiteralSystemId = useLiteralSystemId;
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
     * <p>Resolves an external entity. If the entity cannot be
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
     * resolved, this method should return <code>null</code>. This
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
     * method returns an input source if an entry was found in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
     * catalog for the given external identifier. It should be
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
     * overrided if other behaviour is required.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
     * @param publicId the public identifier, or <code>null</code> if none was supplied
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
     * @param systemId the system identifier
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
     * @throws SAXException any SAX exception, possibly wrapping another exception
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
     * @throws IOException thrown if some i/o error occurs
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
    public InputSource resolveEntity(String publicId, String systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
         throws SAXException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
        String resolvedId = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
        if (publicId != null && systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
            resolvedId = resolvePublic(publicId, systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
        else if (systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
            resolvedId = resolveSystem(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
        if (resolvedId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
            InputSource source = new InputSource(resolvedId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
            source.setPublicId(publicId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
            return source;
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
     /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
      * <p>Resolves an external entity. If the entity cannot be
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
      * resolved, this method should return <code>null</code>. This
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
      * method returns an input source if an entry was found in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
      * catalog for the given external identifier. It should be
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
      * overrided if other behaviour is required.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
      *
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
      * @param name the identifier of the external entity
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
      * @param publicId the public identifier, or <code>null</code> if none was supplied
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
      * @param baseURI the URI with respect to which relative systemIDs are interpreted.
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
      * @param systemId the system identifier
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
      *
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
      * @throws SAXException any SAX exception, possibly wrapping another exception
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
      * @throws IOException thrown if some i/o error occurs
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
      */
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
     public InputSource resolveEntity(String name, String publicId,
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
         String baseURI, String systemId) throws SAXException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
         String resolvedId = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
         if (!getUseLiteralSystemId() && baseURI != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
             // Attempt to resolve the system identifier against the base URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
             try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
                 URI uri = new URI(new URI(baseURI), systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
                 systemId = uri.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
             }
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
             // Ignore the exception. Fallback to the literal system identifier.
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
             catch (URI.MalformedURIException ex) {}
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
         }
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
         if (publicId != null && systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
             resolvedId = resolvePublic(publicId, systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
         }
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
         else if (systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
             resolvedId = resolveSystem(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
         }
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
         if (resolvedId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
             InputSource source = new InputSource(resolvedId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
             source.setPublicId(publicId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
             return source;
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
         }
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
         return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
     /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
      * <p>Locates an external subset for documents which do not explicitly
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
      * provide one. This method always returns <code>null</code>. It
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
      * should be overrided if other behaviour is required.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
      *
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
      * @param name the identifier of the document root element
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
      * @param baseURI the document's base URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
      *
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
      * @throws SAXException any SAX exception, possibly wrapping another exception
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
      * @throws IOException thrown if some i/o error occurs
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
      */
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
     public InputSource getExternalSubset(String name, String baseURI)
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
         throws SAXException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
         return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
     }
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
     * <p>Resolves a resource using the catalog. This method interprets that
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
     * the namespace URI corresponds to uri entries in the catalog.
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
     * Where both a namespace and an external identifier exist, the namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
     * takes precedence.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
     * @param type the type of the resource being resolved
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
     * @param namespaceURI the namespace of the resource being resolved,
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
     * or <code>null</code> if none was supplied
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
     * @param publicId the public identifier of the resource being resolved,
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
     * or <code>null</code> if none was supplied
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
     * @param systemId the system identifier of the resource being resolved,
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
     * or <code>null</code> if none was supplied
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
     * @param baseURI the absolute base URI of the resource being parsed,
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
     * or <code>null</code> if there is no base URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
    public LSInput resolveResource(String type, String namespaceURI,
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
        String publicId, String systemId, String baseURI) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
        String resolvedId = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
            // The namespace is useful for resolving namespace aware
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
            // grammars such as XML schema. Let it take precedence over
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
            // the external identifier if one exists.
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
            if (namespaceURI != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
                resolvedId = resolveURI(namespaceURI);
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
            if (!getUseLiteralSystemId() && baseURI != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
                // Attempt to resolve the system identifier against the base URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
                    URI uri = new URI(new URI(baseURI), systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
                    systemId = uri.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
                // Ignore the exception. Fallback to the literal system identifier.
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
                catch (URI.MalformedURIException ex) {}
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
            // Resolve against an external identifier if one exists. This
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
            // is useful for resolving DTD external subsets and other
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
            // external entities. For XML schemas if there was no namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
            // mapping we might be able to resolve a system identifier
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
            // specified as a location hint.
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
            if (resolvedId == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
                if (publicId != null && systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
                    resolvedId = resolvePublic(publicId, systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
                else if (systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
                    resolvedId = resolveSystem(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
        // Ignore IOException. It cannot be thrown from this method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
        catch (IOException ex) {}
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
        if (resolvedId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
            return new DOMInputImpl(publicId, resolvedId, baseURI);
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
     * <p>Resolves an external entity. If the entity cannot be
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
     * resolved, this method should return <code>null</code>. This
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
     * method only calls <code>resolveIdentifier</code> and returns
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
     * an input source if an entry was found in the catalog. It
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
     * should be overrided if other behaviour is required.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
     * @param resourceIdentifier location of the XML resource to resolve
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
     * @throws XNIException thrown on general error
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
     * @throws IOException thrown if some i/o error occurs
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
    public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
        throws XNIException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
        String resolvedId = resolveIdentifier(resourceIdentifier);
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
        if (resolvedId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
            return new XMLInputSource(resourceIdentifier.getPublicId(),
39080
57563e513b11 8158204: accessExternalSchema property handling is inconsistent and differs from spec.
joehw
parents: 25868
diff changeset
   408
                    resolvedId, resourceIdentifier.getBaseSystemId(), false);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
     * <p>Resolves an identifier using the catalog. This method interprets that
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
     * the namespace of the identifier corresponds to uri entries in the catalog.
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
     * Where both a namespace and an external identifier exist, the namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
     * takes precedence.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
     * @param resourceIdentifier the identifier to resolve
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
     * @throws XNIException thrown on general error
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
     * @throws IOException thrown if some i/o error occurs
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
    public String resolveIdentifier(XMLResourceIdentifier resourceIdentifier)
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
        throws IOException, XNIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
        String resolvedId = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
        // The namespace is useful for resolving namespace aware
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
        // grammars such as XML schema. Let it take precedence over
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
        // the external identifier if one exists.
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
        String namespace = resourceIdentifier.getNamespace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
        if (namespace != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
            resolvedId = resolveURI(namespace);
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
        // Resolve against an external identifier if one exists. This
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
        // is useful for resolving DTD external subsets and other
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
        // external entities. For XML schemas if there was no namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
        // mapping we might be able to resolve a system identifier
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
        // specified as a location hint.
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
        if (resolvedId == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
            String publicId = resourceIdentifier.getPublicId();
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
            String systemId = getUseLiteralSystemId()
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
                ? resourceIdentifier.getLiteralSystemId()
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
                : resourceIdentifier.getExpandedSystemId();
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
            if (publicId != null && systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
                resolvedId = resolvePublic(publicId, systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
            else if (systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
                resolvedId = resolveSystem(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
        return resolvedId;
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
     * <p>Returns the URI mapping in the catalog for the given
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
     * external identifier or <code>null</code> if no mapping
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
     * exists. If the system identifier is an URN in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
     * <code>publicid</code> namespace it is converted into
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
     * a public identifier by URN "unwrapping" as specified
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
     * in the XML Catalogs specification.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
     * @param systemId the system identifier to locate in the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
     * @return the mapped URI or <code>null</code> if no mapping
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
     * was found in the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
     * @throws IOException if an i/o error occurred while reading
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
     * the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
    public final synchronized String resolveSystem (String systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
        throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
        if (fCatalogsChanged) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
            parseCatalogs();
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
            fCatalogsChanged = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
        return (fCatalog != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
            ? fCatalog.resolveSystem(systemId) : null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
     * <p>Returns the URI mapping in the catalog for the given
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
     * external identifier or <code>null</code> if no mapping
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
     * exists. Public identifiers are normalized before
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
     * comparison.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
     * @param publicId the public identifier to locate in the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
     * @param systemId the system identifier to locate in the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
     * @return the mapped URI or <code>null</code> if no mapping
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
     * was found in the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
     * @throws IOException if an i/o error occurred while reading
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
     * the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
    public final synchronized String resolvePublic (String publicId, String systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
        throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
        if (fCatalogsChanged) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
            parseCatalogs();
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
            fCatalogsChanged = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
        return (fCatalog != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
            ? fCatalog.resolvePublic(publicId, systemId) : null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
     * <p>Returns the URI mapping in the catalog for the given URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
     * reference or <code>null</code> if no mapping exists.
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
     * URI comparison is case sensitive. If the URI reference
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
     * is an URN in the <code>publicid</code> namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
     * it is converted into a public identifier by URN "unwrapping"
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
     * as specified in the XML Catalogs specification and then
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
     * resolution is performed following the semantics of
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
     * external identifier resolution.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
     * @param uri the URI to locate in the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
     * @return the mapped URI or <code>null</code> if no mapping
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
     * was found in the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
     * @throws IOException if an i/o error occurred while reading
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
     * the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
    public final synchronized String resolveURI (String uri)
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
        throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
        if (fCatalogsChanged) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
            parseCatalogs();
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
            fCatalogsChanged = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
        return (fCatalog != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
            ? fCatalog.resolveURI(uri) : null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
     * Initialization. Create a CatalogManager and set all
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
     * the properties upfront. This prevents JVM wide system properties
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
     * or a property file somewhere in the environment from affecting
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
     * the behaviour of this catalog resolver.
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
    private void init (String [] catalogs, boolean preferPublic) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
        fCatalogsList = (catalogs != null) ? (String[]) catalogs.clone() : null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
        fPreferPublic = preferPublic;
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
        fResolverCatalogManager = new CatalogManager();
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
        fResolverCatalogManager.setAllowOasisXMLCatalogPI(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
        fResolverCatalogManager.setCatalogClassName("com.sun.org.apache.xml.internal.resolver.Catalog");
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
        fResolverCatalogManager.setCatalogFiles("");
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
        fResolverCatalogManager.setIgnoreMissingProperties(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
        fResolverCatalogManager.setPreferPublic(fPreferPublic);
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
        fResolverCatalogManager.setRelativeCatalogs(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
        fResolverCatalogManager.setUseStaticCatalog(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
        fResolverCatalogManager.setVerbosity(0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
     * Instruct the <code>Catalog</code> to parse each of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
     * catalogs in the list. Only the first catalog will actually be
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
     * parsed immediately. The others will be queued and read if
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
     * they are needed later.
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
    private void parseCatalogs () throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
        if (fCatalogsList != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
            fCatalog = new Catalog(fResolverCatalogManager);
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
            attachReaderToCatalog(fCatalog);
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
            for (int i = 0; i < fCatalogsList.length; ++i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
                String catalog = fCatalogsList[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
                if (catalog != null && catalog.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
                    fCatalog.parseCatalog(catalog);
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
            fCatalog = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
     * Attaches the reader to the catalog.
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
    private void attachReaderToCatalog (Catalog catalog) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
        SAXParserFactory spf = new SAXParserFactoryImpl();
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
        spf.setNamespaceAware(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
        spf.setValidating(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
        SAXCatalogReader saxReader = new SAXCatalogReader(spf);
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
        saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, "catalog",
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
            "com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader");
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
        catalog.addReader("application/xml", saxReader);
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
}