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 import org.w3c.dom.*; 16 import org.apache.xml.serialize.*; 18 19 24 public abstract class DomUtilities { 25 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DomUtilities.class); 26 27 28 29 36 public static NodeList getNodeListByXPath(Document doc, String 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 47 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 ex) { 60 ex.printStackTrace(); 61 } 62 63 return result; 64 } 65 66 67 73 public static String getDocumentAsString(Document doc) { 74 String encoding = "UTF-8"; 75 return getDocumentAsString(doc, encoding); 76 } 77 78 79 80 86 public static String getSmallDocumentAsString(Document doc) { 87 java.io.StringWriter s = new java.io.StringWriter (); 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 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 112 public static String getDocumentAsString(Document doc, String encoding) { 113 java.io.StringWriter s = new java.io.StringWriter (); 114 115 try { 116 Source source = new DOMSource(doc); 118 119 Result resultat = new StreamResult(s); 121 122 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 transformer.transform(source, resultat); 133 s.flush(); 134 logger.debug("[ Result: " + s + " ]"); 135 String res = s.toString(); 136 return res; 137 } 138 catch (Exception e) { 139 logger.warn("Change wrting method"); 140 return getSmallDocumentAsString(doc); 142 } 143 } 144 145 146 147 153 public static Document getDocument(String xmlContent) { 154 Document document = null; 155 try { 156 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 158 159 DocumentBuilder builder = factory.newDocumentBuilder(); 161 162 java.io.InputStream in = new java.io.StringBufferInputStream (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 192 public static Document copy(Document doc) { 193 try { 194 Document copy = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 195 try { 196 Source source = new DOMSource(doc); 198 Result resultat = new DOMResult(copy); 200 201 TransformerFactory factory = TransformerFactory.newInstance(); 203 Transformer transformer = factory.newTransformer(); 204 205 transformer.transform(source, resultat); 207 } 208 catch (Exception e) { 209 logger.warn("Change writing method"); 210 } 212 213 return copy; 214 } 215 catch (Exception ex) { 216 logger.error("Error occured"); 217 return null; 218 } 219 220 } 221 222 223 224 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 |