001: package net.sf.saxon.pattern;
002:
003: import net.sf.saxon.om.NodeInfo;
004: import net.sf.saxon.type.Type;
005: import net.sf.saxon.tinytree.TinyTree;
006:
007: /**
008: * An AnyChildNodePattern is the pattern node(), which matches any node except a root node,
009: * an attribute node, or a namespace node: in other words, any node that is the child of another
010: * node.
011: */
012:
013: public final class AnyChildNodePattern extends NodeTest {
014:
015: /**
016: * Test whether this node test is satisfied by a given node
017: * @param nodeKind The type of node to be matched
018: * @param fingerprint identifies the expanded name of the node to be matched
019: */
020:
021: public boolean matches(int nodeKind, int fingerprint, int annotation) {
022: return (nodeKind == Type.ELEMENT || nodeKind == Type.TEXT
023: || nodeKind == Type.COMMENT || nodeKind == Type.PROCESSING_INSTRUCTION);
024: }
025:
026: /**
027: * Test whether this node test is satisfied by a given node on a TinyTree. The node
028: * must be a document, element, text, comment, or processing instruction node.
029: * This method is provided
030: * so that when navigating a TinyTree a node can be rejected without
031: * actually instantiating a NodeInfo object.
032: *
033: * @param tree the TinyTree containing the node
034: * @param nodeNr the number of the node within the TinyTree
035: * @return true if the node matches the NodeTest, otherwise false
036: */
037:
038: public boolean matches(TinyTree tree, int nodeNr) {
039: int nodeKind = tree.nodeKind[nodeNr];
040: return (nodeKind == Type.ELEMENT || nodeKind == Type.TEXT
041: || nodeKind == Type.COMMENT || nodeKind == Type.PROCESSING_INSTRUCTION);
042: }
043:
044: /**
045: * Test whether this node test is satisfied by a given node. This alternative
046: * method is used in the case of nodes where calculating the fingerprint is expensive,
047: * for example DOM or JDOM nodes.
048: * @param node the node to be matched
049: */
050:
051: public boolean matches(NodeInfo node) {
052: int nodeKind = node.getNodeKind();
053: return (nodeKind == Type.ELEMENT || nodeKind == Type.TEXT
054: || nodeKind == Type.COMMENT || nodeKind == Type.PROCESSING_INSTRUCTION);
055: }
056:
057: /**
058: * Determine the default priority to use if this pattern appears as a match pattern
059: * for a template with no explicit priority attribute.
060: */
061:
062: public double getDefaultPriority() {
063: return -0.5;
064: }
065:
066: /**
067: * Get a mask indicating which kinds of nodes this NodeTest can match. This is a combination
068: * of bits: 1<<Type.ELEMENT for element nodes, 1<<Type.TEXT for text nodes, and so on.
069: */
070:
071: public int getNodeKindMask() {
072: return 1 << Type.ELEMENT | 1 << Type.TEXT | 1 << Type.COMMENT
073: | 1 << Type.PROCESSING_INSTRUCTION;
074: }
075:
076: public String toString() {
077: return "node()";
078: }
079:
080: /**
081: * Returns a hash code value for the object.
082: */
083:
084: public int hashCode() {
085: return "AnyChildNodePattern".hashCode();
086: }
087:
088: }
089:
090: //
091: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
092: // you may not use this file except in compliance with the License. You may obtain a copy of the
093: // License at http://www.mozilla.org/MPL/
094: //
095: // Software distributed under the License is distributed on an "AS IS" basis,
096: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
097: // See the License for the specific language governing rights and limitations under the License.
098: //
099: // The Original Code is: all this file.
100: //
101: // The Initial Developer of the Original Code is Michael H. Kay.
102: //
103: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
104: //
105: // Contributor(s): none.
106: //
|