Hi,

Thanks for your answer. I will have a closer look on it tomorrow moring. I will 
ask again, if there are any questions still open.

Thanks and best regards,
Andreas Grund

-----Ursprüngliche Nachricht-----
Von: Jeremias Maerki [mailto:[EMAIL PROTECTED] 
Gesendet: Dienstag, 29. November 2005 15:57
An: [email protected]
Betreff: Re: FOP and EntityResolver

I have the impression that it's not possible to set an EntityResolver (or an 
URIResolver) so that the XML parser can use it to resolve DTDs in the source 
XML file. So this means that you have to take control of the XML parser 
yourself. That modifies the example that comes with FOP a bit.
You have to use a TransformerHandler instead of a Transformer. The XSL 
transformation becomes a passive operation with the XML parser initiating the 
conversion pipeline. Before it was the
Transformer.transform() method that started it all. Please don't be confused 
with the example below. It is written against FOP 0.90alpha1 but the XML Parser 
and XSLT part is the same for you. Instead of
fop.getDefaultHandler() it's simply driver.getContentHandler().

For performance reasons you might want to call
pFactory.setValidating(false) so the XML parser doesn't validate the XML each 
time, except, of course, if you do want that to happen.

So, you see that in the end you don't set the EntityResolver with FOP but with 
the XML parser which is responsible for the XML validation.

I hope that helps.


    public void convertXML2PDF(InputSource xml, Source xslt, File pdf) 
            throws IOException, 
                ParserConfigurationException, 
                SAXException, 
                TransformerConfigurationException {
        
        OutputStream out = null;
        
        try {
            SAXParserFactory pFactory = SAXParserFactory.newInstance();
            pFactory.setValidating(true);
            pFactory.setNamespaceAware(true);
            SAXParser parser = pFactory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            reader.setEntityResolver(new MyEntityResolver());
            
            // Construct fop with desired output format
            Fop fop = new Fop(MimeConstants.MIME_PDF);
    
            // Setup output stream.  Note: Using BufferedOutputStream
            // for performance reasons (helpful with FileOutputStreams).
            out = new FileOutputStream(pdf);
            out = new BufferedOutputStream(out);
            fop.setOutputStream(out);

            // Setup JAXP using identity transformer
            SAXTransformerFactory factory = 
(SAXTransformerFactory)SAXTransformerFactory.newInstance();
            TransformerHandler handler = factory.newTransformerHandler(xslt);
            
            // Resulting SAX events (the generated FO) must be piped through to 
FOP
            Result res = new SAXResult(fop.getDefaultHandler());
            handler.setResult(res);
            //handler.setResult(new StreamResult(new File("D:/out.fo")));
            handler.getTransformer().setErrorListener(new 
DefaultErrorListener());
            
            reader.setContentHandler(handler);
            
            // Start XSLT transformation and FOP processing by invoking the XML 
parser
            reader.parse(xml);

        } finally {
            out.close();
        }
    }


On 29.11.2005 14:41:25 Andreas Grund wrote:
> Hi,
>  
> I have a short problem with FOP:
> I want to publish an xml document to pdf using an xsl-stylesheet and FOP.
> The doctype-declaration of my xml file contains a DTD used for 
> validation. I wrote a method as it is shown in the examples. I use a 
> javax.xml.transform.Transformer and a 
> javax.xml.transform.sax.SAXResult to publish my content. Every time I 
> call the method transformer.transform, I get a FileNotFoundException 
> with the system id of the given DTD. Is it possible to add a 
> EntityResolver to FOP, so that this resolver resolves the DTD correctly?



Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to