Re: Using a org.w3c.dom.Document as indata to the Driver

The following example will do the trick.

Rgds///
	Magnus Sjöberg

// Small example
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(FoFilename);
DOMSource fosource;
Serializer serializer =
SerializerFactory.getSerializer(OutputProperties.getDefaultMethodProperties("xml"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
CharArrayWriter caw = new CharArrayWriter();
InputSource is;

// Add changes to doc
...

// Get source tree
fosource = new DOMSource(doc);

// Create input source
serializer.setWriter(caw);
serializer.asDOMSerializer().serialize(fosource.getNode());
is = new InputSource(new StringReader(caw.toString()));

Driver driver2 = new Driver(is, bos);
driver2.setRenderer(Driver.RENDER_PDF);
driver2.run();
	    
// Result is in 'bos'

Erik Melkersson wrote:
> 
> Hi!
> 
> I would like to read a fo file to a DOM tree on startup.
> Do some changes to it on runtime and then produce pdf and ps from it.
> 
> I am using version 0.20.1 of fop.
> 
> My try:
> 
> ---
> // Fetch xml -> dom tree
> DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
> DocumentBuilder docBuilder = dbf.newDocumentBuilder();
> Document doc = docBuilder.parse(FOfilename);
> 
> //... some changes to the DOM tree shall be done here (but I aint doing
> any changes yet
> 
> Driver driver = new Driver();
> driver.setRenderer(Driver.RENDER_PDF);
> driver.setOutputStream(new FileOutputStream());
> 
> driver.render(doc); // -> returns fault, se below
> 
> //driver.setInputSource(new DocumentInputSource(doc));
> // -> gives the same fault
> 
> /*
>   //This works (but it doesn't use the preparsed dom tree
>   driver.setInputSource(new InputSource(aPDFfilename));
>   driver.run();
> */
> ---
> 
> The error I get is:
> java.lang.NullPointerException
>         at java.util.Hashtable.get(Hashtable.java:320)
>         at org.apache.fop.fo.FOTreeBuilder.startElement(FOTreeBuilder.java:191)
>         at org.apache.fop.tools.DocumentReader.parse(DocumentReader.java:444)
>         at org.apache.fop.apps.Driver.render(Driver.java:449)
>         at Avi.main(Avi.java:61) (my file)
> 
> I can only find old examples where a method Driver.format() is used...
> and they does not use dom trees as indata.
> 
> Thankful for any help!
> 
> 
> __________________________
> Erik Melkersson, UNIT
> melker@unit.liu.se, 013-285794
> http://www.unit.liu.se/

Received on Thursday, 4 October 2001 10:52:55 UTC