| 1 |  |  | 
| 2 |  |  | 
| 3 |  |  | 
| 4 |  |  | 
| 5 |  |  | 
| 6 |  |  | 
| 7 |  |  | 
| 8 |  | package org.dom4j; | 
| 9 |  |  | 
| 10 |  | import java.io.IOException; | 
| 11 |  | import java.io.ObjectInputStream; | 
| 12 |  | import java.io.Serializable; | 
| 13 |  | import java.util.List; | 
| 14 |  | import java.util.Map; | 
| 15 |  |  | 
| 16 |  | import org.dom4j.rule.Pattern; | 
| 17 |  | import org.dom4j.tree.AbstractDocument; | 
| 18 |  | import org.dom4j.tree.DefaultAttribute; | 
| 19 |  | import org.dom4j.tree.DefaultCDATA; | 
| 20 |  | import org.dom4j.tree.DefaultComment; | 
| 21 |  | import org.dom4j.tree.DefaultDocument; | 
| 22 |  | import org.dom4j.tree.DefaultDocumentType; | 
| 23 |  | import org.dom4j.tree.DefaultElement; | 
| 24 |  | import org.dom4j.tree.DefaultEntity; | 
| 25 |  | import org.dom4j.tree.DefaultProcessingInstruction; | 
| 26 |  | import org.dom4j.tree.DefaultText; | 
| 27 |  | import org.dom4j.tree.QNameCache; | 
| 28 |  | import org.dom4j.util.SimpleSingleton; | 
| 29 |  | import org.dom4j.util.SingletonStrategy; | 
| 30 |  | import org.dom4j.xpath.DefaultXPath; | 
| 31 |  | import org.dom4j.xpath.XPathPattern; | 
| 32 |  | import org.jaxen.VariableContext; | 
| 33 |  |  | 
| 34 |  |  | 
| 35 |  |  | 
| 36 |  |  | 
| 37 |  |  | 
| 38 |  |  | 
| 39 |  |  | 
| 40 |  |  | 
| 41 |  |  | 
| 42 |  |  | 
| 43 |  |  | 
| 44 |  |  | 
| 45 |  |  | 
| 46 |  |  | 
| 47 |  | public class DocumentFactory implements Serializable { | 
| 48 |  | private static SingletonStrategy singleton = null; | 
| 49 |  |  | 
| 50 |  | protected transient QNameCache cache; | 
| 51 |  |  | 
| 52 |  |  | 
| 53 |  | private Map xpathNamespaceURIs; | 
| 54 |  |  | 
| 55 | 90 | private static SingletonStrategy createSingleton() { | 
| 56 | 90 | SingletonStrategy result = null; | 
| 57 |  |  | 
| 58 | 90 | String documentFactoryClassName; | 
| 59 | 90 | try { | 
| 60 | 90 | documentFactoryClassName = System.getProperty("org.dom4j.factory", | 
| 61 |  | "org.dom4j.DocumentFactory"); | 
| 62 |  | } catch (Exception e) { | 
| 63 | 0 | documentFactoryClassName = "org.dom4j.DocumentFactory"; | 
| 64 |  | } | 
| 65 |  |  | 
| 66 | 90 | try { | 
| 67 | 90 | String singletonClass = System.getProperty( | 
| 68 |  | "org.dom4j.DocumentFactory.singleton.strategy", | 
| 69 |  | "org.dom4j.util.SimpleSingleton"); | 
| 70 | 90 | Class clazz = Class.forName(singletonClass); | 
| 71 | 90 | result = (SingletonStrategy) clazz.newInstance(); | 
| 72 |  | } catch (Exception e) { | 
| 73 | 0 | result = new SimpleSingleton(); | 
| 74 |  | } | 
| 75 |  |  | 
| 76 | 90 | result.setSingletonClassName(documentFactoryClassName); | 
| 77 |  |  | 
| 78 | 90 | return result; | 
| 79 |  | } | 
| 80 |  |  | 
| 81 | 541 | public DocumentFactory() { | 
| 82 | 541 | init(); | 
| 83 |  | } | 
| 84 |  |  | 
| 85 |  |  | 
| 86 |  |  | 
| 87 |  |  | 
| 88 |  |  | 
| 89 |  |  | 
| 90 |  |  | 
| 91 |  |  | 
| 92 |  |  | 
| 93 | 27023 | public static synchronized DocumentFactory getInstance() { | 
| 94 | 27023 | if (singleton == null) { | 
| 95 | 90 | singleton = createSingleton(); | 
| 96 |  | } | 
| 97 | 27023 | return (DocumentFactory) singleton.instance(); | 
| 98 |  | } | 
| 99 |  |  | 
| 100 |  |  | 
| 101 | 6270 | public Document createDocument() { | 
| 102 | 6270 | DefaultDocument answer = new DefaultDocument(); | 
| 103 | 6270 | answer.setDocumentFactory(this); | 
| 104 |  |  | 
| 105 | 6270 | return answer; | 
| 106 |  | } | 
| 107 |  |  | 
| 108 |  |  | 
| 109 |  |  | 
| 110 |  |  | 
| 111 |  |  | 
| 112 |  |  | 
| 113 |  |  | 
| 114 |  |  | 
| 115 |  |  | 
| 116 |  |  | 
| 117 |  |  | 
| 118 | 5816 | public Document createDocument(String encoding) { | 
| 119 |  |  | 
| 120 |  |  | 
| 121 |  |  | 
| 122 | 5816 | Document answer = createDocument(); | 
| 123 |  |  | 
| 124 | 5816 | if (answer instanceof AbstractDocument) { | 
| 125 | 5816 | ((AbstractDocument) answer).setXMLEncoding(encoding); | 
| 126 |  | } | 
| 127 |  |  | 
| 128 | 5816 | return answer; | 
| 129 |  | } | 
| 130 |  |  | 
| 131 | 2 | public Document createDocument(Element rootElement) { | 
| 132 | 2 | Document answer = createDocument(); | 
| 133 | 2 | answer.setRootElement(rootElement); | 
| 134 |  |  | 
| 135 | 2 | return answer; | 
| 136 |  | } | 
| 137 |  |  | 
| 138 | 11 | public DocumentType createDocType(String name, String publicId, | 
| 139 |  | String systemId) { | 
| 140 | 11 | return new DefaultDocumentType(name, publicId, systemId); | 
| 141 |  | } | 
| 142 |  |  | 
| 143 | 94468 | public Element createElement(QName qname) { | 
| 144 | 94468 | return new DefaultElement(qname); | 
| 145 |  | } | 
| 146 |  |  | 
| 147 | 465 | public Element createElement(String name) { | 
| 148 | 465 | return createElement(createQName(name)); | 
| 149 |  | } | 
| 150 |  |  | 
| 151 | 4 | public Element createElement(String qualifiedName, String namespaceURI) { | 
| 152 | 4 | return createElement(createQName(qualifiedName, namespaceURI)); | 
| 153 |  | } | 
| 154 |  |  | 
| 155 | 14165 | public Attribute createAttribute(Element owner, QName qname, String value) { | 
| 156 | 14165 | return new DefaultAttribute(qname, value); | 
| 157 |  | } | 
| 158 |  |  | 
| 159 | 1630 | public Attribute createAttribute(Element owner, String name, String value) { | 
| 160 | 1630 | return createAttribute(owner, createQName(name), value); | 
| 161 |  | } | 
| 162 |  |  | 
| 163 | 103 | public CDATA createCDATA(String text) { | 
| 164 | 103 | return new DefaultCDATA(text); | 
| 165 |  | } | 
| 166 |  |  | 
| 167 | 503 | public Comment createComment(String text) { | 
| 168 | 503 | return new DefaultComment(text); | 
| 169 |  | } | 
| 170 |  |  | 
| 171 | 121456 | public Text createText(String text) { | 
| 172 | 121456 | if (text == null) { | 
| 173 | 0 | String msg = "Adding text to an XML document must not be null"; | 
| 174 | 0 | throw new IllegalArgumentException(msg); | 
| 175 |  | } | 
| 176 |  |  | 
| 177 | 121456 | return new DefaultText(text); | 
| 178 |  | } | 
| 179 |  |  | 
| 180 | 4 | public Entity createEntity(String name, String text) { | 
| 181 | 4 | return new DefaultEntity(name, text); | 
| 182 |  | } | 
| 183 |  |  | 
| 184 | 100506 | public Namespace createNamespace(String prefix, String uri) { | 
| 185 | 100506 | return Namespace.get(prefix, uri); | 
| 186 |  | } | 
| 187 |  |  | 
| 188 | 16 | public ProcessingInstruction createProcessingInstruction(String target, | 
| 189 |  | String data) { | 
| 190 | 16 | return new DefaultProcessingInstruction(target, data); | 
| 191 |  | } | 
| 192 |  |  | 
| 193 | 0 | public ProcessingInstruction createProcessingInstruction(String target, | 
| 194 |  | Map data) { | 
| 195 | 0 | return new DefaultProcessingInstruction(target, data); | 
| 196 |  | } | 
| 197 |  |  | 
| 198 | 104391 | public QName createQName(String localName, Namespace namespace) { | 
| 199 | 104391 | return cache.get(localName, namespace); | 
| 200 |  | } | 
| 201 |  |  | 
| 202 | 12760 | public QName createQName(String localName) { | 
| 203 | 12760 | return cache.get(localName); | 
| 204 |  | } | 
| 205 |  |  | 
| 206 | 16 | public QName createQName(String name, String prefix, String uri) { | 
| 207 | 16 | return cache.get(name, Namespace.get(prefix, uri)); | 
| 208 |  | } | 
| 209 |  |  | 
| 210 | 30 | public QName createQName(String qualifiedName, String uri) { | 
| 211 | 30 | return cache.get(qualifiedName, uri); | 
| 212 |  | } | 
| 213 |  |  | 
| 214 |  |  | 
| 215 |  |  | 
| 216 |  |  | 
| 217 |  |  | 
| 218 |  |  | 
| 219 |  |  | 
| 220 |  |  | 
| 221 |  |  | 
| 222 |  |  | 
| 223 |  |  | 
| 224 |  |  | 
| 225 |  |  | 
| 226 |  |  | 
| 227 |  |  | 
| 228 | 589 | public XPath createXPath(String xpathExpression) | 
| 229 |  | throws InvalidXPathException { | 
| 230 | 589 | DefaultXPath xpath = new DefaultXPath(xpathExpression); | 
| 231 |  |  | 
| 232 | 585 | if (xpathNamespaceURIs != null) { | 
| 233 | 3 | xpath.setNamespaceURIs(xpathNamespaceURIs); | 
| 234 |  | } | 
| 235 |  |  | 
| 236 | 585 | return xpath; | 
| 237 |  | } | 
| 238 |  |  | 
| 239 |  |  | 
| 240 |  |  | 
| 241 |  |  | 
| 242 |  |  | 
| 243 |  |  | 
| 244 |  |  | 
| 245 |  |  | 
| 246 |  |  | 
| 247 |  |  | 
| 248 |  |  | 
| 249 |  |  | 
| 250 |  |  | 
| 251 |  |  | 
| 252 | 1 | public XPath createXPath(String xpathExpression, | 
| 253 |  | VariableContext variableContext) { | 
| 254 | 1 | XPath xpath = createXPath(xpathExpression); | 
| 255 | 1 | xpath.setVariableContext(variableContext); | 
| 256 |  |  | 
| 257 | 1 | return xpath; | 
| 258 |  | } | 
| 259 |  |  | 
| 260 |  |  | 
| 261 |  |  | 
| 262 |  |  | 
| 263 |  |  | 
| 264 |  |  | 
| 265 |  |  | 
| 266 |  |  | 
| 267 |  |  | 
| 268 |  |  | 
| 269 |  |  | 
| 270 |  |  | 
| 271 |  |  | 
| 272 |  |  | 
| 273 |  |  | 
| 274 | 0 | public NodeFilter createXPathFilter(String xpathFilterExpression, | 
| 275 |  | VariableContext variableContext) { | 
| 276 | 0 | XPath answer = createXPath(xpathFilterExpression); | 
| 277 |  |  | 
| 278 |  |  | 
| 279 | 0 | answer.setVariableContext(variableContext); | 
| 280 |  |  | 
| 281 | 0 | return answer; | 
| 282 |  | } | 
| 283 |  |  | 
| 284 |  |  | 
| 285 |  |  | 
| 286 |  |  | 
| 287 |  |  | 
| 288 |  |  | 
| 289 |  |  | 
| 290 |  |  | 
| 291 |  |  | 
| 292 |  |  | 
| 293 |  |  | 
| 294 |  |  | 
| 295 |  |  | 
| 296 | 4 | public NodeFilter createXPathFilter(String xpathFilterExpression) { | 
| 297 | 4 | return createXPath(xpathFilterExpression); | 
| 298 |  |  | 
| 299 |  |  | 
| 300 |  | } | 
| 301 |  |  | 
| 302 |  |  | 
| 303 |  |  | 
| 304 |  |  | 
| 305 |  |  | 
| 306 |  |  | 
| 307 |  |  | 
| 308 |  |  | 
| 309 |  |  | 
| 310 |  |  | 
| 311 |  |  | 
| 312 |  |  | 
| 313 |  |  | 
| 314 | 37 | public Pattern createPattern(String xpathPattern) { | 
| 315 | 37 | return new XPathPattern(xpathPattern); | 
| 316 |  | } | 
| 317 |  |  | 
| 318 |  |  | 
| 319 |  |  | 
| 320 |  |  | 
| 321 |  |  | 
| 322 |  |  | 
| 323 |  |  | 
| 324 |  |  | 
| 325 |  |  | 
| 326 |  |  | 
| 327 | 1 | public List getQNames() { | 
| 328 | 1 | return cache.getQNames(); | 
| 329 |  | } | 
| 330 |  |  | 
| 331 |  |  | 
| 332 |  |  | 
| 333 |  |  | 
| 334 |  |  | 
| 335 |  |  | 
| 336 |  |  | 
| 337 |  |  | 
| 338 |  |  | 
| 339 |  |  | 
| 340 | 0 | public Map getXPathNamespaceURIs() { | 
| 341 | 0 | return xpathNamespaceURIs; | 
| 342 |  | } | 
| 343 |  |  | 
| 344 |  |  | 
| 345 |  |  | 
| 346 |  |  | 
| 347 |  |  | 
| 348 |  |  | 
| 349 |  |  | 
| 350 |  |  | 
| 351 |  |  | 
| 352 | 2 | public void setXPathNamespaceURIs(Map namespaceURIs) { | 
| 353 | 2 | this.xpathNamespaceURIs = namespaceURIs; | 
| 354 |  | } | 
| 355 |  |  | 
| 356 |  |  | 
| 357 |  |  | 
| 358 |  |  | 
| 359 |  |  | 
| 360 |  |  | 
| 361 |  |  | 
| 362 |  |  | 
| 363 |  |  | 
| 364 |  |  | 
| 365 |  |  | 
| 366 |  |  | 
| 367 |  |  | 
| 368 |  |  | 
| 369 |  |  | 
| 370 | 0 | protected static DocumentFactory createSingleton(String className) { | 
| 371 |  |  | 
| 372 | 0 | try { | 
| 373 |  |  | 
| 374 |  |  | 
| 375 | 0 | Class theClass = Class.forName(className, true, | 
| 376 |  | DocumentFactory.class.getClassLoader()); | 
| 377 |  |  | 
| 378 | 0 | return (DocumentFactory) theClass.newInstance(); | 
| 379 |  | } catch (Throwable e) { | 
| 380 | 0 | System.out.println("WARNING: Cannot load DocumentFactory: " | 
| 381 |  | + className); | 
| 382 |  |  | 
| 383 | 0 | return new DocumentFactory(); | 
| 384 |  | } | 
| 385 |  | } | 
| 386 |  |  | 
| 387 |  |  | 
| 388 |  |  | 
| 389 |  |  | 
| 390 |  |  | 
| 391 |  |  | 
| 392 |  |  | 
| 393 |  |  | 
| 394 |  |  | 
| 395 |  |  | 
| 396 | 0 | protected QName intern(QName qname) { | 
| 397 | 0 | return cache.intern(qname); | 
| 398 |  | } | 
| 399 |  |  | 
| 400 |  |  | 
| 401 |  |  | 
| 402 |  |  | 
| 403 |  |  | 
| 404 |  |  | 
| 405 |  |  | 
| 406 | 544 | protected QNameCache createQNameCache() { | 
| 407 | 544 | return new QNameCache(this); | 
| 408 |  | } | 
| 409 |  |  | 
| 410 | 3 | private void readObject(ObjectInputStream in) throws IOException, | 
| 411 |  | ClassNotFoundException { | 
| 412 | 3 | in.defaultReadObject(); | 
| 413 | 3 | init(); | 
| 414 |  | } | 
| 415 |  |  | 
| 416 | 544 | protected void init() { | 
| 417 | 544 | cache = createQNameCache(); | 
| 418 |  | } | 
| 419 |  | } | 
| 420 |  |  | 
| 421 |  |  | 
| 422 |  |  | 
| 423 |  |  | 
| 424 |  |  | 
| 425 |  |  | 
| 426 |  |  | 
| 427 |  |  | 
| 428 |  |  | 
| 429 |  |  | 
| 430 |  |  | 
| 431 |  |  | 
| 432 |  |  | 
| 433 |  |  | 
| 434 |  |  | 
| 435 |  |  | 
| 436 |  |  | 
| 437 |  |  | 
| 438 |  |  | 
| 439 |  |  | 
| 440 |  |  | 
| 441 |  |  | 
| 442 |  |  | 
| 443 |  |  | 
| 444 |  |  | 
| 445 |  |  | 
| 446 |  |  | 
| 447 |  |  | 
| 448 |  |  | 
| 449 |  |  | 
| 450 |  |  | 
| 451 |  |  | 
| 452 |  |  | 
| 453 |  |  | 
| 454 |  |  | 
| 455 |  |  | 
| 456 |  |  |