Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/robrohan/treebeard/TreeTransform.java


1   /*
2    * Treebeard: an xml xslt transfomer
3    * Copyright (C) 2002 Rob Rohan
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the
6    * Free Software Foundation; either version 2 of the License, or (at your
7    * option) any later version.
8    * 
9    * This program is distributed in the hope that it will be useful, but 
10   * WITHOUT ANY WARRANTY; without even the implied warranty of 
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
12   * General Public License for more details.
13   * 
14   * You should have received a copy of the GNU General Public License along 
15   * with this program; if not, write to the Free Software Foundation, Inc.,
16   * 675 Mass Ave, Cambridge, MA 02139, USA.
17   *
18   * Email: me@robrohan.com
19   * 
20   * Transformer.java
21   *
22   * Created on July 18, 2002, 9:29 PM
23   */
24  
25  package com.robrohan.treebeard;
26  import java.util.Date;
27  import java.util.Vector;
28  import javax.xml.transform.stream.StreamSource;
29   
30  /**
31   * The Workhorse of treebeard. This is where the xslt is applied to an xml
32   * document
33   * @author  rob
34   */
35  public class TreeTransform {
36      
37      /** Do a normal transform */    
38      public static final int NORMAL_TRANSFORM=1;
39      /** do a fop pdf transfrom (xml+fo_xslt=pdf file) */    
40      public static final int FOP_PDF=2;
41      /** do a fop pdf transfrom with preview (xml+fo_xslt=pdf preview) */    
42      public static final int FOP_PREVIEW=3;
43      
44      /** handle to the selected XSLTFactory (set in settings) */    
45      public String XSLTFactory = "org.apache.xalan.processor.TransformerFactoryImpl";
46  
47      /** The params to send to the xml+xslt process (the xslt params defined with the
48       * <xsl:param tag
49       */    
50      public Vector xslParams = new Vector();
51  
52      /** Creates a new instance of Transformer */
53      public TreeTransform() {;}
54      
55      /** Does the actual transform xml+xslt (non FOP)
56       * @param xmlin the xml input stream
57       * @param xslin the xslt input stream
58       * @param output the output stream to send the results to
59       * @return the amount of time the transfrom took in milliseconds
60       * @throws Exception io, transform, etc
61       */    
62      public long transform(java.io.InputStream xmlin, java.io.InputStream xslin, java.io.OutputStream output) throws Exception {
63         javax.xml.transform.stream.StreamResult strResult = null;
64         Vector params = new Vector();
65         String media = null;
66         
67         //set the factory to the specified one
68         System.setProperty("javax.xml.transform.TransformerFactory",  XSLTFactory);
69                
70         //get an instance and make a result object
71         javax.xml.transform.TransformerFactory tfactory = javax.xml.transform.TransformerFactory.newInstance();
72         
73         strResult = new javax.xml.transform.stream.StreamResult(output);
74         
75         //oof I hope this works...
76         javax.xml.transform.sax.SAXTransformerFactory stf = (javax.xml.transform.sax.SAXTransformerFactory) tfactory;
77             
78         javax.xml.transform.Templates stylesheet = tfactory.newTemplates(
79              new StreamSource(xslin)
80         );
81         
82         javax.xml.transform.Transformer transformer = stylesheet.newTransformer();
83         
84         //set params
85         for(int i=0; i < params.size()-1; i++)
86              transformer.setParameter((String) params.elementAt(i), (String)params.elementAt(i+1));
87              
88          //do the transformation
89          StreamSource input = new StreamSource(xmlin);
90          
91          long start = new Date().getTime();
92          transformer.transform(input, strResult);
93          long finish = new Date().getTime();
94          
95          //return the amount of time it took
96          return finish - start;
97      } 
98      
99      /** Does the actual transform xml+fop_xslt
100      * @param xmlin the xml input stream
101      * @param xslin the xslt input stream
102      * @param output the output stream to send the results to
103      * @return the amount of time the transfrom took in milliseconds
104      * @throws Exception io, transform, etc
105      * @param type the type of transfrom (set in "the send to" dialog).
106      */    
107     public long FOPtransform(
108         java.io.InputStream xmlin, 
109         java.io.InputStream xslin, 
110         java.io.OutputStream output, byte type) throws Exception{
111      
112         //make a new FOP driver
113         org.apache.fop.apps.Driver driver = new org.apache.fop.apps.Driver();
114         //if they choose preview
115         org.apache.fop.viewer.PreviewDialog pd = null;
116         org.apache.avalon.framework.logger.ConsoleLogger csl = new org.apache.avalon.framework.logger.ConsoleLogger();
117                 
118         //make an AWTRenderer and get the resource
119         org.apache.fop.render.awt.AWTRenderer awtr = new org.apache.fop.render.awt.AWTRenderer(
120                 new org.apache.fop.viewer.SecureResourceBundle(
121                     getClass().getResourceAsStream("org.apache.fop.viewer.resources.resources.en")
122                     )
123                 );
124         
125         //if they choose to preview FOP setup the dialog box and driver
126         if(type == com.robrohan.fangorn.Ent.FOP_PREVIEW){
127             //set logger
128             awtr.setLogger(csl);
129             
130             //make a new preveiw
131             pd = new org.apache.fop.viewer.PreviewDialog(
132                 awtr, 
133                 new org.apache.fop.viewer.SecureResourceBundle(
134                     getClass().getResourceAsStream("org.apache.fop.viewer.resources.resources.en")
135                 )
136             );
137             //set the component
138             awtr.setProgressListener(pd);
139             awtr.setComponent(pd);
140             
141             //set the driver to the AWT renderer
142             driver.setRenderer(awtr);
143         
144         //or if they want to make a PDF file set the driver
145         }else if (type == com.robrohan.fangorn.Ent.PDF_SAVE){
146             driver.setRenderer(org.apache.fop.apps.Driver.RENDER_PDF);
147         }
148         
149         driver.setLogger(csl);
150         
151         //temp files for FOP transforming
152         java.io.FileOutputStream fos = new java.io.FileOutputStream("tempXML");
153         java.io.FileOutputStream fos2 = new java.io.FileOutputStream("tempXSL");
154         
155         //dump the passed streams to the temp files (bit of a hack but I can't
156         //use streams directly.... yet.
157         int t;
158         while((t = xmlin.read()) != -1){
159             fos.write(t);
160         }
161         fos.flush();
162         fos.close();
163         
164         while((t = xslin.read()) != -1){
165             fos2.write(t);
166         }
167         fos2.flush();
168         fos2.close();
169         
170         //use the xml and xsl files
171         org.apache.fop.apps.InputHandler inputHandler = 
172             new org.apache.fop.apps.XSLTInputHandler(
173                 new java.io.File("tempXML"), 
174                 new java.io.File("tempXSL")
175             );
176         org.xml.sax.XMLReader parser = inputHandler.getParser();
177         
178         //file writting handled by caller
179         //save pdf way
180         if(type == com.robrohan.fangorn.Ent.PDF_SAVE){ 
181              driver.setOutputStream(output);
182         }
183         
184         driver.render(parser, inputHandler.getInputSource());
185        
186         //show the preview dialog AWT preview way
187         if(type == com.robrohan.fangorn.Ent.FOP_PREVIEW){    
188             pd.show();
189             pd.showPage();
190         }
191         
192         //remove the tempfiles
193         new java.io.File("tempXML").delete();
194         new java.io.File("tempXSL").delete();
195         
196         return 0;
197     }
198 }