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.sort.GlobalOrderComparer; 5 import net.sf.saxon.trans.XPathException; 6 import net.sf.saxon.type.ItemType; 7 import net.sf.saxon.type.Type; 8 import net.sf.saxon.value.BooleanValue; 9 import net.sf.saxon.value.SequenceType; 10 11 12 17 18 public final class IdentityComparison extends BinaryExpression { 19 20 private boolean generateIdEmulation = false; 21 25 31 32 public IdentityComparison(Expression p1, int op, Expression p2) { 33 super(p1, op, p2); 34 } 35 36 40 41 public void setGenerateIdEmulation(boolean flag) { 42 generateIdEmulation = flag; 43 } 44 45 48 49 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 50 51 operand0 = operand0.typeCheck(env, contextItemType); 52 operand1 = operand1.typeCheck(env, contextItemType); 53 54 RoleLocator role0 = new RoleLocator(RoleLocator.BINARY_EXPR, Token.tokens[operator], 0, null); 55 role0.setSourceLocator(this); 56 operand0 = TypeChecker.staticTypeCheck( 57 operand0, SequenceType.OPTIONAL_NODE, false, role0, env); 58 59 RoleLocator role1 = new RoleLocator(RoleLocator.BINARY_EXPR, Token.tokens[operator], 1, null); 60 role1.setSourceLocator(this); 61 operand1 = TypeChecker.staticTypeCheck( 62 operand1, SequenceType.OPTIONAL_NODE, false, role1, env); 63 return this; 64 } 65 66 67 68 71 72 public Item evaluateItem(XPathContext context) throws XPathException { 73 NodeInfo node1 = getNode(operand0, context); 74 if (node1==null) { 75 if (generateIdEmulation) { 76 return BooleanValue.get(getNode(operand1, context)==null); 77 } 78 return null; 79 } 80 81 NodeInfo node2 = getNode(operand1, context); 82 if (node2==null) { 83 if (generateIdEmulation) { 84 return BooleanValue.FALSE; 85 } 86 return null; 87 } 88 89 return BooleanValue.get(compareIdentity(node1, node2)); 90 } 91 92 public boolean effectiveBooleanValue(XPathContext context) throws XPathException { 93 NodeInfo node1 = getNode(operand0, context); 94 if (node1==null) { 95 if (generateIdEmulation) { 96 return getNode(operand1, context) == null; 97 } 98 return false; 99 } 100 101 NodeInfo node2 = getNode(operand1, context); 102 if (node2==null) return false; 103 104 return compareIdentity(node1, node2); 105 } 106 107 private boolean compareIdentity(NodeInfo node1, NodeInfo node2) { 108 109 switch (operator) { 110 case Token.IS: 111 return node1.isSameNodeInfo(node2); 112 case Token.PRECEDES: 115 return GlobalOrderComparer.getInstance().compare(node1, node2) < 0; 116 case Token.FOLLOWS: 117 return GlobalOrderComparer.getInstance().compare(node1, node2) > 0; 118 default: 119 throw new UnsupportedOperationException ("Unknown node identity test"); 120 } 121 } 122 123 private NodeInfo getNode(Expression exp, XPathContext c) throws XPathException { 124 return (NodeInfo)exp.evaluateItem(c); 125 } 126 127 128 132 133 public ItemType getItemType() { 134 return Type.BOOLEAN_TYPE; 135 } 136 137 138 } 139 140 | Popular Tags |