Using xerces

1,523 views
Skip to first unread message

Russell Valentine

unread,
Mar 3, 2011, 11:46:30 AM3/3/11
to Flying Saucer Users
I was wondering if anyone knows the solution to the problem of
including the latest xerces in their classpath and using flyingsaucer
(xercesImpl.jar and all).

Everything works fine without having the xerces jar's in the classpath
but when I add them I get an exception:
org.w3c.dom.DOMException: NOT_FOUND_ERR. I tried having flyingsaucer
parse a very simple xhtml. It seems to die if there is ever an "id"
attribute in a tag. If no "id" then it seems to do okay, at least in
the simple document.

I've tried setting the sax parser in the flying saucer config to use
piccolo instead of the xerces sax parser(still have xerces in
classpath) but I have a similar problem. I have also tried parsing a
xhtml file with the same xerces jars outside flying saucer and it does
alright even with the id attribute.

I'm thinking there is some small thing I'm missing to get this to
work, and hoping someone on the list ran into a similar issue and
could help me out.

Thank you.


Rusell Valentine

Exception:

org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource
(using TRaX transformer). org.w3c.dom.DOMException: NOT_FOUND_ERR: An
attempt is made to reference a node in a context where it does not
exist.
at org.xhtmlrenderer.resource.XMLResource
$XMLResourceBuilder.createXMLResource(XMLResource.java:191)
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:
75)
at
org.xhtmlrenderer.swing.BasicPanel.setDocumentFromString(BasicPanel.java:
371)
...

Caused by: javax.xml.transform.TransformerException:
org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to
reference a node in a context where it does not exist.
at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:
716)
at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:
313)
at org.xhtmlrenderer.resource.XMLResource
$XMLResourceBuilder.createXMLResource(XMLResource.java:189)
... 13 more
Caused by: org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is
made to reference a node in a context where it does not exist.
at org.apache.xerces.dom.ElementImpl.setIdAttributeNS(Unknown
Source)
at
com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM.startElement(SAX2DOM.java:
211)



Simple java example:


import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.XMLReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.io.FileInputStream;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.InputSource;
import org.xml.sax.EntityResolver;
import org.xml.sax.SAXException;

import org.w3c.dom.Document;


public class SAXTest
{


public static void main(String[] args)
{
try {
SAXParserFactory factory =
SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader =
saxParser.getXMLReader();
xmlReader.setFeature("http://xml.org/sax/
features/validation", false);
xmlReader.setEntityResolver(new MyResolver());


Source input = new SAXSource(xmlReader, new
InputSource(new FileInputStream("simple.html")));
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(false);
DOMResult output = new
DOMResult(dbf.newDocumentBuilder().newDocument());
TransformerFactory xformFactory =
TransformerFactory.newInstance();
Transformer idTransform =
xformFactory.newTransformer();
idTransform.transform(input, output);
System.out.println(output.getNode());
} catch (Exception e) {
e.printStackTrace();
}


}

public static class MyResolver implements EntityResolver {
public InputSource resolveEntity(String publicId,
String systemID)
throws SAXException {
System.out.println("publicID =
"+publicId+", systemID="+systemID);
try {
if(publicId.equals("-//W3C//
DTD XHTML 1.0 Strict//EN")) {
return new
InputSource(new FileInputStream("xhtml1-strict.dtd"));
} else if(publicId.equals("-//
W3C//ENTITIES Latin 1 for XHTML//EN")) {
return new
InputSource(new FileInputStream("xhtml-lat1.ent"));
} else if(publicId.equals("-//
W3C//ENTITIES Symbols for XHTML//EN")) {
return new
InputSource(new FileInputStream("xhtml-symbol.ent"));
} else if(publicId.equals("-//
W3C//ENTITIES Special for XHTML//EN")) {
return new
InputSource(new FileInputStream("xhtml-special.ent"));
}
} catch (Exception e ){
e.printStackTrace();
}
return null;
}
}

}



simple.html:

<?xml version="1.0" encoding="UTF-8"?
>
<!DOCTYPE
html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-
strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<link rel="stylesheet" type="text/css" href="main.css" title="Style"
media="screen"/>
</
head>
<body
class="blue">
</
body>
<div
class="header">
<img id="logo" src="toefljr_logo2.png"/
>
</
div>
</html>

Russell Valentine

unread,
Mar 7, 2011, 12:37:11 PM3/7/11
to Flying Saucer Users
I modified my sample class to be more close to what flying saucer
does. More specifically I added these two lines:

xmlReader.setFeature("http://xml.org/sax/features/validation", false);
xmlReader.setFeature("http://xml.org/sax/features/namespaces", true);

This finally reproduces the issue. It is the enabling the namespaces
issue when having xerces in the classpath that causes it to fail. Why
I do not know, but at least I have a solution to the problem by
turning it off.

namespaces seems to be on by default if "xr.load.configure-features=false"


Russell Valentine

Patrick Wright

unread,
Mar 7, 2011, 3:05:26 PM3/7/11
to flying-sa...@googlegroups.com
Thanks for the info, Russell, it's useful to have it documented. I
hope this helps someone in the future.

On Mon, Mar 7, 2011 at 6:37 PM, Russell Valentine

Reply all
Reply to author
Forward
0 new messages