use of javax.xml.transform.TransformerConfigurationException in project apex-core by apache.
the class DTConfiguration method writeToFile.
public void writeToFile(File file, Scope scope, String comment) throws IOException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = new Date();
Document doc;
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException ex) {
throw new RuntimeException(ex);
}
Element rootElement = doc.createElement("configuration");
rootElement.appendChild(doc.createComment(" WARNING: Do not edit this file. Your changes will be overwritten. "));
rootElement.appendChild(doc.createComment(" Written by dtgateway on " + sdf.format(date)));
rootElement.appendChild(doc.createComment(" " + comment + " "));
doc.appendChild(rootElement);
for (Map.Entry<String, ValueEntry> entry : map.entrySet()) {
ValueEntry valueEntry = entry.getValue();
if (scope == null || valueEntry.scope == scope) {
Element property = doc.createElement("property");
rootElement.appendChild(property);
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode(entry.getKey()));
property.appendChild(name);
Element value = doc.createElement("value");
value.appendChild(doc.createTextNode(valueEntry.value));
property.appendChild(value);
if (valueEntry.description != null) {
Element description = doc.createElement("description");
description.appendChild(doc.createTextNode(valueEntry.description));
property.appendChild(description);
}
if (valueEntry.isFinal) {
Element isFinal = doc.createElement("final");
isFinal.appendChild(doc.createTextNode("true"));
property.appendChild(isFinal);
}
}
}
rootElement.appendChild(doc.createComment(" WARNING: Do not edit this file. Your changes will be overwritten. "));
// write the content into xml file
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(source, result);
} catch (TransformerConfigurationException ex) {
throw new RuntimeException(ex);
} catch (TransformerException ex) {
throw new IOException(ex);
}
}
use of javax.xml.transform.TransformerConfigurationException in project jdk8u_jdk by JetBrains.
the class TransformXSLT method enginePerformTransform.
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream baos, Transform transformObject) throws IOException, TransformationException {
try {
Element transformElement = transformObject.getElement();
Element xsltElement = XMLUtils.selectNode(transformElement.getFirstChild(), XSLTSpecNS, "stylesheet", 0);
if (xsltElement == null) {
Object[] exArgs = { "xslt:stylesheet", "Transform" };
throw new TransformationException("xml.WrongContent", exArgs);
}
TransformerFactory tFactory = TransformerFactory.newInstance();
// Process XSLT stylesheets in a secure manner
tFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
/*
* This transform requires an octet stream as input. If the actual
* input is an XPath node-set, then the signature application should
* attempt to convert it to octets (apply Canonical XML]) as described
* in the Reference Processing Model (section 4.3.3.2).
*/
Source xmlSource = new StreamSource(new ByteArrayInputStream(input.getBytes()));
Source stylesheet;
/*
* This complicated transformation of the stylesheet itself is necessary
* because of the need to get the pure style sheet. If we simply say
* Source stylesheet = new DOMSource(this.xsltElement);
* whereby this.xsltElement is not the rootElement of the Document,
* this causes problems;
* so we convert the stylesheet to byte[] and use this as input stream
*/
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(xsltElement);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
stylesheet = new StreamSource(new ByteArrayInputStream(os.toByteArray()));
}
Transformer transformer = tFactory.newTransformer(stylesheet);
// implementations.
try {
transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n");
} catch (Exception e) {
log.log(java.util.logging.Level.WARNING, "Unable to set Xalan line-separator property: " + e.getMessage());
}
if (baos == null) {
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
StreamResult outputTarget = new StreamResult(baos1);
transformer.transform(xmlSource, outputTarget);
return new XMLSignatureInput(baos1.toByteArray());
}
StreamResult outputTarget = new StreamResult(baos);
transformer.transform(xmlSource, outputTarget);
XMLSignatureInput output = new XMLSignatureInput((byte[]) null);
output.setOutputStream(baos);
return output;
} catch (XMLSecurityException ex) {
Object[] exArgs = { ex.getMessage() };
throw new TransformationException("generic.EmptyMessage", exArgs, ex);
} catch (TransformerConfigurationException ex) {
Object[] exArgs = { ex.getMessage() };
throw new TransformationException("generic.EmptyMessage", exArgs, ex);
} catch (TransformerException ex) {
Object[] exArgs = { ex.getMessage() };
throw new TransformationException("generic.EmptyMessage", exArgs, ex);
}
}
use of javax.xml.transform.TransformerConfigurationException in project tdi-studio-se by Talend.
the class HTMLDocGenerator method generateXslFile.
private void generateXslFile(String resource, String xslfile, String cssfile, String folder) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document document = builder.parse(new File(resource));
org.w3c.dom.Element rootElement = document.getDocumentElement();
//$NON-NLS-1$
NodeList list = rootElement.getElementsByTagName("style");
org.w3c.dom.Element element = (org.w3c.dom.Element) list.item(0);
String value = element.getChildNodes().item(0).getNodeValue();
if (value != null) {
if (folder != null) {
//$NON-NLS-1$
CSSParserUtils.createCssFile(value, folder + File.separator + "default.css");
}
if (cssfile != null && !cssfile.equals("")) {
//$NON-NLS-1$
if (folder != null) {
String cssName = new File(cssfile).getName();
if (cssName.equalsIgnoreCase("default.css")) {
//$NON-NLS-1$
//$NON-NLS-1$
cssName = "User_" + cssName;
}
File file = new File(folder + File.separator + cssName);
if (!file.exists()) {
file.createNewFile();
}
FileCopyUtils.copy(cssfile, folder + File.separator + cssName);
}
CSSRuleList ruleList = CSSParserUtils.parserCSSSelectors(null, cssfile);
if (ruleList == null) {
return;
} else {
String newValue = CSSParserUtils.generateCssStyle(cssfile, ruleList, value);
element.getChildNodes().item(0).setNodeValue(newValue);
// replace the old value and generate a new xsl file
DOMSource ds = new DOMSource(document);
StreamResult sr = new StreamResult(new File(xslfile));
TransformerFactory.newInstance().newTransformer().transform(ds, sr);
}
}
}
} catch (ParserConfigurationException e) {
ExceptionHandler.process(e);
} catch (SAXException e) {
ExceptionHandler.process(e);
} catch (IOException e) {
ExceptionHandler.process(e);
} catch (TransformerConfigurationException e) {
ExceptionHandler.process(e);
} catch (TransformerException e) {
ExceptionHandler.process(e);
} catch (TransformerFactoryConfigurationError e) {
ExceptionHandler.process(e);
}
}
use of javax.xml.transform.TransformerConfigurationException in project tdi-studio-se by Talend.
the class ComponentFolderManager method writeXMLContent.
/**
* DOC slanglois Comment method "writeXMLContent".
*
* @param iFile
* @param document
* @param enCode
* @throws CoreException
*/
private void writeXMLContent(IFile iFile, Document document, String enCode) throws CoreException {
PrintWriter pw = null;
XMLWriter writer = null;
byte[] byteArray = null;
// get xml content as inputstream
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = tf.newTransformer();
} catch (TransformerConfigurationException e1) {
// e1.printStackTrace();
org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
}
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, enCode);
//$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
ByteArrayOutputStream sw = new ByteArrayOutputStream();
pw = new PrintWriter(sw);
StreamResult result = new StreamResult(pw);
try {
transformer.transform(source, result);
} catch (TransformerException e1) {
// e1.printStackTrace();
org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
}
try {
sw.flush();
} catch (IOException e1) {
// e1.printStackTrace();
org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
} finally {
if (pw != null) {
pw.close();
}
}
byteArray = sw.toByteArray();
// format the xml content
SAXReader saxReader = new SAXReader();
org.dom4j.Document dom4jDocument = null;
try {
dom4jDocument = saxReader.read(new ByteArrayInputStream(byteArray));
} catch (DocumentException e1) {
// e1.printStackTrace();
org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
}
/** format the output like the webBrowser */
OutputFormat format = OutputFormat.createPrettyPrint();
/** give the xml encoding */
format.setEncoding(enCode);
sw = new ByteArrayOutputStream();
try {
writer = new XMLWriter(sw, format);
} catch (UnsupportedEncodingException e1) {
// e1.printStackTrace();
org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
}
try {
writer.write(dom4jDocument);
writer.flush();
} catch (IOException e1) {
// e1.printStackTrace();
org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
// e.printStackTrace();
org.talend.componentdesigner.exception.ExceptionHandler.process(e);
}
}
}
byteArray = sw.toByteArray();
// write content
iFile.setContents(new ByteArrayInputStream(byteArray), true, false, null);
}
use of javax.xml.transform.TransformerConfigurationException in project ddf by codice.
the class TikaInputTransformer method classLoaderAndBundleContextSetup.
private void classLoaderAndBundleContextSetup(BundleContext bundleContext) {
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
try (InputStream stream = TikaMetadataExtractor.class.getResourceAsStream("/metadata.xslt")) {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
templates = TransformerFactory.newInstance(net.sf.saxon.TransformerFactoryImpl.class.getName(), net.sf.saxon.TransformerFactoryImpl.class.getClassLoader()).newTemplates(new StreamSource(stream));
} catch (TransformerConfigurationException e) {
LOGGER.debug("Couldn't create XML transformer", e);
} catch (IOException e) {
LOGGER.debug("Could not get Tiki metadata XSLT", e);
} finally {
Thread.currentThread().setContextClassLoader(tccl);
}
if (bundleContext == null) {
LOGGER.info("Bundle context is null. Unable to register {} as an osgi service.", TikaInputTransformer.class.getSimpleName());
return;
}
registerService(bundleContext);
IIORegistry.getDefaultInstance().registerServiceProvider(new J2KImageReaderSpi());
IIORegistry.getDefaultInstance().registerServiceProvider(new TIFFImageReaderSpi());
}
Aggregations