KickJava   Java API By Example, From Geeks To Geeks.

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


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: ContextMatchStepPattern.java,v 1.6 2004/02/17 04:35:37 minchau Exp $
18  */

19 package org.apache.xpath.patterns;
20
21 import org.apache.xml.dtm.Axis;
22 import org.apache.xml.dtm.DTM;
23 import org.apache.xml.dtm.DTMAxisTraverser;
24 import org.apache.xml.dtm.DTMFilter;
25 import org.apache.xpath.XPathContext;
26 import org.apache.xpath.axes.WalkerFactory;
27 import org.apache.xpath.objects.XObject;
28 /**
29  * Special context node pattern matcher.
30  */

31 public class ContextMatchStepPattern extends StepPattern
32 {
33
34   /**
35    * Construct a ContextMatchStepPattern.
36    *
37    * @param whatToShow Bit set defined mainly by {@link org.w3c.dom.traversal.NodeFilter}.
38    */

39   public ContextMatchStepPattern(int axis, int paxis)
40   {
41     super(DTMFilter.SHOW_ALL, axis, paxis);
42   }
43
44   /**
45    * Execute this pattern step, including predicates.
46    *
47    *
48    * @param xctxt XPath runtime context.
49    *
50    * @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
51    * {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
52    * {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
53    * {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
54    * {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
55    *
56    * @throws javax.xml.transform.TransformerException
57    */

58   public XObject execute(XPathContext xctxt)
59           throws javax.xml.transform.TransformerException JavaDoc
60   {
61
62     if (xctxt.getIteratorRoot() == xctxt.getCurrentNode())
63       return getStaticScore();
64     else
65       return this.SCORE_NONE;
66   }
67   
68   /**
69    * Execute the match pattern step relative to another step.
70    *
71    *
72    * @param xctxt The XPath runtime context.
73    * NEEDSDOC @param prevStep
74    *
75    * @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
76    * {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
77    * {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
78    * {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
79    * {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
80    *
81    * @throws javax.xml.transform.TransformerException
82    */

83   public XObject executeRelativePathPattern(
84           XPathContext xctxt, StepPattern prevStep)
85             throws javax.xml.transform.TransformerException JavaDoc
86   {
87
88     XObject score = NodeTest.SCORE_NONE;
89     int context = xctxt.getCurrentNode();
90     DTM dtm = xctxt.getDTM(context);
91
92     if (null != dtm)
93     {
94       int predContext = xctxt.getCurrentNode();
95       DTMAxisTraverser traverser;
96       
97       int axis = m_axis;
98       
99       boolean needToTraverseAttrs = WalkerFactory.isDownwardAxisOfMany(axis);
100       boolean iterRootIsAttr = (dtm.getNodeType(xctxt.getIteratorRoot())
101                                  == DTM.ATTRIBUTE_NODE);
102
103       if((Axis.PRECEDING == axis) && iterRootIsAttr)
104       {
105         axis = Axis.PRECEDINGANDANCESTOR;
106       }
107       
108       traverser = dtm.getAxisTraverser(axis);
109
110       for (int relative = traverser.first(context); DTM.NULL != relative;
111               relative = traverser.next(context, relative))
112       {
113         try
114         {
115           xctxt.pushCurrentNode(relative);
116
117           score = execute(xctxt);
118
119           if (score != NodeTest.SCORE_NONE)
120           {
121           //score = executePredicates( xctxt, prevStep, SCORE_OTHER,
122
// predContext, relative);
123
if (executePredicates(xctxt, dtm, context))
124           return score;
125           
126           score = NodeTest.SCORE_NONE;
127           }
128           
129           if(needToTraverseAttrs && iterRootIsAttr
130              && (DTM.ELEMENT_NODE == dtm.getNodeType(relative)))
131           {
132             int xaxis = Axis.ATTRIBUTE;
133             for (int i = 0; i < 2; i++)
134             {
135               DTMAxisTraverser atraverser = dtm.getAxisTraverser(xaxis);
136         
137               for (int arelative = atraverser.first(relative);
138                       DTM.NULL != arelative;
139                       arelative = atraverser.next(relative, arelative))
140               {
141                 try
142                 {
143                   xctxt.pushCurrentNode(arelative);
144         
145                   score = execute(xctxt);
146         
147                   if (score != NodeTest.SCORE_NONE)
148                   {
149               //score = executePredicates( xctxt, prevStep, SCORE_OTHER,
150
// predContext, arelative);
151

152                     if (score != NodeTest.SCORE_NONE)
153                       return score;
154                   }
155                 }
156                 finally
157                 {
158                   xctxt.popCurrentNode();
159                 }
160               }
161               xaxis = Axis.NAMESPACE;
162             }
163           }
164
165         }
166         finally
167         {
168           xctxt.popCurrentNode();
169         }
170       }
171
172     }
173
174     return score;
175   }
176
177 }
178
Popular Tags