180
Implementing XML Based Applications
as a fall back, as a Java resource accessible from the class path of the entity
resolver class. The latter case allows XML schemas to be bundled along with their
dependent XML processing code. Such bundling can be useful when you must
absolutely guarantee the consistency between the XML processing code and the
schemas.
public class CustomEntityResolver implements EntityResolver {
private Properties entityCatalog = null;
public CustomEntityResolver(URL entityCatalogURL)
throws IOException {
entityCatalog = new Properties(entityCatalog);
entityCatalog.load(entityCatalogURL.openStream());
}
// Opens the physical location as a plain URL or if this fails, as
// a Java resource accessible from the class path.
private InputSource openLocation(String location)
throws IOException {
URL url = null;
InputStream entityStream = null;
try { // Wellformed URL?
url = new URL(location);
} catch (MalformedURLException exception) { ... }
if (url != null) { // Wellformed URL.
try { // Try to open the URL.
entityStream = url.openStream();
} catch (IOException exception) { ... }
}
if (entityStream == null) { // Not a URL or not accessible.
try { // Resource path?
String resourcePath = url != null
? url.getPath() : location;
entityStream
= getClass().getResourceAsStream(resourcePath);
} catch (Exception exception1) { ... }
}
if (entityStream != null) { // Readable URL or resource.
InputSource source = new InputSource(entityStream);
source.setSystemId(location);
New Page 1