FileDocCategorySizeDatePackage
BootstrapResolver.javaAPI DocJava SE 6 API6216Tue Jun 10 00:22:58 BST 2008com.sun.org.apache.xml.internal.resolver.helpers

BootstrapResolver

public class BootstrapResolver extends Object implements URIResolver, EntityResolver
A simple bootstrapping resolver.

This class is used as the entity resolver when reading XML Catalogs. It searches for the OASIS XML Catalog DTD, Relax NG Grammar and W3C XML Schema as resources (e.g., in the resolver jar file).

If you have your own DTDs or schemas, you can extend this class and set the BootstrapResolver in your CatalogManager.

see
com.sun.org.apache.xml.internal.resolver.CatalogManager
author
Norman Walsh Norman.Walsh@Sun.COM
version
1.0

Fields Summary
public static final String
xmlCatalogXSD
URI of the W3C XML Schema for OASIS XML Catalog files.
public static final String
xmlCatalogRNG
URI of the RELAX NG Grammar for OASIS XML Catalog files.
public static final String
xmlCatalogPubId
Public identifier for OASIS XML Catalog files.
public static final String
xmlCatalogSysId
System identifier for OASIS XML Catalog files.
private Hashtable
publicMap
Private hash used for public identifiers.
private Hashtable
systemMap
Private hash used for system identifiers.
private Hashtable
uriMap
Private hash used for URIs.
Constructors Summary
public BootstrapResolver()
Constructor.


    
    
    URL url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.dtd");
    if (url != null) {
      publicMap.put(xmlCatalogPubId, url.toString());
      systemMap.put(xmlCatalogSysId, url.toString());
    }

    url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.rng");
    if (url != null) {
      uriMap.put(xmlCatalogRNG, url.toString());
    }

    url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.xsd");
    if (url != null) {
      uriMap.put(xmlCatalogXSD, url.toString());
    }
  
Methods Summary
private java.lang.StringmakeAbsolute(java.lang.String uri)
Attempt to construct an absolute URI

    if (uri == null) {
      uri = "";
    }

    try {
      URL url = new URL(uri);
      return url.toString();
    } catch (MalformedURLException mue) {
      try {
	URL fileURL = FileURL.makeURL(uri);
	return fileURL.toString();
      } catch (MalformedURLException mue2) {
	// bail
	return uri;
      }
    }
  
public javax.xml.transform.Sourceresolve(java.lang.String href, java.lang.String base)
Transformer resolve API.


    String uri = href;
    String fragment = null;
    int hashPos = href.indexOf("#");
    if (hashPos >= 0) {
      uri = href.substring(0, hashPos);
      fragment = href.substring(hashPos+1);
    }

    String result = null;
    if (href != null && uriMap.containsKey(href)) {
      result = (String) uriMap.get(href);
    }

    if (result == null) {
      try {
	URL url = null;

	if (base==null) {
	  url = new URL(uri);
	  result = url.toString();
	} else {
	  URL baseURL = new URL(base);
	  url = (href.length()==0 ? baseURL : new URL(baseURL, uri));
	  result = url.toString();
	}
      } catch (java.net.MalformedURLException mue) {
	// try to make an absolute URI from the current base
	String absBase = makeAbsolute(base);
	if (!absBase.equals(base)) {
	  // don't bother if the absBase isn't different!
	  return resolve(href, absBase);
	} else {
	  throw new TransformerException("Malformed URL "
					 + href + "(base " + base + ")",
					 mue);
	}
      }
    }

    SAXSource source = new SAXSource();
    source.setInputSource(new InputSource(result));
    return source;
  
public org.xml.sax.InputSourceresolveEntity(java.lang.String publicId, java.lang.String systemId)
SAX resolveEntity API.

    String resolved = null;

    if (systemId != null && systemMap.containsKey(systemId)) {
      resolved = (String) systemMap.get(systemId);
    } else if (publicId != null && publicMap.containsKey(publicId)) {
      resolved = (String) publicMap.get(publicId);
    }

    if (resolved != null) {
      try {
	InputSource iSource = new InputSource(resolved);
	iSource.setPublicId(publicId);

	// Ideally this method would not attempt to open the
	// InputStream, but there is a bug (in Xerces, at least)
	// that causes the parser to mistakenly open the wrong
	// system identifier if the returned InputSource does
	// not have a byteStream.
	//
	// It could be argued that we still shouldn't do this here,
	// but since the purpose of calling the entityResolver is
	// almost certainly to open the input stream, it seems to
	// do little harm.
	//
	URL url = new URL(resolved);
	InputStream iStream = url.openStream();
	iSource.setByteStream(iStream);

	return iSource;
      } catch (Exception e) {
	// FIXME: silently fail?
	return null;
      }
    }

    return null;