1 7 8 package org.dom4j; 9 10 import java.io.StringReader ; 11 import java.util.List ; 12 import java.util.Map ; 13 import java.util.StringTokenizer ; 14 15 import org.dom4j.io.SAXReader; 16 import org.dom4j.rule.Pattern; 17 18 import org.jaxen.VariableContext; 19 20 import org.xml.sax.InputSource ; 21 22 31 public final class DocumentHelper { 32 private DocumentHelper() { 33 } 34 35 private static DocumentFactory getDocumentFactory() { 36 return DocumentFactory.getInstance(); 37 } 38 39 public static Document createDocument() { 41 return getDocumentFactory().createDocument(); 42 } 43 44 public static Document createDocument(Element rootElement) { 45 return getDocumentFactory().createDocument(rootElement); 46 } 47 48 public static Element createElement(QName qname) { 49 return getDocumentFactory().createElement(qname); 50 } 51 52 public static Element createElement(String name) { 53 return getDocumentFactory().createElement(name); 54 } 55 56 public static Attribute createAttribute(Element owner, QName qname, 57 String value) { 58 return getDocumentFactory().createAttribute(owner, qname, value); 59 } 60 61 public static Attribute createAttribute(Element owner, String name, 62 String value) { 63 return getDocumentFactory().createAttribute(owner, name, value); 64 } 65 66 public static CDATA createCDATA(String text) { 67 return DocumentFactory.getInstance().createCDATA(text); 68 } 69 70 public static Comment createComment(String text) { 71 return DocumentFactory.getInstance().createComment(text); 72 } 73 74 public static Text createText(String text) { 75 return DocumentFactory.getInstance().createText(text); 76 } 77 78 public static Entity createEntity(String name, String text) { 79 return DocumentFactory.getInstance().createEntity(name, text); 80 } 81 82 public static Namespace createNamespace(String prefix, String uri) { 83 return DocumentFactory.getInstance().createNamespace(prefix, uri); 84 } 85 86 public static ProcessingInstruction createProcessingInstruction(String pi, 87 String d) { 88 return getDocumentFactory().createProcessingInstruction(pi, d); 89 } 90 91 public static ProcessingInstruction createProcessingInstruction(String pi, 92 Map data) { 93 return getDocumentFactory().createProcessingInstruction(pi, data); 94 } 95 96 public static QName createQName(String localName, Namespace namespace) { 97 return getDocumentFactory().createQName(localName, namespace); 98 } 99 100 public static QName createQName(String localName) { 101 return getDocumentFactory().createQName(localName); 102 } 103 104 119 public static XPath createXPath(String xpathExpression) 120 throws InvalidXPathException { 121 return getDocumentFactory().createXPath(xpathExpression); 122 } 123 124 141 public static XPath createXPath(String xpathExpression, 142 VariableContext context) throws InvalidXPathException { 143 return getDocumentFactory().createXPath(xpathExpression, context); 144 } 145 146 159 public static NodeFilter createXPathFilter(String xpathFilterExpression) { 160 return getDocumentFactory().createXPathFilter(xpathFilterExpression); 161 } 162 163 175 public static Pattern createPattern(String xpathPattern) { 176 return getDocumentFactory().createPattern(xpathPattern); 177 } 178 179 193 public static List selectNodes(String xpathFilterExpression, List nodes) { 194 XPath xpath = createXPath(xpathFilterExpression); 195 196 return xpath.selectNodes(nodes); 197 } 198 199 213 public static List selectNodes(String xpathFilterExpression, Node node) { 214 XPath xpath = createXPath(xpathFilterExpression); 215 216 return xpath.selectNodes(node); 217 } 218 219 230 public static void sort(List list, String xpathExpression) { 231 XPath xpath = createXPath(xpathExpression); 232 xpath.sort(list); 233 } 234 235 250 public static void sort(List list, String expression, boolean distinct) { 251 XPath xpath = createXPath(expression); 252 xpath.sort(list, distinct); 253 } 254 255 269 public static Document parseText(String text) throws DocumentException { 270 Document result = null; 271 272 SAXReader reader = new SAXReader(); 273 String encoding = getEncoding(text); 274 275 InputSource source = new InputSource (new StringReader (text)); 276 source.setEncoding(encoding); 277 278 result = reader.read(source); 279 280 if (result.getXMLEncoding() == null) { 283 result.setXMLEncoding(encoding); 284 } 285 286 return result; 287 } 288 289 private static String getEncoding(String text) { 290 String result = null; 291 292 String xml = text.trim(); 293 294 if (xml.startsWith("<?xml")) { 295 int end = xml.indexOf("?>"); 296 String sub = xml.substring(0, end); 297 StringTokenizer tokens = new StringTokenizer (sub, " =\"\'"); 298 299 while (tokens.hasMoreTokens()) { 300 String token = tokens.nextToken(); 301 302 if ("encoding".equals(token)) { 303 if (tokens.hasMoreTokens()) { 304 result = tokens.nextToken(); 305 } 306 307 break; 308 } 309 } 310 } 311 312 return result; 313 } 314 315 336 public static Element makeElement(Branch source, String path) { 337 StringTokenizer tokens = new StringTokenizer (path, "/"); 338 Element parent; 339 340 if (source instanceof Document) { 341 Document document = (Document) source; 342 parent = document.getRootElement(); 343 344 String name = tokens.nextToken(); 347 348 if (parent == null) { 349 parent = document.addElement(name); 350 } 351 } else { 352 parent = (Element) source; 353 } 354 355 Element element = null; 356 357 while (tokens.hasMoreTokens()) { 358 String name = tokens.nextToken(); 359 360 if (name.indexOf(':') > 0) { 361 element = parent.element(parent.getQName(name)); 362 } else { 363 element = parent.element(name); 364 } 365 366 if (element == null) { 367 element = parent.addElement(name); 368 } 369 370 parent = element; 371 } 372 373 return element; 374 } 375 } 376 377 413 | Popular Tags |