KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > util > DomUtilities


1 package org.jahia.clipbuilder.html.util;
2
3 import java.io.*;
4
5 import javax.xml.parsers.*;
6 import javax.xml.transform.*;
7 import javax.xml.transform.dom.*;
8 import javax.xml.transform.stream.*;
9
10 import org.apache.xpath.*;
11 import org.w3c.dom.*;
12 import org.xml.sax.*;
13 import java.io.*;
14 // DOM
15
import org.w3c.dom.*;
16 // Xerces classes.
17
import org.apache.xml.serialize.*;
18
19 /**
20  * Utilities for Dom
21  *
22  *@author Tlili Khaled
23  */

24 public abstract class DomUtilities {
25     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DomUtilities.class);
26
27
28
29     /**
30      * Gets the NodeListByXPath attribute of the DomUtilities class
31      *
32      *@param doc Description of Parameter
33      *@param xPath Description of Parameter
34      *@return The NodeListByXPath value
35      */

36     public static NodeList getNodeListByXPath(Document doc, String JavaDoc xPath) {
37         logger.debug("[ Process XPath: " + xPath + " ]");
38         NodeList result = null;
39         if (doc == null) {
40             logger.error("Document is null, can't apply xpath");
41         }
42
43         try {
44             /*
45              * jdk 1.5
46              */

47             // XPathFactory fabrique = XPathFactory.newInstance();
48
// XPath xpath = fabrique.newXPath();
49
// XPathExpression exp = xpath.compile(xPath);
50
// result = (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
51
result = XPathAPI.selectNodeList(doc, xPath);
52             if (result == null || result.getLength() == 0) {
53                 logger.debug("[ Result set is empty for xPath " + xPath + " ]");
54             }
55             else {
56                 logger.debug("[ Result set not empty. ]");
57             }
58         }
59         catch (Exception JavaDoc ex) {
60             ex.printStackTrace();
61         }
62
63         return result;
64     }
65
66
67     /**
68      * Gets the DocumentAsString attribute of the DomUtilities class
69      *
70      *@param doc Description of Parameter
71      *@return The DocumentAsString value
72      */

73     public static String JavaDoc getDocumentAsString(Document doc) {
74         String JavaDoc encoding = "UTF-8";
75         return getDocumentAsString(doc, encoding);
76     }
77
78
79
80     /**
81      * Gets the Document attribute of the DomUtilities class
82      *
83      *@param doc Description of Parameter
84      *@return The SmallDocumentAsString value
85      */

86     public static String JavaDoc getSmallDocumentAsString(Document doc) {
87         // XERCES 1 or 2 additionnal classes.
88
java.io.StringWriter JavaDoc s = new java.io.StringWriter JavaDoc();
89         OutputFormat of = new OutputFormat("XML", "ISO-8859-1", true);
90         of.setIndent(1);
91         of.setIndenting(true);
92         XMLSerializer serializer = new XMLSerializer(s, of);
93         // As a DOM Serializer
94
try {
95             serializer.asDOMSerializer();
96             serializer.serialize(doc.getDocumentElement());
97         }
98         catch (IOException ex) {
99             ex.printStackTrace();
100         }
101         return s.toString();
102     }
103
104
105     /**
106      * Gets the DocumentAsString attribute of the DomUtilities class
107      *
108      *@param doc Description of Parameter
109      *@param encoding Description of Parameter
110      *@return The DocumentAsString value
111      */

112     public static String JavaDoc getDocumentAsString(Document doc, String JavaDoc encoding) {
113         java.io.StringWriter JavaDoc s = new java.io.StringWriter JavaDoc();
114
115         try {
116             // Create de la source DOM
117
Source source = new DOMSource(doc);
118
119             // Create output string
120
Result resultat = new StreamResult(s);
121
122             // Configuration of transformer
123
TransformerFactory factory = TransformerFactory.newInstance();
124             Transformer transformer = factory.newTransformer();
125             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
126             if (encoding != null) {
127                 transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
128             }
129             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
130
131             // Transformation
132
transformer.transform(source, resultat);
133             s.flush();
134             logger.debug("[ Result: " + s + " ]");
135             String JavaDoc res = s.toString();
136             return res;
137         }
138         catch (Exception JavaDoc e) {
139             logger.warn("Change wrting method");
140             //e.printStackTrace();
141
return getSmallDocumentAsString(doc);
142         }
143     }
144
145
146
147     /**
148      * Gets the Document attribute of the DomUtilities class
149      *
150      *@param xmlContent Description of Parameter
151      *@return The Document value
152      */

153     public static Document getDocument(String JavaDoc xmlContent) {
154         Document document = null;
155         try {
156             // création d'une fabrique de documents
157
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
158
159             // création d'un constructeur de documents
160
DocumentBuilder builder = factory.newDocumentBuilder();
161
162             // Read the string input
163
java.io.InputStream JavaDoc in = new java.io.StringBufferInputStream JavaDoc(xmlContent);
164             document = builder.parse(new InputSource(in));
165
166         }
167         catch (ParserConfigurationException pce) {
168             logger.error("Erreur de configuration du parseur DOM");
169             logger.error("lors de l'appel à fabrique.newDocumentBuilder();");
170         }
171         catch (SAXException se) {
172             logger.error("Erreur lors du parsing du document");
173             logger.error("lors de l'appel à construteur.parse(xml)");
174         }
175         catch (IOException ioe) {
176             logger.error("Erreur d'entrée/sortie");
177             logger.error("lors de l'appel à construteur.parse(xml)");
178         }
179         finally {
180             return document;
181         }
182
183     }
184
185
186     /**
187      * Description of the Method
188      *
189      *@param doc Description of Parameter
190      *@return Description of the Returned Value
191      */

192     public static Document copy(Document doc) {
193         try {
194             Document copy = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
195             try {
196                 // Create de la source DOM
197
Source source = new DOMSource(doc);
198                 // Create output string
199
Result resultat = new DOMResult(copy);
200
201                 // Configuration of transformer
202
TransformerFactory factory = TransformerFactory.newInstance();
203                 Transformer transformer = factory.newTransformer();
204
205                 // Transformation
206
transformer.transform(source, resultat);
207             }
208             catch (Exception JavaDoc e) {
209                 logger.warn("Change writing method");
210                 //e.printStackTrace();
211
}
212
213             return copy;
214         }
215         catch (Exception JavaDoc ex) {
216             logger.error("Error occured");
217             return null;
218         }
219
220     }
221
222
223
224     /**
225      * Remove node
226      *
227      *@param n reference of the node
228      */

229     public static void removeNode(Node n) {
230         Node parent = n.getParentNode();
231         if (parent != null) {
232             parent.removeChild(n);
233         }
234
235     }
236
237 }
238
Popular Tags