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