use of javax.xml.transform.TransformerException in project lucene-solr by apache.
the class LibVersionsCheckTask method xmlToString.
private String xmlToString(File ivyXmlFile) {
StringWriter writer = new StringWriter();
try {
StreamSource inputSource = new StreamSource(new FileInputStream(ivyXmlFile.getPath()));
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(inputSource, new StreamResult(writer));
} catch (TransformerException | IOException e) {
throw new BuildException("Exception reading " + ivyXmlFile.getPath() + ": " + e.toString(), e);
}
return writer.toString();
}
use of javax.xml.transform.TransformerException in project poi by apache.
the class StreamHelper method saveXmlInStream.
/**
* Save the document object in the specified output stream.
*
* @param xmlContent
* The XML document.
* @param outStream
* The OutputStream in which the XML document will be written.
* @return <b>true</b> if the xml is successfully written in the stream,
* else <b>false</b>.
*/
public static boolean saveXmlInStream(Document xmlContent, OutputStream outStream) {
try {
Transformer trans = getIdentityTransformer();
Source xmlSource = new DOMSource(xmlContent);
// prevent close of stream by transformer:
Result outputTarget = new StreamResult(new FilterOutputStream(outStream) {
@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
}
@Override
public void close() throws IOException {
// only flush, don't close!
out.flush();
}
});
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
trans.transform(xmlSource, outputTarget);
} catch (TransformerException e) {
return false;
}
return true;
}
use of javax.xml.transform.TransformerException in project jmeter by apache.
the class XPathUtil method getValueForNode.
/**
* Return value for node
* @param node Node
* @return String
*/
private static String getValueForNode(Node node) {
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException e) {
sw.write(e.getMessageAndLocation());
}
return sw.toString();
}
use of javax.xml.transform.TransformerException in project jackrabbit by apache.
the class WebdavResponseImpl method sendXmlResponse.
/**
* Send Xml response body.
*
* @param serializable
* @param status
* @throws IOException
* @see DavServletResponse#sendXmlResponse(XmlSerializable, int)
*/
public void sendXmlResponse(XmlSerializable serializable, int status) throws IOException {
httpResponse.setStatus(status);
if (serializable != null) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
Document doc = DomUtil.createDocument();
doc.appendChild(serializable.toXml(doc));
// JCR-2636: Need to use an explicit OutputStreamWriter
// instead of relying on the built-in UTF-8 serialization
// to avoid problems with surrogate pairs on Sun JRE 1.5.
Writer writer = new OutputStreamWriter(out, "UTF-8");
DomUtil.transformDocument(doc, writer);
writer.flush();
// TODO: Should this be application/xml? See JCR-1621
httpResponse.setContentType("text/xml; charset=UTF-8");
httpResponse.setContentLength(out.size());
out.writeTo(httpResponse.getOutputStream());
} catch (ParserConfigurationException e) {
log.error(e.getMessage());
throw new IOException(e.getMessage());
} catch (TransformerException e) {
log.error(e.getMessage());
throw new IOException(e.getMessage());
} catch (SAXException e) {
log.error(e.getMessage());
throw new IOException(e.getMessage());
}
}
}
use of javax.xml.transform.TransformerException in project intellij-community by JetBrains.
the class IntentionDump method main.
@Override
public void main(String[] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element intentions = document.createElement("Intentions");
document.appendChild(intentions);
while (((IntentionManagerImpl) IntentionManager.getInstance()).hasActiveRequests()) {
TimeoutUtil.sleep(100);
}
for (IntentionActionMetaData actionMetaData : IntentionManagerSettings.getInstance().getMetaData()) {
Element intention = document.createElement("Intention");
intention.setAttribute("categories", StringUtil.join(actionMetaData.myCategory, ","));
intention.setAttribute("name", actionMetaData.getFamily());
Element description = document.createElement("description");
CDATASection descriptionSection = document.createCDATASection(escapeCDATA(actionMetaData.getDescription().getText()));
description.appendChild(descriptionSection);
intention.appendChild(description);
TextDescriptor[] beforeDescriptors = actionMetaData.getExampleUsagesBefore();
if (beforeDescriptors.length > 0) {
Element before = document.createElement("before");
CDATASection beforeSection = document.createCDATASection(escapeCDATA(beforeDescriptors[0].getText()));
before.appendChild(beforeSection);
intention.appendChild(before);
}
TextDescriptor[] afterDescriptors = actionMetaData.getExampleUsagesAfter();
if (afterDescriptors.length > 0) {
Element after = document.createElement("after");
CDATASection afterSection = document.createCDATASection(escapeCDATA(afterDescriptors[0].getText()));
after.appendChild(afterSection);
intention.appendChild(after);
}
intentions.appendChild(intention);
}
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
final String path = args.length == 2 ? args[1] : PathManager.getHomePath() + File.separator + "AllIntentions.xml";
StreamResult console = new StreamResult(new File(path));
transformer.transform(source, console);
System.exit(0);
} catch (ParserConfigurationException | IOException | TransformerException e) {
// noinspection CallToPrintStackTrace
e.printStackTrace();
System.exit(1);
}
}
Aggregations