View Javadoc

1   package fr.in2p3.jsaga.helpers.xslt;
2   
3   import fr.in2p3.jsaga.Base;
4   import fr.in2p3.jsaga.EngineProperties;
5   import fr.in2p3.jsaga.helpers.XMLFileParser;
6   import org.w3c.dom.Document;
7   
8   import javax.xml.parsers.DocumentBuilderFactory;
9   import javax.xml.parsers.ParserConfigurationException;
10  import javax.xml.transform.*;
11  import javax.xml.transform.dom.DOMResult;
12  import javax.xml.transform.dom.DOMSource;
13  import java.io.*;
14  import java.util.Iterator;
15  import java.util.Map;
16  
17  /* ***************************************************
18   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
19   * ***             http://cc.in2p3.fr/             ***
20   * ***************************************************
21   * File:   CompiledTransformer
22   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
23   * ***************************************************
24   * Description:                                      */
25  
26  /*
27   <plugins>
28       <plugin>
29           <groupId>org.codehaus.mojo</groupId>
30           <artifactId>maven-plugin-xsltc</artifactId>
31           <version>1.0-SNAPSHOT</version>
32           <executions>
33               <execution>
34                   <id>xsltc</id>
35                   <goals><goal>compile</goal></goals>
36               </execution>
37           </executions>
38           <configuration>
39               <stylesheets>
40                   <stylesheet>${basedir}/resources/xsl/jsaga-default-contexts-merge.xsl</stylesheet>
41                   <stylesheet>${basedir}/resources/xsl/jsaga-default-contexts.xsl</stylesheet>
42               </stylesheets>
43               <packageName>fr.in2p3.jsaga.generated.xsl</packageName>
44               <outputDirectory>${project.build.directory}/generated-classes</outputDirectory>
45           </configuration>
46       </plugin>
47   </plugins>
48   <resources>
49       <resource><directory>${project.build.directory}/generated-classes</directory></resource>
50       <resource><directory>resources</directory></resource>
51   </resources>
52   */
53  public class CompiledTransformer {
54      private Templates m_translet;
55  
56      public CompiledTransformer(String packageName, String transletName) throws TransformerConfigurationException, IOException {
57          // load translet
58          TransformerFactory tFactory;
59          // thread-safe instanciation not supported by JDK 1.5
60  //        tFactory = TransformerFactory.newInstance("org.apache.xalan.xsltc.trax.TransformerFactoryImpl", CompiledTransformer.class.getClassLoader());
61          synchronized (this) {
62              // save transformer implementation
63              final String TRANSFORMER_IMPL = "javax.xml.transform.TransformerFactory";
64              String savImpl = System.getProperty(TRANSFORMER_IMPL);
65              System.setProperty(TRANSFORMER_IMPL, "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
66  
67              // create transformer factory
68              tFactory = TransformerFactory.newInstance();
69  
70              // restore transformer implementation
71              if (savImpl != null) {
72                  System.setProperty(TRANSFORMER_IMPL, savImpl);
73              } else {
74                  System.getProperties().remove(TRANSFORMER_IMPL);
75              }
76          }
77          tFactory.setAttribute("debug", "true");
78          tFactory.setAttribute("package-name", packageName);
79          tFactory.setAttribute("translet-name", transletName);
80          tFactory.setAttribute("use-classpath", "true");
81          m_translet = tFactory.newTemplates(null);
82      }
83  
84      public Document transformToDOM(Document xml, File debugFile) throws TransformerException, IOException {
85          // create result container
86          Document doc;
87          try {
88              doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
89          } catch (ParserConfigurationException e) {
90              throw new TransformerException(e);
91          }
92          Result result = new DOMResult(doc);
93  
94          // create transformer
95          Transformer transformer = m_translet.newTransformer();
96          transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
97          transformer.setURIResolver(new TransformerURIResolver());
98          transformer.setErrorListener(new XSLLogger());
99          for (Iterator it = EngineProperties.getProperties().entrySet().iterator(); it.hasNext();) {
100             Map.Entry entry = (Map.Entry) it.next();
101             transformer.setParameter((String) entry.getKey(), entry.getValue());
102         }
103 
104         // process
105         try {
106             transformer.transform(new DOMSource(xml), result);
107         } catch(TransformerException e) {
108             // throw the cause of exception
109             transformer.getErrorListener().fatalError(e);
110         }
111 
112         // debug
113         if (Base.DEBUG && debugFile!=null) {
114             ByteArrayOutputStream out = new ByteArrayOutputStream();
115             XMLFileParser.dump(doc, out);
116             byte[] outBytes = out.toByteArray();
117             if (outBytes!=null) {
118                 OutputStream f = new FileOutputStream(debugFile);
119                 f.write(outBytes);
120                 f.close();
121             }
122         }
123 
124         // return
125         return doc;
126     }
127 }