001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: DocumentFactory.java,v 1.34 2002/02/01 13:04:32 jstrachan Exp $ 008 */ 009 010package org.dom4j; 011 012import java.io.IOException; 013import java.io.ObjectInputStream; 014import java.io.Serializable; 015 016import java.util.List; 017import java.util.Map; 018 019import org.dom4j.rule.Pattern; 020import org.dom4j.tree.DefaultAttribute; 021import org.dom4j.tree.DefaultCDATA; 022import org.dom4j.tree.DefaultComment; 023import org.dom4j.tree.DefaultDocument; 024import org.dom4j.tree.DefaultDocumentType; 025import org.dom4j.tree.DefaultElement; 026import org.dom4j.tree.DefaultEntity; 027import org.dom4j.tree.DefaultProcessingInstruction; 028import org.dom4j.tree.QNameCache; 029import org.dom4j.tree.DefaultAttribute; 030import org.dom4j.tree.DefaultEntity; 031import org.dom4j.tree.DefaultText; 032import org.dom4j.xpath.DefaultXPath; 033import org.dom4j.xpath.XPathPattern; 034 035import org.jaxen.VariableContext; 036 037import org.xml.sax.Attributes; 038 039/** <p><code>DocumentFactory</code> is a collection of factory methods to allow 040 * easy custom building of DOM4J trees. The default tree that is built uses 041 * a doubly linked tree. </p> 042 * 043 * <p>The tree built allows full XPath expressions from anywhere on the 044 * tree.</p> 045 * 046 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 047 * @version $Revision: 1.34 $ 048 */ 049public class DocumentFactory implements Serializable { 050 051 /** The Singleton instance */ 052 private static transient DocumentFactory singleton; 053 054 protected transient QNameCache cache; 055 056 /** Default namespace prefix -> URI mappings for XPath expressions to use */ 057 private Map xpathNamespaceURIs; 058 059 static { 060 String className = null; 061 try { 062 className = System.getProperty( 063 "org.dom4j.factory", 064 "org.dom4j.DocumentFactory" 065 ); 066 } 067 catch (Exception e) { 068 className = "org.dom4j.DocumentFactory"; 069 } 070 singleton = createSingleton( className ); 071 } 072 073 /** <p>Access to singleton implementation of DocumentFactory which 074 * is used if no DocumentFactory is specified when building using the 075 * standard builders.</p> 076 * 077 * @return the default singleon instance 078 */ 079 public static DocumentFactory getInstance() { 080 return singleton; 081 } 082 083 public DocumentFactory() { 084 init(); 085 } 086 087 088 // Factory methods 089 090 public Document createDocument() { 091 DefaultDocument answer = new DefaultDocument(); 092 answer.setDocumentFactory( this ); 093 return answer; 094 } 095 096 public Document createDocument(Element rootElement) { 097 Document answer = createDocument(); 098 answer.setRootElement(rootElement); 099 return answer; 100 } 101 102 public DocumentType createDocType(String name, String publicId, String systemId) { 103 return new DefaultDocumentType( name, publicId, systemId ); 104 } 105 106 public Element createElement(QName qname) { 107 return new DefaultElement(qname); 108 } 109 110 public Element createElement(String name) { 111 return createElement(createQName(name)); 112 } 113 114 public Element createElement(String qualifiedName, String namespaceURI) { 115 return createElement(createQName(qualifiedName, namespaceURI)); 116 } 117 118 public Attribute createAttribute(Element owner, QName qname, String value) { 119 return new DefaultAttribute(qname, value); 120 } 121 122 public Attribute createAttribute(Element owner, String name, String value) { 123 return createAttribute(owner, createQName(name), value); 124 } 125 126 public CDATA createCDATA(String text) { 127 return new DefaultCDATA(text); 128 } 129 130 public Comment createComment(String text) { 131 return new DefaultComment(text); 132 } 133 134 public Text createText(String text) { 135 if ( text == null ) { 136 throw new IllegalArgumentException( "Adding text to an XML document must not be null" ); 137 } 138 return new DefaultText(text); 139 } 140 141 142 public Entity createEntity(String name, String text) { 143 return new DefaultEntity(name, text); 144 } 145 146 public Namespace createNamespace(String prefix, String uri) { 147 return Namespace.get(prefix, uri); 148 } 149 150 public ProcessingInstruction createProcessingInstruction(String target, String data) { 151 return new DefaultProcessingInstruction(target, data); 152 } 153 154 public ProcessingInstruction createProcessingInstruction(String target, Map data) { 155 return new DefaultProcessingInstruction(target, data); 156 } 157 158 public QName createQName(String localName, Namespace namespace) { 159 return cache.get(localName, namespace); 160 } 161 162 public QName createQName(String localName) { 163 return cache.get(localName); 164 } 165 166 public QName createQName(String name, String prefix, String uri) { 167 return cache.get(name, Namespace.get( prefix, uri )); 168 } 169 170 public QName createQName(String qualifiedName, String uri) { 171 return cache.get(qualifiedName, uri); 172 } 173 174 /** <p><code>createXPath</code> parses an XPath expression 175 * and creates a new XPath <code>XPath</code> instance.</p> 176 * 177 * @param xpathExpression is the XPath expression to create 178 * @return a new <code>XPath</code> instance 179 * @throws InvalidXPathException if the XPath expression is invalid 180 */ 181 public XPath createXPath(String xpathExpression) throws InvalidXPathException { 182 DefaultXPath xpath = new DefaultXPath( xpathExpression ); 183 if ( xpathNamespaceURIs != null ) { 184 xpath.setNamespaceURIs( xpathNamespaceURIs ); 185 } 186 return xpath; 187 } 188 189 /** <p><code>createXPath</code> parses an XPath expression 190 * and creates a new XPath <code>XPath</code> instance.</p> 191 * 192 * @param xpathExpression is the XPath expression to create 193 * @param variableContext is the variable context to use when evaluating the XPath 194 * @return a new <code>XPath</code> instance 195 * @throws InvalidXPathException if the XPath expression is invalid 196 */ 197 public XPath createXPath(String xpathExpression, VariableContext variableContext) { 198 XPath xpath = createXPath( xpathExpression ); 199 xpath.setVariableContext( variableContext ); 200 return xpath; 201 } 202 203 /** <p><code>createXPathFilter</code> parses a NodeFilter 204 * from the given XPath filter expression. 205 * XPath filter expressions occur within XPath expressions such as 206 * <code>self::node()[ filterExpression ]</code></p> 207 * 208 * @param xpathFilterExpression is the XPath filter expression 209 * to create 210 * @param variableContext is the variable context to use when evaluating the XPath 211 * @return a new <code>NodeFilter</code> instance 212 */ 213 public NodeFilter createXPathFilter(String xpathFilterExpression, VariableContext variableContext) { 214 XPath answer = createXPath( xpathFilterExpression ); 215 //DefaultXPath answer = new DefaultXPath( xpathFilterExpression ); 216 answer.setVariableContext( variableContext ); 217 return answer; 218 } 219 220 /** <p><code>createXPathFilter</code> parses a NodeFilter 221 * from the given XPath filter expression. 222 * XPath filter expressions occur within XPath expressions such as 223 * <code>self::node()[ filterExpression ]</code></p> 224 * 225 * @param xpathFilterExpression is the XPath filter expression 226 * to create 227 * @return a new <code>NodeFilter</code> instance 228 */ 229 public NodeFilter createXPathFilter(String xpathFilterExpression) { 230 return createXPath( xpathFilterExpression ); 231 //return new DefaultXPath( xpathFilterExpression ); 232 } 233 234 /** <p><code>createPattern</code> parses the given 235 * XPath expression to create an XSLT style {@link Pattern} instance 236 * which can then be used in an XSLT processing model.</p> 237 * 238 * @param xpathPattern is the XPath pattern expression 239 * to create 240 * @return a new <code>Pattern</code> instance 241 */ 242 public Pattern createPattern(String xpathPattern) { 243 return new XPathPattern( xpathPattern ); 244 } 245 246 247 // Properties 248 //------------------------------------------------------------------------- 249 250 /** Returns a list of all the QName instances currently used by this document factory 251 */ 252 public List getQNames() { 253 return cache.getQNames(); 254 } 255 256 /** @return the Map of namespace URIs that will be used by by XPath expressions 257 * to resolve namespace prefixes into namespace URIs. The map is keyed by 258 * namespace prefix and the value is the namespace URI. This value could well be 259 * null to indicate no namespace URIs are being mapped. 260 */ 261 public Map getXPathNamespaceURIs() { 262 return xpathNamespaceURIs; 263 } 264 265 /** Sets the namespace URIs to be used by XPath expressions created by this factory 266 * or by nodes associated with this factory. The keys are namespace prefixes and the 267 * values are namespace URIs. 268 */ 269 public void setXPathNamespaceURIs(Map xpathNamespaceURIs) { 270 this.xpathNamespaceURIs = xpathNamespaceURIs; 271 } 272 273 // Implementation methods 274 //------------------------------------------------------------------------- 275 276 277 /** <p><code>createSingleton</code> creates the singleton instance 278 * from the given class name.</p> 279 * 280 * @param className is the name of the DocumentFactory class to use 281 * @return a new singleton instance. 282 */ 283 protected static DocumentFactory createSingleton(String className) { 284 // let's try and class load an implementation? 285 try { 286 // I'll use the current class loader 287 // that loaded me to avoid problems in J2EE and web apps 288 Class theClass = Class.forName( 289 className, 290 true, 291 DocumentFactory.class.getClassLoader() 292 ); 293 return (DocumentFactory) theClass.newInstance(); 294 } 295 catch (Throwable e) { 296 System.out.println( "WARNING: Cannot load DocumentFactory: " + className ); 297 return new DocumentFactory(); 298 } 299 } 300 301 /** @return the cached QName instance if there is one or adds the given 302 * qname to the cache if not 303 */ 304 protected QName intern(QName qname) { 305 return cache.intern(qname); 306 } 307 308 /** Factory method to create the QNameCache. This method should be overloaded 309 * if you wish to use your own derivation of QName. 310 */ 311 protected QNameCache createQNameCache() { 312 return new QNameCache(this); 313 } 314 315 316 317 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 318 in.defaultReadObject(); 319 init(); 320 } 321 322 protected void init() { 323 cache = createQNameCache(); 324 } 325} 326 327 328 329 330/* 331 * Redistribution and use of this software and associated documentation 332 * ("Software"), with or without modification, are permitted provided 333 * that the following conditions are met: 334 * 335 * 1. Redistributions of source code must retain copyright 336 * statements and notices. Redistributions must also contain a 337 * copy of this document. 338 * 339 * 2. Redistributions in binary form must reproduce the 340 * above copyright notice, this list of conditions and the 341 * following disclaimer in the documentation and/or other 342 * materials provided with the distribution. 343 * 344 * 3. The name "DOM4J" must not be used to endorse or promote 345 * products derived from this Software without prior written 346 * permission of MetaStuff, Ltd. For written permission, 347 * please contact dom4j-info@metastuff.com. 348 * 349 * 4. Products derived from this Software may not be called "DOM4J" 350 * nor may "DOM4J" appear in their names without prior written 351 * permission of MetaStuff, Ltd. DOM4J is a registered 352 * trademark of MetaStuff, Ltd. 353 * 354 * 5. Due credit should be given to the DOM4J Project 355 * (http://dom4j.org/). 356 * 357 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 358 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 359 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 360 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 361 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 362 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 363 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 364 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 365 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 366 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 367 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 368 * OF THE POSSIBILITY OF SUCH DAMAGE. 369 * 370 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 371 * 372 * $Id: DocumentFactory.java,v 1.34 2002/02/01 13:04:32 jstrachan Exp $ 373 */