1 package net.sf.saxon.expr; 2 import net.sf.saxon.om.Item; 3 import net.sf.saxon.om.NamePool; 4 import net.sf.saxon.om.SequenceIterator; 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.EmptySequence; 10 import net.sf.saxon.value.SequenceType; 11 12 import java.io.PrintStream ; 13 14 18 19 class QuantifiedExpression extends Assignation { 20 21 private int operator; 23 public void setOperator(int operator) { 24 this.operator = operator; 25 } 26 27 30 31 public int computeCardinality() { 32 return StaticProperty.EXACTLY_ONE; 33 } 34 35 38 39 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 40 41 if (declaration==null) { 42 return this; 44 } 45 46 50 sequence = sequence.typeCheck(env, contextItemType); 51 if (sequence instanceof EmptySequence) { 52 return BooleanValue.get(operator != Token.SOME); 53 } 54 55 57 Optimizer opt = env.getConfiguration().getOptimizer(); 58 sequence = ExpressionTool.unsorted(opt, sequence, false); 59 60 SequenceType decl = declaration.getRequiredType(); 61 SequenceType sequenceType = SequenceType.makeSequenceType(decl.getPrimaryType(), 62 StaticProperty.ALLOWS_ZERO_OR_MORE); 63 RoleLocator role = new RoleLocator(RoleLocator.VARIABLE, new Integer (nameCode), 0, env.getNamePool()); 64 role.setSourceLocator(this); 65 sequence = TypeChecker.strictTypeCheck( 66 sequence, sequenceType, role, env); 67 ItemType actualItemType = sequence.getItemType(); 68 declaration.refineTypeInformation(actualItemType, 69 StaticProperty.EXACTLY_ONE, 70 null, 71 sequence.getSpecialProperties()); 72 73 declaration = null; 75 action = action.typeCheck(env, contextItemType); 76 return this; 77 } 78 79 96 97 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 98 sequence = sequence.optimize(opt, env, contextItemType); 99 action = action.optimize(opt, env, contextItemType); 100 PromotionOffer offer = new PromotionOffer(opt); 101 offer.containingExpression = this; 102 offer.action = PromotionOffer.RANGE_INDEPENDENT; 103 Binding[] bindingList = {this}; 104 offer.bindingList = bindingList; 105 action = doPromotion(action, offer); 106 if (offer.containingExpression instanceof LetExpression) { 107 offer.containingExpression = 108 offer.containingExpression.typeCheck(env, contextItemType).optimize(opt, env, contextItemType); 109 } 110 return offer.containingExpression; 111 112 } 113 114 115 119 120 public int computeSpecialProperties() { 121 int p = super.computeSpecialProperties(); 122 return p | StaticProperty.NON_CREATIVE; 123 } 124 125 128 129 public Item evaluateItem(XPathContext context) throws XPathException { 130 return BooleanValue.get(effectiveBooleanValue(context)); 131 } 132 133 136 137 public boolean effectiveBooleanValue(XPathContext context) throws XPathException { 138 139 141 SequenceIterator base = sequence.iterate(context); 142 143 146 boolean some = (operator==Token.SOME); 147 while (true) { 148 Item it = base.next(); 149 if (it == null) { 150 break; 151 } 152 context.setLocalVariable(slotNumber, it); 153 if (some == action.effectiveBooleanValue(context)) { 154 return some; 155 } 156 } 157 return !some; 158 } 159 160 161 165 166 public ItemType getItemType() { 167 return Type.BOOLEAN_TYPE; 168 } 169 170 173 174 public void display(int level, NamePool pool, PrintStream out) { 175 out.println(ExpressionTool.indent(level) + Token.tokens[operator] + " $" + getVariableName(pool) + " in"); 176 sequence.display(level+1, pool, out); 177 out.println(ExpressionTool.indent(level) + "satisfies"); 178 action.display(level+1, pool, out); 179 } 180 181 } 182 183 184 185 | Popular Tags |