You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by Armin Pfarr <ap...@vipsurf.de> on 2000/01/26 15:51:55 UTC
Entity Manager
I'd like to associate an Entity-manager with Xerces and fail to get it
right. The classes are quite straightforward
public em extends EntityManager {
public InputStream(String pubid, String sysid) {
if(pubid.equals("foo"))
return new InputStream("file:/c:/dtd/dtd1.dtd");
else
return null;
}
}
Then I attach this manager to a parser using
parser.setEntityManager(new em());
The parser does not use the manager as expected. Any idea?
Armin
AW: Entity Manager
Posted by Armin Pfarr <ap...@vipsurf.de>.
Sorry for posting this invalid fragment. I have a complete "program" that
looks like
package examples;
import java.io.*;
import org.xml.sax.*;
import org.apache.xerces.parsers.*;
class Resolver implements EntityResolver {
public InputSource resolveEntity(String pubId, String sysId)
throws IOException, SAXException {
if(pubId != null) {
System.out.println("resolving entity "+ pubId+ " SYSTEM "+sysId);
InputSource is = new InputSource("file:///c:/dtd/test.dtd");
return is;
} else
return null;
}
}
public class MyParser extends SAXParser {
public MyParser() {
setEntityResolver(new Resolver());
}
public static void main(String args[]) {
MyParser parser = new MyParser();
try {
System.out.println("start parse");
parser.parse(args[0]);
System.out.println("end parse");
} catch(org.xml.sax.SAXParseException e) {
System.out.println("SaxParseException: "+e.getMessage());
System.out.println(getLocationString(e));
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace(System.out);
}
}
private static String getLocationString(SAXParseException ex) {
StringBuffer str = new StringBuffer();
String systemId = ex.getSystemId();
if (systemId != null) {
int index = systemId.lastIndexOf('/');
if (index != -1)
systemId = systemId.substring(index + 1);
str.append(systemId);
}
str.append(':');
str.append(ex.getLineNumber());
str.append(':');
str.append(ex.getColumnNumber());
return str.toString();
}
}
When running this program against an XML-file
<?xml version="1.0" ?>
<!DOCTYPE test PUBLIC "-//Private//Test DTD//EN" "doctypes/test.dtd">
<test>
<title>hello</title>
</test>
I get the following output:
"
start parse
resolving entity -//Private//Test DTD//EN doctypes/test.dtd
SAXParseException: File "doctypes/test.dtd" not found.
test.xml:-1:-1
"
The entity resolver obviously gets called but still the parser tries to use
the System Identifier of the instance that points to a non-existing file
(that was intended!)
Armin
Re: Entity Manager
Posted by Andy Clark <an...@apache.org>.
Armin,
There are a number of problems with your code. The immediate ones
that I see are:
1) It's an EntityResolver, not an EntityManager and it's an
interface so you would "implement" it, not "extend" it.
2) The method is "resolveEntity" and it returns an "InputSource".
I think that you mean to write "InputSource" everywhere that
you write "InputStream".
3) The method also should declare that it throws IOException and
SAXException.
4) "file:/c:/dtd/dtd1.dtd" is the wrong way to use the "file"
protocol. Try "file:///c:/dtd/dtd1.dtd" instead.
Make the appropriate changes and let me know if it works for you.
--
Andy Clark * IBM, JTC - Silicon Valley * andyc@apache.org