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