package textbender.g.xml.sax.ext; // Copyright 2005, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.io.*; import org.xml.sax.*; import org.xml.sax.ext.DefaultHandler2; import textbender.g.util.logging.LoggerX; /** A resolver that searches the class path for entities. */ public final class EntityResolverCP extends DefaultHandler2 { // cf. textbender.g.xml.dom.ls.LSResourceResolverCP, and FIX along same lines /** Constructs an EntityResolverCP. * * @param directoryArray For each directory d of this array, * entities of basename b are sought on the classpath at d/b. * Directories are specified without a trailing '/'. */ public EntityResolverCP( final String[] directoryArray ) { this.directoryArray = directoryArray; } // - E n t i t y - R e s o l v e r - 2 ------------------------------------------------ public InputSource resolveEntity( String name, String publicId, String baseURI, String systemId ) throws SAXException, IOException { // LoggerX.i(getClass()).finest( systemId ); final String baseName = systemId.substring( systemId.lastIndexOf('/') + 1 ); InputStream entityStream = null; // till proven otherwise for( String d : directoryArray ) { StringBuilder pathSB = emptyStringBuilder(); if( !".".equals( d )) // because a relative path './etc/etc' does not resolve into jars, for some reason (though it's OK for loose files) { pathSB.append( d ); pathSB.append( '/' ); } pathSB.append( baseName ); // entityStream = ClassLoader.getSystemResourceAsStream( pathSB.toString() ); ///// but maybe we are not using system classloader (e.g. running as applet), so: entityStream = EntityResolverCP.class.getClassLoader() .getResourceAsStream( pathSB.toString() ); if( entityStream != null ) break; } if( entityStream == null ) // fall back to default resolution { return super.resolveEntity( name, publicId, baseURI, systemId ); } return new InputSource( entityStream ); // OPT, maybe give it system and public ID's, to improve output of error messages? Cf. textbender.g.xml.dom.ls.LSResourceResolverCP } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final String[] directoryArray; /** Empties and returns the reuseable string builder. */ private StringBuilder emptyStringBuilder() { stringBuilder.delete( 0, Integer.MAX_VALUE ); return stringBuilder; } private final StringBuilder stringBuilder = new StringBuilder(); }