📄 ozonedocumentfactoryimpl.java
字号:
/* * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. * * This software is open source. * See the bottom of this file for the licence. * * $Id: OzoneDocumentFactoryImpl.java,v 1.1 2003/11/02 18:10:03 per_nyfelt Exp $ */package org.ozoneDB.xml.dom4j.o3impl;import org.dom4j.*;import org.dom4j.rule.Pattern;import org.dom4j.xpath.DefaultXPath;import org.dom4j.xpath.XPathPattern;import org.jaxen.VariableContext;import org.ozoneDB.OzoneInterface;import org.ozoneDB.OzoneObject;import org.ozoneDB.UnexpectedException;import org.ozoneDB.xml.dom4j.OzoneDocument;import org.ozoneDB.xml.dom4j.OzoneDocumentFactory;import java.io.IOException;import java.io.ObjectInputStream;import java.util.HashMap;import java.util.List;import java.util.Map;/** <p><code>OzoneDocumentFactoryImpl</code> is a collection of factory methods to allow * easy custom building of DOM4J trees. The default tree that is built uses * a doubly linked tree. </p> * * <p>The tree built allows full XPath expressions from anywhere on the * tree.</p> * * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> * @author Per Nyfelt * @version $Revision: 1.1 $ */public class OzoneDocumentFactoryImpl extends OzoneObject implements OzoneDocumentFactory { final static long serialVersionUID = 1L; protected transient O3QNameCache qnameCache; //protected O3QNameCache qnameCache; protected transient O3NamespaceCache namespaceCache; //protected O3NamespaceCache namespaceCache; /** Default namespace prefix -> URI mappings for XPath expressions to use */ private Map xpathNamespaceURIs; /** A map of instances, one for each database */ private static transient Map factories = new HashMap(); public static synchronized OzoneDocumentFactory getInstance(OzoneInterface db) { try { OzoneDocumentFactory factory = (OzoneDocumentFactory)factories.get(db); if ( factory == null) { System.out.println("looking for OzoneDocumentFactory instance (" + OBJECT_NAME + ") for database " + db); factory = (OzoneDocumentFactory) db.objectForName(OBJECT_NAME); if (factory == null) { System.out.println("creating new OzoneDocumentFactory"); factory = OzoneDocumentFactoryImpl.create(db); } factories.put(db, factory); } System.out.println("returning factory " + factory); return factory; } catch (Exception e) { throw new UnexpectedException("failed to get OzoneDocumentFactory instance!", e); } } public static OzoneDocumentFactory create(OzoneInterface db) { return (OzoneDocumentFactory) db.createObject(OzoneDocumentFactoryImpl.class, OzoneInterface.Public, OBJECT_NAME); } public OzoneDocumentFactoryImpl() { } public void onCreate() { namespaceCache = new O3NamespaceCache(database()); qnameCache = new org.ozoneDB.xml.dom4j.o3impl.O3QNameCache(this, database()); } public void onActivate() { //System.out.println("setting transient database reference on namespace");// namespaceCache.setDatabase(database());// qnameCache.setDatabase(database()); factories = new HashMap(); namespaceCache = new O3NamespaceCache(database()); qnameCache = new O3QNameCache(this, database()); } public void onPassivate() { // todo: we should check that the caches has not grown to big, if it has we should shrink it } // Factory methods public Document createDocument() { OzoneDocument answer = (OzoneDocument) database() .createObject(OzoneDocumentImpl.class); return answer; } public Document createDocument(Element rootElement) { Document answer = createDocument(); answer.setRootElement(rootElement); return answer; } public Document createDocument(String name) { OzoneDocument answer = OzoneDocumentImpl.create(database(), name); return answer; } public Document createDocument(Element rootElement, String name) { Document answer = createDocument(name); answer.setRootElement(rootElement); return answer; } public DocumentType createDocType(String name, String publicId, String systemId) { return OzoneDocumentTypeImpl.create(database(), name, publicId, systemId); } public Element createElement(QName qname) { return OzoneElementImpl.create(database(), qname); } public Element createElement(String name) { //return createElement(createQName(name)); return OzoneElementImpl.create(database(), name); } public Element createElement(String qualifiedName, String namespaceURI) { return createElement(createQName(qualifiedName, namespaceURI)); } public Attribute createAttribute(Element owner, QName qname, String value) { //System.out.println("[OzoneDocumentFactoryImpl] creating an OzoneAttributeImpl"); Attribute att = OzoneAttributeImpl.create(database(), owner, qname, value); //System.out.println("created attribute " + att); return att; } public Attribute createAttribute(Element owner, String name, String value) { //System.out.println("[OzoneDocumentFactoryImpl] About to create an attribute"); // skip the cache for now return createAttribute(owner, createQName(name), value); //return OzoneAttributeImpl.create(database(), owner, name, value); } public CDATA createCDATA(String text) { return OzoneCDATAImpl.create(database(), text); } public Comment createComment(String text) { return OzoneCommentImpl.create(database(), text); } public Text createText(String text) { if (text == null) { throw new IllegalArgumentException("Adding text to an XML document must not be null"); } return OzoneTextImpl.create(database(), text); } public Entity createEntity(String name, String text) { return OzoneEntityImpl.create(database(), name, text); } public Namespace createNamespace(String prefix, String uri) { return namespaceCache.get(prefix, uri); } public Namespace getNamespace(String uri) { return namespaceCache.get(uri); } public Namespace getXmlNameSpace() { return namespaceCache.getXmlNamespace(); } public Namespace getNoNamespace() { return namespaceCache.getNoNamespace(); } public ProcessingInstruction createProcessingInstruction(String target, String data) { return OzoneProcessingInstructionImpl.create(database(), target, data); } public ProcessingInstruction createProcessingInstruction(String target, Map data) { return OzoneProcessingInstructionImpl.create(database(), target, data); } //These cannot be created as persistent objects public QName createQName(String localName, Namespace namespace) { return qnameCache.get(localName, namespace); } public QName createQName(String localName) { return qnameCache.get(localName); } public QName createQName(String name, String prefix, String uri) { return qnameCache.get(name, namespaceCache.get(prefix, uri)); } public QName createQName(String qualifiedName, String uri) { return qnameCache.get(qualifiedName, uri); } /** <p><code>createXPath</code> parses an XPath expression * and creates a new XPath <code>XPath</code> instance.</p> * * @param xpathExpression is the XPath expression to create * @return a new <code>XPath</code> instance * @throws org.dom4j.InvalidXPathException if the XPath expression is invalid */ public XPath createXPath(String xpathExpression) throws InvalidXPathException {// Not sure if there is a need for persistent Xpath expressions so reverting to the// Default implementation until I know for sure// OzoneXPath xpath = OzoneXPathImpl.create(database(), xpathExpression);// if (xpathNamespaceURIs != null) {// xpath.setNamespaceURIs(xpathNamespaceURIs);// }// return xpath; XPath xpath = new DefaultXPath(xpathExpression); if (xpathNamespaceURIs != null) { xpath.setNamespaceURIs(xpathNamespaceURIs); } return xpath; } /** <p><code>createXPath</code> parses an XPath expression * and creates a new XPath <code>XPath</code> instance.</p> * * @param xpathExpression is the XPath expression to create * @param variableContext is the variable context to use when evaluating the XPath * @return a new <code>XPath</code> instance * @throws org.dom4j.InvalidXPathException if the XPath expression is invalid */ public XPath createXPath(String xpathExpression, VariableContext variableContext) { XPath xpath = createXPath(xpathExpression); xpath.setVariableContext(variableContext); return xpath; } /** <p><code>createXPathFilter</code> parses a NodeFilter * from the given XPath filter expression. * XPath filter expressions occur within XPath expressions such as * <code>self::node()[ filterExpression ]</code></p> * * @param xpathFilterExpression is the XPath filter expression * to create * @param variableContext is the variable context to use when evaluating the XPath * @return a new <code>NodeFilter</code> instance */ public NodeFilter createXPathFilter(String xpathFilterExpression, VariableContext variableContext) { XPath answer = createXPath(xpathFilterExpression); //OzoneXPath answer = new OzoneXPath( xpathFilterExpression ); answer.setVariableContext(variableContext); return answer; } /** <p><code>createXPathFilter</code> parses a NodeFilter * from the given XPath filter expression. * XPath filter expressions occur within XPath expressions such as * <code>self::node()[ filterExpression ]</code></p> * * @param xpathFilterExpression is the XPath filter expression * to create * @return a new <code>NodeFilter</code> instance */ public NodeFilter createXPathFilter(String xpathFilterExpression) { return createXPath(xpathFilterExpression); //return new OzoneXPath( xpathFilterExpression ); } /** <p><code>createPattern</code> parses the given * XPath expression to create an XSLT style {@link org.dom4j.rule.Pattern} instance * which can then be used in an XSLT processing model.</p> * * @param xpathPattern is the XPath pattern expression * to create * @return a new <code>Pattern</code> instance */ public Pattern createPattern(String xpathPattern) { // not sure if persistable patterns are needed to sticking with // the default implementation until proven to be otherwise //return XPathPatternImpl.create(database(), xpathPattern); return new XPathPattern(xpathPattern); } // Properties //------------------------------------------------------------------------- /** Returns a list of all the QNameImpl instances currently used by this document factory * @deprecated or was it just forgotten when the interfaces war created? */ public List getQNames() { return qnameCache.getQNames(); } /** @return the Map of namespace URIs that will be used by by XPath expressions * to resolve namespace prefixes into namespace URIs. The map is keyed by * namespace prefix and the value is the namespace URI. This value could well be * null to indicate no namespace URIs are being mapped. */ public Map getXPathNamespaceURIs() { return xpathNamespaceURIs; } /** Sets the namespace URIs to be used by XPath expressions created by this factory * or by nodes associated with this factory. The keys are namespace prefixes and the * values are namespace URIs. * @deprecated or was it just forgotten and should be in XPathFactory */ public void setXPathNamespaceURIs(Map xpathNamespaceURIs) { this.xpathNamespaceURIs = xpathNamespaceURIs; } // Implementation methods //------------------------------------------------------------------------- public O3NamespaceCache getNamespaceCache() { return namespaceCache; } public void setNamespaceCache(O3NamespaceCache namespaceCache) { this.namespaceCache = namespaceCache; } /** @return the cached QNameImpl instance if there is one or adds the given * qname to the qnameCache if not */ protected QName intern(QName qname) { return qnameCache.intern(qname); } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); }}/* * Redistribution and use of this software and associated documentation * ("Software"), with or without modification, are permitted provided * that the following conditions are met: * * 1. Redistributions of source code must retain copyright * statements and notices. Redistributions must also contain a * copy of this document. * * 2. Redistributions in binary form must reproduce the * above copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * * 3. The name "DOM4J" must not be used to endorse or promote * products derived from this Software without prior written * permission of MetaStuff, Ltd. For written permission, * please contact dom4j-info@metastuff.com. * * 4. Products derived from this Software may not be called "DOM4J" * nor may "DOM4J" appear in their names without prior written * permission of MetaStuff, Ltd. DOM4J is a registered * trademark of MetaStuff, Ltd. * * 5. Due credit should be given to the DOM4J Project * (http://dom4j.org/). * * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. * * $Id: OzoneDocumentFactoryImpl.java,v 1.1 2003/11/02 18:10:03 per_nyfelt Exp $ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -