package com.sun.j2ee.blueprints.xmldocuments;
import java.io.*;
import java.net.URL;
import java.util.Properties;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
public class CustomEntityResolver implements EntityResolver {
public static final boolean TRACE = false;
public static final String ENTITY_CATALOG
= "/com/sun/j2ee/blueprints/xmldocuments/rsrc/EntityCatalog.properties";
private Properties entityCatalog = null;
private EntityResolver parentResolver = null;
public CustomEntityResolver() {
this((EntityResolver) null);
return;
}
public CustomEntityResolver(EntityResolver parentResolver) {
this.parentResolver = parentResolver;
entityCatalog = new Properties();
InputStream stream = getClass().getResourceAsStream(ENTITY_CATALOG);
if (stream != null) {
if (TRACE) {
System.err.println("Load resource from: " + getClass().getResource(ENTITY_CATALOG));
}
try {
entityCatalog.load(stream);
} catch (IOException exception) {
System.err.println("Can't load from resource: " + ENTITY_CATALOG + ": " + exception);
}
} else {
if (TRACE) {
System.err.println("Can't access resource: " + ENTITY_CATALOG);
}
}
return;
}
public CustomEntityResolver(URL entityCatalogURL) {
this(entityCatalogURL, null);
return;
}
public CustomEntityResolver(URL entityCatalogURL, EntityResolver parentResolver) {
this(parentResolver);
if (entityCatalogURL != null) {
entityCatalog = new Properties(entityCatalog);
try {
entityCatalog.load(entityCatalogURL.openStream());
} catch (IOException exception) {
System.err.println("Can't load from resource: " + entityCatalogURL + ": " + exception);
}
}
return;
}
public CustomEntityResolver(Properties entityCatalog) {
this(entityCatalog, null);
return;
}
public CustomEntityResolver(Properties entityCatalog, EntityResolver parentResolver) {
this(parentResolver);
this.entityCatalog = new Properties(this.entityCatalog);
this.entityCatalog.putAll(entityCatalog);
return;
}
private InputSource resolveEntityFromURL(String entityURL) throws IOException {
URL entityURLURL = null;
InputStream entityURLStream = null;
try { entityURLURL = new URL(entityURL);
} catch (Exception exception) {
if (TRACE) {
System.err.println("entityURL: " + entityURL + ": not a URL");
}
}
if (entityURLURL != null) { try {
entityURLStream = entityURLURL.openStream(); } catch (Exception exception) {
if (TRACE) {
System.err.println("entityURL: " + entityURL + ": not a readable URL");
}
}
}
if (entityURLStream == null) { if (TRACE) {
System.err.print("entityURL: " + entityURL + " is it a resource? ");
}
try { String resourcePath = entityURLURL != null ? entityURLURL.getPath() : entityURL;
entityURLStream = getClass().getResourceAsStream(resourcePath);
if (TRACE) {
System.err.println(entityURLStream != null ? "Yes" : "No");
if (entityURLStream != null) {
System.err.println("Load resource from: " + getClass().getResource(resourcePath));
}
}
} catch (Exception exception1) {
if (TRACE) {
System.err.println("No");
System.err.println(exception1.getMessage());
exception1.printStackTrace(System.err);
}
}
}
if (entityURLStream != null) { InputSource source = new InputSource(entityURLStream);
source.setSystemId(entityURL);
return source;
}
return null;
}
public String mapEntityURI(String entityURI) {
if (TRACE) {
System.err.println("mapEntityURI: " + entityURI);
}
if (entityCatalog != null) {
if (entityURI != null) {
String entityURL = entityCatalog.getProperty(entityURI);
if (TRACE) {
System.err.println("mapEntityURI: " + entityURI + " mapped to: " + entityURL);
}
return entityURL;
}
}
return null;
}
public InputSource resolveEntity(String entityURI, String entityURL) {
if (TRACE) {
System.err.println("Resolving: " + entityURI + " " + entityURL);
}
InputSource source = null;
try {
if (parentResolver != null) {
source = parentResolver.resolveEntity(entityURI, entityURL);
if (source != null) {
if (TRACE) {
System.err.println("Entity resolved by parent resolver: " + entityURI + " " + entityURL + ": " + source);
}
return source;
}
}
} catch (Exception exception) {
if (TRACE) {
System.err.println("Parent resolver failed to resolve: " + entityURI + " " + entityURL + ": " + exception);
}
}
try {
if (entityURI != null) {
String mappedEntityURL = mapEntityURI(entityURI);
if (mappedEntityURL != null) {
source = resolveEntityFromURL(mappedEntityURL);
if (source != null) {
return source;
}
}
}
if (entityURL != null) {
source = resolveEntityFromURL(entityURL);
if (source != null) {
return source;
}
}
} catch (Exception exception) {
System.err.println("Cannot resolve " + entityURI + " using: " + entityCatalog + " " + exception);
}
return null; }
}