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 to DOM trees and writes the result to a {@link Writer}.
38   *
39   * @author Gabriele Renzi
40   * @author Richard Cyganiak (richard@cyganiak.de)
41   */
42  public class XSLTStylesheet {
43  
44      private final static Logger log = LoggerFactory.getLogger(XSLTStylesheet.class);
45  
46      private final Transformer transformer;
47  
48      public XSLTStylesheet(InputStream xsltFile) {
49          try {
50              transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile));
51          } catch (TransformerConfigurationException e) {
52              throw new RuntimeException("Should not happen, we use the default configuration", e);
53          }
54      }
55  
56      /**
57       * Applies the XSLT transformation
58       * 
59       * @param document
60       *            where apply the transformation
61       * @param output
62       *            the {@link java.io.Writer} where write on
63       * 
64       * @throws XSLTStylesheetException
65       *             if there is an error applying the transformation
66       */
67      public synchronized void applyTo(Document document, Writer output) throws XSLTStylesheetException {
68          this.applyTo(document, output, new HashMap<String, String>());
69      }
70  
71      /**
72       * Applies the XSLT transformation
73       * 
74       * @param document
75       *            where apply the transformation
76       * @param output
77       *            the {@link java.io.Writer} where write on
78       * @param parameters
79       *            the parameters to be passed to {@link Transformer}. Pass an empty {@link Map} if no parameters are
80       *            foreseen.
81       * 
82       * @throws XSLTStylesheetException
83       *             if there is an error applying the transformation
84       */
85      public synchronized void applyTo(Document document, Writer output, Map<String, String> parameters)
86              throws XSLTStylesheetException {
87          for (String parameterKey : parameters.keySet()) {
88              transformer.setParameter(parameterKey, parameters.get(parameterKey));
89          }
90          try {
91              transformer.transform(new DOMSource(document, document.getBaseURI()), new StreamResult(output));
92          } catch (TransformerException te) {
93              log.error("------ BEGIN XSLT Transformer Exception ------");
94              log.error("Exception in XSLT Stylesheet transformation.", te);
95              log.error("Input DOM node:", document);
96              log.error("Input DOM node getBaseIRI:", document.getBaseURI());
97              log.error("Output writer:", output);
98              log.error("------ END XSLT Transformer Exception ------");
99              throw new XSLTStylesheetException(" An error occurred during the XSLT transformation", te);
100         }
101     }
102 
103 }