1 /*
2  * $Header: /cvsroot/jaxen/jaxen/src/java/main/org/jaxen/expr/DefaultLocationPath.java,v 1.11 2002/04/26 17:17:34 jstrachan Exp $
3  * $Revision: 1.11 $
4  * $Date: 2002/04/26 17:17:34 $
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: DefaultLocationPath.java,v 1.11 2002/04/26 17:17:34 jstrachan Exp $
60  */
61 
62 
63 
64 package org.jaxen.expr;
65 
66 import org.jaxen.Context;
67 import org.jaxen.ContextSupport;
68 import org.jaxen.UnsupportedAxisException;
69 import org.jaxen.JaxenException;
70 
71 import org.jaxen.util.IdentityHashMap;
72 
73 import org.jaxen.util.SingleObjectIterator;
74 import org.jaxen.util.LinkedIterator;
75 
76 import java.util.List;
77 import java.util.ArrayList;
78 import java.util.LinkedList;
79 import java.util.Iterator;
80 import java.util.Map;
81 import java.util.Collections;
82 
83 abstract class DefaultLocationPath extends DefaultExpr implements LocationPath
84 {
85     private List steps;
86 
87     private final static Object PRESENT = new Object();
88 
DefaultLocationPath()89     public DefaultLocationPath()
90     {
91         this.steps = new LinkedList();
92     }
93 
addStep(Step step)94     public void addStep(Step step)
95     {
96         getSteps().add( step );
97     }
98 
getSteps()99     public List getSteps()
100     {
101         return this.steps;
102     }
103 
simplify()104     public Expr simplify()
105     {
106         Iterator stepIter = getSteps().iterator();
107         Step     eachStep = null;
108 
109         while ( stepIter.hasNext() )
110         {
111             eachStep = (Step) stepIter.next();
112 
113             eachStep.simplify();
114         }
115         return this;
116     }
117 
getText()118     public String getText()
119     {
120         StringBuffer buf = new StringBuffer();
121         Iterator stepIter = getSteps().iterator();
122 
123         while ( stepIter.hasNext() )
124         {
125             buf.append( ((Step)stepIter.next()).getText() );
126 
127             if ( stepIter.hasNext() )
128             {
129                 buf.append( "/" );
130             }
131         }
132 
133         return buf.toString();
134     }
135 
toString()136     public String toString()
137     {
138         StringBuffer buf = new StringBuffer();
139 
140         Iterator stepIter = getSteps().iterator();
141 
142         while( stepIter.hasNext() )
143         {
144             buf.append( stepIter.next().toString() );
145 
146             if ( stepIter.hasNext() )
147             {
148                 buf.append("/");
149             }
150         }
151 
152         return buf.toString();
153     }
154 
isAbsolute()155     public boolean isAbsolute()
156     {
157         return false;
158     }
159 
evaluate(Context context)160     public Object evaluate(Context context) throws JaxenException
161     {
162         List     contextNodeSet  = new ArrayList();
163         Map      unique          = new IdentityHashMap();
164 
165         contextNodeSet.addAll( context.getNodeSet() );
166 
167         Object   eachContextNode = null;
168 
169         Iterator stepIter        = getSteps().iterator();
170         Step     eachStep        = null;
171 
172         List     newNodeSet      = new ArrayList();
173 
174         int      contextSize     = 0;
175 
176       OUTTER:
177         while ( stepIter.hasNext() )
178         {
179             eachStep = (Step) stepIter.next();
180 
181             contextSize = contextNodeSet.size();
182 
183           INNER:
184             for ( int i = 0 ; i < contextSize ; ++i )
185             {
186                 eachContextNode = contextNodeSet.get( i );
187 
188                 Iterator axisNodeIter = eachStep.axisIterator( eachContextNode,
189                                                                context.getContextSupport() );
190 
191                 if ( axisNodeIter == null )
192                 {
193                     continue INNER;
194                 }
195 
196                 Object   eachAxisNode = null;
197 
198                 List interimSet=new ArrayList();
199 
200                 while ( axisNodeIter.hasNext() )
201                 {
202                     eachAxisNode = axisNodeIter.next();
203 
204                     // System.err.println( "----> " + eachAxisNode + " // " + eachStep.matches( eachAxisNode, context.getContextSupport()  ) );
205 
206                     if ( eachStep.matches( eachAxisNode,
207                                            context.getContextSupport() ) )
208                     {
209                         if ( ! unique.containsKey( eachAxisNode ) )
210                         {
211                             unique.put( eachAxisNode,
212                                         PRESENT );
213                             interimSet.add( eachAxisNode );
214                         }
215                     }
216                 }
217 
218 		List filtered = eachStep.getPredicateSet().evaluatePredicates(interimSet,context.getContextSupport() );
219 
220 		newNodeSet.addAll(filtered);
221             }
222 
223             contextNodeSet.clear();
224             contextNodeSet.addAll( newNodeSet );
225             newNodeSet.clear();
226             unique.clear();
227         }
228 
229         return contextNodeSet;
230     }
231 }
232