View Javadoc

1   /*
2    * Copyright 2008 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  package nl.hippo.portal.cms.scripting.xslt;
17  
18  import java.io.InputStream;
19  import java.io.StringWriter;
20  import java.io.Writer;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javax.xml.parsers.SAXParserFactory;
25  import javax.xml.transform.Transformer;
26  import javax.xml.transform.TransformerException;
27  import javax.xml.transform.TransformerFactory;
28  import javax.xml.transform.sax.SAXTransformerFactory;
29  import javax.xml.transform.stream.StreamResult;
30  import javax.xml.transform.stream.StreamSource;
31  
32  /***
33   * @author <a href="mailto:d.dam@hippo.nl">Dennis Dam</a>
34   *
35   * @version $Id$
36   */
37  public class XSLTTransformerServiceImpl implements XSLTTransformerService
38  {
39      private SAXTransformerFactory transformerFactory;
40  
41      private SAXParserFactory saxFactory;
42  
43      public XSLTTransformerServiceImpl()
44      {
45          init();
46      }
47  
48      public void init()
49      {
50          if (saxFactory == null)
51          {
52              System.setProperty("javax.xml.transform.TransformerFactory",
53                      "org.apache.xalan.processor.TransformerFactoryImpl");
54              System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
55              System.setProperty("org.xml.sax.driver", "org.apache.xerces.parsers.SAXParser");
56              transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
57              saxFactory = SAXParserFactory.newInstance();
58              saxFactory.setValidating(false);
59          }
60      }
61  
62      public String transform(InputStream in, InputStream xslt) throws TransformerException
63      {
64          return transform(in, xslt, (Map) null);
65      }
66  
67      public String transform(InputStream in, InputStream xslt, Map parameters) throws TransformerException
68      {
69          StringWriter writer = new StringWriter();
70          transform(in,xslt, parameters, writer);
71          return writer.toString();
72      }
73  
74      public void transform(InputStream in, InputStream xslt, Writer out) throws TransformerException
75      {
76          transform(in, xslt, (Map) null, out );
77      }
78  
79      public void transform(InputStream in, InputStream xslt, Map parameters, Writer out) throws TransformerException
80      {
81          
82  		Transformer transformer = transformerFactory.newTransformer(new StreamSource(xslt));
83          if (parameters != null)
84          {
85              Iterator keys = parameters.keySet().iterator();
86              while (keys.hasNext())
87              {
88                  String key = (String) keys.next();
89                  transformer.setParameter(key, parameters.get(key));
90              }
91          }
92          transformer.transform(new StreamSource(in), new StreamResult(out));
93      }
94  
95  }