| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public class XSLTStylesheet { |
| 44 | |
|
| 45 | 0 | private final static Logger log = LoggerFactory.getLogger(XSLTStylesheet.class); |
| 46 | |
|
| 47 | |
private final Transformer transformer; |
| 48 | |
|
| 49 | 0 | public XSLTStylesheet(InputStream xsltFile) { |
| 50 | |
try { |
| 51 | 0 | transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile)); |
| 52 | 0 | } catch (TransformerConfigurationException e) { |
| 53 | 0 | throw new RuntimeException("Should not happen, we use the default configuration", e); |
| 54 | 0 | } |
| 55 | 0 | } |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public synchronized void applyTo(Document document, Writer output) |
| 63 | |
throws XSLTStylesheetException { |
| 64 | 0 | this.applyTo(document, output, new HashMap<String, String>()); |
| 65 | 0 | } |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public synchronized void applyTo(Document document, Writer output, |
| 75 | |
Map<String, String> parameters) throws XSLTStylesheetException { |
| 76 | 0 | for(String parameterKey : parameters.keySet()) { |
| 77 | 0 | transformer.setParameter(parameterKey, parameters.get(parameterKey)); |
| 78 | |
} |
| 79 | |
try { |
| 80 | 0 | transformer.transform( |
| 81 | |
new DOMSource( |
| 82 | |
document, |
| 83 | |
document.getBaseURI() |
| 84 | |
), |
| 85 | |
new StreamResult(output) |
| 86 | |
); |
| 87 | 0 | } catch (TransformerException te) { |
| 88 | 0 | log.error("------ BEGIN XSLT Transformer Exception ------"); |
| 89 | 0 | log.error("Exception in XSLT Stylesheet transformation.", te); |
| 90 | 0 | log.error("Input DOM node:", document); |
| 91 | 0 | log.error("Input DOM node getBaseURI:", document.getBaseURI()); |
| 92 | 0 | log.error("Output writer:", output); |
| 93 | 0 | log.error("------ END XSLT Transformer Exception ------"); |
| 94 | 0 | throw new XSLTStylesheetException(" An error occurred during the XSLT transformation", te); |
| 95 | 0 | } |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
} |