KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > pattern > KeyPattern


1 package net.sf.saxon.pattern;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.StaticContext;
4 import net.sf.saxon.expr.XPathContext;
5 import net.sf.saxon.expr.PromotionOffer;
6 import net.sf.saxon.om.DocumentInfo;
7 import net.sf.saxon.om.Item;
8 import net.sf.saxon.om.NodeInfo;
9 import net.sf.saxon.om.SequenceIterator;
10 import net.sf.saxon.trans.KeyManager;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.type.ItemType;
13 import net.sf.saxon.value.AtomicValue;
14
15 import java.util.Iterator JavaDoc;
16
17 /**
18 * A KeyPattern is a pattern of the form key(keyname, keyvalue)
19 */

20
21 public final class KeyPattern extends Pattern {
22
23     private int keyfingerprint; // the fingerprint of the key name
24
private Expression keyexp; // the value of the key
25

26     /**
27     * Constructor
28     * @param namecode the name of the key
29     * @param key the value of the key: either a StringValue or a VariableReference
30     */

31
32     public KeyPattern(int namecode, Expression key) {
33         keyfingerprint = namecode & 0xfffff;
34         keyexp = key;
35     }
36
37     /**
38     * Type-check the pattern. This is needed for patterns that contain
39     * variable references or function calls.
40     * @return the optimised Pattern
41     */

42
43     public Pattern analyze(StaticContext env, ItemType contextItemType) throws XPathException {
44         keyexp = keyexp.typeCheck(env, contextItemType);
45         return this;
46     }
47
48     /**
49      * Get the dependencies of the pattern. The only possible dependency for a pattern is
50      * on local variables. This is analyzed in those patterns where local variables may appear.
51      */

52
53     public int getDependencies() {
54         return keyexp.getDependencies();
55     }
56
57     /**
58      * Iterate over the subexpressions within this pattern
59      */

60
61     public Iterator iterateSubExpressions() {
62         return keyexp.iterateSubExpressions();
63     }
64
65     /**
66      * Offer promotion for subexpressions within this pattern. The offer will be accepted if the subexpression
67      * is not dependent on the factors (e.g. the context item) identified in the PromotionOffer.
68      * By default the offer is not accepted - this is appropriate in the case of simple expressions
69      * such as constant values and variable references where promotion would give no performance
70      * advantage. This method is always called at compile time.
71      * <p/>
72      * <p>Unlike the corresponding method on {@link net.sf.saxon.expr.Expression}, this method does not return anything:
73      * it can make internal changes to the pattern, but cannot return a different pattern. Only certain
74      * kinds of promotion are applicable within a pattern: specifically, promotions affecting local
75      * variable references within the pattern.
76      *
77      * @param offer details of the offer, for example the offer to move
78      * expressions that don't depend on the context to an outer level in
79      * the containing expression
80      * @throws net.sf.saxon.trans.XPathException
81      * if any error is detected
82      */

83
84     public void promote(PromotionOffer offer) throws XPathException {
85         keyexp = keyexp.promote(offer);
86     }
87
88    /**
89     * Determine whether this Pattern matches the given Node.
90     * @param e The NodeInfo representing the Element or other node to be tested against the Pattern
91     * @return true if the node matches the Pattern, false otherwise
92     */

93
94     public boolean matches(NodeInfo e, XPathContext context) throws XPathException {
95         DocumentInfo doc = e.getDocumentRoot();
96         if (doc==null) {
97             return false;
98         }
99         KeyManager km = context.getController().getKeyManager();
100         SequenceIterator iter = keyexp.iterate(context);
101         while (true) {
102             Item it = iter.next();
103             if (it == null) {
104                 return false;
105             }
106             SequenceIterator nodes = km.selectByKey(keyfingerprint, doc, (AtomicValue)it, context);
107             while (true) {
108                 NodeInfo n = (NodeInfo)nodes.next();
109                 if (n == null) {
110                     break;
111                 }
112                 if (n.isSameNodeInfo(e)) {
113                     return true;
114                 }
115             }
116         }
117     }
118
119     /**
120     * Get a NodeTest that all the nodes matching this pattern must satisfy
121     */

122
123     public NodeTest getNodeTest() {
124         return AnyNodeTest.getInstance();
125     }
126
127 }
128
129 //
130
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
131
// you may not use this file except in compliance with the License. You may obtain a copy of the
132
// License at http://www.mozilla.org/MPL/
133
//
134
// Software distributed under the License is distributed on an "AS IS" basis,
135
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
136
// See the License for the specific language governing rights and limitations under the License.
137
//
138
// The Original Code is: all this file.
139
//
140
// The Initial Developer of the Original Code is Michael H. Kay.
141
//
142
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
143
//
144
// Contributor(s): none.
145
//
146
Popular Tags