Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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
*
* https://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.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.util.Stack;
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;
/**
*
This data type provides a catalog of resource locations (such as
* DTDs and XML entities), based on the
* OASIS "Open Catalog" standard. 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 Java API for XML
* Processing Specification.
*
*
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
* plain text format or
* XML format. If the xml-commons resolver library is not found
* in the classpath, external catalog files, specified in
* <catalogpath> paths, will be ignored and a warning will
* be logged. In this case, however, processing of inline entries will proceed
* normally.
*
*
Currently, only <dtd> and
* <entity> elements may be specified inline; these
* correspond to OASIS catalog entry types PUBLIC and
* URI respectively.
* Tasks wishing to use <xmlcatalog> must provide a method called
* createXMLCatalog which returns an instance of
* XMLCatalog. Nested DTD and entity definitions are handled by
* the XMLCatalog object and must be labeled dtd and
* entity respectively.
*
*
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:
*
*
In the local filesystem
*
In the classpath
*
Using the Apache xml-commons resolver (if it is available)
*
In URL-space
*
*
*
See {@link
* org.apache.tools.ant.taskdefs.optional.XMLValidateTask
* XMLValidateTask} for an example of a task that has integrated
* support for XMLCatalogs.
*
*
Possible future extension could provide for additional OASIS
* entry types to be specified inline.
*
*/
public class XMLCatalog extends DataType
implements 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 and 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 and 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 and 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 <catalogpath> element.
* Not allowed if this catalog is itself a reference to another
* catalog -- that is, a catalog cannot both refer to another
* and 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 and 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 <dtd> element. Not
* allowed if this catalog is itself a reference to another
* catalog -- that is, a catalog cannot both refer to another
* and 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 <entity> element. Not
* allowed if this catalog is itself a reference to another
* catalog -- that is, a catalog cannot both refer to another
* and 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 <xmlcatalog> into our
* definition. Not allowed if this catalog is itself a reference
* to another catalog -- that is, a catalog cannot both refer to
* another and 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
getElements().addAll(catalog.getElements());
// 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.
*
*
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 and contain elements or
* attributes.
*
* @param r the reference to which this catalog instance is associated
* @exception BuildException if this instance already has been configured.
*/
@Override
public void setRefid(Reference r) throws BuildException {
if (!elements.isEmpty()) {
throw tooManyAttributes();
}
super.setRefid(r);
}
/**
* Implements the EntityResolver.resolveEntity() interface method.
* @param publicId the public id to resolve.
* @param systemId the system id to resolve.
* @throws SAXException if there is a parsing problem.
* @throws IOException if there is an IO problem.
* @return the resolved entity.
* @see org.xml.sax.EntityResolver#resolveEntity
*/
@Override
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
if (isReference()) {
return getRef().resolveEntity(publicId, systemId);
}
dieOnCircularReference();
log("resolveEntity: '" + publicId + "': '" + systemId + "'",
Project.MSG_DEBUG);
InputSource inputSource =
getCatalogResolver().resolveEntity(publicId, systemId);
if (inputSource == null) {
log("No matching catalog entry found, parser will use: '"
+ systemId + "'", Project.MSG_DEBUG);
}
return inputSource;
}
/**
* Implements the URIResolver.resolve() interface method.
* @param href an href attribute.
* @param base the base URI.
* @return a Source object, or null if href cannot be resolved.
* @throws TransformerException if an error occurs.
* @see javax.xml.transform.URIResolver#resolve
*/
@Override
public Source resolve(String href, String base)
throws TransformerException {
if (isReference()) {
return getRef().resolve(href, base);
}
dieOnCircularReference();
SAXSource source = null;
String uri = removeFragment(href);
log("resolve: '" + uri + "' with base: '" + base + "'", Project.MSG_DEBUG);
source = (SAXSource) getCatalogResolver().resolve(uri, base);
if (source == null) {
log("No matching catalog entry found, parser will use: '"
+ href + "'", Project.MSG_DEBUG);
//
// Cannot return a null source, because we have to call
// setEntityResolver (see setEntityResolver javadoc comment)
//
source = new SAXSource();
URL baseURL;
try {
if (base == null) {
baseURL = FILE_UTILS.getFileURL(getProject().getBaseDir());
} else {
baseURL = new URL(base);
}
URL url = uri.isEmpty() ? baseURL : new URL(baseURL, uri);
source.setInputSource(new InputSource(url.toString()));
} catch (MalformedURLException ex) {
// At this point we are probably in failure mode, but
// try to use the bare URI as a last gasp
source.setInputSource(new InputSource(uri));
}
}
setEntityResolver(source);
return source;
}
@Override
protected synchronized void dieOnCircularReference(Stack