Methods Summary |
---|
public void | addConfiguredXMLCatalog(org.apache.tools.ant.types.XMLCatalog catalog)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.
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);
|
public void | addDTD(ResourceLocation dtd)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.
if (isReference()) {
throw noChildrenAllowed();
}
getElements().addElement(dtd);
setChecked(false);
|
public void | addEntity(ResourceLocation entity)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.
addDTD(entity);
|
private org.xml.sax.InputSource | classpathLookup(ResourceLocation matchingEntry)Utility method to lookup a ResourceLocation in the classpath.
InputSource source = null;
AntClassLoader loader = null;
Path cp = classpath;
if (cp != null) {
cp = classpath.concatSystemClasspath("ignore");
} else {
cp = (new Path(getProject())).concatSystemClasspath("last");
}
loader = getProject().createClassLoader(cp);
//
// for classpath lookup we ignore the base directory
//
InputStream is
= loader.getResourceAsStream(matchingEntry.getLocation());
if (is != null) {
source = new InputSource(is);
URL entryURL = loader.getResource(matchingEntry.getLocation());
String sysid = entryURL.toExternalForm();
source.setSystemId(sysid);
log("catalog entry matched a resource in the classpath: '"
+ sysid + "'", Project.MSG_DEBUG);
}
return source;
|
public Path | createCatalogPath()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.
if (isReference()) {
throw noChildrenAllowed();
}
if (this.catalogPath == null) {
this.catalogPath = new Path(getProject());
}
setChecked(false);
return this.catalogPath.createPath();
|
public Path | createClasspath()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.
if (isReference()) {
throw noChildrenAllowed();
}
if (this.classpath == null) {
this.classpath = new Path(getProject());
}
setChecked(false);
return this.classpath.createPath();
|
private org.xml.sax.InputSource | filesystemLookup(ResourceLocation matchingEntry)Utility method to lookup a ResourceLocation in the filesystem.
String uri = matchingEntry.getLocation();
// the following line seems to be necessary on Windows under JDK 1.2
uri = uri.replace(File.separatorChar, '/");
URL baseURL = null;
//
// The ResourceLocation may specify a relative path for its
// location attribute. This is resolved using the appropriate
// base.
//
if (matchingEntry.getBase() != null) {
baseURL = matchingEntry.getBase();
} else {
try {
baseURL = FILE_UTILS.getFileURL(getProject().getBaseDir());
} catch (MalformedURLException ex) {
throw new BuildException("Project basedir cannot be converted to a URL");
}
}
InputSource source = null;
URL url = null;
try {
url = new URL(baseURL, uri);
} catch (MalformedURLException ex) {
// this processing is useful under Windows when the location of the DTD
// has been given as an absolute path
// see Bugzilla Report 23913
File testFile = new File(uri);
if (testFile.exists() && testFile.canRead()) {
log("uri : '"
+ uri + "' matches a readable file", Project.MSG_DEBUG);
try {
url = FILE_UTILS.getFileURL(testFile);
} catch (MalformedURLException ex1) {
throw new BuildException(
"could not find an URL for :" + testFile.getAbsolutePath());
}
} else {
log("uri : '"
+ uri + "' does not match a readable file", Project.MSG_DEBUG);
}
}
if (url != null && url.getProtocol().equals("file")) {
String fileName = FILE_UTILS.fromURI(url.toString());
if (fileName != null) {
log("fileName " + fileName, Project.MSG_DEBUG);
File resFile = new File(fileName);
if (resFile.exists() && resFile.canRead()) {
try {
source = new InputSource(new FileInputStream(resFile));
String sysid = JAXPUtils.getSystemId(resFile);
source.setSystemId(sysid);
log("catalog entry matched a readable file: '"
+ sysid + "'", Project.MSG_DEBUG);
} catch (IOException ex) {
// ignore
}
}
}
}
return source;
|
private ResourceLocation | findMatchingEntry(java.lang.String publicId)Find a ResourceLocation instance for the given publicId.
Enumeration e = getElements().elements();
ResourceLocation element = null;
while (e.hasMoreElements()) {
Object o = e.nextElement();
if (o instanceof ResourceLocation) {
element = (ResourceLocation) o;
if (element.getPublicId().equals(publicId)) {
return element;
}
}
}
return null;
|
public Path | getCatalogPath()Returns the catalog path in which to attempt to resolve DTDs.
return getRef().catalogPath;
|
private org.apache.tools.ant.types.XMLCatalog$CatalogResolver | getCatalogResolver()Factory method for creating the appropriate CatalogResolver
strategy implementation.
Until we query the classpath, we don't know whether the Apache
resolver (Norm Walsh's library from xml-commons) is available or not.
This method determines whether the library is available and creates the
appropriate implementation of CatalogResolver based on the answer.
This is an application of the Gang of Four Strategy Pattern
combined with Template Method.
if (catalogResolver == null) {
AntClassLoader loader = null;
loader = getProject().createClassLoader(Path.systemClasspath);
try {
Class clazz = Class.forName(APACHE_RESOLVER, true, loader);
// The Apache resolver is present - Need to check if it can
// be seen by the catalog resolver class. Start by getting
// the actual loader
ClassLoader apacheResolverLoader = clazz.getClassLoader();
// load the base class through this loader.
Class baseResolverClass
= Class.forName(CATALOG_RESOLVER, true, apacheResolverLoader);
// and find its actual loader
ClassLoader baseResolverLoader
= baseResolverClass.getClassLoader();
// We have the loader which is being used to load the
// CatalogResolver. Can it see the ApacheResolver? The
// base resolver will only be able to create the ApacheResolver
// if it can see it - doesn't use the context loader.
clazz = Class.forName(APACHE_RESOLVER, true, baseResolverLoader);
Object obj = clazz.newInstance();
//
// Success! The xml-commons resolver library is
// available, so use it.
//
catalogResolver = new ExternalResolver(clazz, obj);
} catch (Throwable ex) {
//
// The xml-commons resolver library is not
// available, so we can't use it.
//
catalogResolver = new InternalResolver();
if (getCatalogPath() != null
&& getCatalogPath().list().length != 0) {
log("Warning: XML resolver not found; external catalogs"
+ " will be ignored", Project.MSG_WARN);
}
log("Failed to load Apache resolver: " + ex, Project.MSG_DEBUG);
}
}
return catalogResolver;
|
private Path | getClasspath()Returns the classpath in which to attempt to resolve resources.
return getRef().classpath;
|
private java.util.Vector | getElements()Returns the elements of the catalog - ResourceLocation objects.
return getRef().elements;
|
private org.apache.tools.ant.types.XMLCatalog | getRef()
if (!isReference()) {
return this;
}
return (XMLCatalog) getCheckedRef(XMLCatalog.class, "xmlcatalog");
|
private java.lang.String | removeFragment(java.lang.String uri)Utility method to remove trailing fragment from a URI.
For example,
http://java.sun.com/index.html#chapter1
would return http://java.sun.com/index.html .
String result = uri;
int hashPos = uri.indexOf("#");
if (hashPos >= 0) {
result = uri.substring(0, hashPos);
}
return result;
|
public javax.xml.transform.Source | resolve(java.lang.String href, java.lang.String base)Implements the URIResolver.resolve() interface method.
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 = null;
try {
if (base == null) {
baseURL = FILE_UTILS.getFileURL(getProject().getBaseDir());
} else {
baseURL = new URL(base);
}
URL url = (uri.length() == 0 ? 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;
|
public org.xml.sax.InputSource | resolveEntity(java.lang.String publicId, java.lang.String systemId)Implements the EntityResolver.resolveEntity() interface method.
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;
|
public void | setCatalogPathRef(Reference r)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.
if (isReference()) {
throw tooManyAttributes();
}
createCatalogPath().setRefid(r);
setChecked(false);
|
public void | setClasspath(Path classpath)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.
if (isReference()) {
throw tooManyAttributes();
}
if (this.classpath == null) {
this.classpath = classpath;
} else {
this.classpath.append(classpath);
}
setChecked(false);
|
public void | setClasspathRef(Reference r)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.
if (isReference()) {
throw tooManyAttributes();
}
createClasspath().setRefid(r);
setChecked(false);
|
private void | setEntityResolver(javax.xml.transform.sax.SAXSource source)This is called from the URIResolver to set an EntityResolver
on the SAX parser to be used for new XML documents that are
encountered as a result of the document() function, xsl:import,
or xsl:include. This is done because the XSLT processor calls
out to the SAXParserFactory itself to create a new SAXParser to
parse the new document. The new parser does not automatically
inherit the EntityResolver of the original (although arguably
it should). See below:
"If an application wants to set the ErrorHandler or
EntityResolver for an XMLReader used during a transformation,
it should use a URIResolver to return the SAXSource which
provides (with getXMLReader) a reference to the XMLReader"
...quoted from page 118 of the Java API for XML
Processing 1.1 specification
XMLReader reader = source.getXMLReader();
if (reader == null) {
SAXParserFactory spFactory = SAXParserFactory.newInstance();
spFactory.setNamespaceAware(true);
try {
reader = spFactory.newSAXParser().getXMLReader();
} catch (ParserConfigurationException ex) {
throw new TransformerException(ex);
} catch (SAXException ex) {
throw new TransformerException(ex);
}
}
reader.setEntityResolver(this);
source.setXMLReader(reader);
|
public void | setRefid(Reference r)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.
if (!elements.isEmpty()) {
throw tooManyAttributes();
}
super.setRefid(r);
|
private org.xml.sax.InputSource | urlLookup(ResourceLocation matchingEntry)Utility method to lookup a ResourceLocation in URL-space.
String uri = matchingEntry.getLocation();
URL baseURL = null;
//
// The ResourceLocation may specify a relative url for its
// location attribute. This is resolved using the appropriate
// base.
//
if (matchingEntry.getBase() != null) {
baseURL = matchingEntry.getBase();
} else {
try {
baseURL = FILE_UTILS.getFileURL(getProject().getBaseDir());
} catch (MalformedURLException ex) {
throw new BuildException("Project basedir cannot be converted to a URL");
}
}
InputSource source = null;
URL url = null;
try {
url = new URL(baseURL, uri);
} catch (MalformedURLException ex) {
// ignore
}
if (url != null) {
try {
InputStream is = url.openStream();
if (is != null) {
source = new InputSource(is);
String sysid = url.toExternalForm();
source.setSystemId(sysid);
log("catalog entry matched as a URL: '"
+ sysid + "'", Project.MSG_DEBUG);
}
} catch (IOException ex) {
// ignore
}
}
return source;
|