001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: ContextMatchStepPattern.java,v 1.8 2005/01/24 04:04:40 mcnamara Exp $
018: */
019: package org.apache.xpath.patterns;
020:
021: import org.apache.xml.dtm.Axis;
022: import org.apache.xml.dtm.DTM;
023: import org.apache.xml.dtm.DTMAxisTraverser;
024: import org.apache.xml.dtm.DTMFilter;
025: import org.apache.xpath.XPathContext;
026: import org.apache.xpath.axes.WalkerFactory;
027: import org.apache.xpath.objects.XObject;
028:
029: /**
030: * Special context node pattern matcher.
031: */
032: public class ContextMatchStepPattern extends StepPattern {
033: static final long serialVersionUID = -1888092779313211942L;
034:
035: /**
036: * Construct a ContextMatchStepPattern.
037: *
038: */
039: public ContextMatchStepPattern(int axis, int paxis) {
040: super (DTMFilter.SHOW_ALL, axis, paxis);
041: }
042:
043: /**
044: * Execute this pattern step, including predicates.
045: *
046: *
047: * @param xctxt XPath runtime context.
048: *
049: * @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
050: * {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
051: * {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
052: * {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
053: * {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
054: *
055: * @throws javax.xml.transform.TransformerException
056: */
057: public XObject execute(XPathContext xctxt)
058: throws javax.xml.transform.TransformerException {
059:
060: if (xctxt.getIteratorRoot() == xctxt.getCurrentNode())
061: return getStaticScore();
062: else
063: return this .SCORE_NONE;
064: }
065:
066: /**
067: * Execute the match pattern step relative to another step.
068: *
069: *
070: * @param xctxt The XPath runtime context.
071: * NEEDSDOC @param prevStep
072: *
073: * @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
074: * {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
075: * {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
076: * {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
077: * {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
078: *
079: * @throws javax.xml.transform.TransformerException
080: */
081: public XObject executeRelativePathPattern(XPathContext xctxt,
082: StepPattern prevStep)
083: throws javax.xml.transform.TransformerException {
084:
085: XObject score = NodeTest.SCORE_NONE;
086: int context = xctxt.getCurrentNode();
087: DTM dtm = xctxt.getDTM(context);
088:
089: if (null != dtm) {
090: int predContext = xctxt.getCurrentNode();
091: DTMAxisTraverser traverser;
092:
093: int axis = m_axis;
094:
095: boolean needToTraverseAttrs = WalkerFactory
096: .isDownwardAxisOfMany(axis);
097: boolean iterRootIsAttr = (dtm.getNodeType(xctxt
098: .getIteratorRoot()) == DTM.ATTRIBUTE_NODE);
099:
100: if ((Axis.PRECEDING == axis) && iterRootIsAttr) {
101: axis = Axis.PRECEDINGANDANCESTOR;
102: }
103:
104: traverser = dtm.getAxisTraverser(axis);
105:
106: for (int relative = traverser.first(context); DTM.NULL != relative; relative = traverser
107: .next(context, relative)) {
108: try {
109: xctxt.pushCurrentNode(relative);
110:
111: score = execute(xctxt);
112:
113: if (score != NodeTest.SCORE_NONE) {
114: //score = executePredicates( xctxt, prevStep, SCORE_OTHER,
115: // predContext, relative);
116: if (executePredicates(xctxt, dtm, context))
117: return score;
118:
119: score = NodeTest.SCORE_NONE;
120: }
121:
122: if (needToTraverseAttrs
123: && iterRootIsAttr
124: && (DTM.ELEMENT_NODE == dtm
125: .getNodeType(relative))) {
126: int xaxis = Axis.ATTRIBUTE;
127: for (int i = 0; i < 2; i++) {
128: DTMAxisTraverser atraverser = dtm
129: .getAxisTraverser(xaxis);
130:
131: for (int arelative = atraverser
132: .first(relative); DTM.NULL != arelative; arelative = atraverser
133: .next(relative, arelative)) {
134: try {
135: xctxt.pushCurrentNode(arelative);
136:
137: score = execute(xctxt);
138:
139: if (score != NodeTest.SCORE_NONE) {
140: //score = executePredicates( xctxt, prevStep, SCORE_OTHER,
141: // predContext, arelative);
142:
143: if (score != NodeTest.SCORE_NONE)
144: return score;
145: }
146: } finally {
147: xctxt.popCurrentNode();
148: }
149: }
150: xaxis = Axis.NAMESPACE;
151: }
152: }
153:
154: } finally {
155: xctxt.popCurrentNode();
156: }
157: }
158:
159: }
160:
161: return score;
162: }
163:
164: }
|