1 package net.sf.saxon.pattern; 2 import net.sf.saxon.om.NodeInfo; 3 import net.sf.saxon.type.SchemaType; 4 import net.sf.saxon.type.Type; 5 import net.sf.saxon.type.AnySimpleType; 6 import net.sf.saxon.tinytree.TinyTree; 7 8 14 15 public class NodeKindTest extends NodeTest { 16 17 public static final NodeKindTest DOCUMENT = new NodeKindTest(Type.DOCUMENT); 18 public static final NodeKindTest ELEMENT = new NodeKindTest(Type.ELEMENT); 19 public static final NodeKindTest ATTRIBUTE = new NodeKindTest(Type.ATTRIBUTE); 20 public static final NodeKindTest TEXT = new NodeKindTest(Type.TEXT); 21 public static final NodeKindTest COMMENT = new NodeKindTest(Type.COMMENT); 22 public static final NodeKindTest PROCESSING_INSTRUCTION = new NodeKindTest(Type.PROCESSING_INSTRUCTION); 23 public static final NodeKindTest NAMESPACE = new NodeKindTest(Type.NAMESPACE); 24 25 26 private int kind; 27 28 private NodeKindTest(int nodeKind) { 29 kind = nodeKind; 30 } 31 32 35 36 public static NodeTest makeNodeKindTest(int kind) { 37 switch (kind) { 38 case Type.DOCUMENT: 39 return DOCUMENT; 40 case Type.ELEMENT: 41 return ELEMENT; 42 case Type.ATTRIBUTE: 43 return ATTRIBUTE; 44 case Type.COMMENT: 45 return COMMENT; 46 case Type.TEXT: 47 return TEXT; 48 case Type.PROCESSING_INSTRUCTION: 49 return PROCESSING_INSTRUCTION; 50 case Type.NAMESPACE: 51 return NAMESPACE; 52 case Type.NODE: 53 return AnyNodeTest.getInstance(); 54 default: 55 throw new IllegalArgumentException ("Unknown node kind in NodeKindTest"); 56 } 57 } 58 59 64 65 public boolean matches(int nodeKind, int fingerprint, int annotation) { 66 return (kind == nodeKind); 67 } 68 69 80 81 public boolean matches(TinyTree tree, int nodeNr) { 82 return tree.getNodeKind(nodeNr) == kind; 83 } 84 85 91 92 public boolean matches(NodeInfo node) { 93 return node.getNodeKind() == kind; 94 } 95 96 97 100 101 public final double getDefaultPriority() { 102 return -0.5; 103 } 104 105 109 110 public int getPrimitiveType() { 111 return kind; 112 } 113 114 118 119 public int getNodeKindMask() { 120 return 1<<kind; 121 } 122 123 127 128 public SchemaType getContentType() { 129 if (kind == Type.ATTRIBUTE) { 130 return AnySimpleType.getInstance(); 131 } else { 132 return super.getContentType(); 133 } 134 } 135 136 public String toString() { 137 return toString(kind); 138 } 139 140 public static String toString(int kind) { 141 switch (kind) { 142 case Type.DOCUMENT: 143 return("document-node()" ); 144 case Type.ELEMENT: 145 return( "element()" ); 146 case Type.ATTRIBUTE: 147 return( "attribute()" ); 148 case Type.COMMENT: 149 return( "comment()" ); 150 case Type.TEXT: 151 return( "text()" ); 152 case Type.PROCESSING_INSTRUCTION: 153 return( "processing-instruction()" ); 154 case Type.NAMESPACE: 155 return( "namespace()" ); 156 default: 157 return( "** error **"); 158 } 159 160 } 161 162 165 166 public int hashCode() { 167 return kind; 168 } 169 170 } 171 172 | Popular Tags |