View Javadoc

1   /*
2    * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/expr/DefaultXPathFactory.java,v 1.13 2005/04/17 13:19:29 elharo Exp $
3    * $Revision: 1.13 $
4    * $Date: 2005/04/17 13:19:29 $
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.13 2005/04/17 13:19:29 elharo 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  import org.jaxen.saxpath.Operator;
80  
81  public class DefaultXPathFactory implements XPathFactory
82  {
83      public XPathExpr createXPath( Expr rootExpr ) throws JaxenException
84      {
85          return new DefaultXPathExpr( rootExpr );
86      }
87  
88      public PathExpr createPathExpr( FilterExpr filterExpr,
89                                      LocationPath locationPath ) throws JaxenException
90      {
91          return new DefaultPathExpr( filterExpr,
92                                      locationPath );
93      }
94  
95      public LocationPath createRelativeLocationPath() throws JaxenException
96      {
97          return new DefaultRelativeLocationPath();
98      }
99  
100     public LocationPath createAbsoluteLocationPath() throws JaxenException
101     {
102         return new DefaultAbsoluteLocationPath();
103     }
104 
105     public BinaryExpr createOrExpr( Expr lhs,
106                                     Expr rhs ) throws JaxenException
107     {
108         return new DefaultOrExpr( lhs,
109                                   rhs );
110     }
111 
112     public BinaryExpr createAndExpr( Expr lhs,
113                                      Expr rhs ) throws JaxenException
114     {
115         return new DefaultAndExpr( lhs,
116                                    rhs );
117     }
118 
119     public BinaryExpr createEqualityExpr( Expr lhs,
120                                           Expr rhs,
121                                           int equalityOperator ) throws JaxenException
122     {
123         switch( equalityOperator )
124         {
125             case Operator.EQUALS:
126                 {
127                     return new DefaultEqualsExpr( lhs,
128                                                   rhs );
129                 }
130             case Operator.NOT_EQUALS:
131                 {
132                     return new DefaultNotEqualsExpr( lhs,
133                                                      rhs );
134                 }
135         }
136         throw new JaxenException( "Unhandled operator in createEqualityExpr(): " + equalityOperator );
137     }
138 
139     public BinaryExpr createRelationalExpr( Expr lhs,
140                                             Expr rhs,
141                                             int relationalOperator ) throws JaxenException
142     {
143         switch( relationalOperator )
144         {
145             case Operator.LESS_THAN:
146                 {
147                     return new DefaultLessThanExpr( lhs,
148                                                     rhs );
149                 }
150             case Operator.GREATER_THAN:
151                 {
152                     return new DefaultGreaterThanExpr( lhs,
153                                                        rhs );
154                 }
155             case Operator.LESS_THAN_EQUALS:
156                 {
157                     return new DefaultLessThanEqualExpr( lhs,
158                                                          rhs );
159                 }
160             case Operator.GREATER_THAN_EQUALS:
161                 {
162                     return new DefaultGreaterThanEqualExpr( lhs,
163                                                             rhs );
164                 }
165         }
166         throw new JaxenException( "Unhandled operator in createRelationalExpr(): " + relationalOperator );
167     }
168 
169     public BinaryExpr createAdditiveExpr( Expr lhs,
170                                           Expr rhs,
171                                           int additiveOperator ) throws JaxenException
172     {
173         switch( additiveOperator )
174         {
175             case Operator.ADD:
176                 {
177                     return new DefaultPlusExpr( lhs,
178                                                 rhs );
179                 }
180             case Operator.SUBTRACT:
181                 {
182                     return new DefaultMinusExpr( lhs,
183                                                  rhs );
184                 }
185         }
186         throw new JaxenException( "Unhandled operator in createAdditiveExpr(): " + additiveOperator );
187     }
188 
189     public BinaryExpr createMultiplicativeExpr( Expr lhs,
190                                                 Expr rhs,
191                                                 int multiplicativeOperator ) throws JaxenException
192     {
193         switch( multiplicativeOperator )
194         {
195             case Operator.MULTIPLY:
196                 {
197                     return new DefaultMultiplyExpr( lhs,
198                                                     rhs );
199                 }
200             case Operator.DIV:
201                 {
202                     return new DefaultDivExpr( lhs,
203                                                rhs );
204                 }
205             case Operator.MOD:
206                 {
207                     return new DefaultModExpr( lhs,
208                                                rhs );
209                 }
210         }
211         throw new JaxenException( "Unhandled operator in createMultiplicativeExpr(): " + multiplicativeOperator );
212     }
213 
214     public Expr createUnaryExpr( Expr expr,
215                                  int unaryOperator ) throws JaxenException
216     {
217         switch( unaryOperator )
218         {
219             case Operator.NEGATIVE:
220                 {
221                     return new DefaultUnaryExpr( expr );
222                 }
223         }
224         return expr;
225     }
226 
227     public UnionExpr createUnionExpr( Expr lhs,
228                                       Expr rhs ) throws JaxenException
229     {
230         return new DefaultUnionExpr( lhs,
231                                      rhs );
232     }
233 
234     public FilterExpr createFilterExpr( Expr expr ) throws JaxenException
235     {
236         return new DefaultFilterExpr( expr, createPredicateSet() );
237     }
238 
239     public FunctionCallExpr createFunctionCallExpr( String prefix,
240                                                     String functionName ) throws JaxenException
241     {
242         return new DefaultFunctionCallExpr( prefix,
243                                             functionName );
244     }
245 
246     public NumberExpr createNumberExpr( int number ) throws JaxenException
247     {
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 }