KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > patterns > UnionPattern


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: UnionPattern.java,v 1.13 2004/02/17 04:35:37 minchau Exp $
18  */

19 package org.apache.xpath.patterns;
20
21 import org.apache.xpath.Expression;
22 import org.apache.xpath.ExpressionOwner;
23 import org.apache.xpath.XPathContext;
24 import org.apache.xpath.XPathVisitor;
25 import org.apache.xpath.objects.XObject;
26
27 /**
28  * This class represents a union pattern, which can have multiple individual
29  * StepPattern patterns.
30  * @xsl.usage advanced
31  */

32 public class UnionPattern extends Expression
33 {
34
35   /** Array of the contained step patterns to be tested.
36    * @serial */

37   private StepPattern[] m_patterns;
38   
39   /**
40    * No arguments to process, so this does nothing.
41    */

42   public void fixupVariables(java.util.Vector JavaDoc vars, int globalsSize)
43   {
44     for (int i = 0; i < m_patterns.length; i++)
45     {
46       m_patterns[i].fixupVariables(vars, globalsSize);
47     }
48   }
49
50   
51   /**
52    * Tell if this expression or it's subexpressions can traverse outside
53    * the current subtree.
54    *
55    * @return true if traversal outside the context node's subtree can occur.
56    */

57    public boolean canTraverseOutsideSubtree()
58    {
59      if(null != m_patterns)
60      {
61       int n = m_patterns.length;
62       for (int i = 0; i < n; i++)
63       {
64         if(m_patterns[i].canTraverseOutsideSubtree())
65           return true;
66       }
67      }
68      return false;
69    }
70
71   /**
72    * Set the contained step patterns to be tested.
73    *
74    *
75    * @param patterns the contained step patterns to be tested.
76    */

77   public void setPatterns(StepPattern[] patterns)
78   {
79     m_patterns = patterns;
80     if(null != patterns)
81     {
82         for(int i = 0; i < patterns.length; i++)
83         {
84             patterns[i].exprSetParent(this);
85         }
86     }
87     
88   }
89
90   /**
91    * Get the contained step patterns to be tested.
92    *
93    *
94    * @return an array of the contained step patterns to be tested.
95    */

96   public StepPattern[] getPatterns()
97   {
98     return m_patterns;
99   }
100
101   /**
102    * Test a node to see if it matches any of the patterns in the union.
103    *
104    * @param xctxt XPath runtime context.
105    *
106    * @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
107    * {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
108    * {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
109    * {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
110    * {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
111    *
112    * @throws javax.xml.transform.TransformerException
113    */

114   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException JavaDoc
115   {
116
117     XObject bestScore = null;
118     int n = m_patterns.length;
119
120     for (int i = 0; i < n; i++)
121     {
122       XObject score = m_patterns[i].execute(xctxt);
123
124       if (score != NodeTest.SCORE_NONE)
125       {
126         if (null == bestScore)
127           bestScore = score;
128         else if (score.num() > bestScore.num())
129           bestScore = score;
130       }
131     }
132
133     if (null == bestScore)
134     {
135       bestScore = NodeTest.SCORE_NONE;
136     }
137
138     return bestScore;
139   }
140   
141   class UnionPathPartOwner implements ExpressionOwner
142   {
143     int m_index;
144     
145     UnionPathPartOwner(int index)
146     {
147         m_index = index;
148     }
149     
150     /**
151      * @see ExpressionOwner#getExpression()
152      */

153     public Expression getExpression()
154     {
155       return m_patterns[m_index];
156     }
157
158
159     /**
160      * @see ExpressionOwner#setExpression(Expression)
161      */

162     public void setExpression(Expression exp)
163     {
164         exp.exprSetParent(UnionPattern.this);
165         m_patterns[m_index] = (StepPattern)exp;
166     }
167   }
168   
169   /**
170    * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
171    */

172   public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
173   {
174     visitor.visitUnionPattern(owner, this);
175     if(null != m_patterns)
176     {
177         int n = m_patterns.length;
178         for(int i = 0; i < n; i++)
179         {
180             m_patterns[i].callVisitors(new UnionPathPartOwner(i), visitor);
181         }
182     }
183   }
184   
185   /**
186    * @see Expression#deepEquals(Expression)
187    */

188   public boolean deepEquals(Expression expr)
189   {
190     if(!isSameClass(expr))
191         return false;
192         
193     UnionPattern up = (UnionPattern)expr;
194         
195     if(null != m_patterns)
196     {
197         int n = m_patterns.length;
198         if((null == up.m_patterns) || (up.m_patterns.length != n))
199             return false;
200             
201         for(int i = 0; i < n; i++)
202         {
203             if(!m_patterns[i].deepEquals(up.m_patterns[i]))
204                 return false;
205         }
206     }
207     else if(up.m_patterns != null)
208         return false;
209         
210     return true;
211     
212   }
213
214
215 }
216
Popular Tags