001: package net.sf.saxon.pattern;
002:
003: import net.sf.saxon.om.NodeInfo;
004: import net.sf.saxon.type.SchemaType;
005: import net.sf.saxon.type.Type;
006: import net.sf.saxon.type.AnySimpleType;
007: import net.sf.saxon.tinytree.TinyTree;
008:
009: /**
010: * NodeTest is an interface that enables a test of whether a node has a particular
011: * name and kind. A NodeKindTest matches the node kind only.
012: *
013: * @author Michael H. Kay
014: */
015:
016: public class NodeKindTest extends NodeTest {
017:
018: public static final NodeKindTest DOCUMENT = new NodeKindTest(
019: Type.DOCUMENT);
020: public static final NodeKindTest ELEMENT = new NodeKindTest(
021: Type.ELEMENT);
022: public static final NodeKindTest ATTRIBUTE = new NodeKindTest(
023: Type.ATTRIBUTE);
024: public static final NodeKindTest TEXT = new NodeKindTest(Type.TEXT);
025: public static final NodeKindTest COMMENT = new NodeKindTest(
026: Type.COMMENT);
027: public static final NodeKindTest PROCESSING_INSTRUCTION = new NodeKindTest(
028: Type.PROCESSING_INSTRUCTION);
029: public static final NodeKindTest NAMESPACE = new NodeKindTest(
030: Type.NAMESPACE);
031:
032: private int kind;
033:
034: private NodeKindTest(int nodeKind) {
035: kind = nodeKind;
036: }
037:
038: /**
039: * Make a test for a given kind of node
040: */
041:
042: public static NodeTest makeNodeKindTest(int kind) {
043: switch (kind) {
044: case Type.DOCUMENT:
045: return DOCUMENT;
046: case Type.ELEMENT:
047: return ELEMENT;
048: case Type.ATTRIBUTE:
049: return ATTRIBUTE;
050: case Type.COMMENT:
051: return COMMENT;
052: case Type.TEXT:
053: return TEXT;
054: case Type.PROCESSING_INSTRUCTION:
055: return PROCESSING_INSTRUCTION;
056: case Type.NAMESPACE:
057: return NAMESPACE;
058: case Type.NODE:
059: return AnyNodeTest.getInstance();
060: default:
061: throw new IllegalArgumentException(
062: "Unknown node kind in NodeKindTest");
063: }
064: }
065:
066: /**
067: * Test whether this node test is satisfied by a given node
068: * @param nodeKind The type of node to be matched
069: * @param fingerprint identifies the expanded name of the node to be matched
070: */
071:
072: public boolean matches(int nodeKind, int fingerprint, int annotation) {
073: return (kind == nodeKind);
074: }
075:
076: /**
077: * Test whether this node test is satisfied by a given node on a TinyTree. The node
078: * must be a document, element, text, comment, or processing instruction node.
079: * This method is provided
080: * so that when navigating a TinyTree a node can be rejected without
081: * actually instantiating a NodeInfo object.
082: *
083: * @param tree the TinyTree containing the node
084: * @param nodeNr the number of the node within the TinyTree
085: * @return true if the node matches the NodeTest, otherwise false
086: */
087:
088: public boolean matches(TinyTree tree, int nodeNr) {
089: return tree.getNodeKind(nodeNr) == kind;
090: }
091:
092: /**
093: * Test whether this node test is satisfied by a given node. This alternative
094: * method is used in the case of nodes where calculating the fingerprint is expensive,
095: * for example DOM or JDOM nodes.
096: * @param node the node to be matched
097: */
098:
099: public boolean matches(NodeInfo node) {
100: return node.getNodeKind() == kind;
101: }
102:
103: /**
104: * Determine the default priority of this node test when used on its own as a Pattern
105: */
106:
107: public final double getDefaultPriority() {
108: return -0.5;
109: }
110:
111: /**
112: * Determine the types of nodes to which this pattern applies. Used for optimisation.
113: * @return the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT
114: */
115:
116: public int getPrimitiveType() {
117: return kind;
118: }
119:
120: /**
121: * Get a mask indicating which kinds of nodes this NodeTest can match. This is a combination
122: * of bits: 1<<Type.ELEMENT for element nodes, 1<<Type.TEXT for text nodes, and so on.
123: */
124:
125: public int getNodeKindMask() {
126: return 1 << kind;
127: }
128:
129: /**
130: * Get the content type allowed by this NodeTest (that is, the type annotation of the matched nodes).
131: * Return AnyType if there are no restrictions. The default implementation returns AnyType.
132: */
133:
134: public SchemaType getContentType() {
135: if (kind == Type.ATTRIBUTE) {
136: return AnySimpleType.getInstance();
137: } else {
138: return super .getContentType();
139: }
140: }
141:
142: public String toString() {
143: return toString(kind);
144: }
145:
146: public static String toString(int kind) {
147: switch (kind) {
148: case Type.DOCUMENT:
149: return ("document-node()");
150: case Type.ELEMENT:
151: return ("element()");
152: case Type.ATTRIBUTE:
153: return ("attribute()");
154: case Type.COMMENT:
155: return ("comment()");
156: case Type.TEXT:
157: return ("text()");
158: case Type.PROCESSING_INSTRUCTION:
159: return ("processing-instruction()");
160: case Type.NAMESPACE:
161: return ("namespace()");
162: default:
163: return ("** error **");
164: }
165:
166: }
167:
168: /**
169: * Returns a hash code value for the object.
170: */
171:
172: public int hashCode() {
173: return kind;
174: }
175:
176: /**
177: * Indicates whether some other object is "equal to" this one.
178: */
179: public boolean equals(Object other) {
180: return other instanceof NodeKindTest
181: && ((NodeKindTest) other).kind == kind;
182: }
183:
184: }
185:
186: //
187: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
188: // you may not use this file except in compliance with the License. You may obtain a copy of the
189: // License at http://www.mozilla.org/MPL/
190: //
191: // Software distributed under the License is distributed on an "AS IS" basis,
192: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
193: // See the License for the specific language governing rights and limitations under the License.
194: //
195: // The Original Code is: all this file.
196: //
197: // The Initial Developer of the Original Code is Michael H. Kay.
198: //
199: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
200: //
201: // Contributor(s): none.
202: //
|