1 package net.sf.saxon.query; 2 3 import net.sf.saxon.expr.*; 4 import net.sf.saxon.instruct.GeneralVariable; 5 import net.sf.saxon.instruct.GlobalParam; 6 import net.sf.saxon.instruct.GlobalVariable; 7 import net.sf.saxon.instruct.SlotManager; 8 import net.sf.saxon.om.NamePool; 9 import net.sf.saxon.trans.XPathException; 10 import net.sf.saxon.type.ItemType; 11 import net.sf.saxon.type.Type; 12 import net.sf.saxon.value.SequenceType; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 22 23 public class GlobalVariableDefinition implements VariableDeclaration, Declaration { 24 25 protected List references = new ArrayList (10); 26 private SequenceType requiredType; 27 private Expression value; 28 private int nameCode; 29 private boolean isParameter; 30 private String variableName; 31 private String systemId; private int lineNumber; 34 38 39 public void setRequiredType(SequenceType type) { 40 requiredType = type; 41 } 42 43 47 48 public SequenceType getRequiredType() { 49 return requiredType; 50 } 51 52 56 public void setNameCode(int nameCode) { 57 this.nameCode = nameCode; 58 } 59 60 64 public int getNameCode() { 65 return nameCode; 66 } 67 71 public void setLineNumber(int lineNumber) { 72 this.lineNumber = lineNumber; 73 } 74 75 78 79 public int getLineNumber() { 80 return lineNumber; 81 } 82 83 87 88 public int getColumnNumber() { 89 return -1; 90 } 91 92 96 97 public String getPublicId() { 98 return null; 99 } 100 101 104 public void setSystemId(String systemId) { 105 this.systemId = systemId; 106 } 107 108 111 112 public String getSystemId() { 113 return systemId; 114 } 115 116 120 public String getVariableName() { 121 return variableName; 122 } 123 124 128 public void setVariableName(String variableName) { 129 this.variableName = variableName; 130 } 131 132 136 public void setValueExpression(Expression val) { 137 this.value = val; 138 } 139 140 144 public void setIsParameter(boolean b) { 145 isParameter = b; 146 } 147 148 152 public void registerReference(BindingReference ref) { 153 references.add(ref); 154 } 155 156 159 160 public Iterator iterateReferences() { 161 return references.iterator(); 162 } 163 164 165 172 173 public GlobalVariable compile(StaticQueryContext env, int slot) throws XPathException { 174 175 GlobalVariable var; 176 if (isParameter) { 177 var = new GlobalParam(); 178 var.setExecutable(env.getExecutable()); 179 var.setRequiredParam(value==null); 180 } else { 181 var = new GlobalVariable(); 182 var.setExecutable(env.getExecutable()); 183 } 184 185 var.setSelectExpression(value); 186 var.setNameCode(nameCode); 187 var.setRequiredType(requiredType); 188 var.setVariableName(variableName); 189 var.setSlotNumber(slot); 190 191 int loc = var.getExecutable().getLocationMap().allocateLocationId(systemId, lineNumber); 192 var.setLocationId(loc); 193 194 Iterator iter = references.iterator(); 195 while (iter.hasNext()) { 196 BindingReference binding = (BindingReference)iter.next(); 197 binding.setStaticType(requiredType, null, 0); 198 binding.fixup(var); 199 } 200 env.getExecutable().registerGlobalVariable(var); 201 return var; 202 } 203 204 209 210 public static void typeCheck(StaticQueryContext env, GeneralVariable var) throws XPathException { 211 212 Expression value = var.getSelectExpression(); 213 if (value != null) { 214 if (value instanceof ComputedExpression) { 215 ((ComputedExpression)value).setParentExpression(var); 216 } 217 RoleLocator role = new RoleLocator( 218 RoleLocator.VARIABLE, env.getNamePool().getDisplayName(var.getNameCode()), 0, null); 219 Expression value2 = TypeChecker.strictTypeCheck( 220 value.simplify(env).typeCheck(env, Type.ITEM_TYPE), 221 var.getRequiredType(), 222 role, env); 223 value2 = value2.optimize(env.getConfiguration().getOptimizer(), env, Type.ITEM_TYPE); 224 var.setSelectExpression(value2); 225 if (value2 instanceof ComputedExpression) { 226 ((ComputedExpression)value2).setParentExpression(var); 227 } 228 SlotManager map = env.getConfiguration().makeSlotManager(); 230 int slots = ExpressionTool.allocateSlots(value2, 0, map); 231 if (slots > 0) { 232 ((GlobalVariable)var).setContainsLocals(map); 233 } 234 } 235 236 if (var.getRequiredType() == SequenceType.ANY_SEQUENCE && !(var instanceof GlobalParam)) { 237 try { 239 ItemType itemType = value.getItemType(); 240 int cardinality = value.getCardinality(); 241 var.setRequiredType(SequenceType.makeSequenceType(itemType, cardinality)); 242 } catch (Exception err) { 243 } 245 } 246 } 247 248 249 253 public void explain(NamePool pool) { 254 System.err.println("declare variable " + pool.getDisplayName(nameCode) + " := "); 255 if (value != null) { 256 value.display(4, pool, System.err); 257 } 258 System.err.println(";"); 259 } 260 } 261 262 263 | Popular Tags |