View Javadoc

1   /*
2    * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/DefaultNavigator.java,v 1.19 2006/02/05 21:47:41 elharo Exp $
3    * $Revision: 1.19 $
4    * $Date: 2006/02/05 21:47:41 $
5    *
6    * ====================================================================
7    *
8    * Copyright 2000-2002 bob mcwhirter & James Strachan.
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions are
13   * met:
14   * 
15   *   * Redistributions of source code must retain the above copyright
16   *     notice, this list of conditions and the following disclaimer.
17   * 
18   *   * Redistributions in binary form must reproduce the above copyright
19   *     notice, this list of conditions and the following disclaimer in the
20   *     documentation and/or other materials provided with the distribution.
21   * 
22   *   * Neither the name of the Jaxen Project nor the names of its
23   *     contributors may be used to endorse or promote products derived 
24   *     from this software without specific prior written permission.
25   * 
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
30   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   *
38   * ====================================================================
39   * This software consists of voluntary contributions made by many 
40   * individuals on behalf of the Jaxen Project and was originally 
41   * created by bob mcwhirter <bob@werken.com> and 
42   * James Strachan <jstrachan@apache.org>.  For more information on the 
43   * Jaxen Project, please see <http://www.jaxen.org/>.
44   * 
45   * $Id: DefaultNavigator.java,v 1.19 2006/02/05 21:47:41 elharo Exp $
46   */
47  
48  
49  
50  package org.jaxen;
51  
52  import java.util.Iterator;
53  
54  import org.jaxen.pattern.Pattern;
55  import org.jaxen.util.AncestorAxisIterator;
56  import org.jaxen.util.AncestorOrSelfAxisIterator;
57  import org.jaxen.util.DescendantAxisIterator;
58  import org.jaxen.util.DescendantOrSelfAxisIterator;
59  import org.jaxen.util.FollowingAxisIterator;
60  import org.jaxen.util.FollowingSiblingAxisIterator;
61  import org.jaxen.util.PrecedingAxisIterator;
62  import org.jaxen.util.PrecedingSiblingAxisIterator;
63  import org.jaxen.util.SelfAxisIterator;
64  
65  /*** Default implementation of {@link Navigator}.
66   *
67   *  <p>
68   *  This implementation is an abstract class, since
69   *  some required operations cannot be implemented without
70   *  additional knowledge of the object model.
71   *  </p>
72   *
73   *  <p>
74   *  When possible, default method implementations build
75   *  upon each other, to reduce the number of methods required
76   *  to be implemented for each object model.  All methods,
77   *  of course, may be overridden, to provide more-efficient
78   *  implementations.
79   *  </p>
80   *
81   *  @author bob mcwhirter (bob@werken.com)
82   *  @author Erwin Bolwidt (ejb@klomp.org)
83   */
84  public abstract class DefaultNavigator implements Navigator
85  {
86  
87      /*** Throws <code>UnsupportedAxisException</code>
88       * 
89       * @param contextNode
90       * @return never returns
91       * @throws UnsupportedAxisException always
92       */
93      public Iterator getChildAxisIterator(Object contextNode) throws UnsupportedAxisException
94      {
95          throw new UnsupportedAxisException("child");
96      }
97  
98      /* (non-Javadoc)
99       * @see org.jaxen.Navigator#getDescendantAxisIterator(java.lang.Object)
100      */
101     public Iterator getDescendantAxisIterator(Object contextNode) throws UnsupportedAxisException
102     {
103         return new DescendantAxisIterator( contextNode,
104                                            this );
105     }
106 
107     /*** Throws <code>UnsupportedAxisException</code>
108      * 
109      * @param  contextNode
110      * @return never returns
111      * @throws UnsupportedAxisException
112      */
113     public Iterator getParentAxisIterator(Object contextNode) throws UnsupportedAxisException
114     {
115         throw new UnsupportedAxisException("parent");
116     }
117 
118     public Iterator getAncestorAxisIterator(Object contextNode) throws UnsupportedAxisException
119     {
120         return new AncestorAxisIterator( contextNode,
121                                          this );
122     }
123 
124 
125     public Iterator getFollowingSiblingAxisIterator(Object contextNode) throws UnsupportedAxisException
126     {
127         return new FollowingSiblingAxisIterator( contextNode,
128                                                  this );
129     }
130 
131 
132     public Iterator getPrecedingSiblingAxisIterator(Object contextNode) throws UnsupportedAxisException
133     {
134         return new PrecedingSiblingAxisIterator( contextNode,
135                                                  this );
136     }
137 
138     public Iterator getFollowingAxisIterator(Object contextNode) throws UnsupportedAxisException
139     {
140         return new FollowingAxisIterator( contextNode,
141                                           this );
142 
143         // throw new UnsupportedAxisException("following");
144     }
145 
146 
147     public Iterator getPrecedingAxisIterator(Object contextNode) throws UnsupportedAxisException
148     {
149         return new PrecedingAxisIterator( contextNode,
150                                          this );
151 
152         // throw new UnsupportedAxisException("preceding");
153     }
154 
155     /*** Throws <code>UnsupportedAxisException</code>. Subclasses that 
156      * support the attribute axis must override this method.
157      * 
158      * @param contextNode
159      * @return never returns
160      * @throws UnsupportedAxisException
161      */
162     public Iterator getAttributeAxisIterator(Object contextNode) throws UnsupportedAxisException
163     {
164         throw new UnsupportedAxisException("attribute");
165     }
166 
167     /*** Throws <code>UnsupportedAxisException</code>. Subclasses that 
168      * support the namespace axis must override this method.
169      * 
170      * @param contextNode
171      * @return never returns
172      * @throws UnsupportedAxisException
173      */
174     public Iterator getNamespaceAxisIterator(Object contextNode) throws UnsupportedAxisException
175     {
176         throw new UnsupportedAxisException("namespace");
177     }
178 
179     public Iterator getSelfAxisIterator(Object contextNode) throws UnsupportedAxisException
180     {
181         return new SelfAxisIterator( contextNode );
182     }
183 
184     public Iterator getDescendantOrSelfAxisIterator(Object contextNode) throws UnsupportedAxisException
185     {
186         return new DescendantOrSelfAxisIterator( contextNode,
187                                                  this );
188     }
189 
190     public Iterator getAncestorOrSelfAxisIterator(Object contextNode) throws UnsupportedAxisException
191     {
192         return new AncestorOrSelfAxisIterator( contextNode,
193                                                this );
194     }
195 
196     public Object getDocumentNode(Object contextNode)
197     {
198         return null;
199     }
200     
201     public String translateNamespacePrefixToUri(String prefix, Object element)
202     {
203         return null;
204     }
205 
206     public String getProcessingInstructionTarget(Object obj)
207     {
208         return null;
209     }
210 
211     public String getProcessingInstructionData(Object obj)
212     {
213         return null;
214     }
215 
216     public short getNodeType(Object node)
217     {
218         if ( isElement(node) ) 
219         {
220             return Pattern.ELEMENT_NODE;
221         }
222         else if ( isAttribute(node) ) 
223         {
224             return Pattern.ATTRIBUTE_NODE;
225         }
226         else if ( isText(node) ) 
227         {
228             return Pattern.TEXT_NODE;
229         }
230         else if ( isComment(node) ) 
231         {
232             return Pattern.COMMENT_NODE;
233         }
234         else if ( isDocument(node) ) 
235         {
236             return Pattern.DOCUMENT_NODE;
237         }
238         else if ( isProcessingInstruction(node) ) 
239         {
240             return Pattern.PROCESSING_INSTRUCTION_NODE;
241         }
242         else if ( isNamespace(node) ) 
243         {
244             return Pattern.NAMESPACE_NODE;
245         }
246         else {
247             return Pattern.UNKNOWN_NODE;
248         }
249     }
250     
251     /***
252      * Default inefficient implementation. Subclasses 
253      * should override this method.
254      *
255      * @param contextNode   the node whose parent to return
256      * @return the parent node
257      * @throws UnsupportedAxisException if the parent axis is not supported
258      */
259     public Object getParentNode(Object contextNode) throws UnsupportedAxisException
260     {
261         Iterator iter = getParentAxisIterator( contextNode );
262         if ( iter != null && iter.hasNext() )
263         {
264             return iter.next();
265         }
266         return null;
267     }
268 
269     /***
270      *  Default implementation that always returns null. Override in subclass
271      *  if the subclass can load documents. 
272      *
273      * @param url the URL of the document to load
274      *
275      * @return null
276      * @throws FunctionCallException if an error occurs while loading the
277      *    URL; e.g. an I/O error or the document is malformed
278      */
279     public Object getDocument(String url) throws FunctionCallException
280     {
281         return null;
282     }
283 
284     /***
285      *  Default implementation that cannot find elements. Override in subclass
286      *  if subclass does know about attribute types.
287      *
288      *  @param contextNode   a node from the document in which to look for the
289      *                       id
290      *  @param elementId   id to look for
291      *
292      *  @return   null
293      */
294     public Object getElementById(Object contextNode, String elementId)
295     {
296         return null;
297     }
298     
299 }