corpus-services  1.0
XSLTransformer.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package de.uni_hamburg.corpora.utilities;
7 
8 import java.io.StringWriter;
9 import java.util.HashMap;
10 import java.util.Map;
11 import javax.xml.transform.Transformer;
12 import javax.xml.transform.TransformerConfigurationException;
13 import javax.xml.transform.TransformerException;
14 import javax.xml.transform.TransformerFactory;
15 import javax.xml.transform.stream.StreamResult;
16 import javax.xml.transform.stream.StreamSource;
17 import net.sf.saxon.Configuration;
18 import net.sf.saxon.serialize.MessageEmitter;
19 import net.sf.saxon.trans.XPathException;
20 
28 public class XSLTransformer {
29 
30  private TransformerFactory tranformerFactory;
31  private Transformer transformer;
32  private String transformerFactoryImpl = "net.sf.saxon.TransformerFactoryImpl";
33  private Map<String, Object> parameters = new HashMap<>();
34  private Map<String, String> outputProperties = new HashMap<>();
35 
39  public XSLTransformer() throws TransformerConfigurationException {
40  tranformerFactory = TransformerFactory.newInstance(transformerFactoryImpl, null);
41  }
42 
47  public XSLTransformer(String impl) throws TransformerConfigurationException {
48  transformerFactoryImpl = impl;
49  tranformerFactory = TransformerFactory.newInstance(transformerFactoryImpl, null);
50  }
51 
61  public String transform(String xml, String xsl) throws TransformerException {
62  StreamSource xslSource = TypeConverter.String2StreamSource(xsl);
63  StreamSource xmlSource = TypeConverter.String2StreamSource(xml);
64  return transform(xmlSource, xslSource);
65  }
66 
76  public String transform(StreamSource xmlSource, StreamSource xslSource) throws TransformerException {
77  final StringWriter messageOut = new StringWriter();
78  String result = null;
79  try {
80  if(xslSource != null){
81  transformer = tranformerFactory.newTransformer(xslSource);
82 
83  //trying to get xsl:message into error reports
84  ((net.sf.saxon.jaxp.TransformerImpl) transformer).getUnderlyingController().setRecoveryPolicy(Configuration.DO_NOT_RECOVER);
85  ((net.sf.saxon.jaxp.TransformerImpl) transformer).getUnderlyingController().setMessageEmitter(new MessageEmitter() {
86  @Override
87  public void open() throws XPathException {
88  setWriter(messageOut);
89  super.open();
90  }
91  });
92  } else{
93  transformer = tranformerFactory.newTransformer();
94  }
95 
96  // set the output properties for XSLT transformation
97  for (Map.Entry<String, String> param : outputProperties.entrySet()) {
98  transformer.setOutputProperty(param.getKey(), param.getValue());
99  }
100  // set the parameters for XSLT transformation
101  for (Map.Entry<String, Object> param : parameters.entrySet()) {
102  transformer.setParameter(param.getKey(), param.getValue());
103  }
104 
105  //transform and fetch result
106  StringWriter resultWriter = new StringWriter();
107  transformer.transform(xmlSource, new StreamResult(resultWriter));
108  result = resultWriter.toString();
109 
110  } catch (TransformerException e) {
111  //System.out.println("Message: " + e.getLocalizedMessage());
112  String message = messageOut.toString(); // this is the "exception message\n" that you want
113  System.out.println("MESSAGE: " + message);
114  throw new TransformerException(message, e); // rethrow using the captured message, if you really want that "exception message" available to a caller in e.getMessage()
115  }
116 
117  return result;
118  }
119 
120 
129  public String transform(StreamSource xmlSource) throws TransformerException {
130  return transform(xmlSource, null);
131  }
132 
133 
142  public String transform(String xml) throws TransformerException {
143  StreamSource xmlSource = TypeConverter.String2StreamSource(xml);
144  return transform(xmlSource);
145  }
146 
147 
155  public void setParameter(String parameterName, Object parameterValue) {
156  parameters.put(parameterName, parameterValue);
157  }
158 
165  public void setParameters(Map<String, Object> params) {
166  parameters = params;
167  }
168 
175  public Object getParameter(String parameterName) {
176  return parameters.get(parameterName);
177  }
178 
186  public Map getParameters() {
187  return parameters;
188  }
189 
197  public void setOutputProperty(String propertyName, String propertyValue) {
198  outputProperties.put(propertyName, propertyValue);
199  }
200 
207  public void setOutputProperties(Map<String, String> outputProps) {
208  outputProperties = outputProps;
209  }
210 
217  public Object getOutputProperty(String propertyName) {
218  return outputProperties.get(propertyName);
219  }
220 
228  public Map getOutputProperties() {
229  return outputProperties;
230  }
231 
239  public void setTransformerFactoryImpl(String impl) {
240  transformerFactoryImpl = impl;
241  tranformerFactory = TransformerFactory.newInstance(transformerFactoryImpl, null);
242  }
243 
251  public String getTransformerFactoryImpl() {
252  return transformerFactoryImpl;
253  }
254 
255 }
void setParameter(String parameterName, Object parameterValue)
static StreamSource String2StreamSource(String s)
String transform(StreamSource xmlSource, StreamSource xslSource)
void setParameters(Map< String, Object > params)
void setOutputProperties(Map< String, String > outputProps)
void setOutputProperty(String propertyName, String propertyValue)