KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom4j > o3impl > OzoneDocumentFactoryImpl


1 /*
2  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: OzoneDocumentFactoryImpl.java,v 1.1 2003/11/02 18:10:03 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
11
12 import org.dom4j.*;
13 import org.dom4j.rule.Pattern;
14 import org.dom4j.xpath.DefaultXPath;
15 import org.dom4j.xpath.XPathPattern;
16 import org.jaxen.VariableContext;
17 import org.ozoneDB.OzoneInterface;
18 import org.ozoneDB.OzoneObject;
19 import org.ozoneDB.UnexpectedException;
20 import org.ozoneDB.xml.dom4j.OzoneDocument;
21 import org.ozoneDB.xml.dom4j.OzoneDocumentFactory;
22
23 import java.io.IOException JavaDoc;
24 import java.io.ObjectInputStream JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /** <p><code>OzoneDocumentFactoryImpl</code> is a collection of factory methods to allow
30  * easy custom building of DOM4J trees. The default tree that is built uses
31  * a doubly linked tree. </p>
32  *
33  * <p>The tree built allows full XPath expressions from anywhere on the
34  * tree.</p>
35  *
36  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
37  * @author Per Nyfelt
38  * @version $Revision: 1.1 $
39  */

40 public class OzoneDocumentFactoryImpl extends OzoneObject implements OzoneDocumentFactory {
41
42     final static long serialVersionUID = 1L;
43
44     protected transient O3QNameCache qnameCache;
45     //protected O3QNameCache qnameCache;
46

47     protected transient O3NamespaceCache namespaceCache;
48     //protected O3NamespaceCache namespaceCache;
49

50     /** Default namespace prefix -> URI mappings for XPath expressions to use */
51     private Map JavaDoc xpathNamespaceURIs;
52
53     /** A map of instances, one for each database */
54     private static transient Map JavaDoc factories = new HashMap JavaDoc();
55
56     public static synchronized OzoneDocumentFactory getInstance(OzoneInterface db) {
57         try {
58             OzoneDocumentFactory factory = (OzoneDocumentFactory)factories.get(db);
59             if ( factory == null) {
60                 System.out.println("looking for OzoneDocumentFactory instance (" + OBJECT_NAME + ") for database " + db);
61                 factory = (OzoneDocumentFactory) db.objectForName(OBJECT_NAME);
62                 if (factory == null) {
63                     System.out.println("creating new OzoneDocumentFactory");
64                     factory = OzoneDocumentFactoryImpl.create(db);
65                 }
66                 factories.put(db, factory);
67             }
68             System.out.println("returning factory " + factory);
69             return factory;
70         } catch (Exception JavaDoc e) {
71             throw new UnexpectedException("failed to get OzoneDocumentFactory instance!", e);
72         }
73     }
74
75     public static OzoneDocumentFactory create(OzoneInterface db) {
76         return (OzoneDocumentFactory) db.createObject(OzoneDocumentFactoryImpl.class,
77                 OzoneInterface.Public,
78                 OBJECT_NAME);
79     }
80
81     public OzoneDocumentFactoryImpl() {
82     }
83
84     public void onCreate() {
85         namespaceCache = new O3NamespaceCache(database());
86         qnameCache = new org.ozoneDB.xml.dom4j.o3impl.O3QNameCache(this, database());
87     }
88
89     public void onActivate() {
90         //System.out.println("setting transient database reference on namespace");
91
// namespaceCache.setDatabase(database());
92
// qnameCache.setDatabase(database());
93
factories = new HashMap JavaDoc();
94         namespaceCache = new O3NamespaceCache(database());
95         qnameCache = new O3QNameCache(this, database());
96     }
97
98     public void onPassivate() {
99         // todo: we should check that the caches has not grown to big, if it has we should shrink it
100
}
101
102
103     // Factory methods
104

105     public Document createDocument() {
106         OzoneDocument answer = (OzoneDocument) database()
107                 .createObject(OzoneDocumentImpl.class);
108         return answer;
109     }
110
111     public Document createDocument(Element rootElement) {
112         Document answer = createDocument();
113         answer.setRootElement(rootElement);
114         return answer;
115     }
116
117
118     public Document createDocument(String JavaDoc name) {
119         OzoneDocument answer = OzoneDocumentImpl.create(database(), name);
120         return answer;
121     }
122
123     public Document createDocument(Element rootElement, String JavaDoc name) {
124         Document answer = createDocument(name);
125         answer.setRootElement(rootElement);
126         return answer;
127     }
128
129     public DocumentType createDocType(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) {
130         return OzoneDocumentTypeImpl.create(database(), name, publicId, systemId);
131     }
132
133     public Element createElement(QName qname) {
134         return OzoneElementImpl.create(database(), qname);
135     }
136
137     public Element createElement(String JavaDoc name) {
138         //return createElement(createQName(name));
139
return OzoneElementImpl.create(database(), name);
140     }
141
142     public Element createElement(String JavaDoc qualifiedName, String JavaDoc namespaceURI) {
143         return createElement(createQName(qualifiedName, namespaceURI));
144     }
145
146     public Attribute createAttribute(Element owner, QName qname, String JavaDoc value) {
147         //System.out.println("[OzoneDocumentFactoryImpl] creating an OzoneAttributeImpl");
148
Attribute att = OzoneAttributeImpl.create(database(), owner, qname, value);
149         //System.out.println("created attribute " + att);
150
return att;
151     }
152
153     public Attribute createAttribute(Element owner, String JavaDoc name, String JavaDoc value) {
154         //System.out.println("[OzoneDocumentFactoryImpl] About to create an attribute");
155
// skip the cache for now
156
return createAttribute(owner, createQName(name), value);
157         //return OzoneAttributeImpl.create(database(), owner, name, value);
158
}
159
160     public CDATA createCDATA(String JavaDoc text) {
161         return OzoneCDATAImpl.create(database(), text);
162     }
163
164     public Comment createComment(String JavaDoc text) {
165         return OzoneCommentImpl.create(database(), text);
166     }
167
168     public Text createText(String JavaDoc text) {
169         if (text == null) {
170             throw new IllegalArgumentException JavaDoc("Adding text to an XML document must not be null");
171         }
172         return OzoneTextImpl.create(database(), text);
173     }
174
175
176     public Entity createEntity(String JavaDoc name, String JavaDoc text) {
177         return OzoneEntityImpl.create(database(), name, text);
178     }
179
180     public Namespace createNamespace(String JavaDoc prefix, String JavaDoc uri) {
181         return namespaceCache.get(prefix, uri);
182     }
183
184     public Namespace getNamespace(String JavaDoc uri) {
185         return namespaceCache.get(uri);
186     }
187
188     public Namespace getXmlNameSpace() {
189         return namespaceCache.getXmlNamespace();
190     }
191
192     public Namespace getNoNamespace() {
193         return namespaceCache.getNoNamespace();
194     }
195
196     public ProcessingInstruction createProcessingInstruction(String JavaDoc target, String JavaDoc data) {
197         return OzoneProcessingInstructionImpl.create(database(), target, data);
198     }
199
200     public ProcessingInstruction createProcessingInstruction(String JavaDoc target, Map JavaDoc data) {
201         return OzoneProcessingInstructionImpl.create(database(), target, data);
202     }
203
204     //These cannot be created as persistent objects
205
public QName createQName(String JavaDoc localName, Namespace namespace) {
206         return qnameCache.get(localName, namespace);
207     }
208
209     public QName createQName(String JavaDoc localName) {
210         return qnameCache.get(localName);
211     }
212
213     public QName createQName(String JavaDoc name, String JavaDoc prefix, String JavaDoc uri) {
214         return qnameCache.get(name, namespaceCache.get(prefix, uri));
215     }
216
217     public QName createQName(String JavaDoc qualifiedName, String JavaDoc uri) {
218         return qnameCache.get(qualifiedName, uri);
219     }
220
221     /** <p><code>createXPath</code> parses an XPath expression
222      * and creates a new XPath <code>XPath</code> instance.</p>
223      *
224      * @param xpathExpression is the XPath expression to create
225      * @return a new <code>XPath</code> instance
226      * @throws org.dom4j.InvalidXPathException if the XPath expression is invalid
227      */

228     public XPath createXPath(String JavaDoc xpathExpression) throws InvalidXPathException {
229 // Not sure if there is a need for persistent Xpath expressions so reverting to the
230
// Default implementation until I know for sure
231
// OzoneXPath xpath = OzoneXPathImpl.create(database(), xpathExpression);
232
// if (xpathNamespaceURIs != null) {
233
// xpath.setNamespaceURIs(xpathNamespaceURIs);
234
// }
235
// return xpath;
236
XPath xpath = new DefaultXPath(xpathExpression);
237         if (xpathNamespaceURIs != null) {
238             xpath.setNamespaceURIs(xpathNamespaceURIs);
239         }
240         return xpath;
241     }
242
243     /** <p><code>createXPath</code> parses an XPath expression
244      * and creates a new XPath <code>XPath</code> instance.</p>
245      *
246      * @param xpathExpression is the XPath expression to create
247      * @param variableContext is the variable context to use when evaluating the XPath
248      * @return a new <code>XPath</code> instance
249      * @throws org.dom4j.InvalidXPathException if the XPath expression is invalid
250      */

251     public XPath createXPath(String JavaDoc xpathExpression, VariableContext variableContext) {
252         XPath xpath = createXPath(xpathExpression);
253         xpath.setVariableContext(variableContext);
254         return xpath;
255     }
256
257     /** <p><code>createXPathFilter</code> parses a NodeFilter
258      * from the given XPath filter expression.
259      * XPath filter expressions occur within XPath expressions such as
260      * <code>self::node()[ filterExpression ]</code></p>
261      *
262      * @param xpathFilterExpression is the XPath filter expression
263      * to create
264      * @param variableContext is the variable context to use when evaluating the XPath
265      * @return a new <code>NodeFilter</code> instance
266      */

267     public NodeFilter createXPathFilter(String JavaDoc xpathFilterExpression, VariableContext variableContext) {
268         XPath answer = createXPath(xpathFilterExpression);
269         //OzoneXPath answer = new OzoneXPath( xpathFilterExpression );
270
answer.setVariableContext(variableContext);
271         return answer;
272     }
273
274     /** <p><code>createXPathFilter</code> parses a NodeFilter
275      * from the given XPath filter expression.
276      * XPath filter expressions occur within XPath expressions such as
277      * <code>self::node()[ filterExpression ]</code></p>
278      *
279      * @param xpathFilterExpression is the XPath filter expression
280      * to create
281      * @return a new <code>NodeFilter</code> instance
282      */

283     public NodeFilter createXPathFilter(String JavaDoc xpathFilterExpression) {
284         return createXPath(xpathFilterExpression);
285         //return new OzoneXPath( xpathFilterExpression );
286
}
287
288     /** <p><code>createPattern</code> parses the given
289      * XPath expression to create an XSLT style {@link org.dom4j.rule.Pattern} instance
290      * which can then be used in an XSLT processing model.</p>
291      *
292      * @param xpathPattern is the XPath pattern expression
293      * to create
294      * @return a new <code>Pattern</code> instance
295      */

296     public Pattern createPattern(String JavaDoc xpathPattern) {
297         // not sure if persistable patterns are needed to sticking with
298
// the default implementation until proven to be otherwise
299
//return XPathPatternImpl.create(database(), xpathPattern);
300
return new XPathPattern(xpathPattern);
301     }
302
303
304     // Properties
305
//-------------------------------------------------------------------------
306

307     /** Returns a list of all the QNameImpl instances currently used by this document factory
308      * @deprecated or was it just forgotten when the interfaces war created?
309      */

310     public List JavaDoc getQNames() {
311         return qnameCache.getQNames();
312     }
313
314     /** @return the Map of namespace URIs that will be used by by XPath expressions
315      * to resolve namespace prefixes into namespace URIs. The map is keyed by
316      * namespace prefix and the value is the namespace URI. This value could well be
317      * null to indicate no namespace URIs are being mapped.
318      */

319     public Map JavaDoc getXPathNamespaceURIs() {
320         return xpathNamespaceURIs;
321     }
322
323     /** Sets the namespace URIs to be used by XPath expressions created by this factory
324      * or by nodes associated with this factory. The keys are namespace prefixes and the
325      * values are namespace URIs.
326      * @deprecated or was it just forgotten and should be in XPathFactory
327      */

328     public void setXPathNamespaceURIs(Map JavaDoc xpathNamespaceURIs) {
329         this.xpathNamespaceURIs = xpathNamespaceURIs;
330     }
331
332     // Implementation methods
333
//-------------------------------------------------------------------------
334
public O3NamespaceCache getNamespaceCache() {
335         return namespaceCache;
336     }
337
338     public void setNamespaceCache(O3NamespaceCache namespaceCache) {
339         this.namespaceCache = namespaceCache;
340     }
341
342     /** @return the cached QNameImpl instance if there is one or adds the given
343      * qname to the qnameCache if not
344      */

345     protected QName intern(QName qname) {
346         return qnameCache.intern(qname);
347     }
348
349     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
350         in.defaultReadObject();
351     }
352
353 }
354
355
356 /*
357  * Redistribution and use of this software and associated documentation
358  * ("Software"), with or without modification, are permitted provided
359  * that the following conditions are met:
360  *
361  * 1. Redistributions of source code must retain copyright
362  * statements and notices. Redistributions must also contain a
363  * copy of this document.
364  *
365  * 2. Redistributions in binary form must reproduce the
366  * above copyright notice, this list of conditions and the
367  * following disclaimer in the documentation and/or other
368  * materials provided with the distribution.
369  *
370  * 3. The name "DOM4J" must not be used to endorse or promote
371  * products derived from this Software without prior written
372  * permission of MetaStuff, Ltd. For written permission,
373  * please contact dom4j-info@metastuff.com.
374  *
375  * 4. Products derived from this Software may not be called "DOM4J"
376  * nor may "DOM4J" appear in their names without prior written
377  * permission of MetaStuff, Ltd. DOM4J is a registered
378  * trademark of MetaStuff, Ltd.
379  *
380  * 5. Due credit should be given to the DOM4J Project
381  * (http://dom4j.org/).
382  *
383  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
384  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
385  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
386  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
387  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
388  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
389  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
390  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
391  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
392  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
393  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
394  * OF THE POSSIBILITY OF SUCH DAMAGE.
395  *
396  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
397  *
398  * $Id: OzoneDocumentFactoryImpl.java,v 1.1 2003/11/02 18:10:03 per_nyfelt Exp $
399  */

400
Popular Tags