Problem getting external DTD file running JDOM SAXBuilder

K

Kent Lichty

Why is it that the simplest problems are the most vexing? This should
be a simple problem to solve, but I am ready to tear out my hair over
it.

This is the age old problem of where to stick the external DTD file
when parsing an XML document. My boss does not like the "standard"
approach of sticking the DTD file out on the web because some clients
may not have web access, or there are problems going through
firewalls, etc. So we need to keep the DTD file locally; but where to
stick it? I come from an AS/400 background where this simple issue of
finding a file is never a problem, but in the Java/PC programming
environment it is unbelievably complicated!

The problem is that the system does not look for the DTD file in the
classpath (in which case it would be no problem) but in the file
system path. Well, that file system path will vary depending where our
application is running, and I cant just hardcode a path in the xml
file to indicate the location of the DTD file. This approach of course
works but it is unacceptable.

So how do the rest of you handle this problem? I have tried using the
EntityResolver class which SHOULD WORK, but my parser seems to simply
ignore it. Maybe some of you can tell me what I am doing wrong here. I
am using the JDOM SAXBuilder class; maybe it just doesn't handle that
method correctly, or at least how I expect it to.

Here is my resolveEntity method:

public InputSource resolveEntity(String publicId, String systemId)
{
InputSource inputSource = null;
String hibernateDTDFilePath =
SystemDatabase.getClassesFolderPath()
+ System.getProperty("file.separator")
+ "hibernate-mapping-2.0.dtd";
try
{

FileReader fileReader = new FileReader(hibernateDTDFilePath);
inputSource = new InputSource(fileReader);
}
catch (FileNotFoundException fnfe)
{
log.error("FileNotFoundException occurred. " + fnfe.getMessage());
fnfe.printStackTrace();
}

return inputSource;
}


The irony here is that the parser is clearly calling this method (I
verified in debug) and the "SystemId" parameter that is being passed
in is the FULL PATH of the DTD file!! Yet it still isn't found! The
inputSource I am returning is exactly correct, but my SAXBuilder
parser simply ignores it, and still attempts to find the DTD file in
the system file path and of course cannot find it.

Here is the DOCTYPE section of my XML document:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
SYSTEM "hibernate-mapping-2.0.dtd">
<!-- "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
-->

I have tried many different combinations (removing the PUBLIC, etc);
this is just the latest variant.


Here is where/how I am registering my EntityResolver with my
SAXBuilder class:


SAXBuilder builder = new SAXBuilder(false);
builder.setValidation(false);

HibernateDTDEntityResolver entityResolver = new
HibernateDTDEntityResolver();
builder.setEntityResolver(entityResolver);

The bottom line is that the SAXBuilder just seems to ignore this
EntityResolver stuff. My assumption is that it would replace the
reference to hibernate-mapping-2.0.dtd and replace it with the
returned inputSource. But, this isn't happening and it just complains
that it cant find the file.

I would really appreciate any help with this; it is driving me crazy!

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top