use of javax.xml.transform.stream.StreamResult in project jOOQ by jOOQ.
the class XMLDatabase method info.
private InformationSchema info() {
if (info == null) {
String xml = getProperties().getProperty(P_XML_FILE);
String xsl = getProperties().getProperty(P_XSL_FILE);
InputStream xmlIs = null;
InputStream xslIs = null;
log.info("Using XML file", xml);
try {
xmlIs = XMLDatabase.class.getResourceAsStream(xml);
if (xmlIs == null)
xmlIs = new FileInputStream(xml);
if (StringUtils.isBlank(xsl)) {
info = JAXB.unmarshal(new File(xml), InformationSchema.class);
} else {
log.info("Using XSL file", xsl);
xslIs = XMLDatabase.class.getResourceAsStream(xsl);
if (xslIs == null)
xslIs = new FileInputStream(xsl);
try {
StringWriter writer = new StringWriter();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslIs));
transformer.transform(new StreamSource(xmlIs), new StreamResult(writer));
info = JAXB.unmarshal(new StringReader(writer.getBuffer().toString()), InformationSchema.class);
} catch (TransformerException e) {
throw new RuntimeException("Error while transforming XML file " + xml + " with XSL file " + xsl, e);
}
}
} catch (IOException e) {
throw new RuntimeException("Error while opening files " + xml + " or " + xsl, e);
} finally {
if (xmlIs != null) {
try {
xmlIs.close();
} catch (Exception ignore) {
}
}
if (xslIs != null) {
try {
xslIs.close();
} catch (Exception ignore) {
}
}
}
}
return info;
}
use of javax.xml.transform.stream.StreamResult in project hadoop by apache.
the class TestQueueConfigurationParser method testQueueConfigurationParser.
/**
* test xml generation
* @throws ParserConfigurationException
* @throws Exception
*/
@Test(timeout = 5000)
public void testQueueConfigurationParser() throws ParserConfigurationException, Exception {
JobQueueInfo info = new JobQueueInfo("root", "rootInfo");
JobQueueInfo infoChild1 = new JobQueueInfo("child1", "child1Info");
JobQueueInfo infoChild2 = new JobQueueInfo("child2", "child1Info");
info.addChild(infoChild1);
info.addChild(infoChild2);
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
Document document = builder.newDocument();
// test QueueConfigurationParser.getQueueElement
Element e = QueueConfigurationParser.getQueueElement(document, info);
// transform result to string for check
DOMSource domSource = new DOMSource(e);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
String str = writer.toString();
assertTrue(str.endsWith("<queue><name>root</name><properties/><state>running</state><queue><name>child1</name><properties/><state>running</state></queue><queue><name>child2</name><properties/><state>running</state></queue></queue>"));
}
use of javax.xml.transform.stream.StreamResult 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.stream.StreamResult 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.stream.StreamResult in project jphp by jphp-compiler.
the class Debugger method write.
protected void write(CommandArguments args, AbstractCommand command) {
try {
Document document = xmlBuilder.newDocument();
command.run(this, args, document);
StringWriter xmlWriter = new StringWriter();
StreamResult xmlResult = new StreamResult(xmlWriter);
transformer.transform(new DOMSource(document), xmlResult);
byte[] xmlBytes = xmlWriter.toString().getBytes();
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
out.write(String.valueOf(xmlBytes.length).getBytes());
out.write("\0".getBytes());
out.write(xmlBytes);
out.write("\0".getBytes());
out.flush();
} catch (IOException | TransformerException e) {
throw new DebuggerException(e);
}
}
Aggregations