package textbender.g.xml.dom.ls; // Copyright 2007, 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.net.*; import org.w3c.dom.ls.*; import textbender.g.util.logging.LoggerX; /** A resolver that searches the class path for resources. */ public class LSResourceResolverCP implements LSResourceResolver { // cf. textbender.g.xml.sax.ext.EntityResolverCP /** Constructs a LSResourceResolverCP. */ public LSResourceResolverCP( final DOMImplementationLS dom ) { this.dom = dom; } // - L S - R e s o u r c e - R e s o l v e r ------------------------------------------ /** Resolves the resource by submitting its systemId * (adjusted for the baseURI) * to ClassLoader.{@linkplain java.lang.ClassLoader#getResource(String) getResource}. */ public LSInput resolveResource( final String type, final String namespaceURI, final String publicId, final String systemId, final String baseURI ) { // textbender.g.util.logging.LoggerX.i(getClass()).finest( systemId ); if( systemId == null ) return null; final String path; // (PARENT-PART/)?SIMPLE-FILENAME try { final URI systemURI = new URI( systemId ); if( systemURI.getScheme() != null || systemURI.getAuthority() != null ) return null; // 'http' or something, not a classpath-relative resource String s = systemURI.getPath(); if( s.startsWith( "/" )) path = s.substring( 1 ); else if( baseURI != null ) { URI base = new URI( baseURI ); // of the entity making the request s = base.resolve( systemURI ).getPath(); if( s.startsWith( "/" )) path = s.substring( 1 ); else path = s; } else path = s; } catch( URISyntaxException x ) { LoggerX.i(getClass()).warning( "skipping resolution: " + x ); return null; } final URL resourceURL = LSResourceResolverCP.class.getClassLoader().getResource( path ); if( resourceURL == null ) return null; final LSInput resourceInput = dom.createLSInput(); // OPT to reuse try { resourceInput.setByteStream( resourceURL.openStream() ); } catch( Exception x ){ throw new Failure( publicId, systemId, x ); } // IOException, URISyntaxException resourceInput.setPublicId( publicId ); // parser may report these in error messages resourceInput.setSystemId( path ); resourceInput.setBaseURI( "/" ); // without which, baseURI of subsequent calls to this method may be prepended by the document's URI return resourceInput; } // ==================================================================================== /** Signals an unexpected failure of resource resolution. */ public static final class Failure extends RuntimeException { private static final long serialVersionUID = 1L; public Failure( String publicId, String systemId, Throwable cause ) { super( "resolving: " + publicId + ", " + systemId, cause ); } } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final DOMImplementationLS dom; }