1 package net.sf.saxon.value; 2 import net.sf.saxon.expr.ExpressionTool; 3 import net.sf.saxon.expr.StaticProperty; 4 import net.sf.saxon.expr.XPathContext; 5 import net.sf.saxon.expr.Token; 6 import net.sf.saxon.om.*; 7 import net.sf.saxon.pattern.*; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.type.ItemType; 10 import net.sf.saxon.type.Type; 11 import net.sf.saxon.style.StandardNames; 12 13 import java.io.PrintStream ; 14 15 18 19 public class SingletonNode extends Value { 20 21 protected NodeInfo node = null; 22 23 24 29 30 public SingletonNode(NodeInfo node) { 31 this.node = node; 32 } 33 34 38 39 public int getImplementationMethod() { 40 return EVALUATE_METHOD; 41 } 42 43 48 49 public void process(XPathContext context) throws XPathException { 50 if (node != null) { 51 context.getReceiver().append(node, 0, NodeInfo.ALL_NAMESPACES); 52 } 53 } 54 55 56 62 63 public ItemType getItemType() { 64 switch (node.getNodeKind()) { 65 case Type.DOCUMENT: 66 AxisIterator iter = node.iterateAxis(Axis.CHILD); 68 ItemType elementType = null; 69 while (true) { 70 NodeInfo n = (NodeInfo)iter.next(); 71 if (n==null) { 72 break; 73 } 74 int kind = n.getNodeKind(); 75 if (kind==Type.TEXT) { 76 elementType = null; 77 break; 78 } else if (kind==Type.ELEMENT) { 79 if (elementType != null) { 80 elementType = null; 81 break; 82 } 83 elementType = new SingletonNode(n).getItemType(); 84 } 85 } 86 if (elementType == null) { 87 return NodeKindTest.DOCUMENT; 88 } else { 89 return new DocumentNodeTest((NodeTest)elementType); 90 } 91 92 case Type.ELEMENT: 93 int eltype = node.getTypeAnnotation(); 94 if (eltype == -1 || eltype == StandardNames.XDT_UNTYPED || eltype == StandardNames.XS_ANY_TYPE) { 95 return new NameTest(Type.ELEMENT, node.getFingerprint(), node.getNamePool()); 96 } else { 97 return new CombinedNodeTest( 98 new NameTest(Type.ELEMENT, node.getFingerprint(), node.getNamePool()), 99 Token.INTERSECT, 100 new ContentTypeTest(Type.ELEMENT, node.getConfiguration().getSchemaType(eltype), 101 node.getConfiguration())); 102 } 103 104 case Type.ATTRIBUTE: 105 int attype = node.getTypeAnnotation(); 106 if (attype == -1 || attype == StandardNames.XDT_UNTYPED_ATOMIC) { 107 return new NameTest(Type.ATTRIBUTE, node.getFingerprint(), node.getNamePool()); 108 } else { 109 return new CombinedNodeTest( 110 new NameTest(Type.ATTRIBUTE, node.getFingerprint(), node.getNamePool()), 111 Token.INTERSECT, 112 new ContentTypeTest(Type.ATTRIBUTE, node.getConfiguration().getSchemaType(attype), 113 node.getConfiguration())); 114 } 115 116 case Type.TEXT: 117 return NodeKindTest.TEXT; 118 119 case Type.COMMENT: 120 return NodeKindTest.COMMENT; 121 122 case Type.PROCESSING_INSTRUCTION: 123 return NodeKindTest.PROCESSING_INSTRUCTION; 124 125 case Type.NAMESPACE: 126 return NodeKindTest.NAMESPACE; 127 128 default: 129 throw new IllegalArgumentException ("Unknown node kind " + node.getNodeKind()); 130 } 131 } 132 133 136 137 public int getCardinality() { 138 if (node==null) { 139 return StaticProperty.EMPTY; 140 } else { 141 return StaticProperty.EXACTLY_ONE; 142 } 143 } 144 145 148 149 public int getLength() throws XPathException { 150 return (node==null ? 0 : 1); 151 } 152 153 158 159 public Item itemAt(int n) throws XPathException { 160 if (n==0 && node!=null) { 161 return node; 162 } else { 163 return null; 164 } 165 166 } 167 168 171 172 public NodeInfo getNode() { 173 return node; 174 } 175 176 181 182 public int getSpecialProperties() { 183 return StaticProperty.ORDERED_NODESET | 184 StaticProperty.NON_CREATIVE; 185 } 186 187 190 191 public SequenceIterator iterate(XPathContext context) { 192 return SingletonIterator.makeIterator(node); 193 } 194 195 198 199 public Item evaluateItem(XPathContext context) { 200 return node; 201 } 202 203 206 207 public boolean effectiveBooleanValue(XPathContext context) { 208 return (node != null); 209 } 210 211 217 218 public String getStringValue() { 219 return (node==null ? "" : node.getStringValue()); 220 } 221 222 236 237 public String evaluateAsString(XPathContext context) throws XPathException { 238 if (node==null) return ""; 239 return node.getStringValue(); 240 } 241 242 243 246 247 public void display(int depth, NamePool pool, PrintStream out) { 248 if (node==null) { 249 out.println(ExpressionTool.indent(depth) + "Empty node-set"); 250 } else { 251 out.println(ExpressionTool.indent(depth) + "Node " + Navigator.getPath(node)); 252 } 253 } 254 255 258 259 public Object convertToJava(Class target, XPathContext context) throws XPathException { 260 if (node == null) { 261 return null; 262 } 263 if (target.isAssignableFrom(node.getClass())) { 264 return node; 265 } 266 if (target == String .class) { 267 return node.getStringValue(); 268 } 269 return super.convertToJava(target, context); 270 } 271 272 } 273 274 292 | Popular Tags |