1 package org.splat.manox;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012-2014
9 import java.io.FileNotFoundException;
10 import java.io.FileOutputStream;
11 import java.util.HashMap;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.transform.Transformer;
16 import javax.xml.transform.TransformerConfigurationException;
17 import javax.xml.transform.TransformerException;
18 import javax.xml.transform.TransformerFactory;
19 import javax.xml.transform.dom.DOMSource;
20 import javax.xml.transform.stream.StreamResult;
22 import org.apache.log4j.Logger;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
30 private final static Logger LOGGER = Logger.getLogger(XDOM.class);
32 public static org.w3c.dom.Document createDocument (final String name) {
33 // ---------------------------------------------------------------
34 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
36 DocumentBuilder dbuilder = dfactory.newDocumentBuilder();
37 org.w3c.dom.Document document = dbuilder.newDocument();
39 Element root = document.createElement(name);
40 document.appendChild(root);
44 catch (Exception error) {
45 LOGGER.error("Could not create document " + name + ", reason:", error);
50 public static Node getChildNode (final String name, final Node from) {
51 // --------------------------------------------------------
52 NodeList nlist = from.getChildNodes();
53 for (int i=0; i<nlist.getLength(); i++) {
54 Node cnode = nlist.item(i);
55 if (cnode.getNodeName().equals(name)) {
62 public static HashMap<String,Node> getNamedChildNodes (Node node) {
63 // -----------------------------------------------------------------
64 HashMap<String,Node> result = new HashMap<String,Node>();
65 NodeList nlist = node.getChildNodes();
66 for (int i=0; i<nlist.getLength(); i++) {
68 String name = node.getNodeName();
69 if (name.startsWith("#")) {
72 result.put(name, node);
77 public static boolean saveDocument (final File xmlOutputFile, final org.w3c.dom.Document doc) {
78 // ---------------------------------------------------------------------------------
79 // Open output stream where XML Document will be saved
80 // File xmlOutputFile = new File(fileName);
82 Transformer transformer;
84 fos = new FileOutputStream(xmlOutputFile);
86 catch (FileNotFoundException error) {
87 LOGGER.error("Could not open the " + xmlOutputFile.getName() + " file, reason:", error);
90 // Use a Transformer for output
91 TransformerFactory transformerFactory = TransformerFactory.newInstance();
93 transformer = transformerFactory.newTransformer();
95 catch (TransformerConfigurationException error) {
96 LOGGER.error("Transformer configuration error, reason:", error);
99 DOMSource source = new DOMSource(doc);
100 StreamResult result = new StreamResult(fos);
101 // Transform source into result will do save
103 transformer.transform(source, result);
105 catch (TransformerException error) {
106 LOGGER.error("Error during XML transformation, reason:", error);