| 1 |  |  | 
| 2 |  |  | 
| 3 |  |  | 
| 4 |  |  | 
| 5 |  |  | 
| 6 |  |  | 
| 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 |  |  | 
| 23 |  |  | 
| 24 |  |  | 
| 25 |  |  | 
| 26 |  |  | 
| 27 |  |  | 
| 28 |  |  | 
| 29 |  |  | 
| 30 |  |  | 
| 31 |  | public final class DocumentHelper { | 
| 32 | 0 | private DocumentHelper() { | 
| 33 |  | } | 
| 34 |  |  | 
| 35 | 12496 | private static DocumentFactory getDocumentFactory() { | 
| 36 | 12496 | return DocumentFactory.getInstance(); | 
| 37 |  | } | 
| 38 |  |  | 
| 39 |  |  | 
| 40 | 412 | public static Document createDocument() { | 
| 41 | 412 | return getDocumentFactory().createDocument(); | 
| 42 |  | } | 
| 43 |  |  | 
| 44 | 2 | public static Document createDocument(Element rootElement) { | 
| 45 | 2 | return getDocumentFactory().createDocument(rootElement); | 
| 46 |  | } | 
| 47 |  |  | 
| 48 | 2000 | public static Element createElement(QName qname) { | 
| 49 | 2000 | return getDocumentFactory().createElement(qname); | 
| 50 |  | } | 
| 51 |  |  | 
| 52 | 4 | public static Element createElement(String name) { | 
| 53 | 4 | return getDocumentFactory().createElement(name); | 
| 54 |  | } | 
| 55 |  |  | 
| 56 | 0 | public static Attribute createAttribute(Element owner, QName qname, | 
| 57 |  | String value) { | 
| 58 | 0 | return getDocumentFactory().createAttribute(owner, qname, value); | 
| 59 |  | } | 
| 60 |  |  | 
| 61 | 0 | public static Attribute createAttribute(Element owner, String name, | 
| 62 |  | String value) { | 
| 63 | 0 | return getDocumentFactory().createAttribute(owner, name, value); | 
| 64 |  | } | 
| 65 |  |  | 
| 66 | 0 | public static CDATA createCDATA(String text) { | 
| 67 | 0 | return DocumentFactory.getInstance().createCDATA(text); | 
| 68 |  | } | 
| 69 |  |  | 
| 70 | 0 | public static Comment createComment(String text) { | 
| 71 | 0 | return DocumentFactory.getInstance().createComment(text); | 
| 72 |  | } | 
| 73 |  |  | 
| 74 | 0 | public static Text createText(String text) { | 
| 75 | 0 | return DocumentFactory.getInstance().createText(text); | 
| 76 |  | } | 
| 77 |  |  | 
| 78 | 0 | public static Entity createEntity(String name, String text) { | 
| 79 | 0 | return DocumentFactory.getInstance().createEntity(name, text); | 
| 80 |  | } | 
| 81 |  |  | 
| 82 | 0 | public static Namespace createNamespace(String prefix, String uri) { | 
| 83 | 0 | return DocumentFactory.getInstance().createNamespace(prefix, uri); | 
| 84 |  | } | 
| 85 |  |  | 
| 86 | 2 | public static ProcessingInstruction createProcessingInstruction(String pi, | 
| 87 |  | String d) { | 
| 88 | 2 | return getDocumentFactory().createProcessingInstruction(pi, d); | 
| 89 |  | } | 
| 90 |  |  | 
| 91 | 0 | public static ProcessingInstruction createProcessingInstruction(String pi, | 
| 92 |  | Map data) { | 
| 93 | 0 | return getDocumentFactory().createProcessingInstruction(pi, data); | 
| 94 |  | } | 
| 95 |  |  | 
| 96 | 0 | public static QName createQName(String localName, Namespace namespace) { | 
| 97 | 0 | return getDocumentFactory().createQName(localName, namespace); | 
| 98 |  | } | 
| 99 |  |  | 
| 100 | 10000 | public static QName createQName(String localName) { | 
| 101 | 10000 | return getDocumentFactory().createQName(localName); | 
| 102 |  | } | 
| 103 |  |  | 
| 104 |  |  | 
| 105 |  |  | 
| 106 |  |  | 
| 107 |  |  | 
| 108 |  |  | 
| 109 |  |  | 
| 110 |  |  | 
| 111 |  |  | 
| 112 |  |  | 
| 113 |  |  | 
| 114 |  |  | 
| 115 |  |  | 
| 116 |  |  | 
| 117 |  |  | 
| 118 |  |  | 
| 119 | 57 | public static XPath createXPath(String xpathExpression) | 
| 120 |  | throws InvalidXPathException { | 
| 121 | 57 | return getDocumentFactory().createXPath(xpathExpression); | 
| 122 |  | } | 
| 123 |  |  | 
| 124 |  |  | 
| 125 |  |  | 
| 126 |  |  | 
| 127 |  |  | 
| 128 |  |  | 
| 129 |  |  | 
| 130 |  |  | 
| 131 |  |  | 
| 132 |  |  | 
| 133 |  |  | 
| 134 |  |  | 
| 135 |  |  | 
| 136 |  |  | 
| 137 |  |  | 
| 138 |  |  | 
| 139 |  |  | 
| 140 |  |  | 
| 141 | 1 | public static XPath createXPath(String xpathExpression, | 
| 142 |  | VariableContext context) throws InvalidXPathException { | 
| 143 | 1 | return getDocumentFactory().createXPath(xpathExpression, context); | 
| 144 |  | } | 
| 145 |  |  | 
| 146 |  |  | 
| 147 |  |  | 
| 148 |  |  | 
| 149 |  |  | 
| 150 |  |  | 
| 151 |  |  | 
| 152 |  |  | 
| 153 |  |  | 
| 154 |  |  | 
| 155 |  |  | 
| 156 |  |  | 
| 157 |  |  | 
| 158 |  |  | 
| 159 | 4 | public static NodeFilter createXPathFilter(String xpathFilterExpression) { | 
| 160 | 4 | return getDocumentFactory().createXPathFilter(xpathFilterExpression); | 
| 161 |  | } | 
| 162 |  |  | 
| 163 |  |  | 
| 164 |  |  | 
| 165 |  |  | 
| 166 |  |  | 
| 167 |  |  | 
| 168 |  |  | 
| 169 |  |  | 
| 170 |  |  | 
| 171 |  |  | 
| 172 |  |  | 
| 173 |  |  | 
| 174 |  |  | 
| 175 | 14 | public static Pattern createPattern(String xpathPattern) { | 
| 176 | 14 | return getDocumentFactory().createPattern(xpathPattern); | 
| 177 |  | } | 
| 178 |  |  | 
| 179 |  |  | 
| 180 |  |  | 
| 181 |  |  | 
| 182 |  |  | 
| 183 |  |  | 
| 184 |  |  | 
| 185 |  |  | 
| 186 |  |  | 
| 187 |  |  | 
| 188 |  |  | 
| 189 |  |  | 
| 190 |  |  | 
| 191 |  |  | 
| 192 |  |  | 
| 193 | 0 | public static List selectNodes(String xpathFilterExpression, List nodes) { | 
| 194 | 0 | XPath xpath = createXPath(xpathFilterExpression); | 
| 195 |  |  | 
| 196 | 0 | return xpath.selectNodes(nodes); | 
| 197 |  | } | 
| 198 |  |  | 
| 199 |  |  | 
| 200 |  |  | 
| 201 |  |  | 
| 202 |  |  | 
| 203 |  |  | 
| 204 |  |  | 
| 205 |  |  | 
| 206 |  |  | 
| 207 |  |  | 
| 208 |  |  | 
| 209 |  |  | 
| 210 |  |  | 
| 211 |  |  | 
| 212 |  |  | 
| 213 | 0 | public static List selectNodes(String xpathFilterExpression, Node node) { | 
| 214 | 0 | XPath xpath = createXPath(xpathFilterExpression); | 
| 215 |  |  | 
| 216 | 0 | return xpath.selectNodes(node); | 
| 217 |  | } | 
| 218 |  |  | 
| 219 |  |  | 
| 220 |  |  | 
| 221 |  |  | 
| 222 |  |  | 
| 223 |  |  | 
| 224 |  |  | 
| 225 |  |  | 
| 226 |  |  | 
| 227 |  |  | 
| 228 |  |  | 
| 229 |  |  | 
| 230 | 0 | public static void sort(List list, String xpathExpression) { | 
| 231 | 0 | XPath xpath = createXPath(xpathExpression); | 
| 232 | 0 | xpath.sort(list); | 
| 233 |  | } | 
| 234 |  |  | 
| 235 |  |  | 
| 236 |  |  | 
| 237 |  |  | 
| 238 |  |  | 
| 239 |  |  | 
| 240 |  |  | 
| 241 |  |  | 
| 242 |  |  | 
| 243 |  |  | 
| 244 |  |  | 
| 245 |  |  | 
| 246 |  |  | 
| 247 |  |  | 
| 248 |  |  | 
| 249 |  |  | 
| 250 | 0 | public static void sort(List list, String expression, boolean distinct) { | 
| 251 | 0 | XPath xpath = createXPath(expression); | 
| 252 | 0 | xpath.sort(list, distinct); | 
| 253 |  | } | 
| 254 |  |  | 
| 255 |  |  | 
| 256 |  |  | 
| 257 |  |  | 
| 258 |  |  | 
| 259 |  |  | 
| 260 |  |  | 
| 261 |  |  | 
| 262 |  |  | 
| 263 |  |  | 
| 264 |  |  | 
| 265 |  |  | 
| 266 |  |  | 
| 267 |  |  | 
| 268 |  |  | 
| 269 | 5552 | public static Document parseText(String text) throws DocumentException { | 
| 270 | 5552 | Document result = null; | 
| 271 |  |  | 
| 272 | 5552 | SAXReader reader = new SAXReader(); | 
| 273 | 5552 | String encoding = getEncoding(text); | 
| 274 |  |  | 
| 275 | 5552 | InputSource source = new InputSource(new StringReader(text)); | 
| 276 | 5552 | source.setEncoding(encoding); | 
| 277 |  |  | 
| 278 | 5552 | result = reader.read(source); | 
| 279 |  |  | 
| 280 |  |  | 
| 281 |  |  | 
| 282 | 5552 | if (result.getXMLEncoding() == null) { | 
| 283 | 5528 | result.setXMLEncoding(encoding); | 
| 284 |  | } | 
| 285 |  |  | 
| 286 | 5552 | return result; | 
| 287 |  | } | 
| 288 |  |  | 
| 289 | 5552 | private static String getEncoding(String text) { | 
| 290 | 5552 | String result = null; | 
| 291 |  |  | 
| 292 | 5552 | String xml = text.trim(); | 
| 293 |  |  | 
| 294 | 5552 | if (xml.startsWith("<?xml")) { | 
| 295 | 25 | int end = xml.indexOf("?>"); | 
| 296 | 25 | String sub = xml.substring(0, end); | 
| 297 | 25 | StringTokenizer tokens = new StringTokenizer(sub, " =\"\'"); | 
| 298 |  |  | 
| 299 | 25 | while (tokens.hasMoreTokens()) { | 
| 300 | 99 | String token = tokens.nextToken(); | 
| 301 |  |  | 
| 302 | 99 | if ("encoding".equals(token)) { | 
| 303 | 24 | if (tokens.hasMoreTokens()) { | 
| 304 | 24 | result = tokens.nextToken(); | 
| 305 |  | } | 
| 306 |  |  | 
| 307 | 24 | break; | 
| 308 |  | } | 
| 309 |  | } | 
| 310 |  | } | 
| 311 |  |  | 
| 312 | 5552 | return result; | 
| 313 |  | } | 
| 314 |  |  | 
| 315 |  |  | 
| 316 |  |  | 
| 317 |  |  | 
| 318 |  |  | 
| 319 |  |  | 
| 320 |  |  | 
| 321 |  |  | 
| 322 |  |  | 
| 323 |  |  | 
| 324 |  |  | 
| 325 |  |  | 
| 326 |  |  | 
| 327 |  |  | 
| 328 |  |  | 
| 329 |  |  | 
| 330 |  |  | 
| 331 |  |  | 
| 332 |  |  | 
| 333 |  |  | 
| 334 |  |  | 
| 335 |  |  | 
| 336 | 6 | public static Element makeElement(Branch source, String path) { | 
| 337 | 6 | StringTokenizer tokens = new StringTokenizer(path, "/"); | 
| 338 | 6 | Element parent; | 
| 339 |  |  | 
| 340 | 6 | if (source instanceof Document) { | 
| 341 | 4 | Document document = (Document) source; | 
| 342 | 4 | parent = document.getRootElement(); | 
| 343 |  |  | 
| 344 |  |  | 
| 345 |  |  | 
| 346 | 4 | String name = tokens.nextToken(); | 
| 347 |  |  | 
| 348 | 4 | if (parent == null) { | 
| 349 | 1 | parent = document.addElement(name); | 
| 350 |  | } | 
| 351 |  | } else { | 
| 352 | 2 | parent = (Element) source; | 
| 353 |  | } | 
| 354 |  |  | 
| 355 | 6 | Element element = null; | 
| 356 |  |  | 
| 357 | 6 | while (tokens.hasMoreTokens()) { | 
| 358 | 14 | String name = tokens.nextToken(); | 
| 359 |  |  | 
| 360 | 14 | if (name.indexOf(':') > 0) { | 
| 361 | 4 | element = parent.element(parent.getQName(name)); | 
| 362 |  | } else { | 
| 363 | 10 | element = parent.element(name); | 
| 364 |  | } | 
| 365 |  |  | 
| 366 | 14 | if (element == null) { | 
| 367 | 6 | element = parent.addElement(name); | 
| 368 |  | } | 
| 369 |  |  | 
| 370 | 14 | parent = element; | 
| 371 |  | } | 
| 372 |  |  | 
| 373 | 6 | return element; | 
| 374 |  | } | 
| 375 |  | } | 
| 376 |  |  | 
| 377 |  |  | 
| 378 |  |  | 
| 379 |  |  | 
| 380 |  |  | 
| 381 |  |  | 
| 382 |  |  | 
| 383 |  |  | 
| 384 |  |  | 
| 385 |  |  | 
| 386 |  |  | 
| 387 |  |  | 
| 388 |  |  | 
| 389 |  |  | 
| 390 |  |  | 
| 391 |  |  | 
| 392 |  |  | 
| 393 |  |  | 
| 394 |  |  | 
| 395 |  |  | 
| 396 |  |  | 
| 397 |  |  | 
| 398 |  |  | 
| 399 |  |  | 
| 400 |  |  | 
| 401 |  |  | 
| 402 |  |  | 
| 403 |  |  | 
| 404 |  |  | 
| 405 |  |  | 
| 406 |  |  | 
| 407 |  |  | 
| 408 |  |  | 
| 409 |  |  | 
| 410 |  |  | 
| 411 |  |  | 
| 412 |  |  |