By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,120 Members | 1,799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,120 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.xerce s.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>(NullPointerE xception.java:60)
at org.apache.xml.dtm.ref.sax2dtm.SAX2DTM.endElement( SAX2DTM.java:1886)
at util.pdf.EasyGenerationContentHandlerProxy.endElem ent(EasyGenerationContentHandlerProxy.java:208)
at util.pdf.EasyGenerationContentHandlerProxy.endElem ent(EasyGenerationContentHandlerProxy.java:123)
at util.pdf.AusgangspostXMLReader.generateFor(Ausgang spostXMLReader.java:159)
at util.pdf.AusgangspostXMLReader.parse(AusgangspostX MLReader.java:57)
at util.pdf.AusgangspostXMLReader.parse(AusgangspostX MLReader.java:33)
at org.apache.xml.dtm.ref.DTMManagerDefault.getDTM(DT MManagerDefault.java:493)
at org.apache.xalan.transformer.TransformerImpl.trans form(TransformerImpl.java:658)
at org.apache.xalan.transformer.TransformerImpl.trans form(TransformerImpl.java:1129)
at org.apache.xalan.transformer.TransformerImpl.trans form(TransformerImpl.java:1107)
at util.pdf.PDFHelper.convertObj2PDF(PDFHelper.java:1 11)

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
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: 1
Hi

did u find any solution for this problem

even m facing same situation

Please advice what to do

Thanks in advance
Apr 11 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.