1 package net.sf.saxon.instruct; 2 import net.sf.saxon.expr.TypeChecker; 3 import net.sf.saxon.expr.XPathContext; 4 import net.sf.saxon.om.ValueRepresentation; 5 import net.sf.saxon.trans.DynamicError; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.type.AtomicType; 8 import net.sf.saxon.type.ItemType; 9 import net.sf.saxon.value.AtomicValue; 10 import net.sf.saxon.value.EmptySequence; 11 import net.sf.saxon.value.ValidationErrorValue; 12 import net.sf.saxon.value.Value; 13 14 15 21 22 public final class Bindery { 23 24 private ValueRepresentation[] globals; private boolean[] busy; private GlobalParameterSet globalParameters; private SlotManager globalVariableMap; 29 32 33 public void allocateGlobals(SlotManager map) { 34 globalVariableMap = map; 35 int n = map.getNumberOfVariables()+1; 36 globals = new ValueRepresentation[n]; 37 busy = new boolean[n]; 38 for (int i=0; i<n; i++) { 39 globals[i] = null; 40 busy[i] = false; 41 } 42 } 43 44 48 49 public void defineGlobalParameters(GlobalParameterSet params) { 50 globalParameters = params; 51 } 52 53 61 62 public boolean useGlobalParameter(int fingerprint, GlobalParam binding, XPathContext context) throws XPathException { 63 int slot = binding.getSlotNumber(); 64 if (globals[slot] != null) { 65 return true; 66 } 67 68 if (globalParameters==null) { 69 return false; 70 } 71 Object obj = globalParameters.get(fingerprint); 72 if (obj==null) { 73 return false; 74 } 75 76 Value val; 77 try { 78 val = Value.convertJavaObjectToXPath( 79 obj, binding.getRequiredType(), context.getController().getConfiguration()); 80 if (val==null) { 81 val = EmptySequence.getInstance(); 82 } 83 } catch (XPathException err) { 84 err.setLocator(binding); 85 throw err; 86 } 87 88 ItemType reqItemType = binding.getRequiredType().getPrimaryType(); 89 if (val instanceof AtomicValue && reqItemType instanceof AtomicType) { 90 val = ((AtomicValue)val).convert((AtomicType)reqItemType, context, true); 94 if (val instanceof ValidationErrorValue) { 95 throw ((ValidationErrorValue)val).getException(); 96 } 97 } else { 98 DynamicError err = TypeChecker.testConformance(val, binding.getRequiredType()); 102 if (err != null) { 103 throw err; 104 } 105 } 106 globals[slot] = val; 107 return true; 108 } 109 110 115 116 public void defineGlobalVariable(GlobalVariable binding, ValueRepresentation value) { 117 globals[binding.getSlotNumber()] = value; 118 } 119 120 126 127 public void setExecuting(GlobalVariable binding, boolean executing) 128 throws XPathException { 129 int slot = binding.getSlotNumber(); 130 if (executing) { 131 if (busy[slot]) { 132 throw new XPathException.Circularity("Circular definition"); 133 } 134 busy[slot]=true; 138 } else { 139 busy[slot]=false; 140 } 141 } 142 143 148 149 public ValueRepresentation getGlobalVariableValue(GlobalVariable binding) { 150 return globals[binding.getSlotNumber()]; 151 } 152 153 158 159 public ValueRepresentation getGlobalVariable(int slot) { 160 return globals[slot]; 161 } 162 163 167 168 public void assignGlobalVariable(GlobalVariable binding, ValueRepresentation value) { 169 defineGlobalVariable(binding, value); 170 } 171 172 176 177 public SlotManager getGlobalVariableMap() { 178 return globalVariableMap; 179 } 180 181 185 186 public ValueRepresentation[] getGlobalVariables() { 187 return globals; 188 } 189 190 } 191 192 | Popular Tags |