use of javax.xml.transform.Transformer in project jersey by jersey.
the class SourceEntityProviderTest method extractContent.
private static String extractContent(Source source) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
TransformerFactory transFactory = TransformerFactory.newInstance();
// identity transformation
Transformer transformer = transFactory.newTransformer();
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);
transformer.transform(source, result);
return writer.toString();
}
use of javax.xml.transform.Transformer in project AndroidAsync by koush.
the class DocumentBody method prepare.
private void prepare() {
if (bout != null)
return;
try {
DOMSource source = new DOMSource(document);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
bout = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(bout, Charsets.UTF_8);
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
writer.flush();
} catch (Exception e) {
}
}
use of javax.xml.transform.Transformer 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.Transformer 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.Transformer in project Mycat-Server by MyCATApache.
the class FirewallConfig method updateToFile.
public static synchronized void updateToFile(String host, List<UserConfig> userConfigs) throws Exception {
LOGGER.debug("set white host:" + host + "user:" + userConfigs);
String filename = SystemConfig.getHomePath() + File.separator + "conf" + File.separator + "server.xml";
//String filename = "E:\\MyProject\\Mycat-Server\\src\\main\\resources\\server.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new IgnoreDTDEntityResolver());
Document xmldoc = builder.parse(filename);
Element whitehost = (Element) xmldoc.getElementsByTagName("whitehost").item(0);
Element firewall = (Element) xmldoc.getElementsByTagName("firewall").item(0);
if (firewall == null) {
firewall = xmldoc.createElement("firewall");
Element root = xmldoc.getDocumentElement();
root.appendChild(firewall);
if (whitehost == null) {
whitehost = xmldoc.createElement("whitehost");
firewall.appendChild(whitehost);
}
}
for (UserConfig userConfig : userConfigs) {
String user = userConfig.getName();
Element hostEle = xmldoc.createElement("host");
hostEle.setAttribute("host", host);
hostEle.setAttribute("user", user);
whitehost.appendChild(hostEle);
}
TransformerFactory factory2 = TransformerFactory.newInstance();
Transformer former = factory2.newTransformer();
String systemId = xmldoc.getDoctype().getSystemId();
if (systemId != null) {
former.setOutputProperty(javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, systemId);
}
former.transform(new DOMSource(xmldoc), new StreamResult(new File(filename)));
}
Aggregations