use of javax.xml.transform.Transformer in project tinker by Tencent.
the class JavaXmlUtil method saveDocument.
/**
* save document
*
* @param document
* @param outputFullFilename
*/
public static void saveDocument(final Document document, final String outputFullFilename) {
OutputStream outputStream = null;
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, Constant.Encoding.UTF8);
outputStream = new FileOutputStream(outputFullFilename);
StreamResult result = new StreamResult(outputStream);
transformer.transform(domSource, result);
} catch (Exception e) {
throw new JavaXmlUtilException(e);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception e) {
throw new JavaXmlUtilException(e);
}
}
}
}
use of javax.xml.transform.Transformer in project sonarqube by SonarSource.
the class DebtModelXMLExporter method prettyFormatXml.
private static String prettyFormatXml(String xml) {
try {
Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT);
Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
StreamResult res = new StreamResult(new ByteArrayOutputStream());
serializer.transform(xmlSource, res);
return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray(), StandardCharsets.UTF_8);
} catch (TransformerException ignored) {
// Ignore, raw XML will be returned
}
return xml;
}
use of javax.xml.transform.Transformer in project che by eclipse.
the class Launching method serializeDocumentInt.
/**
* Serializes a XML document into a string - encoded in UTF8 format,
* with platform line separators.
*
* @param doc document to serialize
* @return the document as a string
* @throws TransformerException if an unrecoverable error occurs during the serialization
* @throws IOException if the encoding attempted to be used is not supported
*/
private static String serializeDocumentInt(Document doc) throws TransformerException, IOException {
ByteArrayOutputStream s = new ByteArrayOutputStream();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
//$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
//$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult outputTarget = new StreamResult(s);
transformer.transform(source, outputTarget);
//$NON-NLS-1$
return s.toString("UTF8");
}
use of javax.xml.transform.Transformer in project KeepScore by nolanlawson.
the class XmlHelper method prettyPrint.
public static String prettyPrint(String unformattedXml) {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
StreamSource source = new StreamSource(new StringReader(unformattedXml));
transformer.transform(source, result);
return result.getWriter().toString();
} catch (Throwable t) {
// should never happen
throw new RuntimeException(t);
}
}
use of javax.xml.transform.Transformer in project openhab1-addons by openhab.
the class Helper method nodeToString.
/***
* Converts a xml Node into String
*
* @param node to convert
* @return converted string
*/
public static String nodeToString(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 te) {
logger.warn("nodeToString Transformer Exception", te);
}
return sw.toString();
}
Aggregations