|
From: <leg...@at...> - 2003-07-30 12:47:49
|
The following issue has been updated:
Updater: Emmanuel Ligne (mailto:emm...@at...)
Date: Wed, 30 Jul 2003 7:27 AM
Changes:
Attachment changed to DTDEntityResolver.java
---------------------------------------------------------------------
For a full history of the issue, see:
http://opensource.atlassian.com/projects/hibernate/secure/ViewIssue.jspa?key=HB-215&page=history
---------------------------------------------------------------------
View the issue:
http://opensource.atlassian.com/projects/hibernate/secure/ViewIssue.jspa?key=HB-215
Here is an overview of the issue:
---------------------------------------------------------------------
Key: HB-215
Summary: Resolving XML external entities from a XML file enclosed in a jar
Type: Patch
Status: Unassigned
Priority: Minor
Project: Hibernate2
Components:
core
Versions:
2.0.1
Assignee:
Reporter: Emmanuel Ligne
Created: Mon, 28 Jul 2003 6:14 AM
Updated: Wed, 30 Jul 2003 7:27 AM
Environment: Hibernate 2.0.1, windows 2000, jdk 1.4.1
Description:
When specifying something like <!ENTITY frame SYSTEM "/mypath/Frame.xml">, if both enclosing and enclosed XML files are in jar file, Frame.xml is not searched in the enclosing file jar
origin (as should be done regarding specif of relative URI search priority).
Updated net.sf.hibernate.util.DTDEntityResolver as well as its users
(ie XMLHelper and Configuration) so that optionnally a ClassLoader
can be passed. This classloader is tried to load resources as stream
given their systemId if the systemId is told to start with file:///
(ie relative uri).
code for DTDEntityResolver :
public class DTDEntityResolver implements EntityResolver {
Log log = LogFactory.getLog(DTDEntityResolver.class);
private final ClassLoader loader ;
public DTDEntityResolver(ClassLoader loader) {
super() ;
this.loader = loader ;
}
private static final String URL = "http://hibernate.sourceforge.net/";
private static final String FILE = "file:///" ;
public InputSource resolveEntity (String publicId, String systemId) {
if ( systemId!=null) {
if (systemId.startsWith(URL) ) {
log.debug("trying to locate " + systemId + " in classpath under net/sf/hibernate/");
// Search for DTD
ClassLoader classLoader = this.getClass().getClassLoader();
InputStream dtdStream = classLoader.getResourceAsStream( "net/sf/hibernate/" + systemId.substring( URL.length() ) );
if (dtdStream==null) {
log.debug(systemId + "not found in classpath");
return null;
}
else {
log.debug("found " + systemId + " in classpath");
InputSource source = new InputSource(dtdStream);
source.setPublicId(publicId);
source.setSystemId(systemId);
return source;
}
}
else if (systemId.startsWith(FILE))
{
InputStream childStream = loader.getResourceAsStream(systemId.substring(FILE.length())) ;
if (childStream == null)
return null ;
else {
log.debug("found " + systemId + " from classLoader");
InputSource source = new InputSource(childStream);
source.setPublicId(publicId);
source.setSystemId(systemId);
return source;
}
} else
return null ;
} else {
// use the default behaviour
return null;
}
}
}
Impacted code in XMLHelper :
Added constructor
public static SAXReader createSAXReader(String file, ClassLoader cl) {
SAXReader reader = new SAXReader();
reader.setEntityResolver(new DTDEntityResolver(cl));
reader.setErrorHandler( new ErrorLogger(file) );
reader.setMergeAdjacentText(true);
reader.setValidation(true);
return reader;
}
Modified DTD_RESOLVER instanciation :
private static final EntityResolver DTD_RESOLVER = new DTDEntityResolver(XMLHelper.class.getClassLoader());
Configuration.java :
Modified some addXXX methods to use the new feature of XMLHelper :
public Configuration addInputStream(InputStream xmlInputStream, ClassLoader src) throws MappingException {
try {
add( XMLHelper.createSAXReader("XML InputStream", src).read( new InputSource(xmlInputStream) ) );
return this;
}
catch (MappingException me) {
throw me;
}
catch (Exception e) {
log.error("Could not configure datastore from input stream", e);
throw new MappingException(e);
}
}
/**
* Read mappings from an application resource
* @param path a resource
* @param classLoader a <tt>ClassLoader</tt> to use
*/
public Configuration addResource(String path, ClassLoader classLoader) throws MappingException {
log.info("Mapping resource: " + path);
InputStream rsrc = classLoader.getResourceAsStream(path);
if (rsrc==null) throw new MappingException("Resource: " + path + " not found");
return addInputStream(rsrc, classLoader);
}
/**
* Read a mapping from an application resource, using a convention.
* The class <tt>foo.bar.Foo</tt> is mapped by the file <tt>foo/bar/Foo.hbm.xml</tt>.
* @param persistentClass the mapped class
*/
public Configuration addClass(Class persistentClass) throws MappingException {
String fileName = persistentClass.getName().replace(StringHelper.DOT,'/') + ".hbm.xml";
log.info("Mapping resource: " + fileName);
ClassLoader cl = persistentClass.getClassLoader() ;
InputStream rsrc = cl.getResourceAsStream(fileName);
if (rsrc==null) throw new MappingException("Resource: " + fileName + " not found");
return addInputStream(rsrc, cl);
}
/**
* Read all mappings from a jar file
* @param resource an application resource (a jar file)
*/
public Configuration addJar(String resource) throws MappingException {
log.info("Searching for mapping documents in jar: " + resource);
final JarFile jarFile;
final ClassLoader cl ;
try {
cl = Thread.currentThread().getContextClassLoader() ;
jarFile = new JarFile(
cl.getResource(resource).getFile()
);
}
catch (IOException ioe) {
log.error("Could not configure datastore from jar", ioe);
throw new MappingException(ioe);
}
if (jarFile==null) throw new MappingException("Resource: " + resource + " not found");
Enumeration enum = jarFile.entries();
while( enum.hasMoreElements() ) {
ZipEntry z = (ZipEntry) enum.nextElement();
if( z.getName().endsWith(".hbm.xml") ) {
log.info( "Found mapping documents in jar: " + z.getName() );
try {
addInputStream( jarFile.getInputStream(z), cl );
}
catch (MappingException me) {
throw me;
}
catch (Exception e) {
log.error("Could not configure datastore from jar", e);
throw new MappingException(e);
}
}
}
return this;
}
---------------------------------------------------------------------
JIRA INFORMATION:
This message is automatically generated by JIRA.
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
If you want more information on JIRA, or have a bug to report see:
http://www.atlassian.com/software/jira
|