1 package net.sf.saxon.expr; 2 3 import net.sf.saxon.instruct.UserFunction; 4 import net.sf.saxon.instruct.UserFunctionParameter; 5 import net.sf.saxon.type.ItemType; 6 import net.sf.saxon.type.Type; 7 import net.sf.saxon.value.SequenceType; 8 import net.sf.saxon.value.Value; 9 10 import java.util.ArrayList ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 20 21 public class RangeVariableDeclaration implements VariableDeclaration { 22 23 private int nameCode; 24 private SequenceType requiredType; 25 private String variableName; 26 private List references = new ArrayList (5); 27 28 32 33 public void setNameCode(int nameCode) { 34 this.nameCode = nameCode; 35 } 36 37 41 42 public int getNameCode() { 43 return nameCode; 44 } 45 46 50 51 public SequenceType getRequiredType() { 52 return requiredType; 53 } 54 55 59 public void setRequiredType(SequenceType requiredType) { 60 this.requiredType = requiredType; 61 } 62 63 public void setVariableName(String variableName) { 64 this.variableName = variableName; 65 } 66 67 public String getVariableName() { 68 return variableName; 69 } 70 71 public void registerReference(BindingReference ref) { 72 references.add(ref); 73 } 74 75 public void setReferenceList(List references) { 76 this.references = references; 77 } 78 79 public List getReferenceList() { 80 return references; 81 } 82 83 90 public int getReferenceCount(Binding binding) { 91 return getReferenceCount(references, binding); 92 } 93 94 109 110 public static int getReferenceCount(List references, Binding binding) { 111 for (int i=references.size()-1; i>=0; i--) { 113 if (((VariableReference)references.get(i)).getBinding() == null) { 114 references.remove(i); 115 } 116 } 117 if (references.size() != 1) { 118 return references.size(); 120 } 121 VariableReference ref = (VariableReference)references.get(0); 122 Expression child = ref; 123 Container parent = ref.getParentExpression(); 124 boolean filtered = (parent instanceof FilterExpression && child == ((FilterExpression)parent).getBaseExpression()); 125 int count = 0; 126 while (parent != null) { 127 128 if (parent instanceof ComputedExpression) { 129 if (parent == binding) { 132 return 1; 133 } else if (filtered && parent instanceof FilterExpression && child == ((FilterExpression)parent).getBaseExpression()) { 134 return FILTERED; 135 } else if (ExpressionTool.isRepeatedSubexpression((ComputedExpression)parent, child)) { 136 return 10; 137 } else { 138 child = (ComputedExpression)parent; 139 parent = child.getParentExpression(); 140 if (count++ > 10000) { 141 throw new IllegalStateException ("The expression tree appears to contain a cycle"); 142 } 143 } 144 } else if (parent instanceof UserFunction) { 145 UserFunctionParameter[] params = ((UserFunction)parent).getParameterDefinitions(); 146 for (int p=0; p<params.length; p++) { 147 if (params[p] == binding) { 148 int refs = params[p].getReferenceCount(); 149 return refs; 150 } 151 } 152 return 10; 153 } else { 154 return 10; 156 } 157 } 158 return 10; 159 } 160 161 public static final int FILTERED = 10000; 162 163 public void fixupReferences(Binding binding) { 164 for (Iterator iter=references.iterator(); iter.hasNext();) { 165 BindingReference ref = (BindingReference)iter.next(); 166 ref.setStaticType(requiredType, null, 0); 167 ref.fixup(binding); 170 } 171 } 172 173 public void refineTypeInformation(ItemType type, int cardinality, 174 Value constantValue, int properties) { 175 for (Iterator iter=references.iterator(); iter.hasNext();) { 176 BindingReference ref = (BindingReference)iter.next(); 177 if (ref instanceof VariableReference) { 178 ItemType oldItemType = ((VariableReference)ref).getItemType(); 179 ItemType newItemType = oldItemType; 180 if (Type.isSubType(type, oldItemType)) { 181 newItemType = type; 182 } 183 int newcard = cardinality & ((VariableReference)ref).getCardinality(); 184 if (newcard==0) { 185 newcard = ((VariableReference)ref).getCardinality(); 187 } 188 SequenceType seqType = SequenceType.makeSequenceType(newItemType, newcard); 189 190 ref.setStaticType(seqType, constantValue, properties); 191 } 192 } 193 } 194 } 195 196 | Popular Tags |