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