]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/SPlat/src/org/splat/manox/XDOM.java
Salome HOME
3c2eea1d242c8800ae1ee008ba3f59422d5078ce
[tools/siman.git] / Workspace / SPlat / src / org / splat / manox / XDOM.java
1 package org.splat.manox;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.io.File;
9 import java.io.FileNotFoundException;
10 import java.io.FileOutputStream;
11 import java.util.HashMap;
12
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;
21
22 import org.apache.log4j.Logger;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
26
27
28 public class XDOM {
29
30     private final static  Logger  LOGGER = Logger.getLogger(XDOM.class);
31
32     public static org.w3c.dom.Document createDocument (final String name) {
33 //  ---------------------------------------------------------------
34       DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
35       try {
36         DocumentBuilder      dbuilder = dfactory.newDocumentBuilder();
37         org.w3c.dom.Document document = dbuilder.newDocument();
38             
39         Element root = document.createElement(name); 
40         document.appendChild(root);
41
42         return document;
43       }
44       catch (Exception error) {
45         LOGGER.error("Could not create document " + name + ", reason:", error);
46         return null;
47       }
48         }
49
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)) {
56                         return cnode;
57                 }
58       }
59       return null;
60     }
61
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++) {
67         node = nlist.item(i);
68         String name = node.getNodeName();
69         if (name.startsWith("#")) {
70                         continue;
71                 }
72         result.put(name, node);
73       }
74       return result;
75         }
76         
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);
81       FileOutputStream  fos;
82       Transformer       transformer;
83       try {
84         fos = new FileOutputStream(xmlOutputFile);
85       }
86       catch (FileNotFoundException error) {
87         LOGGER.error("Could not open the " + xmlOutputFile.getName() + " file, reason:", error);
88         return false;
89       }
90 //    Use a Transformer for output
91       TransformerFactory transformerFactory = TransformerFactory.newInstance();
92       try {
93         transformer = transformerFactory.newTransformer();
94       }
95       catch (TransformerConfigurationException error) {
96         LOGGER.error("Transformer configuration error, reason:", error);
97         return false;
98       }
99       DOMSource    source = new DOMSource(doc);
100       StreamResult result = new StreamResult(fos);
101 //    Transform source into result will do save
102       try {
103         transformer.transform(source, result);
104       }
105       catch (TransformerException error) {
106         LOGGER.error("Error during XML transformation, reason:", error);
107       }
108       return true;
109     }
110 }