1 23 24 package org.infoglue.cms.util.dom; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.File ; 29 import java.io.FileWriter ; 30 import java.io.IOException ; 31 import java.io.StringWriter ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import org.apache.log4j.Logger; 36 import org.apache.xerces.parsers.DOMParser; 37 import org.dom4j.Document; 38 import org.dom4j.DocumentHelper; 39 import org.dom4j.Element; 40 import org.dom4j.Node; 41 import org.dom4j.io.DOMReader; 42 import org.dom4j.io.OutputFormat; 43 import org.dom4j.io.XMLWriter; 44 import org.infoglue.cms.exception.SystemException; 45 import org.infoglue.cms.io.FileHelper; 46 import org.xml.sax.InputSource ; 47 48 53 54 public class DOMBuilder 55 { 56 private final static Logger logger = Logger.getLogger(DOMBuilder.class.getName()); 57 58 61 62 public Document createDocument() 63 { 64 return DocumentHelper.createDocument(); 65 } 66 67 70 71 public Document getDocument(String xml) throws Exception 72 { 73 if(xml == null) 74 return null; 75 76 try 77 { 78 InputSource xmlSource = new InputSource (new ByteArrayInputStream (xml.getBytes("UTF-8"))); 79 DOMParser parser = new DOMParser(); 80 parser.parse(xmlSource); 81 return buildDocment(parser.getDocument()); 82 } 83 catch(Exception e) 84 { 85 e.printStackTrace(); 86 throw new SystemException(e); 87 } 88 } 89 90 91 92 public Document buildDocment(org.w3c.dom.Document domDocument) 93 { 94 DOMReader xmlReader = new DOMReader(); 95 return xmlReader.read(domDocument); 96 } 97 98 99 102 103 public Document getDocument(File file) throws Exception 104 { 105 if(file == null) 106 return null; 107 108 Document document = null; 109 110 try 111 { 112 String xml = FileHelper.readUTF8FromFile(file); 113 document = getDocument(xml); 114 115 125 } 128 catch(Exception e) 129 { 130 e.printStackTrace(); 131 } 132 133 return document; 134 } 135 136 139 140 public Node selectSingleNode(Node contextNode, String xpathExpression, String namespaceName, String namespaceValue) 141 { 142 Map namespaces = new HashMap (); 143 namespaces.put(namespaceName, namespaceValue); 144 org.dom4j.XPath xpath = contextNode.createXPath( xpathExpression ); 145 xpath.setNamespaceURIs(namespaces); 146 return xpath.selectSingleNode(contextNode); 147 } 148 149 150 153 154 public Element addElement(Document document, String elementName) 155 { 156 Element newElement = document.addElement(elementName); 157 return newElement; 158 } 159 160 163 164 public Element addElement(Element element, String elementName) 165 { 166 Element newElement = element.addElement(elementName); 167 return newElement; 168 } 169 170 173 174 public void insertElement(Element element, Element childElement) 175 { 176 element.content().add(childElement); 177 } 178 179 182 183 public Element addTextElement(Element element, String text) 184 { 185 Element newElement = element.addText(text); 186 return newElement; 187 } 188 189 190 193 194 public Element addCDATAElement(Element element, String text) 195 { 196 Element newElement = element.addCDATA(text); 197 return newElement; 198 } 199 200 203 204 public void addAttribute(Element element, String attributeName, String attributeValue) 205 { 206 element.addAttribute(attributeName, attributeValue); 207 } 208 209 212 213 public void write(Document document, String fileName) throws Exception 214 { 215 OutputFormat format = OutputFormat.createCompactFormat(); 216 format.setEncoding("UTF-8"); 217 XMLWriter writer = new XMLWriter(new FileWriter (fileName), format); 218 writer.write(document); 219 writer.close(); 220 221 227 } 228 229 232 233 public void writePretty(Document document, String fileName) throws IOException 234 { 235 OutputFormat format = OutputFormat.createPrettyPrint(); 236 XMLWriter writer = new XMLWriter(new FileWriter (fileName), format); 237 writer.write(document); 238 writer.close(); 239 } 240 241 244 245 public void writeDebug(Document document) throws Exception 246 { 247 OutputFormat format = OutputFormat.createPrettyPrint(); 248 XMLWriter writer = new XMLWriter( System.out, format ); 249 writer.write( document ); 250 } 251 252 public String encodeString(String value, String encoding) throws Exception 253 { 254 String encodedValue = new String (value.getBytes("UTF-8"), "ISO-8859-1"); 255 return encodedValue; 256 } 257 258 261 262 public void writeDebug(Element element) throws Exception 263 { 264 OutputFormat format = OutputFormat.createPrettyPrint(); 265 XMLWriter writer = new XMLWriter( System.out, format ); 266 writer.write( element ); 267 } 268 269 272 273 private String getEncodedString(Element element) throws Exception 274 { 275 OutputFormat outFormat = OutputFormat.createCompactFormat(); 276 outFormat.setEncoding("UTF-8"); 277 ByteArrayOutputStream bao = new ByteArrayOutputStream (); 278 XMLWriter out = new XMLWriter(bao, outFormat); 279 out.write(element); 280 out.flush(); 281 String s = bao.toString(); 282 logger.info("OUT: " + s); 283 return s; 284 } 285 286 public String getFormattedDocument(Document doc, String encoding) 287 { 288 return getFormattedDocument(doc, true, false, encoding); 289 } 290 291 public String getFormattedDocument(Document doc, boolean compact, boolean supressDecl, String encoding) 292 { 293 OutputFormat format = compact ? OutputFormat.createCompactFormat() : OutputFormat.createPrettyPrint(); 294 format.setSuppressDeclaration(supressDecl); 295 format.setEncoding(encoding); 296 format.setExpandEmptyElements(false); 297 StringWriter stringWriter = new StringWriter (); 298 XMLWriter writer = new XMLWriter(stringWriter, format); 299 try 300 { 301 writer.write(doc); 302 } 303 catch (IOException e) 304 { 305 e.printStackTrace(); 306 } 307 return stringWriter.toString(); 308 } 309 310 } | Popular Tags |