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       */
62      public synchronized void applyTo(Document document, Writer output)
63              throws XSLTStylesheetException {
64          this.applyTo(document, output, new HashMap<String, String>());
65      }
66  
67      /**
68       * Applies the XSLT transformation
69       * @param document where apply the transformation
70       * @param output the {@link java.io.Writer} where write on
71       * @param parameters the parameters to be passed to {@link Transformer}.
72       *              Pass an empty {@link Map} if no parameters are foreseen.
73       */
74      public synchronized void applyTo(Document document, Writer output,
75                                       Map<String, String> parameters) throws XSLTStylesheetException {
76          for(String parameterKey : parameters.keySet()) {
77              transformer.setParameter(parameterKey, parameters.get(parameterKey));
78          }
79          try {
80              transformer.transform(
81                      new DOMSource(
82                              document,
83                              document.getBaseURI()
84                      ),
85                      new StreamResult(output)
86              );
87          } catch (TransformerException te) {
88              log.error("------ BEGIN XSLT Transformer Exception ------");
89              log.error("Exception in XSLT Stylesheet transformation.", te);
90              log.error("Input DOM node:", document);
91              log.error("Input DOM node getBaseURI:", document.getBaseURI());
92              log.error("Output writer:", output);
93              log.error("------ END XSLT Transformer Exception ------");
94              throw new XSLTStylesheetException(" An error occurred during the XSLT transformation", te);
95          }
96      }
97      
98  }