use of javax.xml.transform.Templates in project nhin-d by DirectProject.
the class XslConversion method run.
/**
* Perform the XSL conversion using the provided map file and message.
*
* @param mapFile
* The map file.
* @param message
* The message.
* @return an XSL conversion.
* @throws Exception
*/
public String run(String mapFile, String message) throws Exception {
long start = System.currentTimeMillis();
String retXml = "";
Transformer transformer = null;
try {
if (conversions.containsKey(mapFile)) {
Templates temp = conversions.get(mapFile);
transformer = temp.newTransformer();
LOGGER.info("From xsl cache");
} else {
synchronized (conversions) {
if (!conversions.containsKey(mapFile)) {
/*
* Use the static TransformerFactory.newInstance()
* method to instantiate a TransformerFactory. The
* javax.xml.transform.TransformerFactory system
* property setting determines the actual class to
* instantiate --
* org.apache.xalan.transformer.TransformerImpl.
*/
TransformerFactory tFactory = TransformerFactory.newInstance();
/*
* Use the TransformerFactory to instantiate a Template
* that is thread safe for use in generating Transfomers
*/
InputStream is = this.getClass().getClassLoader().getResourceAsStream(mapFile);
if (is == null) {
LOGGER.info("Mapfile did not read " + mapFile);
}
Templates temp = tFactory.newTemplates(new StreamSource(is));
transformer = temp.newTransformer();
conversions.put(mapFile, temp);
}
}
}
CharArrayWriter to = new CharArrayWriter();
transformer.transform(new StreamSource(new CharArrayReader(message.toCharArray())), new StreamResult(to));
retXml = to.toString();
} catch (TransformerConfigurationException e) {
LOGGER.error("Exception occured during XSL conversion", e);
throw e;
} catch (TransformerException e) {
LOGGER.error("Exception occured during XSL conversion", e);
throw e;
}
if (LOGGER.isInfoEnabled()) {
long elapse = System.currentTimeMillis() - start;
LOGGER.info("Started at " + new Timestamp(start).toString());
LOGGER.info("Elapsed conversion time was " + elapse + "ms");
}
return retXml;
}
use of javax.xml.transform.Templates in project intellij-community by JetBrains.
the class XsltDocumentationProvider method getTemplate.
private Templates getTemplate() throws TransformerConfigurationException, IOException {
Templates t = com.intellij.reference.SoftReference.dereference(myTemplates);
if (t == null) {
t = TransformerFactory.newInstance().newTemplates(makeSource("resources/documentation.xsl"));
myTemplates = new SoftReference<>(t);
}
return t;
}
use of javax.xml.transform.Templates in project camel by apache.
the class XsltBuilder method setTransformerSource.
/**
* Sets the XSLT transformer from a Source
*
* @param source the source
* @throws TransformerConfigurationException is thrown if creating a XSLT transformer failed.
*/
public void setTransformerSource(Source source) throws TransformerConfigurationException {
TransformerFactory factory = converter.getTransformerFactory();
if (errorListener != null) {
factory.setErrorListener(errorListener);
} else {
// use a logger error listener so users can see from the logs what the error may be
factory.setErrorListener(new XsltErrorListener());
}
if (getUriResolver() != null) {
factory.setURIResolver(getUriResolver());
}
// Check that the call to newTemplates() returns a valid template instance.
// In case of an xslt parse error, it will return null and we should stop the
// deployment and raise an exception as the route will not be setup properly.
Templates templates = factory.newTemplates(source);
if (templates != null) {
setTemplate(templates);
} else {
throw new TransformerConfigurationException("Error creating XSLT template. " + "This is most likely be caused by a XML parse error. " + "Please verify your XSLT file configured.");
}
}
use of javax.xml.transform.Templates in project robovm by robovm.
the class TransformerFactoryImpl method newTransformer.
/**
* Process the source into a Transformer object. Care must
* be given to know that this object can not be used concurrently
* in multiple threads.
*
* @param source An object that holds a URL, input stream, etc.
*
* @return A Transformer object capable of
* being used for transformation purposes in a single thread.
*
* @throws TransformerConfigurationException May throw this during the parse when it
* is constructing the Templates object and fails.
*/
public Transformer newTransformer(Source source) throws TransformerConfigurationException {
try {
Templates tmpl = newTemplates(source);
/* this can happen if an ErrorListener is present and it doesn't
throw any exception in fatalError.
The spec says: "a Transformer must use this interface
instead of throwing an exception" - the newTemplates() does
that, and returns null.
*/
if (tmpl == null)
return null;
Transformer transformer = tmpl.newTransformer();
transformer.setURIResolver(m_uriResolver);
return transformer;
} catch (TransformerConfigurationException ex) {
if (m_errorListener != null) {
try {
m_errorListener.fatalError(ex);
// TODO: but the API promises to never return null...
return null;
} catch (TransformerConfigurationException ex1) {
throw ex1;
} catch (TransformerException ex1) {
throw new TransformerConfigurationException(ex1);
}
}
throw ex;
}
}
use of javax.xml.transform.Templates in project jdk8u_jdk by JetBrains.
the class XslSubstringTest method testTransform.
private String testTransform(String xsl) throws Exception {
//Prepare sources for transormation
Source src = new StreamSource(new StringReader(xml));
Source xslsrc = new StreamSource(new StringReader(xslPre + xsl + xslPost));
//Create factory, template and transformer
TransformerFactory tf = TransformerFactory.newInstance();
Templates tmpl = tf.newTemplates(xslsrc);
Transformer t = tmpl.newTransformer();
//Prepare output stream
StringWriter xmlResultString = new StringWriter();
StreamResult xmlResultStream = new StreamResult(xmlResultString);
//Transform
t.transform(src, xmlResultStream);
return xmlResultString.toString().trim();
}
Aggregations