1 package net.sf.saxon.expr; 2 import net.sf.saxon.om.Item; 3 import net.sf.saxon.om.NodeInfo; 4 import net.sf.saxon.om.SequenceIterator; 5 import net.sf.saxon.om.SingletonIterator; 6 import net.sf.saxon.pattern.AnyNodeTest; 7 import net.sf.saxon.trans.StaticError; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.type.AtomicType; 10 import net.sf.saxon.type.ItemType; 11 12 13 14 17 18 public abstract class SingleNodeExpression extends ComputedExpression { 19 20 23 24 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 25 if (contextItemType == null) { 26 StaticError err = new StaticError("Cannot select a node here: the context item is undefined"); 27 err.setErrorCode("XPDY0002"); 28 err.setIsTypeError(true); 29 err.setLocator(this); 30 throw err; 31 } 32 if (contextItemType instanceof AtomicType) { 33 StaticError err = new StaticError("Cannot select a node here: the context item is an atomic value"); 34 err.setErrorCode("XPTY0020"); 35 err.setIsTypeError(true); 36 err.setLocator(this); 37 throw err; 38 } 39 return this; 40 } 41 42 59 60 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 61 return typeCheck(env, contextItemType); 63 } 64 65 66 69 70 public int computeCardinality() { 71 return StaticProperty.ALLOWS_ZERO_OR_ONE; 72 } 73 74 78 79 public ItemType getItemType() { 80 return AnyNodeTest.getInstance(); 81 } 82 83 88 89 public int getIntrinsicDependencies() { 90 return StaticProperty.DEPENDS_ON_CONTEXT_ITEM; 91 } 92 93 public int computeSpecialProperties() { 94 return StaticProperty.ORDERED_NODESET | 95 StaticProperty.CONTEXT_DOCUMENT_NODESET | 96 StaticProperty.SINGLE_DOCUMENT_NODESET | 97 StaticProperty.NON_CREATIVE; 98 } 99 100 103 104 public abstract NodeInfo getNode(XPathContext context) throws XPathException; 105 106 110 111 public SequenceIterator iterate(XPathContext context) throws XPathException { 112 return SingletonIterator.makeIterator(getNode(context)); 113 } 114 115 public Item evaluateItem(XPathContext context) throws XPathException { 116 return getNode(context); 117 } 118 119 public boolean effectiveBooleanValue(XPathContext context) throws XPathException { 120 return getNode(context) != null; 121 } 122 123 } 124 125 126 127 | Popular Tags |