use of javax.xml.transform.TransformerFactory in project camel by apache.
the class SchematronEndpoint method createTransformerFactory.
private void createTransformerFactory() throws ClassNotFoundException {
// provide the class loader of this component to work in OSGi environments
Class<TransformerFactory> factoryClass = getCamelContext().getClassResolver().resolveMandatoryClass(SAXON_TRANSFORMER_FACTORY_CLASS_NAME, TransformerFactory.class, SchematronComponent.class.getClassLoader());
LOG.debug("Using TransformerFactoryClass {}", factoryClass);
transformerFactory = getCamelContext().getInjector().newInstance(factoryClass);
transformerFactory.setURIResolver(new ClassPathURIResolver(Constants.SCHEMATRON_TEMPLATES_ROOT_DIR, this.uriResolver));
transformerFactory.setAttribute(LINE_NUMBERING, true);
}
use of javax.xml.transform.TransformerFactory in project liquibase by liquibase.
the class DefaultXmlWriter method write.
@Override
public void write(Document doc, OutputStream outputStream) throws IOException {
try {
TransformerFactory factory = TransformerFactory.newInstance();
try {
factory.setAttribute("indent-number", 4);
} catch (Exception e) {
//guess we can't set it, that's ok
;
}
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
//need to nest outputStreamWriter to get around JDK 5 bug. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446
OutputStreamWriter writer = new OutputStreamWriter(outputStream, LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
transformer.transform(new DOMSource(doc), new StreamResult(writer));
writer.flush();
writer.close();
} catch (TransformerException e) {
throw new IOException(e.getMessage());
}
}
use of javax.xml.transform.TransformerFactory in project cas by apereo.
the class SamlUtils method transformSamlObject.
/**
* Transform saml object to String.
*
* @param configBean the config bean
* @param samlObject the saml object
* @return the string
* @throws SamlException the saml exception
*/
public static StringWriter transformSamlObject(final OpenSamlConfigBean configBean, final XMLObject samlObject) throws SamlException {
final StringWriter writer = new StringWriter();
try {
final Marshaller marshaller = configBean.getMarshallerFactory().getMarshaller(samlObject.getElementQName());
if (marshaller != null) {
final Element element = marshaller.marshall(samlObject);
final DOMSource domSource = new DOMSource(element);
final StreamResult result = new StreamResult(writer);
final TransformerFactory tf = TransformerFactory.newInstance();
final Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
}
} catch (final Exception e) {
throw new SamlException(e.getMessage(), e);
}
return writer;
}
use of javax.xml.transform.TransformerFactory in project asciidoctor-fopub by asciidoctor.
the class InputHandler method transformTo.
/**
* Transforms the input document to the input format expected by FOP using XSLT.
* @param result the Result object where the result of the XSL transformation is sent to
* @throws FOPException in case of an error during processing
*/
protected void transformTo(Result result) throws FOPException {
try {
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
if (uriResolver != null) {
factory.setURIResolver(uriResolver);
}
factory.setErrorListener(this);
Transformer transformer;
Source xsltSource = createXSLTSource();
if (xsltSource == null) {
// FO Input
transformer = factory.newTransformer();
} else {
// XML/XSLT input
transformer = factory.newTransformer(xsltSource);
// Set the value of parameters, if any, defined for stylesheet
if (xsltParams != null) {
for (int i = 0; i < xsltParams.size(); i += 2) {
transformer.setParameter((String) xsltParams.elementAt(i), (String) xsltParams.elementAt(i + 1));
}
}
}
transformer.setErrorListener(this);
// Create a SAXSource from the input Source file
Source src = createMainSource();
// Start XSLT transformation and FOP processing
transformer.transform(src, result);
} catch (Exception e) {
throw new FOPException(e);
}
}
use of javax.xml.transform.TransformerFactory in project hazelcast by hazelcast.
the class ConfigXmlGenerator method format.
private String format(final String input, int indent) {
if (!formatted) {
return input;
}
StreamResult xmlOutput = null;
try {
final Source xmlInput = new StreamSource(new StringReader(input));
xmlOutput = new StreamResult(new StringWriter());
TransformerFactory transformerFactory = TransformerFactory.newInstance();
/* Older versions of Xalan still use this method of setting indent values.
* Attempt to make this work but don't completely fail if it's a problem.
*/
try {
transformerFactory.setAttribute("indent-number", indent);
} catch (IllegalArgumentException e) {
if (LOGGER.isFinestEnabled()) {
LOGGER.finest("Failed to set indent-number attribute; cause: " + e.getMessage());
}
}
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
/* Newer versions of Xalan will look for a fully-qualified output property in order to specify amount of
* indentation to use. Attempt to make this work as well but again don't completely fail if it's a problem.
*/
try {
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
} catch (IllegalArgumentException e) {
if (LOGGER.isFinestEnabled()) {
LOGGER.finest("Failed to set indent-amount property; cause: " + e.getMessage());
}
}
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
} catch (Exception e) {
LOGGER.warning(e);
return input;
} finally {
if (xmlOutput != null) {
closeResource(xmlOutput.getWriter());
}
}
}
Aggregations