1 package net.sf.saxon.pattern; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.tinytree.TinyTree; 4 import net.sf.saxon.functions.Nilled; 5 import net.sf.saxon.om.NodeInfo; 6 import net.sf.saxon.om.NamePool; 7 import net.sf.saxon.style.StandardNames; 8 import net.sf.saxon.type.*; 9 10 17 18 public class ContentTypeTest extends NodeTest { 19 20 private int kind; private SchemaType schemaType; 22 private int requiredType; 23 private Configuration config; 24 private boolean nillable = false; 25 private boolean matchDTDTypes = false; 26 27 31 37 38 public ContentTypeTest(int nodeKind, SchemaType schemaType, Configuration config) { 39 this.kind = nodeKind; 40 this.schemaType = schemaType; 41 this.requiredType = schemaType.getFingerprint(); 42 if (requiredType == -1) { 43 requiredType = StandardNames.XDT_UNTYPED; } 45 this.config = config; 46 } 47 48 52 public void setNillable(boolean nillable) { 53 this.nillable = nillable; 54 } 55 56 60 61 public boolean isNillable() { 62 return nillable; 63 } 64 65 70 71 public void setMatchDTDTypes(boolean matched) { 72 this.matchDTDTypes = matched; 73 } 74 75 80 81 public boolean matchesDTDTypes() { 82 return matchDTDTypes; 83 } 84 85 public SchemaType getSchemaType() { 86 return schemaType; 87 } 88 89 public ItemType getSuperType() { 90 return NodeKindTest.makeNodeKindTest(kind); 91 } 92 93 99 100 public boolean matches(int nodeKind, int fingerprint, int annotation) { 101 if (kind != nodeKind) { 102 return false; 103 } 104 return matchesAnnotation(annotation); 105 } 106 107 118 119 public boolean matches(TinyTree tree, int nodeNr) { 120 if (kind != tree.getNodeKind(nodeNr)) { 121 return false; 122 } 123 return matchesAnnotation(tree.getTypeAnnotation(nodeNr)) 124 && (nillable || !tree.isNilled(nodeNr)); 125 } 126 127 133 134 public boolean matches(NodeInfo node) { 135 return node.getNodeKind() == kind && 136 matchesAnnotation(node.getTypeAnnotation()) 137 && (nillable || !Nilled.isNilled(node)); 138 } 139 140 private boolean matchesAnnotation(int annotation) { 141 if (requiredType == StandardNames.XS_ANY_TYPE) { 142 return true; 143 } 144 145 if (annotation == -1) { 146 annotation = (kind==Type.ATTRIBUTE ? StandardNames.XDT_UNTYPED_ATOMIC : StandardNames.XDT_UNTYPED); 147 } 148 149 if (matchDTDTypes) { 150 annotation = annotation & NamePool.FP_MASK; 151 } else if (((annotation & NodeInfo.IS_DTD_TYPE) != 0)) { 152 return (requiredType == StandardNames.XDT_UNTYPED_ATOMIC); 153 } 154 155 if (annotation == requiredType) { 156 return true; 157 } 158 159 try { SchemaType type = config.getSchemaType(annotation & NamePool.FP_MASK).getBaseType(); 162 while (type != null) { 163 if (type.getFingerprint() == requiredType) { 164 return true; 165 } 166 type = type.getBaseType(); 167 } 168 } catch (UnresolvedReferenceException e) { 169 throw new IllegalStateException (e.getMessage()); 170 } 171 return false; 172 } 173 174 177 178 public final double getDefaultPriority() { 179 return 0; 180 } 181 182 186 187 public int getPrimitiveType() { 188 return kind; 189 } 190 191 195 196 public int getNodeKindMask() { 197 return 1<<kind; 198 } 199 200 204 205 public SchemaType getContentType() { 206 return schemaType; 207 } 208 209 213 214 public AtomicType getAtomizedItemType() { 215 SchemaType type = config.getSchemaType(requiredType); 216 if (type instanceof AtomicType) { 217 return (AtomicType)type; 218 } else if (type instanceof ListType) { 219 SimpleType mem = ((ListType)type).getItemType(); 220 if (mem instanceof AtomicType) { 221 return (AtomicType)mem; 222 } 223 } 224 return Type.ANY_ATOMIC_TYPE; 225 } 226 227 public String toString() { 228 return (kind == Type.ELEMENT ? "element(*, " : "attribute(*, ") + 229 schemaType.getDescription() + ')'; 230 } 231 232 235 236 public int hashCode() { 237 return kind<<20 ^ requiredType; 238 } 239 240 } 241 242 | Popular Tags |