ClassLoaderEntityResolver implements EntityResolver { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { if (log.isDebugEnabled()) log.debug("Resolving publicId [" + publicId +"] systemId [" + systemId +"] classloader ["+classLoader+"]"); Resource resource = Resource.getResource(classLoader, systemId); if(resource != null) { return resource.asInputSource(); } return null; } ClassLoaderEntityResolver(ClassLoader classLoader);  ClassLoaderEntityResolver(Resource resource); @Override InputSource resolveEntity(String publicId, String systemId);  }
@Test public void localClassPathFileOnRootOfClasspath() throws SAXException, IOException { ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoaderEntityResolver resolver = new ClassLoaderEntityResolver(localClassLoader); String systemId="AppConstants.properties"; InputSource inputSource = resolver.resolveEntity(publicId, systemId); assertNotNull(inputSource); }
@Test public void localClassPathFileOnRootOfClasspathAbsolute() throws SAXException, IOException { ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoaderEntityResolver resolver = new ClassLoaderEntityResolver(localClassLoader); String systemId="/AppConstants.properties"; InputSource inputSource = resolver.resolveEntity(publicId, systemId); assertNotNull(inputSource); }
@Test public void localClassPathAbsolute() throws SAXException, IOException { ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoaderEntityResolver resolver = new ClassLoaderEntityResolver(localClassLoader); String systemId="/Xslt/importDocument/lookup.xml"; InputSource inputSource = resolver.resolveEntity(publicId, systemId); assertNotNull(inputSource); }
@Test public void bytesClassPath() throws SAXException, IOException, ConfigurationException { ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader(); URL file = this.getClass().getResource(JAR_FILE); assertNotNull("jar url not found", file); JarFile jarFile = new JarFile(file.getFile()); assertNotNull("jar file not found",jarFile); JarFileClassLoader cl = new JarFileClassLoader(localClassLoader); cl.setJar(file.getFile()); cl.configure(null, ""); ClassLoaderEntityResolver resolver = new ClassLoaderEntityResolver(cl); String systemId="/ClassLoader/Xslt/names.xsl"; InputSource inputSource = resolver.resolveEntity(publicId, systemId); assertNotNull(inputSource); }
@Test public void bytesClassPathAbsolute() throws SAXException, IOException, ConfigurationException { ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader(); URL file = this.getClass().getResource(JAR_FILE); assertNotNull("jar url not found", file); JarFile jarFile = new JarFile(file.getFile()); assertNotNull("jar file not found",jarFile); JarFileClassLoader cl = new JarFileClassLoader(localClassLoader); cl.setJar(file.getFile()); cl.configure(null, ""); ClassLoaderEntityResolver resolver = new ClassLoaderEntityResolver(cl); String systemId="ClassLoader/Xslt/names.xsl"; InputSource inputSource = resolver.resolveEntity(publicId, systemId); assertNotNull(inputSource); }
@Test @Ignore("Fixed the original problem in XmlUtils.identityTransform(), that did not set a systemId for relative resolutions") public void localClassPathFullPath() throws SAXException, IOException, ConfigurationException { ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader(); final URL context = this.getClass().getResource("/"); assertNotNull(context); final String randomName = "myFile-"+Misc.createRandomUUID(); URL file = new URL(context, randomName); assertNotNull(file); ClassLoader dummyClassLoader = new ClassLoader(localClassLoader) { @Override public URL getResource(String name) { System.out.println("dummyClassLoader name ["+name+"]"); try { if(randomName.equals(name)) { return new URL(context, "file.xml"); } else { return new URL(context, "file-not-found"); } } catch (MalformedURLException e) { fail("what? "+e.getMessage()); return null; } } }; ClassLoaderEntityResolver resolver = new ClassLoaderEntityResolver(dummyClassLoader); try{ file.openStream(); fail("This should fail!"); } catch (IOException e) {} String systemId = file.getFile(); System.out.println("file to resolve ["+systemId+"]"); InputSource inputSource = resolver.resolveEntity(publicId, systemId); assertNotNull(inputSource); }