KickJava   Java API By Example, From Geeks To Geeks.

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


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: AbstractNode.java,v 1.3 2003/06/10 16:18:36 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.ozoneDB.OzoneObject;
15
16 import java.io.IOException JavaDoc;
17 import java.io.Serializable JavaDoc;
18 import java.io.Writer JavaDoc;
19 import java.util.List JavaDoc;
20
21 /** <p><code>AbstractNode</code> is an abstract base class for
22  * tree implementors to use for implementation inheritence.</p>
23  *
24  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
25  * @version $Revision: 1.3 $
26  */

27 public abstract class AbstractNode extends OzoneObject implements Node, Cloneable JavaDoc, Serializable JavaDoc {
28
29     protected static final String JavaDoc[] NODE_TYPE_NAMES = {
30         "Node", "Element", "Attribute", "Text", "CDATA", "Entity", "Entity", "ProcessingInstruction",
31         "Comment", "Document", "DocumentType", "DocumentFragment", "Notation", "Namespace", "Unknown"
32     };
33
34     /** The factory instances used by default */
35     private static NodeFactory NODE_FACTORY;
36     private static XPathFactory XPATH_FACTORY;
37
38     private NodeFactory nodeFactory;
39     private XPathFactory xpathFactory;
40
41     protected AbstractNode() {
42     }
43
44     public void onCreate() {
45         super.onCreate();
46         NODE_FACTORY = OzoneDocumentFactoryImpl.getInstance(database());
47         XPATH_FACTORY = OzoneDocumentFactoryImpl.getInstance(database());
48     }
49
50     public short getNodeType() {
51         return UNKNOWN_NODE;
52     }
53
54     public String JavaDoc getNodeTypeName() {
55         int type = getNodeType();
56         if (type < 0 || type >= NODE_TYPE_NAMES.length) {
57             return "Unknown";
58         }
59         return NODE_TYPE_NAMES[type];
60     }
61
62     public Document getDocument() {
63         Element element = getParent();
64         return (element != null) ? element.getDocument() : null;
65     }
66
67     public void setDocument(Document document) {
68     }
69
70     public Element getParent() {
71         return null;
72     }
73
74     public void setParent(Element parent) {
75     }
76
77     public boolean supportsParent() {
78         return false;
79     }
80
81     public boolean isReadOnly() {
82         return true;
83     }
84
85     public boolean hasContent() {
86         return false;
87     }
88
89     public String JavaDoc getPath() {
90         return getPath(null);
91     }
92
93     public String JavaDoc getUniquePath() {
94         return getUniquePath(null);
95     }
96
97
98     public Object JavaDoc clone() {
99         if (isReadOnly()) {
100             return self();
101         } else {
102             try {
103                 Node answer = (Node) super.clone();
104                 answer.setParent(null);
105                 answer.setDocument(null);
106                 return answer;
107             } catch (CloneNotSupportedException JavaDoc e) {
108                 // should never happen
109
throw new RuntimeException JavaDoc("This should never happen. Caught: " + e);
110             }
111         }
112     }
113
114     public Node detach() {
115         Element parent = getParent();
116         if (parent != null) {
117             parent.remove((Node)self());
118         } else {
119             Document document = getDocument();
120             if (document != null) {
121                 document.remove((Node)self());
122             }
123         }
124         setParent(null);
125         setDocument(null);
126         return (Node)self();
127     }
128
129     public String JavaDoc getName() {
130         return null;
131     }
132
133     public void setName(String JavaDoc name) {
134         throw new UnsupportedOperationException JavaDoc("This node cannot be modified");
135     }
136
137     public String JavaDoc getText() {
138         return null;
139     }
140
141     public String JavaDoc getStringValue() {
142         return getText();
143     }
144
145     public void setText(String JavaDoc text) {
146         throw new UnsupportedOperationException JavaDoc("This node cannot be modified");
147     }
148
149
150     public void write(Writer JavaDoc writer) throws IOException JavaDoc {
151         writer.write(asXML());
152     }
153
154
155     // XPath methods
156

157     public Object JavaDoc selectObject(String JavaDoc xpathExpression) {
158         XPath xpath = createXPath(xpathExpression);
159         return xpath.evaluate(self());
160     }
161
162     public List JavaDoc selectNodes(String JavaDoc xpathExpression) {
163         XPath xpath = createXPath(xpathExpression);
164         return xpath.selectNodes(self());
165     }
166
167     public List JavaDoc selectNodes(
168             String JavaDoc xpathExpression,
169             String JavaDoc comparisonXPathExpression
170             ) {
171         return selectNodes(
172                 xpathExpression, comparisonXPathExpression, false
173         );
174     }
175
176     public List JavaDoc selectNodes(
177             String JavaDoc xpathExpression,
178             String JavaDoc comparisonXPathExpression,
179             boolean removeDuplicates
180             ) {
181         XPath xpath = createXPath(xpathExpression);
182         XPath sortBy = createXPath(comparisonXPathExpression);
183         return xpath.selectNodes(self(), sortBy, removeDuplicates);
184     }
185
186     public Node selectSingleNode(String JavaDoc xpathExpression) {
187         XPath xpath = createXPath(xpathExpression);
188         return xpath.selectSingleNode(self());
189     }
190
191     public String JavaDoc valueOf(String JavaDoc xpathExpression) {
192         XPath xpath = createXPath(xpathExpression);
193         return xpath.valueOf(self());
194     }
195
196     public Number JavaDoc numberValueOf(String JavaDoc xpathExpression) {
197         XPath xpath = createXPath(xpathExpression);
198         return xpath.numberValueOf(self());
199     }
200
201     public boolean matches(String JavaDoc patternText) {
202         NodeFilter filter = createXPathFilter(patternText);
203         return filter.matches((Node)self());
204     }
205
206     public XPath createXPath(String JavaDoc xpathExpression) {
207         return getXPathFactory().createXPath(xpathExpression);
208     }
209
210     public NodeFilter createXPathFilter(String JavaDoc patternText) {
211         return getXPathFactory().createXPathFilter(patternText);
212     }
213
214     public Pattern createPattern(String JavaDoc patternText) {
215         return getXPathFactory().createPattern(patternText);
216     }
217
218
219     public Node asXPathResult(Element parent) {
220         if (supportsParent()) {
221             return (Node)self();
222         }
223         return createXPathResult(parent);
224     }
225
226     /**
227      * @deprecated Use getNodeFactory() and getXPathFactory() instead.
228      */

229     public DocumentFactory getDocumentFactory() {
230         return new DelegateDocumentFactory(getNodeFactory(), getXPathFactory());
231     }
232
233     /**
234      * @deprecated Use setNodeFactory() and setXPathFactory() instead.
235      */

236     public void setDocumentFactory(DocumentFactory factory) {
237         this.nodeFactory = factory;
238         this.xpathFactory = factory;
239     }
240
241     protected NodeFactory getNodeFactory() {
242         return (nodeFactory == null) ? NODE_FACTORY : nodeFactory;
243     }
244
245     protected void setNodeFactory(NodeFactory nodeFactory) {
246         this.nodeFactory = nodeFactory;
247     }
248
249     protected XPathFactory getXPathFactory() {
250         return (xpathFactory == null) ? XPATH_FACTORY : xpathFactory;
251     }
252
253     protected void setXPathFactory(XPathFactory xpathFactory) {
254         this.xpathFactory = xpathFactory;
255     }
256
257     protected Node createXPathResult(Element parent) {
258         throw new RuntimeException JavaDoc("asXPathResult() not yet implemented fully for: " + this);
259     }
260
261 }
262
263
264 /*
265  * Redistribution and use of this software and associated documentation
266  * ("Software"), with or without modification, are permitted provided
267  * that the following conditions are met:
268  *
269  * 1. Redistributions of source code must retain copyright
270  * statements and notices. Redistributions must also contain a
271  * copy of this document.
272  *
273  * 2. Redistributions in binary form must reproduce the
274  * above copyright notice, this list of conditions and the
275  * following disclaimer in the documentation and/or other
276  * materials provided with the distribution.
277  *
278  * 3. The name "DOM4J" must not be used to endorse or promote
279  * products derived from this Software without prior written
280  * permission of MetaStuff, Ltd. For written permission,
281  * please contact dom4j-info@metastuff.com.
282  *
283  * 4. Products derived from this Software may not be called "DOM4J"
284  * nor may "DOM4J" appear in their names without prior written
285  * permission of MetaStuff, Ltd. DOM4J is a registered
286  * trademark of MetaStuff, Ltd.
287  *
288  * 5. Due credit should be given to the DOM4J Project
289  * (http://dom4j.org/).
290  *
291  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
292  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
293  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
294  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
295  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
296  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
297  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
298  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
300  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
301  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
302  * OF THE POSSIBILITY OF SUCH DAMAGE.
303  *
304  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
305  *
306  * $Id: AbstractNode.java,v 1.3 2003/06/10 16:18:36 per_nyfelt Exp $
307  */

308
Popular Tags