View Javadoc

1   /*
2    * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/expr/DefaultXPathFactory.java,v 1.12 2005/04/11 18:21:39 elharo Exp $
3    * $Revision: 1.12 $
4    * $Date: 2005/04/11 18:21:39 $
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.12 2005/04/11 18:21:39 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 Integer(number) );
249         return new DefaultNumberExpr( new Double( number ) );
250     }
251 
252     public NumberExpr createNumberExpr( double number ) throws JaxenException
253     {
254         return new DefaultNumberExpr( new Double( number ) );
255     }
256 
257     public LiteralExpr createLiteralExpr( String literal ) throws JaxenException
258     {
259         return new DefaultLiteralExpr( literal );
260     }
261 
262     public VariableReferenceExpr createVariableReferenceExpr( String prefix,
263                                                               String variable ) throws JaxenException
264     {
265         return new DefaultVariableReferenceExpr( prefix,
266                                                  variable );
267     }
268 
269     public Step createNameStep( int axis,
270                                 String prefix,
271                                 String localName ) throws JaxenException
272     {
273         IterableAxis iter = getIterableAxis( axis );
274         return new DefaultNameStep( iter,
275                                     prefix,
276                                     localName,
277                                     createPredicateSet() );
278     }
279 
280     public Step createTextNodeStep( int axis ) throws JaxenException
281     {
282         IterableAxis iter = getIterableAxis( axis );
283         return new DefaultTextNodeStep( iter, createPredicateSet() );
284     }
285 
286     public Step createCommentNodeStep( int axis ) throws JaxenException
287     {
288         IterableAxis iter = getIterableAxis( axis );
289         return new DefaultCommentNodeStep( iter, createPredicateSet() );
290     }
291 
292     public Step createAllNodeStep( int axis ) throws JaxenException
293     {
294         IterableAxis iter = getIterableAxis( axis );
295         return new DefaultAllNodeStep( iter, createPredicateSet() );
296     }
297 
298     public Step createProcessingInstructionNodeStep( int axis,
299                                                      String piName ) throws JaxenException
300     {
301         IterableAxis iter = getIterableAxis( axis );
302         return new DefaultProcessingInstructionNodeStep( iter,
303                                                          piName,
304                                                          createPredicateSet() );
305     }
306 
307     public Predicate createPredicate( Expr predicateExpr ) throws JaxenException
308     {
309         return new DefaultPredicate( predicateExpr );
310     }
311 
312     protected IterableAxis getIterableAxis( int axis )
313     {
314         IterableAxis iter = null;
315         switch( axis )
316         {
317             case Axis.CHILD:
318                 {
319                     iter = new IterableChildAxis( axis );
320                     break;
321                 }
322             case Axis.DESCENDANT:
323                 {
324                     iter = new IterableDescendantAxis( axis );
325                     break;
326                 }
327             case Axis.PARENT:
328                 {
329                     iter = new IterableParentAxis( axis );
330                     break;
331                 }
332             case Axis.FOLLOWING_SIBLING:
333                 {
334                     iter = new IterableFollowingSiblingAxis( axis );
335                     break;
336                 }
337             case Axis.PRECEDING_SIBLING:
338                 {
339                     iter = new IterablePrecedingSiblingAxis( axis );
340                     break;
341                 }
342             case Axis.FOLLOWING:
343                 {
344                     iter = new IterableFollowingAxis( axis );
345                     break;
346                 }
347             case Axis.PRECEDING:
348                 {
349                     iter = new IterablePrecedingAxis( axis );
350                     break;
351                 }
352             case Axis.ATTRIBUTE:
353                 {
354                     iter = new IterableAttributeAxis( axis );
355                     break;
356                 }
357             case Axis.NAMESPACE:
358                 {
359                     iter = new IterableNamespaceAxis( axis );
360                     break;
361                 }
362             case Axis.SELF:
363                 {
364                     iter = new IterableSelfAxis( axis );
365                     break;
366                 }
367             case Axis.DESCENDANT_OR_SELF:
368                 {
369                     iter = new IterableDescendantOrSelfAxis( axis );
370                     break;
371                 }
372             case Axis.ANCESTOR_OR_SELF:
373                 {
374                     iter = new IterableAncestorOrSelfAxis( axis );
375                     break;
376                 }
377             case Axis.ANCESTOR:
378                 {
379                     iter = new IterableAncestorAxis( axis );
380                     break;
381                 }
382         }
383         return iter;
384     }
385 
386     public PredicateSet createPredicateSet() throws JaxenException
387     {
388         return new PredicateSet();
389     }
390 }