resolver.java
来自「JAVA 所有包」· Java 代码 · 共 692 行 · 第 1/2 页
JAVA
692 行
// Resolver.java - Represents an extension of OASIS Open Catalog files./* * Copyright 2001-2004 The Apache Software Foundation or its licensors, * as applicable. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.sun.org.apache.xml.internal.resolver;import java.io.IOException;import java.io.InputStream;import java.io.FileNotFoundException;import java.util.Enumeration;import java.util.Vector;import java.net.URL;import java.net.URLConnection;import java.net.MalformedURLException;import com.sun.org.apache.xml.internal.resolver.readers.SAXCatalogReader;import com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader;import com.sun.org.apache.xml.internal.resolver.readers.TR9401CatalogReader;import javax.xml.parsers.SAXParserFactory;/** * An extension to OASIS Open Catalog files, this class supports * suffix-based matching and an external RFC2483 resolver. * * @see Catalog * * @author Norman Walsh * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a> * * @version 1.0 */public class Resolver extends Catalog { /** * The URISUFFIX Catalog Entry type. * * <p>URI suffix entries match URIs that end in a specified suffix.</p> */ public static final int URISUFFIX = CatalogEntry.addEntryType("URISUFFIX", 2); /** * The SYSTEMSUFFIX Catalog Entry type. * * <p>System suffix entries match system identifiers that end in a * specified suffix.</p> */ public static final int SYSTEMSUFFIX = CatalogEntry.addEntryType("SYSTEMSUFFIX", 2); /** * The RESOLVER Catalog Entry type. * * <p>A hook for providing support for web-based backup resolvers.</p> */ public static final int RESOLVER = CatalogEntry.addEntryType("RESOLVER", 1); /** * The SYSTEMREVERSE Catalog Entry type. * * <p>This is a bit of a hack. There's no actual SYSTEMREVERSE entry, * but this entry type is used to indicate that a reverse lookup is * being performed. (This allows the Resolver to implement * RFC2483 I2N and I2NS.) */ public static final int SYSTEMREVERSE = CatalogEntry.addEntryType("SYSTEMREVERSE", 1); /** * Setup readers. */ public void setupReaders() { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); spf.setValidating(false); SAXCatalogReader saxReader = new SAXCatalogReader(spf); saxReader.setCatalogParser(null, "XMLCatalog", "com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader"); saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, "catalog", "com.sun.org.apache.xml.internal.resolver.readers.ExtendedXMLCatalogReader"); addReader("application/xml", saxReader); TR9401CatalogReader textReader = new TR9401CatalogReader(); addReader("text/plain", textReader); } /** * Cleanup and process a Catalog entry. * * <p>This method processes each Catalog entry, changing mapped * relative system identifiers into absolute ones (based on the current * base URI), and maintaining other information about the current * catalog.</p> * * @param entry The CatalogEntry to process. */ public void addEntry(CatalogEntry entry) { int type = entry.getEntryType(); if (type == URISUFFIX) { String suffix = normalizeURI(entry.getEntryArg(0)); String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); entry.setEntryArg(1, fsi); catalogManager.debug.message(4, "URISUFFIX", suffix, fsi); } else if (type == SYSTEMSUFFIX) { String suffix = normalizeURI(entry.getEntryArg(0)); String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); entry.setEntryArg(1, fsi); catalogManager.debug.message(4, "SYSTEMSUFFIX", suffix, fsi); } super.addEntry(entry); } /** * Return the applicable URI. * * <p>If a URI entry exists in the Catalog * for the URI specified, return the mapped value.</p> * * <p>In the Resolver (as opposed to the Catalog) class, if the * URI isn't found by the usual algorithm, URISUFFIX entries are * considered.</p> * * <p>URI comparison is case sensitive.</p> * * @param uri The URI to locate in the catalog. * * @return The resolved URI. * * @throws MalformedURLException The system identifier of a * subordinate catalog cannot be turned into a valid URL. * @throws IOException Error reading subordinate catalog file. */ public String resolveURI(String uri) throws MalformedURLException, IOException { String resolved = super.resolveURI(uri); if (resolved != null) { return resolved; } Enumeration en = catalogEntries.elements(); while (en.hasMoreElements()) { CatalogEntry e = (CatalogEntry) en.nextElement(); if (e.getEntryType() == RESOLVER) { resolved = resolveExternalSystem(uri, e.getEntryArg(0)); if (resolved != null) { return resolved; } } else if (e.getEntryType() == URISUFFIX) { String suffix = e.getEntryArg(0); String result = e.getEntryArg(1); if (suffix.length() <= uri.length() && uri.substring(uri.length()-suffix.length()).equals(suffix)) { return result; } } } // Otherwise, look in the subordinate catalogs return resolveSubordinateCatalogs(Catalog.URI, null, null, uri); } /** * Return the applicable SYSTEM system identifier, resorting * to external RESOLVERs if necessary. * * <p>If a SYSTEM entry exists in the Catalog * for the system ID specified, return the mapped value.</p> * * <p>In the Resolver (as opposed to the Catalog) class, if the * URI isn't found by the usual algorithm, SYSTEMSUFFIX entries are * considered.</p> * * <p>On Windows-based operating systems, the comparison between * the system identifier provided and the SYSTEM entries in the * Catalog is case-insensitive.</p> * * @param systemId The system ID to locate in the catalog. * * @return The system identifier to use for systemId. * * @throws MalformedURLException The formal system identifier of a * subordinate catalog cannot be turned into a valid URL. * @throws IOException Error reading subordinate catalog file. */ public String resolveSystem(String systemId) throws MalformedURLException, IOException { String resolved = super.resolveSystem(systemId); if (resolved != null) { return resolved; } Enumeration en = catalogEntries.elements(); while (en.hasMoreElements()) { CatalogEntry e = (CatalogEntry) en.nextElement(); if (e.getEntryType() == RESOLVER) { resolved = resolveExternalSystem(systemId, e.getEntryArg(0)); if (resolved != null) { return resolved; } } else if (e.getEntryType() == SYSTEMSUFFIX) { String suffix = e.getEntryArg(0); String result = e.getEntryArg(1); if (suffix.length() <= systemId.length() && systemId.substring(systemId.length()-suffix.length()).equals(suffix)) { return result; } } } return resolveSubordinateCatalogs(Catalog.SYSTEM, null, null, systemId); } /** * Return the applicable PUBLIC or SYSTEM identifier, resorting * to external resolvers if necessary. * * <p>This method searches the Catalog and returns the system * identifier specified for the given system or * public identifiers. If * no appropriate PUBLIC or SYSTEM entry is found in the Catalog, * null is returned.</p> * * <p>Note that a system or public identifier in the current catalog * (or subordinate catalogs) will be used in preference to an * external resolver. Further, if a systemId is present, the external * resolver(s) will be queried for that before the publicId.</p> * * @param publicId The public identifier to locate in the catalog. * Public identifiers are normalized before comparison. * @param systemId The nominal system identifier for the entity * in question (as provided in the source document). * * @throws MalformedURLException The formal system identifier of a * subordinate catalog cannot be turned into a valid URL. * @throws IOException Error reading subordinate catalog file. * * @return The system identifier to use. * Note that the nominal system identifier is not returned if a * match is not found in the catalog, instead null is returned * to indicate that no match was found. */ public String resolvePublic(String publicId, String systemId) throws MalformedURLException, IOException { String resolved = super.resolvePublic(publicId, systemId); if (resolved != null) { return resolved; } Enumeration en = catalogEntries.elements(); while (en.hasMoreElements()) { CatalogEntry e = (CatalogEntry) en.nextElement(); if (e.getEntryType() == RESOLVER) { if (systemId != null) { resolved = resolveExternalSystem(systemId, e.getEntryArg(0)); if (resolved != null) { return resolved; } } resolved = resolveExternalPublic(publicId, e.getEntryArg(0)); if (resolved != null) { return resolved; } } } return resolveSubordinateCatalogs(Catalog.PUBLIC, null, publicId, systemId); } /** * Query an external RFC2483 resolver for a system identifier. * * @param systemId The system ID to locate. * @param resolver The name of the resolver to use. * * @return The system identifier to use for the systemId. */ protected String resolveExternalSystem(String systemId, String resolver) throws MalformedURLException, IOException { Resolver r = queryResolver(resolver, "i2l", systemId, null); if (r != null) { return r.resolveSystem(systemId); } else { return null; } } /** * Query an external RFC2483 resolver for a public identifier. * * @param publicId The system ID to locate. * @param resolver The name of the resolver to use. * * @return The system identifier to use for the systemId. */ protected String resolveExternalPublic(String publicId, String resolver) throws MalformedURLException, IOException { Resolver r = queryResolver(resolver, "fpi2l", publicId, null); if (r != null) { return r.resolvePublic(publicId, null); } else { return null; } } /** * Query an external RFC2483 resolver. * * @param resolver The URL of the RFC2483 resolver. * @param command The command to send the resolver.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?