View Javadoc
1   package net.logAnalyzer.utils;
2   
3   import java.io.IOException;
4   import java.io.StringWriter;
5   
6   import javax.xml.transform.OutputKeys;
7   import javax.xml.transform.Transformer;
8   import javax.xml.transform.TransformerConfigurationException;
9   import javax.xml.transform.TransformerException;
10  import javax.xml.transform.TransformerFactory;
11  import javax.xml.transform.dom.DOMSource;
12  import javax.xml.transform.stream.StreamResult;
13  
14  
15  import net.logAnalyzer.LogAnalyzerBatch;
16  
17  import org.apache.log4j.Logger;
18  import org.w3c.dom.Attr;
19  import org.w3c.dom.Document;
20  import org.w3c.dom.Element;
21  
22  /***
23   * This class provides utilities methods manipulating XML documents.
24   * 
25   * @author Karim REFEYTON
26   * @version 1.0
27   */
28  public class XMLUtils {
29  
30      /***
31       * Class logger.
32       */
33      private static Logger logger = Logger.getLogger(XMLUtils.class
34              .getName());
35      /***
36       * Creation forbidden...
37       */
38      private XMLUtils() {
39          super();
40      }
41  
42      /***
43       * Adds an element to the root node.
44       * 
45       * @param name
46       *            Element name.
47       * @return New element.
48       */
49      public static Element addElement(Document document, String name) {
50          return addElement(document, document.getDocumentElement(), name);
51      }
52  
53      /***
54       * Adds an element to the specified parent node.
55       * 
56       * @param parent
57       *            Parent element.
58       * @param name
59       *            Element name.
60       * @return New element.
61       */
62      public static Element addElement(Document document, Element parent,
63              String name) {
64          return addElement(document, parent, name, null, false);
65      }
66  
67      /***
68       * Adds an element to the root node.
69       * 
70       * @param name
71       *            Element name.
72       * @param value
73       *            Element value.
74       * @param inCDATA
75       *            If <tt>true</tt>, the key is embraced in a CDATA.
76       * @return New element.
77       */
78      public static Element addElement(Document document, String name,
79              String value, boolean inCDATA) {
80          return addElement(document, document.getDocumentElement(), name, value,
81                  inCDATA);
82      }
83  
84      /***
85       * Adds an element to the specified parent node.
86       * 
87       * @param parent
88       *            Parent element.
89       * @param name
90       *            Element name.
91       * @param value
92       *            Element value.
93       * @param inCDATA
94       *            If <tt>true</tt>, the key is embraced in a CDATA.
95       * @return New element.
96       */
97      public static Element addElement(Document document, Element parent,
98              String name, String value, boolean inCDATA) {
99          // Creates the new element
100         Element newElement = document.createElement(name);
101         if (value != null) {
102             // Adds value to the new element
103             if (inCDATA) {
104                 newElement.appendChild(document.createCDATASection(value));
105             } else {
106                 newElement.appendChild(document.createTextNode(value));
107             }
108         }
109         // Adds the new element to the parent as child
110         parent.appendChild(newElement);
111         return newElement;
112     }
113 
114     /***
115      * Adds an attribute to the specified node.
116      * 
117      * @param node
118      *            Node element.
119      * @param name
120      *            Attribute name.
121      * @param value
122      *            Attribute value.
123      * @return New attribute.
124      */
125     public static Attr addAttribute(Document document, Element node,
126             String name, String value) {
127         // Creates the new element
128         Attr newAttribute = document.createAttribute(name);
129         // Adds value to the new element
130         newAttribute.setValue(value);
131         // Adds the new element to the parent as child
132         node.setAttributeNode(newAttribute);
133         return newAttribute;
134     }
135 
136     /***
137      * Returns a string description of an XML document.
138      * 
139      * @param document
140      *            XML document to serialize in a string.
141      * @return XML Document in string format.
142      * @throws IOException
143      *             If parser configuration is invalid.
144      */
145     public static String toString(Document document) throws IOException {
146         StringWriter writer = new StringWriter();
147         /*OutputFormat format = new OutputFormat();
148         format.setIndent(0);
149         format.setIndenting(true);
150         format.setEncoding("iso-8859-1");
151         format.setPreserveSpace(true);
152         XMLSerializer serializer = new XMLSerializer(writer, format);
153         serializer.serialize(document);*/
154         DOMSource domSource = new DOMSource(document);
155         StreamResult streamResult = new StreamResult(writer);
156         TransformerFactory tf = TransformerFactory.newInstance();
157         Transformer serializer;
158         try {
159             serializer = tf.newTransformer();
160             serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
161             serializer.setOutputProperty(OutputKeys.INDENT,"yes");
162             serializer.transform(domSource, streamResult); 
163         } catch (TransformerConfigurationException tce) {
164             tce.printStackTrace(System.err);
165             logger.debug("XML serialize KO : ", tce);
166         } catch (TransformerException te) {
167             te.printStackTrace(System.err);
168             logger.debug("XML serialize KO : ", te);
169         }
170         return writer.getBuffer().toString();
171     }
172 }