KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > transformer > KeyIterator


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: KeyIterator.java,v 1.16 2004/02/16 20:41:30 minchau Exp $
18  */

19 package org.apache.xalan.transformer;
20
21 import java.util.Vector JavaDoc;
22
23 import javax.xml.transform.TransformerException JavaDoc;
24
25 import org.apache.xalan.res.XSLMessages;
26 import org.apache.xalan.res.XSLTErrorResources;
27 import org.apache.xalan.templates.KeyDeclaration;
28 import org.apache.xml.dtm.Axis;
29 import org.apache.xml.dtm.DTMIterator;
30 import org.apache.xml.utils.QName;
31 import org.apache.xpath.XPath;
32 import org.apache.xpath.axes.OneStepIteratorForward;
33
34 /**
35  * This class implements an optimized iterator for
36  * "key()" patterns, matching each node to the
37  * match attribute in one or more xsl:key declarations.
38  * @xsl.usage internal
39  */

40 public class KeyIterator extends OneStepIteratorForward
41 {
42
43   /** Key name.
44    * @serial */

45   private QName m_name;
46
47   /**
48    * Get the key name from a key declaration this iterator will process
49    *
50    *
51    * @return Key name
52    */

53   public QName getName()
54   {
55     return m_name;
56   }
57
58   /** Vector of Key declarations in the stylesheet.
59    * @serial */

60   private Vector JavaDoc m_keyDeclarations;
61
62   /**
63    * Get the key declarations from the stylesheet
64    *
65    *
66    * @return Vector containing the key declarations from the stylesheet
67    */

68   public Vector JavaDoc getKeyDeclarations()
69   {
70     return m_keyDeclarations;
71   }
72
73   /**
74     * Create a KeyIterator object.
75     *
76     * @param compiler A reference to the Compiler that contains the op map.
77     * @param opPos The position within the op map, which contains the
78     * location path expression for this itterator.
79     *
80     * @throws javax.xml.transform.TransformerException
81     */

82   KeyIterator(QName name, Vector JavaDoc keyDeclarations)
83   {
84     super(Axis.ALL);
85     m_keyDeclarations = keyDeclarations;
86     // m_prefixResolver = nscontext;
87
m_name = name;
88   }
89
90   /**
91    * Test whether a specified node is visible in the logical view of a
92    * TreeWalker or NodeIterator. This function will be called by the
93    * implementation of TreeWalker and NodeIterator; it is not intended to
94    * be called directly from user code.
95    *
96    * @param testnode The node to check to see if it passes the filter or not.
97    *
98    * @return a constant to determine whether the node is accepted,
99    * rejected, or skipped, as defined above .
100    */

101   public short acceptNode(int testNode)
102   {
103     boolean foundKey = false;
104     KeyIterator ki = (KeyIterator) m_lpi;
105     org.apache.xpath.XPathContext xctxt = ki.getXPathContext();
106     Vector JavaDoc keys = ki.getKeyDeclarations();
107
108     QName name = ki.getName();
109     try
110     {
111       // System.out.println("lookupKey: "+lookupKey);
112
int nDeclarations = keys.size();
113
114       // Walk through each of the declarations made with xsl:key
115
for (int i = 0; i < nDeclarations; i++)
116       {
117         KeyDeclaration kd = (KeyDeclaration) keys.elementAt(i);
118
119         // Only continue if the name on this key declaration
120
// matches the name on the iterator for this walker.
121
if (!kd.getName().equals(name))
122           continue;
123
124         foundKey = true;
125         // xctxt.setNamespaceContext(ki.getPrefixResolver());
126

127         // See if our node matches the given key declaration according to
128
// the match attribute on xsl:key.
129
XPath matchExpr = kd.getMatch();
130         double score = matchExpr.getMatchScore(xctxt, testNode);
131
132         if (score == kd.getMatch().MATCH_SCORE_NONE)
133           continue;
134
135         return DTMIterator.FILTER_ACCEPT;
136
137       } // end for(int i = 0; i < nDeclarations; i++)
138
}
139     catch (TransformerException JavaDoc se)
140     {
141
142       // TODO: What to do?
143
}
144
145     if (!foundKey)
146       throw new RuntimeException JavaDoc(
147         XSLMessages.createMessage(
148           XSLTErrorResources.ER_NO_XSLKEY_DECLARATION,
149           new Object JavaDoc[] { name.getLocalName()}));
150           
151     return DTMIterator.FILTER_REJECT;
152   }
153
154 }
155
Popular Tags