ClassLoaderXmlEntityResolver extends ClassLoaderURIResolver implements XMLEntityResolver { @Override public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException { if (log.isDebugEnabled()) log.debug("resolveEntity publicId ["+resourceIdentifier.getPublicId()+"] baseSystemId ["+resourceIdentifier.getBaseSystemId()+"] expandedSystemId ["+resourceIdentifier.getExpandedSystemId()+"] literalSystemId ["+resourceIdentifier.getLiteralSystemId()+"] namespace ["+resourceIdentifier.getNamespace()+"]"); if (resourceIdentifier.getBaseSystemId() == null && resourceIdentifier.getExpandedSystemId() == null && resourceIdentifier.getLiteralSystemId() == null && resourceIdentifier.getNamespace() == null && resourceIdentifier.getPublicId() == null) { return null; } String base = resourceIdentifier.getBaseSystemId(); String href = resourceIdentifier.getLiteralSystemId(); if (href == null) { return null; } Resource resource; try { resource = resolveToResource(href, base); } catch (TransformerException e) { throw new XNIException(e); } InputStream inputStream = resource.getURL().openStream(); return new XMLInputSource(null, resource.getSystemId(), null, inputStream, null); } ClassLoaderXmlEntityResolver(ClassLoader classLoader); @Override XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier);  }
@Test public void localClassPathFileOnRootOfClasspath() throws SAXException, IOException { ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoaderXmlEntityResolver resolver = new ClassLoaderXmlEntityResolver(localClassLoader); XMLResourceIdentifier resourceIdentifier = getXMLResourceIdentifier("AppConstants.properties"); XMLInputSource inputSource = resolver.resolveEntity(resourceIdentifier); assertNotNull(inputSource); }
@Test public void localClassPathFileOnRootOfClasspathAbsolute() throws SAXException, IOException { ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoaderXmlEntityResolver resolver = new ClassLoaderXmlEntityResolver(localClassLoader); XMLResourceIdentifier resourceIdentifier = getXMLResourceIdentifier("/AppConstants.properties"); XMLInputSource inputSource = resolver.resolveEntity(resourceIdentifier); assertNotNull(inputSource); }
@Test public void localClassPathAbsolute() throws SAXException, IOException { ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoaderXmlEntityResolver resolver = new ClassLoaderXmlEntityResolver(localClassLoader); XMLResourceIdentifier resourceIdentifier = getXMLResourceIdentifier("/Xslt/importDocument/lookup.xml"); XMLInputSource inputSource = resolver.resolveEntity(resourceIdentifier); 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, ""); ClassLoaderXmlEntityResolver resolver = new ClassLoaderXmlEntityResolver(cl); XMLResourceIdentifier resourceIdentifier = getXMLResourceIdentifier("ClassLoader/Xslt/names.xsl"); XMLInputSource inputSource = resolver.resolveEntity(resourceIdentifier); 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, ""); ClassLoaderXmlEntityResolver resolver = new ClassLoaderXmlEntityResolver(cl); XMLResourceIdentifier resourceIdentifier = getXMLResourceIdentifier("/ClassLoader/Xslt/names.xsl"); XMLInputSource inputSource = resolver.resolveEntity(resourceIdentifier); assertNotNull(inputSource); }