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 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.TransformerFactory 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.TransformerFactory in project goci by EBISPOT.
the class ChromosomeRenderlet method render.
public void render(RenderletNexus nexus, C context, E entity) {
int position = getPosition();
int height = SVGCanvas.canvasHeight;
int width = SVGCanvas.canvasWidth;
double chromWidth = (double) width / 12;
double chromHeight = (double) height / 2;
double xCoordinate;
double yCoordinate = 0;
if (position < 12) {
xCoordinate = position * chromWidth;
} else {
xCoordinate = (position - 12) * chromWidth;
yCoordinate = (double) height / 2;
}
InputStream in = null;
try {
in = getSVGFile().openStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document chromSVG = db.parse(in);
Element root = chromSVG.getDocumentElement();
Element g = (Element) root.getElementsByTagName("g").item(0).cloneNode(true);
setChromBands(g, nexus);
StringBuilder builder = new StringBuilder();
builder.append("translate(");
builder.append(Double.toString(xCoordinate));
builder.append(",");
builder.append(Double.toString(yCoordinate));
builder.append(")");
g.setAttribute("transform", builder.toString());
g.setAttribute("gwasname", getName());
g.removeAttribute("title");
SVGArea currentArea = new SVGArea(xCoordinate, yCoordinate, chromWidth, chromHeight, 0);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(g), new StreamResult(buffer));
String str = buffer.toString();
RenderingEvent<E> event = new RenderingEvent<E>(entity, str, currentArea, this);
nexus.renderingEventOccurred(event);
} catch (ParserConfigurationException e) {
throw new RuntimeException("Failed to read in template chromosome SVG from original resource", e);
} catch (SAXException e) {
throw new RuntimeException("Failed to read in template chromosome SVG from original resource", e);
} catch (IOException e) {
throw new RuntimeException("Failed to render chromosome SVG", e);
} catch (TransformerConfigurationException e) {
throw new RuntimeException("Failed to render chromosome SVG", e);
} catch (TransformerException e) {
throw new RuntimeException("Failed to render chromosome SVG", e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// tried our best!
}
}
}
}
use of javax.xml.transform.TransformerFactory in project OpenAM by OpenRock.
the class ValidationErrorHandler method print.
/**
* Prints a Node tree recursively.
* @param node A DOM tree Node
* @param encoding character encoding
* @return An xml String representation of the DOM tree.
*/
public static String print(Node node, String encoding) {
if (node == null) {
return null;
}
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty("omit-xml-declaration", "yes");
transformer.setOutputProperty("encoding", encoding);
DOMSource source = new DOMSource(node);
ByteArrayOutputStream os = new ByteArrayOutputStream(2000);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
return os.toString(encoding);
} catch (Exception e) {
return null;
}
}
Aggregations