use of javax.xml.transform.TransformerFactory 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)));
}
use of javax.xml.transform.TransformerFactory in project OpenAttestation by OpenAttestation.
the class ConverterUtil method formateXMLString.
public static String formateXMLString(String inputXML) {
StreamResult xmlOutput = null;
try {
Source xmlInput = new StreamSource(new StringReader(inputXML));
StringWriter stringWriter = new StringWriter();
xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
} catch (Exception e) {
// simple exception handling, please review it
throw new RuntimeException(e);
}
return xmlOutput.getWriter().toString();
}
use of javax.xml.transform.TransformerFactory 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.TransformerFactory 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.TransformerFactory in project android-selector-intellij-plugin by importre.
the class AndroidSelectorDialog method createDrawableV21.
private void createDrawableV21(String filename, String color, String pressed) throws Exception {
VirtualFile child = dir.findChild(drawableV21Dir);
if (child == null) {
child = dir.createChildDirectory(null, drawableV21Dir);
}
VirtualFile newXmlFile = child.findChild(filename);
if (newXmlFile != null && newXmlFile.exists()) {
newXmlFile.delete(null);
}
newXmlFile = child.createChildData(null, filename);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element root = doc.createElement("ripple");
root.setAttributeNS(nsUri, "xmlns:android", androidUri);
root.setAttribute("android:color", pressed);
doc.appendChild(root);
Element item = doc.createElement("item");
item.setAttribute("android:drawable", color);
root.appendChild(item);
OutputStream os = newXmlFile.getOutputStream(null);
PrintWriter out = new PrintWriter(os);
StringWriter writer = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(INDENT_SPACE, "4");
transformer.transform(new DOMSource(doc), new StreamResult(writer));
out.println(writer.getBuffer().toString());
out.close();
}
Aggregations