001: package net.sf.saxon.pattern;
002:
003: import net.sf.saxon.expr.Expression;
004: import net.sf.saxon.expr.StaticContext;
005: import net.sf.saxon.expr.XPathContext;
006: import net.sf.saxon.expr.PromotionOffer;
007: import net.sf.saxon.om.DocumentInfo;
008: import net.sf.saxon.om.Item;
009: import net.sf.saxon.om.NodeInfo;
010: import net.sf.saxon.om.SequenceIterator;
011: import net.sf.saxon.trans.KeyManager;
012: import net.sf.saxon.trans.XPathException;
013: import net.sf.saxon.type.ItemType;
014: import net.sf.saxon.value.AtomicValue;
015:
016: import java.util.Iterator;
017:
018: /**
019: * A KeyPattern is a pattern of the form key(keyname, keyvalue)
020: */
021:
022: public final class KeyPattern extends Pattern {
023:
024: private int keyfingerprint; // the fingerprint of the key name
025: private Expression keyexp; // the value of the key
026:
027: /**
028: * Constructor
029: * @param namecode the name of the key
030: * @param key the value of the key: either a StringValue or a VariableReference
031: */
032:
033: public KeyPattern(int namecode, Expression key) {
034: keyfingerprint = namecode & 0xfffff;
035: keyexp = key;
036: }
037:
038: /**
039: * Type-check the pattern. This is needed for patterns that contain
040: * variable references or function calls.
041: * @return the optimised Pattern
042: */
043:
044: public Pattern analyze(StaticContext env, ItemType contextItemType)
045: throws XPathException {
046: keyexp = keyexp.typeCheck(env, contextItemType);
047: return this ;
048: }
049:
050: /**
051: * Get the dependencies of the pattern. The only possible dependency for a pattern is
052: * on local variables. This is analyzed in those patterns where local variables may appear.
053: */
054:
055: public int getDependencies() {
056: return keyexp.getDependencies();
057: }
058:
059: /**
060: * Iterate over the subexpressions within this pattern
061: */
062:
063: public Iterator iterateSubExpressions() {
064: return keyexp.iterateSubExpressions();
065: }
066:
067: /**
068: * Offer promotion for subexpressions within this pattern. The offer will be accepted if the subexpression
069: * is not dependent on the factors (e.g. the context item) identified in the PromotionOffer.
070: * By default the offer is not accepted - this is appropriate in the case of simple expressions
071: * such as constant values and variable references where promotion would give no performance
072: * advantage. This method is always called at compile time.
073: * <p/>
074: * <p>Unlike the corresponding method on {@link net.sf.saxon.expr.Expression}, this method does not return anything:
075: * it can make internal changes to the pattern, but cannot return a different pattern. Only certain
076: * kinds of promotion are applicable within a pattern: specifically, promotions affecting local
077: * variable references within the pattern.
078: *
079: * @param offer details of the offer, for example the offer to move
080: * expressions that don't depend on the context to an outer level in
081: * the containing expression
082: * @throws net.sf.saxon.trans.XPathException
083: * if any error is detected
084: */
085:
086: public void promote(PromotionOffer offer) throws XPathException {
087: keyexp = keyexp.promote(offer);
088: }
089:
090: /**
091: * Determine whether this Pattern matches the given Node.
092: * @param e The NodeInfo representing the Element or other node to be tested against the Pattern
093: * @return true if the node matches the Pattern, false otherwise
094: */
095:
096: public boolean matches(NodeInfo e, XPathContext context)
097: throws XPathException {
098: DocumentInfo doc = e.getDocumentRoot();
099: if (doc == null) {
100: return false;
101: }
102: KeyManager km = context.getController().getKeyManager();
103: SequenceIterator iter = keyexp.iterate(context);
104: while (true) {
105: Item it = iter.next();
106: if (it == null) {
107: return false;
108: }
109: SequenceIterator nodes = km.selectByKey(keyfingerprint,
110: doc, (AtomicValue) it, context);
111: while (true) {
112: NodeInfo n = (NodeInfo) nodes.next();
113: if (n == null) {
114: break;
115: }
116: if (n.isSameNodeInfo(e)) {
117: return true;
118: }
119: }
120: }
121: }
122:
123: /**
124: * Get a NodeTest that all the nodes matching this pattern must satisfy
125: */
126:
127: public NodeTest getNodeTest() {
128: return AnyNodeTest.getInstance();
129: }
130:
131: }
132:
133: //
134: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
135: // you may not use this file except in compliance with the License. You may obtain a copy of the
136: // License at http://www.mozilla.org/MPL/
137: //
138: // Software distributed under the License is distributed on an "AS IS" basis,
139: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
140: // See the License for the specific language governing rights and limitations under the License.
141: //
142: // The Original Code is: all this file.
143: //
144: // The Initial Developer of the Original Code is Michael H. Kay.
145: //
146: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
147: //
148: // Contributor(s): none.
149: //
|