jaxp/src/java.xml/share/classes/com/sun/org/apache/xml/internal/resolver/Resolver.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;
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
import java.io.InputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
import java.io.FileNotFoundException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
import java.util.Enumeration;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import java.util.Vector;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.net.URL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.net.URLConnection;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.net.MalformedURLException;
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    28
import javax.xml.parsers.SAXParserFactory;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    29
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
    30
import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xml.internal.resolver.readers.SAXCatalogReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xml.internal.resolver.readers.TR9401CatalogReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * An extension to OASIS Open Catalog files, this class supports
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * suffix-based matching and an external RFC2483 resolver.
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * @see Catalog
41021
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    40
 * @deprecated The JDK internal Catalog API in package
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    41
 * {@code com.sun.org.apache.xml.internal.resolver}
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    42
 * 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
    43
 * 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
    44
 * 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
    45
 * <p>
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    46
 * 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
    47
 * the use of Catalog by simply setting a path to a Catalog file as a property.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * @author Norman Walsh
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 *
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    52
 * @version 1.0
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 */
41021
444f2b1bcc8c 8165784: Deprecate the internal Catalog API in JDK 9
joehw
parents: 26516
diff changeset
    54
@Deprecated(since="9", forRemoval=true)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
public class Resolver extends Catalog {
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
   * The URISUFFIX Catalog Entry type.
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
   * <p>URI suffix entries match URIs that end in a specified suffix.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
  public static final int URISUFFIX = CatalogEntry.addEntryType("URISUFFIX", 2);
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
   * The SYSTEMSUFFIX Catalog Entry type.
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
   * <p>System suffix entries match system identifiers that end in a
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
   * specified suffix.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
  public static final int SYSTEMSUFFIX = CatalogEntry.addEntryType("SYSTEMSUFFIX", 2);
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
   * The RESOLVER Catalog Entry type.
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
   * <p>A hook for providing support for web-based backup resolvers.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
  public static final int RESOLVER = CatalogEntry.addEntryType("RESOLVER", 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   * The SYSTEMREVERSE Catalog Entry type.
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   * <p>This is a bit of a hack. There's no actual SYSTEMREVERSE entry,
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
   * but this entry type is used to indicate that a reverse lookup is
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
   * being performed. (This allows the Resolver to implement
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
   * RFC2483 I2N and I2NS.)
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  public static final int SYSTEMREVERSE
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    = CatalogEntry.addEntryType("SYSTEMREVERSE", 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
   * Setup readers.
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
  public void setupReaders() {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    93
    SAXParserFactory spf = catalogManager.useServicesMechanism() ?
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    94
                    SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    spf.setNamespaceAware(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    spf.setValidating(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    SAXCatalogReader saxReader = new SAXCatalogReader(spf);
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
26516
8e6a45c22cea 8056202: Xerces Update: Catalog Resolver
joehw
parents: 25868
diff changeset
   100
    saxReader.setCatalogParser(null, "XCatalog",
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
                               "com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader");
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
                               "catalog",
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
                               "com.sun.org.apache.xml.internal.resolver.readers.ExtendedXMLCatalogReader");
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    addReader("application/xml", saxReader);
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    TR9401CatalogReader textReader = new TR9401CatalogReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    addReader("text/plain", textReader);
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
   * Cleanup and process a Catalog entry.
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
   * <p>This method processes each Catalog entry, changing mapped
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   * relative system identifiers into absolute ones (based on the current
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
   * base URI), and maintaining other information about the current
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   * catalog.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
   * @param entry The CatalogEntry to process.
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
  public void addEntry(CatalogEntry entry) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    int type = entry.getEntryType();
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    if (type == URISUFFIX) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
      String suffix = normalizeURI(entry.getEntryArg(0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
      entry.setEntryArg(1, fsi);
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
      catalogManager.debug.message(4, "URISUFFIX", suffix, fsi);
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    } else if (type == SYSTEMSUFFIX) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
      String suffix = normalizeURI(entry.getEntryArg(0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
      entry.setEntryArg(1, fsi);
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
      catalogManager.debug.message(4, "SYSTEMSUFFIX", suffix, fsi);
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    super.addEntry(entry);
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
   * Return the applicable URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
   * <p>If a URI entry exists in the Catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
   * for the URI specified, return the mapped value.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
   * <p>In the Resolver (as opposed to the Catalog) class, if the
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
   * URI isn't found by the usual algorithm, URISUFFIX entries are
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
   * considered.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
   * <p>URI comparison is case sensitive.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
   * @param uri The URI to locate in the catalog.
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
   * @return The resolved URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
   * @throws MalformedURLException The system identifier of a
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
   * subordinate catalog cannot be turned into a valid URL.
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
   * @throws IOException Error reading subordinate catalog file.
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
  public String resolveURI(String uri)
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    throws MalformedURLException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
    String resolved = super.resolveURI(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
      return resolved;
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    Enumeration en = catalogEntries.elements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
    while (en.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
      CatalogEntry e = (CatalogEntry) en.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
      if (e.getEntryType() == RESOLVER) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
        resolved = resolveExternalSystem(uri, e.getEntryArg(0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
        if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
          return resolved;
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
      } else if (e.getEntryType() == URISUFFIX) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        String suffix = e.getEntryArg(0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        String result = e.getEntryArg(1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
        if (suffix.length() <= uri.length()
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
            && uri.substring(uri.length()-suffix.length()).equals(suffix)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
          return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
    // Otherwise, look in the subordinate catalogs
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    return resolveSubordinateCatalogs(Catalog.URI,
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
                                      null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
                                      null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
                                      uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
   * Return the applicable SYSTEM system identifier, resorting
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
   * to external RESOLVERs if necessary.
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
   * <p>If a SYSTEM entry exists in the Catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
   * for the system ID specified, return the mapped value.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
   * <p>In the Resolver (as opposed to the Catalog) class, if the
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
   * URI isn't found by the usual algorithm, SYSTEMSUFFIX entries are
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
   * considered.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
   * <p>On Windows-based operating systems, the comparison between
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
   * the system identifier provided and the SYSTEM entries in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
   * Catalog is case-insensitive.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
   * @param systemId The system ID to locate in the catalog.
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
   * @return The system identifier to use for systemId.
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
   * @throws MalformedURLException The formal system identifier of a
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
   * subordinate catalog cannot be turned into a valid URL.
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
   * @throws IOException Error reading subordinate catalog file.
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
  public String resolveSystem(String systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
    throws MalformedURLException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
    String resolved = super.resolveSystem(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
    if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
      return resolved;
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    Enumeration en = catalogEntries.elements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
    while (en.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
      CatalogEntry e = (CatalogEntry) en.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
      if (e.getEntryType() == RESOLVER) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
        resolved = resolveExternalSystem(systemId, e.getEntryArg(0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
        if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
          return resolved;
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
      } else if (e.getEntryType() == SYSTEMSUFFIX) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
        String suffix = e.getEntryArg(0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
        String result = e.getEntryArg(1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
        if (suffix.length() <= systemId.length()
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
            && systemId.substring(systemId.length()-suffix.length()).equals(suffix)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
          return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
    return resolveSubordinateCatalogs(Catalog.SYSTEM,
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
                                      null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
                                      null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
                                      systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
   * Return the applicable PUBLIC or SYSTEM identifier, resorting
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
   * to external resolvers if necessary.
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
   * <p>This method searches the Catalog and returns the system
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
   * identifier specified for the given system or
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
   * public identifiers. If
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
   * no appropriate PUBLIC or SYSTEM entry is found in the Catalog,
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
   * null is returned.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
   * <p>Note that a system or public identifier in the current catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
   * (or subordinate catalogs) will be used in preference to an
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
   * external resolver. Further, if a systemId is present, the external
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
   * resolver(s) will be queried for that before the publicId.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
   * @param publicId The public identifier to locate in the catalog.
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
   * Public identifiers are normalized before comparison.
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
   * @param systemId The nominal system identifier for the entity
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
   * in question (as provided in the source document).
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
   * @throws MalformedURLException The formal system identifier of a
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
   * subordinate catalog cannot be turned into a valid URL.
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
   * @throws IOException Error reading subordinate catalog file.
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
   * @return The system identifier to use.
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
   * Note that the nominal system identifier is not returned if a
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
   * match is not found in the catalog, instead null is returned
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
   * to indicate that no match was found.
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
  public String resolvePublic(String publicId, String systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
    throws MalformedURLException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
    String resolved = super.resolvePublic(publicId, systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
    if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
      return resolved;
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
    Enumeration en = catalogEntries.elements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
    while (en.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
      CatalogEntry e = (CatalogEntry) en.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
      if (e.getEntryType() == RESOLVER) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
        if (systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
          resolved = resolveExternalSystem(systemId,
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
                                           e.getEntryArg(0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
          if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
            return resolved;
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
        resolved = resolveExternalPublic(publicId, e.getEntryArg(0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
        if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
          return resolved;
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
    return resolveSubordinateCatalogs(Catalog.PUBLIC,
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
                                      null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
                                      publicId,
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
                                      systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
     * Query an external RFC2483 resolver for a system identifier.
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
     * @param systemId The system ID to locate.
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
     * @param resolver The name of the resolver to use.
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
     * @return The system identifier to use for the systemId.
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
    protected String resolveExternalSystem(String systemId, String resolver)
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
        throws MalformedURLException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
        Resolver r = queryResolver(resolver, "i2l", systemId, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
        if (r != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
            return r.resolveSystem(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
     * Query an external RFC2483 resolver for a public identifier.
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
     * @param publicId The system ID to locate.
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
     * @param resolver The name of the resolver to use.
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
     * @return The system identifier to use for the systemId.
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
    protected String resolveExternalPublic(String publicId, String resolver)
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
        throws MalformedURLException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
        Resolver r = queryResolver(resolver, "fpi2l", publicId, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
        if (r != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
            return r.resolvePublic(publicId, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
     * Query an external RFC2483 resolver.
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
     * @param resolver The URL of the RFC2483 resolver.
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
     * @param command The command to send the resolver.
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
     * @param arg1 The first argument to the resolver.
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
     * @param arg2 The second argument to the resolver, usually null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
     * @return The Resolver constructed.
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
    protected Resolver queryResolver(String resolver,
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
                                     String command,
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
                                     String arg1,
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
                                     String arg2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
        InputStream iStream = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
        String RFC2483 = resolver + "?command=" + command
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
            + "&format=tr9401&uri=" + arg1
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
            + "&uri2=" + arg2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
        String line = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
            URL url = new URL(RFC2483);
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
            URLConnection urlCon = url.openConnection();
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
            urlCon.setUseCaches(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
            Resolver r = (Resolver) newCatalog();
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
            String cType = urlCon.getContentType();
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
            // I don't care about the character set or subtype
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
            if (cType.indexOf(";") > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
                cType = cType.substring(0, cType.indexOf(";"));
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
            r.parseCatalog(cType, urlCon.getInputStream());
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
            return r;
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
        } catch (CatalogException cex) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
          if (cex.getExceptionType() == CatalogException.UNPARSEABLE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
            catalogManager.debug.message(1, "Unparseable catalog: " + RFC2483);
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
          } else if (cex.getExceptionType()
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
                     == CatalogException.UNKNOWN_FORMAT) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
            catalogManager.debug.message(1, "Unknown catalog format: " + RFC2483);
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
          return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
        } catch (MalformedURLException mue) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
            catalogManager.debug.message(1, "Malformed resolver URL: " + RFC2483);
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
        } catch (IOException ie) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
            catalogManager.debug.message(1, "I/O Exception opening resolver: " + RFC2483);
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
     * Append two vectors, returning the result.
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
     * @param vec The first vector
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
     * @param appvec The vector to be appended
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
     * @return The vector vec, with appvec's elements appended to it
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
    private Vector appendVector(Vector vec, Vector appvec) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
        if (appvec != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
            for (int count = 0; count < appvec.size(); count++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
                vec.addElement(appvec.elementAt(count));
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
        return vec;
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
     * Find the URNs for a given system identifier in all catalogs.
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
     * @param systemId The system ID to locate.
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
     * @return A vector of URNs that map to the systemId.
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
    public Vector resolveAllSystemReverse(String systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
        throws MalformedURLException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
        Vector resolved = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
        // If there's a SYSTEM entry in this catalog, use it
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
        if (systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
            Vector localResolved = resolveLocalSystemReverse(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
            resolved = appendVector(resolved, localResolved);
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
        // Otherwise, look in the subordinate catalogs
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
        Vector subResolved = resolveAllSubordinateCatalogs(SYSTEMREVERSE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
                                                           null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
                                                           null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
                                                           systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
        return appendVector(resolved, subResolved);
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
     * Find the URN for a given system identifier.
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
     * @param systemId The system ID to locate.
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
     * @return A (single) URN that maps to the systemId.
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
    public String resolveSystemReverse(String systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
        throws MalformedURLException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
        Vector resolved = resolveAllSystemReverse(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
        if (resolved != null && resolved.size() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
            return (String) resolved.elementAt(0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
     * Return the applicable SYSTEM system identifiers.
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
     * <p>If one or more SYSTEM entries exists in the Catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
     * for the system ID specified, return the mapped values.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
     * <p>The caller is responsible for doing any necessary
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
     * normalization of the system identifier before calling
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
     * this method. For example, a relative system identifier in
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
     * a document might be converted to an absolute system identifier
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
     * before attempting to resolve it.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
     * <p>Note that this function will force all subordinate catalogs
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
     * to be loaded.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
     * <p>On Windows-based operating systems, the comparison between
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
     * the system identifier provided and the SYSTEM entries in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
     * Catalog is case-insensitive.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
     * @param systemId The system ID to locate in the catalog.
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
     * @return The system identifier to use for the notation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
     * @throws MalformedURLException The formal system identifier of a
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
     * subordinate catalog cannot be turned into a valid URL.
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
     * @throws IOException Error reading subordinate catalog file.
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
    public Vector resolveAllSystem(String systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
        throws MalformedURLException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
        Vector resolutions = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
        // If there are SYSTEM entries in this catalog, start with them
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
        if (systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
            Vector localResolutions = resolveAllLocalSystem(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
            resolutions = appendVector(resolutions, localResolutions);
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
        // Then look in the subordinate catalogs
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
        Vector subResolutions = resolveAllSubordinateCatalogs(SYSTEM,
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
                                                              null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
                                                              null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
                                                              systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
        resolutions = appendVector(resolutions, subResolutions);
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
        if (resolutions.size() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
            return resolutions;
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
     * Return all applicable SYSTEM system identifiers in this
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
     * catalog.
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
     * <p>If one or more SYSTEM entries exists in the catalog file
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
     * for the system ID specified, return the mapped values.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
     * @param systemId The system ID to locate in the catalog
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
     * @return A vector of the mapped system identifiers or null
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
    private Vector resolveAllLocalSystem(String systemId) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
        Vector map = new Vector();
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   531
        String osname = SecuritySupport.getSystemProperty("os.name");
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
        boolean windows = (osname.indexOf("Windows") >= 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
        Enumeration en = catalogEntries.elements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
        while (en.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
            CatalogEntry e = (CatalogEntry) en.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
            if (e.getEntryType() == SYSTEM
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
                && (e.getEntryArg(0).equals(systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
                    || (windows
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
                        && e.getEntryArg(0).equalsIgnoreCase(systemId)))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
                map.addElement(e.getEntryArg(1));
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
        if (map.size() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
            return map;
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
     * Find the URNs for a given system identifier in the current catalog.
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
     * @param systemId The system ID to locate.
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
     * @return A vector of URNs that map to the systemId.
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
    private Vector resolveLocalSystemReverse(String systemId) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
        Vector map = new Vector();
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   559
        String osname = SecuritySupport.getSystemProperty("os.name");
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
        boolean windows = (osname.indexOf("Windows") >= 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
        Enumeration en = catalogEntries.elements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
        while (en.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
            CatalogEntry e = (CatalogEntry) en.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
            if (e.getEntryType() == SYSTEM
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
                && (e.getEntryArg(1).equals(systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
                    || (windows
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
                        && e.getEntryArg(1).equalsIgnoreCase(systemId)))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
                map.addElement(e.getEntryArg(0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
        if (map.size() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
            return map;
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
     * Search the subordinate catalogs, in order, looking for all
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
     * match.
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
     * <p>This method searches the Catalog and returns all of the system
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
     * identifiers specified for the given entity type with the given
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
     * name, public, and system identifiers. In some contexts, these
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
     * may be null.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
     * @param entityType The CatalogEntry type for which this query is
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
     * being conducted. This is necessary in order to do the approprate
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
     * query on a subordinate catalog.
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
     * @param entityName The name of the entity being searched for, if
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
     * appropriate.
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
     * @param publicId The public identifier of the entity in question
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
     * (as provided in the source document).
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
     * @param systemId The nominal system identifier for the entity
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
     * in question (as provided in the source document).
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
     * @throws MalformedURLException The formal system identifier of a
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
     * delegated catalog cannot be turned into a valid URL.
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
     * @throws IOException Error reading delegated catalog file.
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
     * @return The system identifier to use.
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
     * Note that the nominal system identifier is not returned if a
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
     * match is not found in the catalog, instead null is returned
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
     * to indicate that no match was found.
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
    private synchronized Vector resolveAllSubordinateCatalogs(int entityType,
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
                                              String entityName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
                                              String publicId,
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
                                              String systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
        throws MalformedURLException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
        Vector resolutions = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
        for (int catPos = 0; catPos < catalogs.size(); catPos++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
            Resolver c = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
                c = (Resolver) catalogs.elementAt(catPos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
            } catch (ClassCastException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
                String catfile = (String) catalogs.elementAt(catPos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
                c = (Resolver) newCatalog();
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
                    c.parseCatalog(catfile);
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
                } catch (MalformedURLException mue) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
                    catalogManager.debug.message(1, "Malformed Catalog URL", catfile);
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
                } catch (FileNotFoundException fnfe) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
                    catalogManager.debug.message(1, "Failed to load catalog, file not found",
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
                          catfile);
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
                } catch (IOException ioe) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
                    catalogManager.debug.message(1, "Failed to load catalog, I/O error", catfile);
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
                catalogs.setElementAt(c, catPos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
            String resolved = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
            // Ok, now what are we supposed to call here?
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
            if (entityType == DOCTYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
                resolved = c.resolveDoctype(entityName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
                                            publicId,
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
                                            systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
                if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
                    // Only find one DOCTYPE resolution
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
                    resolutions.addElement(resolved);
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
                    return resolutions;
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
            } else if (entityType == DOCUMENT) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
                resolved = c.resolveDocument();
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
                if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
                    // Only find one DOCUMENT resolution
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
                    resolutions.addElement(resolved);
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
                    return resolutions;
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
            } else if (entityType == ENTITY) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
                resolved = c.resolveEntity(entityName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
                                           publicId,
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
                                           systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
                if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
                    // Only find one ENTITY resolution
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
                    resolutions.addElement(resolved);
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
                    return resolutions;
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
            } else if (entityType == NOTATION) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
                resolved = c.resolveNotation(entityName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
                                             publicId,
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
                                             systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
                if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
                    // Only find one NOTATION resolution
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
                    resolutions.addElement(resolved);
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
                    return resolutions;
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
            } else if (entityType == PUBLIC) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
                resolved = c.resolvePublic(publicId, systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
                if (resolved != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
                    // Only find one PUBLIC resolution
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
                    resolutions.addElement(resolved);
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
                    return resolutions;
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
            } else if (entityType == SYSTEM) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
                Vector localResolutions = c.resolveAllSystem(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
                resolutions = appendVector(resolutions, localResolutions);
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
                break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
            } else if (entityType == SYSTEMREVERSE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
                Vector localResolutions = c.resolveAllSystemReverse(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
                resolutions = appendVector(resolutions, localResolutions);
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
        if (resolutions != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
            return resolutions;
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
}