View Javadoc

1   /*
2    * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/expr/DefaultXPathFactory.java,v 1.11 2003/09/05 04:08:48 proyal Exp $
3    * $Revision: 1.11 $
4    * $Date: 2003/09/05 04:08:48 $
5    *
6    * ====================================================================
7    *
8    * Copyright (C) 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
13   * are met:
14   *
15   * 1. Redistributions of source code must retain the above copyright
16   *    notice, this list of conditions, and the following disclaimer.
17   *
18   * 2. Redistributions in binary form must reproduce the above copyright
19   *    notice, this list of conditions, and the disclaimer that follows
20   *    these conditions in the documentation and/or other materials
21   *    provided with the distribution.
22   *
23   * 3. The name "Jaxen" must not be used to endorse or promote products
24   *    derived from this software without prior written permission.  For
25   *    written permission, please contact license@jaxen.org.
26   *
27   * 4. Products derived from this software may not be called "Jaxen", nor
28   *    may "Jaxen" appear in their name, without prior written permission
29   *    from the Jaxen Project Management (pm@jaxen.org).
30   *
31   * In addition, we request (but do not require) that you include in the
32   * end-user documentation provided with the redistribution and/or in the
33   * software itself an acknowledgement equivalent to the following:
34   *     "This product includes software developed by the
35   *      Jaxen Project (http://www.jaxen.org/)."
36   * Alternatively, the acknowledgment may be graphical using the logos
37   * available at http://www.jaxen.org/
38   *
39   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42   * DISCLAIMED.  IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
43   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50   * SUCH DAMAGE.
51   *
52   * ====================================================================
53   * This software consists of voluntary contributions made by many
54   * individuals on behalf of the Jaxen Project and was originally
55   * created by bob mcwhirter <bob@werken.com> and
56   * James Strachan <jstrachan@apache.org>.  For more information on the
57   * Jaxen Project, please see <http://www.jaxen.org/>.
58   *
59   * $Id: DefaultXPathFactory.java,v 1.11 2003/09/05 04:08:48 proyal Exp $
60   */
61  package org.jaxen.expr;
62  
63  import org.jaxen.JaxenException;
64  import org.jaxen.expr.iter.IterableAncestorAxis;
65  import org.jaxen.expr.iter.IterableAncestorOrSelfAxis;
66  import org.jaxen.expr.iter.IterableAttributeAxis;
67  import org.jaxen.expr.iter.IterableAxis;
68  import org.jaxen.expr.iter.IterableChildAxis;
69  import org.jaxen.expr.iter.IterableDescendantAxis;
70  import org.jaxen.expr.iter.IterableDescendantOrSelfAxis;
71  import org.jaxen.expr.iter.IterableFollowingAxis;
72  import org.jaxen.expr.iter.IterableFollowingSiblingAxis;
73  import org.jaxen.expr.iter.IterableNamespaceAxis;
74  import org.jaxen.expr.iter.IterableParentAxis;
75  import org.jaxen.expr.iter.IterablePrecedingAxis;
76  import org.jaxen.expr.iter.IterablePrecedingSiblingAxis;
77  import org.jaxen.expr.iter.IterableSelfAxis;
78  import org.jaxen.saxpath.Axis;
79  
80  public class DefaultXPathFactory implements XPathFactory
81  {
82      public XPathExpr createXPath( Expr rootExpr ) throws JaxenException
83      {
84          return new DefaultXPathExpr( rootExpr );
85      }
86  
87      public PathExpr createPathExpr( FilterExpr filterExpr,
88                                      LocationPath locationPath ) throws JaxenException
89      {
90          return new DefaultPathExpr( filterExpr,
91                                      locationPath );
92      }
93  
94      public LocationPath createRelativeLocationPath() throws JaxenException
95      {
96          return new DefaultRelativeLocationPath();
97      }
98  
99      public LocationPath createAbsoluteLocationPath() throws JaxenException
100     {
101         return new DefaultAbsoluteLocationPath();
102     }
103 
104     public BinaryExpr createOrExpr( Expr lhs,
105                                     Expr rhs ) throws JaxenException
106     {
107         return new DefaultOrExpr( lhs,
108                                   rhs );
109     }
110 
111     public BinaryExpr createAndExpr( Expr lhs,
112                                      Expr rhs ) throws JaxenException
113     {
114         return new DefaultAndExpr( lhs,
115                                    rhs );
116     }
117 
118     public BinaryExpr createEqualityExpr( Expr lhs,
119                                           Expr rhs,
120                                           int equalityOperator ) throws JaxenException
121     {
122         switch( equalityOperator )
123         {
124             case EQUALS:
125                 {
126                     return new DefaultEqualsExpr( lhs,
127                                                   rhs );
128                 }
129             case NOT_EQUALS:
130                 {
131                     return new DefaultNotEqualsExpr( lhs,
132                                                      rhs );
133                 }
134         }
135         throw new JaxenException( "Unhandled operator in createEqualityExpr(): " + equalityOperator );
136     }
137 
138     public BinaryExpr createRelationalExpr( Expr lhs,
139                                             Expr rhs,
140                                             int relationalOperator ) throws JaxenException
141     {
142         switch( relationalOperator )
143         {
144             case LESS_THAN:
145                 {
146                     return new DefaultLessThanExpr( lhs,
147                                                     rhs );
148                 }
149             case GREATER_THAN:
150                 {
151                     return new DefaultGreaterThanExpr( lhs,
152                                                        rhs );
153                 }
154             case LESS_THAN_EQUALS:
155                 {
156                     return new DefaultLessThanEqualExpr( lhs,
157                                                          rhs );
158                 }
159             case GREATER_THAN_EQUALS:
160                 {
161                     return new DefaultGreaterThanEqualExpr( lhs,
162                                                             rhs );
163                 }
164         }
165         throw new JaxenException( "Unhandled operator in createRelationalExpr(): " + relationalOperator );
166     }
167 
168     public BinaryExpr createAdditiveExpr( Expr lhs,
169                                           Expr rhs,
170                                           int additiveOperator ) throws JaxenException
171     {
172         switch( additiveOperator )
173         {
174             case ADD:
175                 {
176                     return new DefaultPlusExpr( lhs,
177                                                 rhs );
178                 }
179             case SUBTRACT:
180                 {
181                     return new DefaultMinusExpr( lhs,
182                                                  rhs );
183                 }
184         }
185         throw new JaxenException( "Unhandled operator in createAdditiveExpr(): " + additiveOperator );
186     }
187 
188     public BinaryExpr createMultiplicativeExpr( Expr lhs,
189                                                 Expr rhs,
190                                                 int multiplicativeOperator ) throws JaxenException
191     {
192         switch( multiplicativeOperator )
193         {
194             case MULTIPLY:
195                 {
196                     return new DefaultMultiplyExpr( lhs,
197                                                     rhs );
198                 }
199             case DIV:
200                 {
201                     return new DefaultDivExpr( lhs,
202                                                rhs );
203                 }
204             case MOD:
205                 {
206                     return new DefaultModExpr( lhs,
207                                                rhs );
208                 }
209         }
210         throw new JaxenException( "Unhandled operator in createMultiplicativeExpr(): " + multiplicativeOperator );
211     }
212 
213     public Expr createUnaryExpr( Expr expr,
214                                  int unaryOperator ) throws JaxenException
215     {
216         switch( unaryOperator )
217         {
218             case NEGATIVE:
219                 {
220                     return new DefaultUnaryExpr( expr );
221                 }
222         }
223         return expr;
224     }
225 
226     public UnionExpr createUnionExpr( Expr lhs,
227                                       Expr rhs ) throws JaxenException
228     {
229         return new DefaultUnionExpr( lhs,
230                                      rhs );
231     }
232 
233     public FilterExpr createFilterExpr( Expr expr ) throws JaxenException
234     {
235         return new DefaultFilterExpr( expr, createPredicateSet() );
236     }
237 
238     public FunctionCallExpr createFunctionCallExpr( String prefix,
239                                                     String functionName ) throws JaxenException
240     {
241         return new DefaultFunctionCallExpr( prefix,
242                                             functionName );
243     }
244 
245     public NumberExpr createNumberExpr( int number ) throws JaxenException
246     {
247         // return new DefaultNumberExpr( new Integer(number) );
248         return new DefaultNumberExpr( new Double( number ) );
249     }
250 
251     public NumberExpr createNumberExpr( double number ) throws JaxenException
252     {
253         return new DefaultNumberExpr( new Double( number ) );
254     }
255 
256     public LiteralExpr createLiteralExpr( String literal ) throws JaxenException
257     {
258         return new DefaultLiteralExpr( literal );
259     }
260 
261     public VariableReferenceExpr createVariableReferenceExpr( String prefix,
262                                                               String variable ) throws JaxenException
263     {
264         return new DefaultVariableReferenceExpr( prefix,
265                                                  variable );
266     }
267 
268     public Step createNameStep( int axis,
269                                 String prefix,
270                                 String localName ) throws JaxenException
271     {
272         IterableAxis iter = getIterableAxis( axis );
273         return new DefaultNameStep( iter,
274                                     prefix,
275                                     localName,
276                                     createPredicateSet() );
277     }
278 
279     public Step createTextNodeStep( int axis ) throws JaxenException
280     {
281         IterableAxis iter = getIterableAxis( axis );
282         return new DefaultTextNodeStep( iter, createPredicateSet() );
283     }
284 
285     public Step createCommentNodeStep( int axis ) throws JaxenException
286     {
287         IterableAxis iter = getIterableAxis( axis );
288         return new DefaultCommentNodeStep( iter, createPredicateSet() );
289     }
290 
291     public Step createAllNodeStep( int axis ) throws JaxenException
292     {
293         IterableAxis iter = getIterableAxis( axis );
294         return new DefaultAllNodeStep( iter, createPredicateSet() );
295     }
296 
297     public Step createProcessingInstructionNodeStep( int axis,
298                                                      String piName ) throws JaxenException
299     {
300         IterableAxis iter = getIterableAxis( axis );
301         return new DefaultProcessingInstructionNodeStep( iter,
302                                                          piName,
303                                                          createPredicateSet() );
304     }
305 
306     public Predicate createPredicate( Expr predicateExpr ) throws JaxenException
307     {
308         return new DefaultPredicate( predicateExpr );
309     }
310 
311     protected IterableAxis getIterableAxis( int axis )
312     {
313         IterableAxis iter = null;
314         switch( axis )
315         {
316             case Axis.CHILD:
317                 {
318                     iter = new IterableChildAxis( axis );
319                     break;
320                 }
321             case Axis.DESCENDANT:
322                 {
323                     iter = new IterableDescendantAxis( axis );
324                     break;
325                 }
326             case Axis.PARENT:
327                 {
328                     iter = new IterableParentAxis( axis );
329                     break;
330                 }
331             case Axis.FOLLOWING_SIBLING:
332                 {
333                     iter = new IterableFollowingSiblingAxis( axis );
334                     break;
335                 }
336             case Axis.PRECEDING_SIBLING:
337                 {
338                     iter = new IterablePrecedingSiblingAxis( axis );
339                     break;
340                 }
341             case Axis.FOLLOWING:
342                 {
343                     iter = new IterableFollowingAxis( axis );
344                     break;
345                 }
346             case Axis.PRECEDING:
347                 {
348                     iter = new IterablePrecedingAxis( axis );
349                     break;
350                 }
351             case Axis.ATTRIBUTE:
352                 {
353                     iter = new IterableAttributeAxis( axis );
354                     break;
355                 }
356             case Axis.NAMESPACE:
357                 {
358                     iter = new IterableNamespaceAxis( axis );
359                     break;
360                 }
361             case Axis.SELF:
362                 {
363                     iter = new IterableSelfAxis( axis );
364                     break;
365                 }
366             case Axis.DESCENDANT_OR_SELF:
367                 {
368                     iter = new IterableDescendantOrSelfAxis( axis );
369                     break;
370                 }
371             case Axis.ANCESTOR_OR_SELF:
372                 {
373                     iter = new IterableAncestorOrSelfAxis( axis );
374                     break;
375                 }
376             case Axis.ANCESTOR:
377                 {
378                     iter = new IterableAncestorAxis( axis );
379                     break;
380                 }
381         }
382         return iter;
383     }
384 
385     public PredicateSet createPredicateSet() throws JaxenException
386     {
387         return new PredicateSet();
388     }
389 }