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

Quick Search    Search Deep

Source code: com/rohanclan/ashpool/core/BasicXSLEngine.java


1   /*
2    * Ashpool - XML Database
3    * Copyright (C) 2003 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   */
19  
20  package com.rohanclan.ashpool.core;
21  
22  import java.io.FileOutputStream;
23  import java.io.InputStream;
24  
25  /**
26   * A basic xslt transforming engine. Just applies an XSLT style sheet to an 
27   * xml document.
28   *
29   * @author Rob Rohan
30   */
31  
32  /** creates a new instance of treebeard */
33  public class BasicXSLEngine {
34       
35      /** which transformer to use: saxon by default */
36      public static String XSLTFactory = "com.icl.saxon.TransformerFactoryImpl";
37      public static String SAXFactory  = "com.icl.saxon.aelfred.SAXParserFactoryImpl";
38      //org.apache.xerces.parsers.SAXParser
39      
40      /** the params to pass to the style sheet */
41      public java.util.Vector params;
42      
43      /** Creates a new instance of BasicXSLEngine */
44      public BasicXSLEngine() {
45          doFactoryReset();
46          params = new java.util.Vector();
47      }  
48      
49      /** change the xslt factory (call doFactoryRest) */
50      public void setXSLFactory(String factory){
51          XSLTFactory = factory;
52      }
53      
54      /** change the sax factory (call doFactoryRest) */
55      public void setSAXFactory(String factory){
56          SAXFactory = factory;
57      }
58      
59      /** set a single param */
60      public void setParam(String name, String value){
61          params.add(name);
62          params.add(value);
63      }
64      
65      /** set fresh params */
66      public void clearParams(){
67          params.removeAllElements();
68      }
69      
70      /** set a bunch of params */
71      public void setParams(java.util.Vector inparams){
72          params = inparams;
73      }
74      
75      public void doFactoryReset(){
76         //set the factory to the specified one - this blows up some apps that assume
77          //a validating parser, so only use the one i want if there isn't something
78          //already.
79          if(System.getProperty("javax.xml.transform.TransformerFactory") == null
80              || System.getProperty("javax.xml.parsers.SAXParserFactory") == null){
81              try{
82                  System.setProperty("javax.xml.transform.TransformerFactory",  XSLTFactory);
83                  System.setProperty("javax.xml.parsers.SAXParserFactory", SAXFactory);
84              }catch(Exception e){
85                  System.err.println("Can't change System parser and transformer, bummer." + e.toString());
86                  e.printStackTrace(System.err);
87              }
88          }
89      }
90      
91      /** Performs an xslt transformation
92       * @param xslin a stream containing the xsl style sheet
93       * @param xmlin a stream containing the xml
94       * @param output the stream to write the results to
95       */    
96      public long transform(InputStream xmlin, InputStream xslin, java.io.OutputStream output) 
97          throws Exception {
98         long start = new java.util.Date().getTime();
99         
100        javax.xml.transform.stream.StreamResult strResult = null;
101        String media = null;
102       
103        //get an instance and make a result object
104        //javax.xml.transform.TransformerFactory tfactory 
105        //     = javax.xml.transform.TransformerFactory.newInstance();
106        javax.xml.transform.TransformerFactory tfactory 
107             = com.icl.saxon.TransformerFactoryImpl.newInstance();
108        
109        
110        strResult = new javax.xml.transform.stream.StreamResult(output);
111        
112        
113        //javax.xml.transform.sax.SAXTransformerFactory stf 
114        //     = (javax.xml.transform.sax.SAXTransformerFactory) tfactory;
115        javax.xml.transform.sax.SAXTransformerFactory stf 
116             = (javax.xml.transform.sax.SAXTransformerFactory) tfactory;
117        
118        
119        javax.xml.transform.Templates stylesheet = tfactory.newTemplates(
120             new javax.xml.transform.stream.StreamSource(xslin)
121        );
122        
123        javax.xml.transform.Transformer transformer = stylesheet.newTransformer();
124             
125        //set params in the style sheets
126        for(int i=0; i < params.size()-1; i++)
127             transformer.setParameter(
128                 (String)params.elementAt(i), (String)params.elementAt(i+1)
129              );
130             
131         //do the transformation
132        javax.xml.transform.stream.StreamSource input = 
133             new javax.xml.transform.stream.StreamSource(xmlin);
134        
135        transformer.transform(input, strResult);
136        long finish = new java.util.Date().getTime();
137        
138        return finish - start;
139     }
140 }