FOP, XSLT: problems with SAX2DTM Parsing trying to convert to XSL-FO in Java

J

Jens Mueller

Hi there,

this is a Java-XML Question, so I am not sure whether this is the
right place, haven't found anything better ....

I try to convert a Java object to XML via SAX and let the FOP
Transformer convert that via XSLT to valid XSL-FO. So I define a
SAXReader which fires the SAX Events for the Java Object. This works
fine and the Transformation to PDF is ok.
However, I have one object which contains an XHTML String and the tags
inside that string shall be passed to the XSLT without changing them.
I have a XSL which would handle them properly and convert them to
valid XSL-FO, I just don't know how to get them thru SAX ...
My understanding is that I have to just make them fire the correct SAX
events as well. My Idea was to use a SAX Parser for that part, let it
handle the XHTML text and then continue with my other object's
attributes:

code sample (will not compile, ok ;-):

class MyObjectReader{
public void parse(MyObject obj) throws SAXException {

// this works fine
handler.startDocument();
handler.startElement("address");
handler.element("city", obj.getCity());
handler.element("street", obj.getStreet());
handler.endElement("address");
// now try to let a vanilla SAX parser do the xhtml
XMLReader reader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
reader.setContentHandler(handler);
handler.startElement("xhml");
reader.parse(new InputSource(new StringReader(obj.getXHTMLString())));
handler.endElement("xhml");
handler.endDocument();

}
}

The whole thing gets used inside the FOP Transformation:

class PDFHelper{
public void convertObj2PDF(){

Driver driver = new Driver();
//Setup Renderer (output format)
driver.setRenderer(Driver.RENDER_PDF);
TransformerFactory factory = TransformerFactory.newInstance();
// create transformer with the appropriate xsl
Transformer transformer = factory.newTransformer(new StreamSource(new
File(indir, xslFileName)));

// Setup input for XSLT transformation
// this returns a source which uses the above Reader
Source src = obj.getSource();
// Resulting SAX events (the generated FO) must be piped through to
FOP
Result res = new SAXResult(driver.getContentHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
}
}

now what happens is that I get a NullPointerException when, inside the
XSL Transformation, after the "embedded" Parser has finished, the
reader tries to end the "xhtml" element (line:
handler.endElement("xhml");).
The exception looks like this:

[12.04.05 17:07:21:531 CEST] 5dd35dd3 SystemOut O [12.04.05
17:07:21,406 CEST] util.pdf.PDFHelper - convertObj2PDF:
obj=vo.AusgangspostVO@754d754d : Error in Transformer: null
java.lang.NullPointerException
at java.lang.Throwable.<init>(Throwable.java)
at java.lang.Throwable.<init>(Throwable.java)
at java.lang.NullPointerException.<init>(NullPointerException.java:60)
at org.apache.xml.dtm.ref.sax2dtm.SAX2DTM.endElement(SAX2DTM.java:1886)
at util.pdf.EasyGenerationContentHandlerProxy.endElement(EasyGenerationContentHandlerProxy.java:208)
at util.pdf.EasyGenerationContentHandlerProxy.endElement(EasyGenerationContentHandlerProxy.java:123)
at util.pdf.AusgangspostXMLReader.generateFor(AusgangspostXMLReader.java:159)
at util.pdf.AusgangspostXMLReader.parse(AusgangspostXMLReader.java:57)
at util.pdf.AusgangspostXMLReader.parse(AusgangspostXMLReader.java:33)
at org.apache.xml.dtm.ref.DTMManagerDefault.getDTM(DTMManagerDefault.java:493)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:658)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1129)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1107)
at util.pdf.PDFHelper.convertObj2PDF(PDFHelper.java:111)

I guess this might be bcause the SAX2DTM class used by the XSLT is not
compatible with my org.apache.xerces.parsers.SAXParser. Is this the
right direction? Can I fix the problem by using another parser?
Am I getting something terribly wrong?

I am using FOP 0.20.5 with xalan 2.4.1 and xerces 2.2.1
The app is running with jdk 1.4 and I cannot switch to newer versions
of xerces or xalan due to problems with the appserver.

Any hints are highly appreciated (even to a better suited newsgroup
....)

Regards

Jens
 

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,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top