jaxp/src/java.xml/share/classes/com/sun/org/apache/xml/internal/resolver/tools/CatalogResolver.java
author joehw
Wed, 14 Sep 2016 13:18:17 -0700
changeset 41021 444f2b1bcc8c
parent 26516 8e6a45c22cea
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
/*
26516
8e6a45c22cea 8056202: Xerces Update: Catalog Resolver
joehw
parents: 25868
diff changeset
     2
 * Licensed to the Apache Software Foundation (ASF) under one or more
8e6a45c22cea 8056202: Xerces Update: Catalog Resolver
joehw
parents: 25868
diff changeset
     3
 * contributor license agreements.  See the NOTICE file distributed with
8e6a45c22cea 8056202: Xerces Update: Catalog Resolver
joehw
parents: 25868
diff changeset
     4
 * this work for additional information regarding copyright ownership.
8e6a45c22cea 8056202: Xerces Update: Catalog Resolver
joehw
parents: 25868
diff changeset
     5
 * The ASF licenses this file to You under the Apache License, Version 2.0
8e6a45c22cea 8056202: Xerces Update: Catalog Resolver
joehw
parents: 25868
diff changeset
     6
 * (the "License"); you may not use this file except in compliance with
8e6a45c22cea 8056202: Xerces Update: Catalog Resolver
joehw
parents: 25868
diff changeset
     7
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
package com.sun.org.apache.xml.internal.resolver.tools;
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    20
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
import java.io.InputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
import java.net.URL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import java.net.MalformedURLException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
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.XMLReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import org.xml.sax.InputSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import org.xml.sax.EntityResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import javax.xml.transform.sax.SAXSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import javax.xml.transform.Source;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import javax.xml.transform.URIResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import javax.xml.transform.TransformerException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import javax.xml.parsers.ParserConfigurationException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import javax.xml.parsers.SAXParserFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.xml.internal.resolver.Catalog;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import com.sun.org.apache.xml.internal.resolver.CatalogManager;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.xml.internal.resolver.helpers.FileURL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * A SAX EntityResolver/JAXP URIResolver that uses catalogs.
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * <p>This class implements both a SAX EntityResolver and a JAXP URIResolver.
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * </p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * <p>This resolver understands OASIS TR9401 catalogs, XCatalogs, and the
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * current working draft of the OASIS Entity Resolution Technical
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * Committee specification.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * @see Catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 * @see org.xml.sax.EntityResolver
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 * @see javax.xml.transform.URIResolver
41021
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    55
 * @deprecated The JDK internal Catalog API in package
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    56
 * {@code com.sun.org.apache.xml.internal.resolver}
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    57
 * 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: 26516
diff changeset
    58
 * deprecated and subject to removal in a future release. Users of the API
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    59
 * should migrate to the {@linkplain javax.xml.catalog new public API}.
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    60
 * <p>
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    61
 * 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: 26516
diff changeset
    62
 * the use of Catalog by simply setting a path to a Catalog file as a property.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
 * @author Norman Walsh
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
 * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
 *
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    67
 * @version 1.0
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
 */
