⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlcatalog.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* *  Licensed to the Apache Software Foundation (ASF) under one or more *  contributor license agreements.  See the NOTICE file distributed with *  this work for additional information regarding copyright ownership. *  The ASF licenses this file to You 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 org.apache.tools.ant.types;import java.lang.reflect.Method;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import java.util.Enumeration;import java.util.Vector;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParserFactory;import javax.xml.transform.Source;import javax.xml.transform.TransformerException;import javax.xml.transform.URIResolver;import javax.xml.transform.sax.SAXSource;import org.apache.tools.ant.AntClassLoader;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.JAXPUtils;import org.xml.sax.EntityResolver;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;/** * <p>This data type provides a catalog of resource locations (such as * DTDs and XML entities), based on the <a * href="http://oasis-open.org/committees/entity/spec-2001-08-06.html"> * OASIS "Open Catalog" standard</a>.  The catalog entries are used * both for Entity resolution and URI resolution, in accordance with * the {@link org.xml.sax.EntityResolver EntityResolver} and {@link * javax.xml.transform.URIResolver URIResolver} interfaces as defined * in the <a href="http://java.sun.com/xml/jaxp">Java API for XML * Processing Specification</a>.</p> * * <p>Resource locations can be specified either in-line or in * external catalog file(s), or both.  In order to use an external * catalog file, the xml-commons resolver library ("resolver.jar") * must be in your classpath.  External catalog files may be either <a * href="http://oasis-open.org/committees/entity/background/9401.html"> * plain text format</a> or <a * href="http://www.oasis-open.org/committees/entity/spec-2001-08-06.html"> * XML format</a>.  If the xml-commons resolver library is not found * in the classpath, external catalog files, specified in * <code>&lt;catalogpath&gt;</code> paths, will be ignored and a warning will * be logged.  In this case, however, processing of inline entries will proceed * normally.</p> * * <p>Currently, only <code>&lt;dtd&gt;</code> and * <code>&lt;entity&gt;</code> elements may be specified inline; these * correspond to OASIS catalog entry types <code>PUBLIC</code> and * <code>URI</code> respectively.</p> * * <p>The following is a usage example:</p> * * <code> * &lt;xmlcatalog&gt;<br> * &nbsp;&nbsp;&lt;dtd publicId="" location="/path/to/file.jar" /&gt;<br> * &nbsp;&nbsp;&lt;dtd publicId="" location="/path/to/file2.jar" /&gt;<br> * &nbsp;&nbsp;&lt;entity publicId="" location="/path/to/file3.jar" /&gt;<br> * &nbsp;&nbsp;&lt;entity publicId="" location="/path/to/file4.jar" /&gt;<br> * &nbsp;&nbsp;&lt;catalogpath&gt;<br> * &nbsp;&nbsp;&nbsp;&nbsp;&lt;pathelement location="/etc/sgml/catalog"/&gt;<br> * &nbsp;&nbsp;&lt;/catalogpath&gt;<br> * &nbsp;&nbsp;&lt;catalogfiles dir="/opt/catalogs/" includes="**\catalog.xml" /&gt;<br> * &lt;/xmlcatalog&gt;<br> * </code> * <p> * Tasks wishing to use <code>&lt;xmlcatalog&gt;</code> must provide a method called * <code>createXMLCatalog</code> which returns an instance of * <code>XMLCatalog</code>. Nested DTD and entity definitions are handled by * the XMLCatalog object and must be labeled <code>dtd</code> and * <code>entity</code> respectively.</p> * * <p>The following is a description of the resolution algorithm: * entities/URIs/dtds are looked up in each of the following contexts, * stopping when a valid and readable resource is found: * <ol> * <li>In the local filesystem</li> * <li>In the classpath</li> * <li>Using the Apache xml-commons resolver (if it is available)</li> * <li>In URL-space</li> * </ol> * </p> * * <p>See {@link * org.apache.tools.ant.taskdefs.optional.XMLValidateTask * XMLValidateTask} for an example of a task that has integrated * support for XMLCatalogs.</p> * * <p>Possible future extension could provide for additional OASIS * entry types to be specified inline.</p> * */public class XMLCatalog extends DataType    implements Cloneable, EntityResolver, URIResolver {    /** helper for some File.toURL connversions */    private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();    //-- Fields ----------------------------------------------------------------    /** Holds dtd/entity objects until needed. */    private Vector elements = new Vector();    /**     * Classpath in which to attempt to resolve resources.     */    private Path classpath;    /**     * Path listing external catalog files to search when resolving entities     */    private Path catalogPath;    /**     * The name of the bridge to the Apache xml-commons resolver     * class, used to determine whether resolver.jar is present in the     * classpath.     */    public static final String APACHE_RESOLVER        = "org.apache.tools.ant.types.resolver.ApacheCatalogResolver";    /**     * Resolver base class     */    public static final String CATALOG_RESOLVER        = "org.apache.xml.resolver.tools.CatalogResolver";        //-- Methods ---------------------------------------------------------------    /**     * Default constructor     */    public XMLCatalog() {        setChecked(false);    }    /**     * Returns the elements of the catalog - ResourceLocation objects.     *     * @return the elements of the catalog - ResourceLocation objects     */    private Vector getElements() {        return getRef().elements;    }    /**     * Returns the classpath in which to attempt to resolve resources.     *     * @return the classpath     */    private Path getClasspath() {        return getRef().classpath;    }    /**     * Allows nested classpath elements. Not allowed if this catalog     * is itself a reference to another catalog -- that is, a catalog     * cannot both refer to another <em>and</em> contain elements or     * other attributes.     *     * @return a Path instance to be configured.     */    public Path createClasspath() {        if (isReference()) {            throw noChildrenAllowed();        }        if (this.classpath == null) {            this.classpath = new Path(getProject());        }        setChecked(false);        return this.classpath.createPath();    }    /**     * Allows simple classpath string.  Not allowed if this catalog is     * itself a reference to another catalog -- that is, a catalog     * cannot both refer to another <em>and</em> contain elements or     * other attributes.     *     * @param classpath the classpath to use to look up entities.     */    public void setClasspath(Path classpath) {        if (isReference()) {            throw tooManyAttributes();        }        if (this.classpath == null) {            this.classpath = classpath;        } else {            this.classpath.append(classpath);        }        setChecked(false);    }    /**     * Allows classpath reference.  Not allowed if this catalog is     * itself a reference to another catalog -- that is, a catalog     * cannot both refer to another <em>and</em> contain elements or     * other attributes.     *     * @param r an Ant reference containing a classpath.     */    public void setClasspathRef(Reference r) {        if (isReference()) {            throw tooManyAttributes();        }        createClasspath().setRefid(r);        setChecked(false);    }    /** Creates a nested <code>&lt;catalogpath&gt;</code> element.     * Not allowed if this catalog is itself a reference to another     * catalog -- that is, a catalog cannot both refer to another     * <em>and</em> contain elements or other attributes.     *     * @return a path to be configured as the catalog path.     * @exception BuildException     * if this is a reference and no nested elements are allowed.     */    public Path createCatalogPath() {        if (isReference()) {            throw noChildrenAllowed();        }        if (this.catalogPath == null) {            this.catalogPath = new Path(getProject());        }        setChecked(false);        return this.catalogPath.createPath();    }    /**     * Allows catalogpath reference.  Not allowed if this catalog is     * itself a reference to another catalog -- that is, a catalog     * cannot both refer to another <em>and</em> contain elements or     * other attributes.     *     * @param r an Ant reference containing a classpath to be used as     * the catalog path.     */    public void setCatalogPathRef(Reference r) {        if (isReference()) {            throw tooManyAttributes();        }        createCatalogPath().setRefid(r);        setChecked(false);    }    /**     * Returns the catalog path in which to attempt to resolve DTDs.     *     * @return the catalog path     */    public Path getCatalogPath() {        return getRef().catalogPath;    }    /**     * Creates the nested <code>&lt;dtd&gt;</code> element.  Not     * allowed if this catalog is itself a reference to another     * catalog -- that is, a catalog cannot both refer to another     * <em>and</em> contain elements or other attributes.     *     * @param dtd the information about the PUBLIC resource mapping to     *            be added to the catalog     * @exception BuildException if this is a reference and no nested     *       elements are allowed.     */    public void addDTD(ResourceLocation dtd) throws BuildException {        if (isReference()) {            throw noChildrenAllowed();        }        getElements().addElement(dtd);        setChecked(false);    }    /**     * Creates the nested <code>&lt;entity&gt;</code> element.    Not     * allowed if this catalog is itself a reference to another     * catalog -- that is, a catalog cannot both refer to another     * <em>and</em> contain elements or other attributes.     *     * @param entity the information about the URI resource mapping to be     *       added to the catalog.     * @exception BuildException if this is a reference and no nested     *       elements are allowed.     */    public void addEntity(ResourceLocation entity) throws BuildException {        addDTD(entity);    }    /**     * Loads a nested <code>&lt;xmlcatalog&gt;</code> into our     * definition.  Not allowed if this catalog is itself a reference     * to another catalog -- that is, a catalog cannot both refer to     * another <em>and</em> contain elements or other attributes.     *     * @param catalog Nested XMLCatalog     */    public void addConfiguredXMLCatalog(XMLCatalog catalog) {        if (isReference()) {            throw noChildrenAllowed();        }        // Add all nested elements to our catalog        Vector newElements = catalog.getElements();        Vector ourElements = getElements();        Enumeration e = newElements.elements();        while (e.hasMoreElements()) {            ourElements.addElement(e.nextElement());        }        // Append the classpath of the nested catalog        Path nestedClasspath = catalog.getClasspath();        createClasspath().append(nestedClasspath);        // Append the catalog path of the nested catalog        Path nestedCatalogPath = catalog.getCatalogPath();        createCatalogPath().append(nestedCatalogPath);        setChecked(false);    }    /**     * Makes this instance in effect a reference to another XMLCatalog     * instance.     *     * <p>You must not set another attribute or nest elements inside     * this element if you make it a reference.  That is, a catalog     * cannot both refer to another <em>and</em> contain elements or     * attributes.</p>     *     * @param r the reference to which this catalog instance is associated     * @exception BuildException if this instance already has been configured.     */    public void setRefid(Reference r) throws BuildException {        if (!elements.isEmpty()) {            throw tooManyAttributes();        }        super.setRefid(r);    }    /**     * Implements the EntityResolver.resolveEntity() interface method.

⌨️ 快捷键说明

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