1 package net.sf.saxon.style; 2 import net.sf.saxon.expr.ComputedExpression; 3 import net.sf.saxon.expr.Expression; 4 import net.sf.saxon.expr.ExpressionTool; 5 import net.sf.saxon.expr.StaticProperty; 6 import net.sf.saxon.instruct.Executable; 7 import net.sf.saxon.instruct.GeneralVariable; 8 import net.sf.saxon.instruct.GlobalVariable; 9 import net.sf.saxon.instruct.LocalVariable; 10 import net.sf.saxon.pattern.NodeKindTest; 11 import net.sf.saxon.trans.XPathException; 12 import net.sf.saxon.value.EmptySequence; 13 import net.sf.saxon.value.SequenceType; 14 15 19 20 public class XSLVariable extends XSLVariableDeclaration { 21 22 private int state = 0; 23 27 public void prepareAttributes() throws XPathException { 28 if (state==2) return; 29 if (state==1) { 30 compileError("Circular reference to variable", "XTDE0640"); 31 } 32 state = 1; 33 super.prepareAttributes(); 35 state = 2; 36 } 37 38 42 43 public boolean isInstruction() { 44 return true; 45 } 46 47 51 52 public SequenceType getRequiredType() { 53 SequenceType defaultType = (requiredType==null ? SequenceType.ANY_SEQUENCE : requiredType); 55 if (assignable) { 56 return defaultType; 57 } else if (requiredType != null) { 58 return requiredType; 59 } else if (select!=null) { 60 if (select instanceof EmptySequence) { 61 return defaultType; 63 } else { 64 try { 65 return SequenceType.makeSequenceType(select.getItemType(), select.getCardinality()); 67 } catch (Exception err) { 68 return defaultType; 71 } 72 } 73 } else if (hasChildNodes()) { 74 return SequenceType.makeSequenceType(NodeKindTest.DOCUMENT, StaticProperty.EXACTLY_ONE); 75 } else { 76 return SequenceType.SINGLE_STRING; 78 } 79 } 80 81 86 87 public Expression compile(Executable exec) throws XPathException { 88 89 if (references.size()==0 && !assignable) { 90 redundant = true; 91 } 92 93 if (!redundant) { 94 GeneralVariable inst; 95 if (global) { 96 inst = new GlobalVariable(); 97 ((GlobalVariable)inst).setExecutable(getExecutable()); 98 if (select instanceof ComputedExpression) { 99 ((ComputedExpression)select).setParentExpression(inst); 100 } 101 initializeInstruction(exec, inst); 102 inst.setVariableName(getVariableName()); 103 inst.setSlotNumber(getSlotNumber()); 104 inst.setRequiredType(getRequiredType()); 105 ExpressionTool.makeParentReferences(inst); 106 fixupBinding(inst); 107 return inst; 108 } else { 109 throw new AssertionError ("Local variable found when compiling a global variable"); 110 } 122 } 123 124 return null; 125 } 126 127 public Expression compileLocalVariable(Executable exec) throws XPathException { 128 129 if (references.size()==0 && !assignable) { 130 redundant = true; 131 } 132 133 if (!redundant) { 134 GeneralVariable inst; 135 if (global) { 136 throw new AssertionError ("Global variable found when compiling local variable"); 137 } else { 150 inst = new LocalVariable(); 151 if (select instanceof ComputedExpression) { 152 ((ComputedExpression)select).setParentExpression(inst); 153 } 154 initializeInstruction(exec, inst); 155 inst.setVariableName(getVariableName()); 156 inst.setRequiredType(getRequiredType()); 157 ExpressionTool.makeParentReferences(inst); 158 return inst; 159 } 160 } 161 162 return null; 163 } 164 165 166 } 167 168 | Popular Tags |