View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.any23.extractor.rdfa;
19  
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.w3c.dom.Document;
23  
24  import javax.xml.transform.Transformer;
25  import javax.xml.transform.TransformerConfigurationException;
26  import javax.xml.transform.TransformerException;
27  import javax.xml.transform.TransformerFactory;
28  import javax.xml.transform.dom.DOMSource;
29  import javax.xml.transform.stream.StreamResult;
30  import javax.xml.transform.stream.StreamSource;
31  import java.io.InputStream;
32  import java.io.Writer;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * An XSLT stylesheet loaded from an InputStream, can be applied
38   * to DOM trees and writes the result to a {@link Writer}.
39   *
40   * @author Gabriele Renzi
41   * @author Richard Cyganiak (richard@cyganiak.de)
42   */
43  public class XSLTStylesheet {
44  
45      private final static Logger log = LoggerFactory.getLogger(XSLTStylesheet.class);
46  
47      private final Transformer transformer;
48  
49      public XSLTStylesheet(InputStream xsltFile) {
50          try {
51              transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile));
52          } catch (TransformerConfigurationException e) {
53              throw new RuntimeException("Should not happen, we use the default configuration", e);
54          }
55      }
56  
57      /**
58       * Applies the XSLT transformation
59       * @param document where apply the transformation
60       * @param output the {@link java.io.Writer} where write on
61       * @throws XSLTStylesheetException if there is an error applying the transformation
62       */
63      public synchronized void applyTo(Document document, Writer output)
64              throws XSLTStylesheetException {
65          this.applyTo(document, output, new HashMap<String, String>());
66      }
67  
68      /**
69       * Applies the XSLT transformation
70       * @param document where apply the transformation
71       * @param output the {@link java.io.Writer} where write on
72       * @param parameters the parameters to be passed to {@link Transformer}.
73       *              Pass an empty {@link Map} if no parameters are foreseen.
74       * @throws XSLTStylesheetException if there is an error applying the transformation
75       */
76      public synchronized void applyTo(Document document, Writer output,
77                                       Map<String, String> parameters) throws XSLTStylesheetException {
78          for(String parameterKey : parameters.keySet()) {
79              transformer.setParameter(parameterKey, parameters.get(parameterKey));
80          }
81          try {
82              transformer.transform(
83                      new DOMSource(
84                              document,
85                              document.getBaseURI()
86                      ),
87                      new StreamResult(output)
88              );
89          } catch (TransformerException te) {
90              log.error("------ BEGIN XSLT Transformer Exception ------");
91              log.error("Exception in XSLT Stylesheet transformation.", te);
92              log.error("Input DOM node:", document);
93              log.error("Input DOM node getBaseIRI:", document.getBaseURI());
94              log.error("Output writer:", output);
95              log.error("------ END XSLT Transformer Exception ------");
96              throw new XSLTStylesheetException(" An error occurred during the XSLT transformation", te);
97          }
98      }
99      
100 }