xmlcatalogresolver.java

来自「JAVA 所有包」· Java 代码 · 共 584 行 · 第 1/2 页

JAVA
584
字号
             return source;         }           return null;    }     /**      * <p>Locates an external subset for documents which do not explicitly      * provide one. This method always returns <code>null</code>. It      * should be overrided if other behaviour is required.</p>      *       * @param name the identifier of the document root element       * @param baseURI the document's base URI      *       * @throws SAXException any SAX exception, possibly wrapping another exception      * @throws IOException thrown if some i/o error occurs      */     public InputSource getExternalSubset(String name, String baseURI)         throws SAXException, IOException {         return null;     }    /**      * <p>Resolves a resource using the catalog. This method interprets that      * the namespace URI corresponds to uri entries in the catalog.     * Where both a namespace and an external identifier exist, the namespace     * takes precedence.</p>     *      * @param type the type of the resource being resolved     * @param namespaceURI the namespace of the resource being resolved,      * or <code>null</code> if none was supplied     * @param publicId the public identifier of the resource being resolved,     * or <code>null</code> if none was supplied     * @param systemId the system identifier of the resource being resolved,     * or <code>null</code> if none was supplied     * @param baseURI the absolute base URI of the resource being parsed,      * or <code>null</code> if there is no base URI     */    public LSInput resolveResource(String type, String namespaceURI,        String publicId, String systemId, String baseURI) {        String resolvedId = null;                try {            // The namespace is useful for resolving namespace aware            // grammars such as XML schema. Let it take precedence over            // the external identifier if one exists.            if (namespaceURI != null) {                resolvedId = resolveURI(namespaceURI);            }                        if (!getUseLiteralSystemId() && baseURI != null) {                // Attempt to resolve the system identifier against the base URI.                try {                    URI uri = new URI(new URI(baseURI), systemId);                    systemId = uri.toString();                }                // Ignore the exception. Fallback to the literal system identifier.                catch (URI.MalformedURIException ex) {}            }                    // Resolve against an external identifier if one exists. This            // is useful for resolving DTD external subsets and other             // external entities. For XML schemas if there was no namespace             // mapping we might be able to resolve a system identifier             // specified as a location hint.            if (resolvedId == null) {                if (publicId != null && systemId != null) {                    resolvedId = resolvePublic(publicId, systemId);                }                else if (systemId != null) {                    resolvedId = resolveSystem(systemId);                }            }        }        // Ignore IOException. It cannot be thrown from this method.        catch (IOException ex) {}                if (resolvedId != null) {            return new DOMInputImpl(publicId, resolvedId, baseURI);        }          return null;    }            /**     * <p>Resolves an external entity. If the entity cannot be     * resolved, this method should return <code>null</code>. This     * method only calls <code>resolveIdentifier</code> and returns     * an input source if an entry was found in the catalog. It     * should be overrided if other behaviour is required.</p>     *     * @param resourceIdentifier location of the XML resource to resolve     *     * @throws XNIException thrown on general error     * @throws IOException thrown if some i/o error occurs      */    public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)        throws XNIException, IOException {        String resolvedId = resolveIdentifier(resourceIdentifier);        if (resolvedId != null) {            return new XMLInputSource(resourceIdentifier.getPublicId(),                                       resolvedId,                                       resourceIdentifier.getBaseSystemId());        }        return null;    }	    /**      * <p>Resolves an identifier using the catalog. This method interprets that      * the namespace of the identifier corresponds to uri entries in the catalog.     * Where both a namespace and an external identifier exist, the namespace     * takes precedence.</p>     *      * @param resourceIdentifier the identifier to resolve     *      * @throws XNIException thrown on general error     * @throws IOException thrown if some i/o error occurs     */    public String resolveIdentifier(XMLResourceIdentifier resourceIdentifier)        throws IOException, XNIException {                String resolvedId = null;        // The namespace is useful for resolving namespace aware        // grammars such as XML schema. Let it take precedence over        // the external identifier if one exists.        String namespace = resourceIdentifier.getNamespace();        if (namespace != null) {            resolvedId = resolveURI(namespace);        }                // Resolve against an external identifier if one exists. This        // is useful for resolving DTD external subsets and other         // external entities. For XML schemas if there was no namespace         // mapping we might be able to resolve a system identifier         // specified as a location hint.        if (resolvedId == null) {            String publicId = resourceIdentifier.getPublicId();            String systemId = getUseLiteralSystemId()                 ? resourceIdentifier.getLiteralSystemId()                : resourceIdentifier.getExpandedSystemId();            if (publicId != null && systemId != null) {                resolvedId = resolvePublic(publicId, systemId);            }            else if (systemId != null) {                resolvedId = resolveSystem(systemId);            }        }        return resolvedId;    }        /**     * <p>Returns the URI mapping in the catalog for the given     * external identifier or <code>null</code> if no mapping     * exists. If the system identifier is an URN in the     * <code>publicid</code> namespace it is converted into     * a public identifier by URN "unwrapping" as specified     * in the XML Catalogs specification.</p>     *      * @param systemId the system identifier to locate in the catalog     *      * @return the mapped URI or <code>null</code> if no mapping     * was found in the catalog     *      * @throws IOException if an i/o error occurred while reading     * the catalog     */    public final synchronized String resolveSystem (String systemId)         throws IOException {        if (fCatalogsChanged) {            parseCatalogs();            fCatalogsChanged = false;        }        return (fCatalog != null)             ? fCatalog.resolveSystem(systemId) : null;    }        /**     * <p>Returns the URI mapping in the catalog for the given     * external identifier or <code>null</code> if no mapping     * exists. Public identifiers are normalized before     * comparison.</p>     *      * @param publicId the public identifier to locate in the catalog     * @param systemId the system identifier to locate in the catalog     *      * @return the mapped URI or <code>null</code> if no mapping     * was found in the catalog     *      * @throws IOException if an i/o error occurred while reading     * the catalog     */    public final synchronized String resolvePublic (String publicId, String systemId)         throws IOException {        if (fCatalogsChanged) {            parseCatalogs();            fCatalogsChanged = false;        }        return (fCatalog != null)             ? fCatalog.resolvePublic(publicId, systemId) : null;    }        /**     * <p>Returns the URI mapping in the catalog for the given URI     * reference or <code>null</code> if no mapping exists.      * URI comparison is case sensitive. If the URI reference      * is an URN in the <code>publicid</code> namespace      * it is converted into a public identifier by URN "unwrapping"      * as specified in the XML Catalogs specification and then     * resolution is performed following the semantics of      * external identifier resolution.</p>     *      * @param uri the URI to locate in the catalog     *      * @return the mapped URI or <code>null</code> if no mapping     * was found in the catalog     *      * @throws IOException if an i/o error occurred while reading     * the catalog     */    public final synchronized String resolveURI (String uri)         throws IOException {        if (fCatalogsChanged) {            parseCatalogs();            fCatalogsChanged = false;        }        return (fCatalog != null)            ? fCatalog.resolveURI(uri) : null;    }        /**     * Initialization. Create a CatalogManager and set all      * the properties upfront. This prevents JVM wide system properties      * or a property file somewhere in the environment from affecting      * the behaviour of this catalog resolver.     */    private void init (String [] catalogs, boolean preferPublic) {        fCatalogsList = (catalogs != null) ? (String[]) catalogs.clone() : null;        fPreferPublic = preferPublic;        fResolverCatalogManager = new CatalogManager();        fResolverCatalogManager.setAllowOasisXMLCatalogPI(false);        fResolverCatalogManager.setCatalogClassName("com.sun.org.apache.xml.internal.resolver.Catalog");        fResolverCatalogManager.setCatalogFiles("");        fResolverCatalogManager.setIgnoreMissingProperties(true);        fResolverCatalogManager.setPreferPublic(fPreferPublic);        fResolverCatalogManager.setRelativeCatalogs(false);        fResolverCatalogManager.setUseStaticCatalog(false);        fResolverCatalogManager.setVerbosity(0);    }        /**     * Instruct the <code>Catalog</code> to parse each of the       * catalogs in the list. Only the first catalog will actually be      * parsed immediately. The others will be queued and read if      * they are needed later.     */    private void parseCatalogs () throws IOException {        if (fCatalogsList != null) {            fCatalog = new Catalog(fResolverCatalogManager);            attachReaderToCatalog(fCatalog);            for (int i = 0; i < fCatalogsList.length; ++i) {                String catalog = fCatalogsList[i];                if (catalog != null && catalog.length() > 0) {                    fCatalog.parseCatalog(catalog);                }            }        }        else {            fCatalog = null;        }    }        /**     * Attaches the reader to the catalog.     */    private void attachReaderToCatalog (Catalog catalog) {        SAXParserFactory spf = new SAXParserFactoryImpl();        spf.setNamespaceAware(true);        spf.setValidating(false);        SAXCatalogReader saxReader = new SAXCatalogReader(spf);        saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, "catalog",             "com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader");        catalog.addReader("application/xml", saxReader);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?