View Javadoc

1   /*
2    * Copyright 2001-2004 (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.29 2004/08/05 10:16:48 maartenc Exp $
8    */
9   
10  package org.dom4j.tree;
11  
12  import java.io.IOException;
13  import java.io.Serializable;
14  import java.io.Writer;
15  import java.util.List;
16  
17  import org.dom4j.Document;
18  import org.dom4j.DocumentFactory;
19  import org.dom4j.Element;
20  import org.dom4j.Node;
21  import org.dom4j.NodeFilter;
22  import org.dom4j.XPath;
23  import org.dom4j.rule.Pattern;
24  
25  /*** <p><code>AbstractNode</code> is an abstract base class for 
26    * tree implementors to use for implementation inheritence.</p>
27   *
28   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
29   * @version $Revision: 1.29 $
30   */
31  public abstract class AbstractNode implements Node, Cloneable, Serializable {
32      
33      protected static final String[] NODE_TYPE_NAMES ={
34          "Node", "Element", "Attribute", "Text", "CDATA", "Entity", "Entity", "ProcessingInstruction", 
35          "Comment", "Document", "DocumentType", "DocumentFragment", "Notation", "Namespace","Unknown" 
36      };
37      
38      /*** The <code>DocumentFactory</code> instance used by default */
39      private static final DocumentFactory DOCUMENT_FACTORY = DocumentFactory.getInstance();
40      
41  
42      public AbstractNode() {
43      }
44  
45      public short getNodeType() {
46          return UNKNOWN_NODE;
47      }
48  
49      public String getNodeTypeName() {
50          int type = getNodeType();
51          if ( type < 0 || type >= NODE_TYPE_NAMES.length ) {
52              return "Unknown";
53          }
54          return NODE_TYPE_NAMES[type];
55      }
56      
57      public Document getDocument() {
58          Element element = getParent();
59          return ( element != null ) ? element.getDocument() : null;
60      }
61      
62      public void setDocument(Document document) {
63      }
64      
65      public Element getParent() {
66          return null;
67      }
68  
69      public void setParent(Element parent) {
70      }
71      
72      public boolean supportsParent() {
73          return false;
74      }
75  
76      public boolean isReadOnly() {
77          return true;
78      }
79      
80      public boolean hasContent() {
81          return false;
82      }
83      
84      public String getPath() {
85          return getPath(null);
86      }
87      
88      public String getUniquePath() {
89          return getUniquePath(null);
90      }
91      
92  
93      public Object clone() {
94          if ( isReadOnly() ) {
95              return this;
96          }
97          else {
98              try {
99                  Node answer = (Node) super.clone();
100                 answer.setParent( null );
101                 answer.setDocument( null );
102                 return answer;
103             }
104             catch (CloneNotSupportedException e) {
105                 // should never happen
106                 throw new RuntimeException( "This should never happen. Caught: " + e );
107             }
108         }
109     }
110 
111     public Node detach() {
112         Element parent = getParent();
113         if ( parent != null ) {
114             parent.remove( this );
115         }
116         else {
117             Document document = getDocument();
118             if ( document != null ) {
119                 document.remove( this );
120             }
121         }
122         setParent(null);
123         setDocument(null);
124         return this;
125     }
126     
127     public String getName() {
128         return null;
129     }
130 
131     public void setName(String name) {
132         throw new UnsupportedOperationException( "This node cannot be modified" );
133     }
134 
135     public String getText() {
136         return null;
137     }
138     
139     public String getStringValue() {
140         return getText();
141     }
142     
143     public void setText(String text) {
144         throw new UnsupportedOperationException( "This node cannot be modified" );
145     }
146     
147     
148     public void write(Writer writer) throws IOException {
149         writer.write( asXML() );
150     }
151         
152 
153     // XPath methods
154     
155     public Object selectObject(String xpathExpression) {
156         XPath xpath = createXPath(xpathExpression);
157         return xpath.selectObject(this);
158     }
159     
160     public List selectNodes(String xpathExpression) {
161         XPath xpath = createXPath(xpathExpression);
162         return xpath.selectNodes(this);
163     }
164     
165     public List selectNodes( 
166         String xpathExpression, 
167         String comparisonXPathExpression 
168     ) {
169         return selectNodes( 
170             xpathExpression,  comparisonXPathExpression, false 
171         );
172     }
173     
174     public List selectNodes(
175         String xpathExpression, 
176         String comparisonXPathExpression, 
177         boolean removeDuplicates
178     ) {
179         XPath xpath = createXPath(xpathExpression);
180         XPath sortBy = createXPath(comparisonXPathExpression);
181         return xpath.selectNodes(this, sortBy, removeDuplicates);
182     }
183     
184     public Node selectSingleNode(String xpathExpression) {
185         XPath xpath = createXPath(xpathExpression);
186         return xpath.selectSingleNode(this);
187     }
188     
189     public String valueOf(String xpathExpression) {
190         XPath xpath = createXPath(xpathExpression);
191         return xpath.valueOf(this);
192     }
193     
194     public Number numberValueOf(String xpathExpression) {
195         XPath xpath = createXPath(xpathExpression);
196         return xpath.numberValueOf(this);
197     }
198     
199     public boolean matches(String patternText) {        
200         NodeFilter filter = createXPathFilter(patternText);
201         return filter.matches(this);
202     }
203     
204     public XPath createXPath(String xpathExpression) {
205         return getDocumentFactory().createXPath(xpathExpression);
206     }
207     
208     public NodeFilter createXPathFilter(String patternText) {
209         return getDocumentFactory().createXPathFilter(patternText);
210     }
211     
212     public Pattern createPattern(String patternText) {
213         return getDocumentFactory().createPattern(patternText);
214     }
215     
216     
217     public Node asXPathResult(Element parent) {
218         if (supportsParent()) {
219             return this;
220         }
221         return createXPathResult(parent);
222     }
223     
224     protected DocumentFactory getDocumentFactory() {
225         return DOCUMENT_FACTORY;
226     }
227     
228     protected Node createXPathResult(Element parent) {
229         throw new RuntimeException("asXPathResult() not yet implemented fully for: " + this );
230     }
231     
232 }
233 
234 
235 
236 
237 /*
238  * Redistribution and use of this software and associated documentation
239  * ("Software"), with or without modification, are permitted provided
240  * that the following conditions are met:
241  *
242  * 1. Redistributions of source code must retain copyright
243  *    statements and notices.  Redistributions must also contain a
244  *    copy of this document.
245  *
246  * 2. Redistributions in binary form must reproduce the
247  *    above copyright notice, this list of conditions and the
248  *    following disclaimer in the documentation and/or other
249  *    materials provided with the distribution.
250  *
251  * 3. The name "DOM4J" must not be used to endorse or promote
252  *    products derived from this Software without prior written
253  *    permission of MetaStuff, Ltd.  For written permission,
254  *    please contact dom4j-info@metastuff.com.
255  *
256  * 4. Products derived from this Software may not be called "DOM4J"
257  *    nor may "DOM4J" appear in their names without prior written
258  *    permission of MetaStuff, Ltd. DOM4J is a registered
259  *    trademark of MetaStuff, Ltd.
260  *
261  * 5. Due credit should be given to the DOM4J Project - 
262  *    http://www.dom4j.org
263  *
264  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
265  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
266  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
267  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
268  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
269  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
270  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
271  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
272  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
273  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
274  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
275  * OF THE POSSIBILITY OF SUCH DAMAGE.
276  *
277  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
278  *
279  * $Id: AbstractNode.java,v 1.29 2004/08/05 10:16:48 maartenc Exp $
280  */