41021
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    69
@Deprecated(since="9", forRemoval=true)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
public class CatalogResolver implements EntityResolver, URIResolver {
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
  /** Make the parser Namespace aware? */
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
  public boolean namespaceAware = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
  /** Make the parser validating? */
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
  public boolean validating = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
  /** The underlying catalog */
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
  private Catalog catalog = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
  /** The catalog manager */
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
  private CatalogManager catalogManager = CatalogManager.getStaticManager();
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
  /** Constructor */
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
  public CatalogResolver() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    initializeCatalogs(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  /** Constructor */
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
  public CatalogResolver(boolean privateCatalog) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    initializeCatalogs(privateCatalog);
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
  /** Constructor */
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  public CatalogResolver(CatalogManager manager) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    catalogManager = manager;
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    initializeCatalogs(!catalogManager.getUseStaticCatalog());
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
  /** Initialize catalog */
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
  private void initializeCatalogs(boolean privateCatalog) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    catalog = catalogManager.getCatalog();
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
  /** Return the underlying catalog */
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
  public Catalog getCatalog() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    return catalog;
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
   * Implements the guts of the <code>resolveEntity</code> method
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
   * for the SAX interface.
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
   * <p>Presented with an optional public identifier and a system
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
   * identifier, this function attempts to locate a mapping in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   * catalogs.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   * <p>If such a mapping is found, it is returned.  If no mapping is
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
   * found, null is returned.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
   * @param publicId  The public identifier for the entity in question.
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
   * This may be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
   * @param systemId  The system identifier for the entity in question.
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
   * XML requires a system identifier on all external entities, so this
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
   * value is always specified.
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
   * @return The resolved identifier (a URI reference).
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
  public String getResolvedEntity (String publicId, String systemId) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    String resolved = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    if (catalog == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
      catalogManager.debug.message(1, "Catalog resolution attempted with null catalog; ignored");
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
      return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    if (systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
      try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
        resolved = catalog.resolveSystem(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
      } catch (MalformedURLException me) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
        catalogManager.debug.message(1, "Malformed URL exception trying to resolve",
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
                      publicId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        resolved = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
      } catch (IOException ie) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
        catalogManager.debug.message(1, "I/O exception trying to resolve", publicId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
        resolved = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
    if (resolved == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
      if (publicId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
          resolved = catalog.resolvePublic(publicId, systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        } catch (MalformedURLException me) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
          catalogManager.debug.message(1, "Malformed URL exception trying to resolve",
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
                        publicId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
        } catch (IOException ie) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
          catalogManager.debug.message(1, "I/O exception trying to resolve", publicId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
      if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
        catalogManager.debug.message(2, "Resolved public", publicId, resolved);
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
      catalogManager.debug.message(2, "Resolved system", systemId, resolved);
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    return resolved;
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
   * Implements the <code>resolveEntity</code> method
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
   * for the SAX interface.
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
   * <p>Presented with an optional public identifier and a system
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
   * identifier, this function attempts to locate a mapping in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
   * catalogs.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
   * <p>If such a mapping is found, the resolver attempts to open
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
   * the mapped value as an InputSource and return it. Exceptions are
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
   * ignored and null is returned if the mapped value cannot be opened
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
   * as an input source.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
   * <p>If no mapping is found (or an error occurs attempting to open
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
   * the mapped value as an input source), null is returned and the system
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
   * will use the specified system identifier as if no entityResolver
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
   * was specified.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
   * @param publicId  The public identifier for the entity in question.
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
   * This may be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
   * @param systemId  The system identifier for the entity in question.
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
   * XML requires a system identifier on all external entities, so this
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
   * value is always specified.
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
   * @return An InputSource for the mapped identifier, or null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
  public InputSource resolveEntity (String publicId, String systemId) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
    String resolved = getResolvedEntity(publicId, systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
    if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
      try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
        InputSource iSource = new InputSource(resolved);
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
        iSource.setPublicId(publicId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
        // Ideally this method would not attempt to open the
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
        // InputStream, but there is a bug (in Xerces, at least)
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
        // that causes the parser to mistakenly open the wrong
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
        // system identifier if the returned InputSource does
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
        // not have a byteStream.
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
        //
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
        // It could be argued that we still shouldn't do this here,
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
        // but since the purpose of calling the entityResolver is
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
        // almost certainly to open the input stream, it seems to
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
        // do little harm.
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
        //
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
        URL url = new URL(resolved);
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
        InputStream iStream = url.openStream();
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
        iSource.setByteStream(iStream);
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
        return iSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
      } catch (Exception e) {
26516
8e6a45c22cea 8056202: Xerces Update: Catalog Resolver
joehw
parents: 25868
diff changeset
   224
        catalogManager.debug.message(1,
8e6a45c22cea 8056202: Xerces Update: Catalog Resolver
joehw
parents: 25868
diff changeset
   225
                                     "Failed to create InputSource ("
8e6a45c22cea 8056202: Xerces Update: Catalog Resolver
joehw
parents: 25868
diff changeset
   226
                                     + e.toString()
8e6a45c22cea 8056202: Xerces Update: Catalog Resolver
joehw
parents: 25868
diff changeset
   227
                                     + ")", resolved);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
  /** JAXP URIResolver API */
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
  public Source resolve(String href, String base)
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
    throws TransformerException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
    String uri = href;
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
    String fragment = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
    int hashPos = href.indexOf("#");
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
    if (hashPos >= 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
      uri = href.substring(0, hashPos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
      fragment = href.substring(hashPos+1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
    String result = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
      result = catalog.resolveURI(href);
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
    } catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
      // nop;
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
    if (result == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
      try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
        URL url = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
        if (base==null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
          url = new URL(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
          result = url.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
          URL baseURL = new URL(base);
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
          url = (href.length()==0 ? baseURL : new URL(baseURL, uri));
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
          result = url.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
      } catch (java.net.MalformedURLException mue) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
        // try to make an absolute URI from the current base
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
        String absBase = makeAbsolute(base);
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
        if (!absBase.equals(base)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
          // don't bother if the absBase isn't different!
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
          return resolve(href, absBase);
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
          throw new TransformerException("Malformed URL "
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
                                         + href + "(base " + base + ")",
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
                                         mue);
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
    catalogManager.debug.message(2, "Resolved URI", href, result);
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
    SAXSource source = new SAXSource();
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
    source.setInputSource(new InputSource(result));
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
    setEntityResolver(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
    return source;
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
   * <p>Establish an entityResolver for newly resolved URIs.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
   * <p>This is called from the URIResolver to set an EntityResolver
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
   * on the SAX parser to be used for new XML documents that are
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
   * encountered as a result of the document() function, xsl:import,
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
   * or xsl:include.  This is done because the XSLT processor calls
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
   * out to the SAXParserFactory itself to create a new SAXParser to
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
   * parse the new document.  The new parser does not automatically
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
   * inherit the EntityResolver of the original (although arguably
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
   * it should).  See below:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
   * <tt>"If an application wants to set the ErrorHandler or
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
   * EntityResolver for an XMLReader used during a transformation,
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
   * it should use a URIResolver to return the SAXSource which
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
   * provides (with getXMLReader) a reference to the XMLReader"</tt>
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
   * <p>...quoted from page 118 of the Java API for XML
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
   * Processing 1.1 specification</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
  private void setEntityResolver(SAXSource source) throws TransformerException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
    XMLReader reader = source.getXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
    if (reader == null) {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   313
      SAXParserFactory spFactory = catalogManager.useServicesMechanism() ?
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   314
                    SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
      spFactory.setNamespaceAware(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
      try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
        reader = spFactory.newSAXParser().getXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
      catch (ParserConfigurationException ex) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
        throw new TransformerException(ex);
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
      catch (SAXException ex) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
        throw new TransformerException(ex);
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
    reader.setEntityResolver(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
    source.setXMLReader(reader);
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
  /** Attempt to construct an absolute URI */
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
  private String makeAbsolute(String uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
    if (uri == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
      uri = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
      URL url = new URL(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
      return url.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
    } catch (MalformedURLException mue) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
      try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
        URL fileURL = FileURL.makeURL(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
        return fileURL.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
      } catch (MalformedURLException mue2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
        // bail
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
        return uri;
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
}