1 package net.sf.saxon.style; 2 import net.sf.saxon.expr.*; 3 import net.sf.saxon.instruct.*; 4 import net.sf.saxon.om.Axis; 5 import net.sf.saxon.om.AxisIterator; 6 import net.sf.saxon.om.Navigator; 7 import net.sf.saxon.om.NodeInfo; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.value.SequenceType; 10 import net.sf.saxon.value.Whitespace; 11 12 17 18 public class XSLParam extends XSLVariableDeclaration { 19 20 Expression conversion = null; 21 22 protected boolean allowsValue() { 23 return !(getParent() instanceof XSLFunction); 24 } 26 27 protected boolean allowsRequired() { 28 return !(getParent() instanceof XSLFunction); 29 } 31 32 protected boolean allowsTunnelAttribute() { 33 return true; 34 } 35 36 public void validate() throws XPathException { 37 38 NodeInfo parent = getParent(); 39 boolean local = (parent instanceof XSLTemplate || parent instanceof XSLFunction); 40 global = (parent instanceof XSLStylesheet); 41 42 if (!local && !global) { 43 compileError("xsl:param must be immediately within a template, function or stylesheet", "XTSE0010"); 44 } 45 46 if (!global) { 47 AxisIterator preceding = iterateAxis(Axis.PRECEDING_SIBLING); 48 while (true) { 49 NodeInfo node = (NodeInfo)preceding.next(); 50 if (node == null) { 51 break; 52 } 53 if (node instanceof XSLParam) { 54 if (this.getVariableFingerprint() == ((XSLParam)node).getVariableFingerprint()) { 55 compileError("The name of the parameter is not unique", "XTSE0580"); 56 } 57 } else if (node instanceof StyleElement) { 58 compileError("xsl:param must be the first element within a template or function", "XTSE0010"); 59 } else { 60 if (!Whitespace.isWhite(node.getStringValueCS())) { 62 compileError("xsl:param must not be preceded by text", "XTSE0010"); 63 } 64 } 65 } 66 67 SlotManager p = getContainingSlotManager(); 68 if (p==null) { 69 compileError("Local variable must be declared within a template or function", "XTSE0010"); 70 } else { 71 setSlotNumber(p.allocateSlotNumber(getVariableFingerprint())); 72 } 73 74 } 75 76 if (requiredParam) { 77 if (select != null) { 78 compileError("The select attribute should be omitted when required='yes'", "XTSE0010"); 80 } 81 if (hasChildNodes()) { 82 compileError("A parameter specifying required='yes' must have empty content", "XTSE0010"); 83 } 84 } 85 86 super.validate(); 87 } 88 89 93 94 public Expression compile(Executable exec) throws XPathException { 95 96 if (getParent() instanceof XSLFunction) { 97 return null; 99 } else { 100 int slot = getSlotNumber(); 101 if (requiredType != null) { 102 SuppliedParameterReference pref = new SuppliedParameterReference(slot); 103 pref.setLocationId(staticContext.getLocationMap().allocateLocationId(getSystemId(), getLineNumber())); 104 RoleLocator role = new RoleLocator(RoleLocator.VARIABLE, getVariableName(), 0, null); 105 role.setSourceLocator(new ExpressionLocation(this)); 106 conversion = TypeChecker.staticTypeCheck( 107 pref, 108 requiredType, 109 false, 110 role, getStaticContext()); 111 } 112 113 GeneralVariable inst; 114 if (global) { 115 inst = new GlobalParam(); 116 ((GlobalParam)inst).setExecutable(getExecutable()); 117 if (isRequiredParam()) { 118 getExecutable().addRequiredParam(getVariableFingerprint()); 119 } 120 if (select instanceof ComputedExpression) { 121 ((ComputedExpression)select).setParentExpression(inst); 122 } 123 } else { 124 inst = new LocalParam(); 125 ((LocalParam)inst).setConversion(conversion); 126 } 127 initializeInstruction(exec, inst); 128 inst.setVariableName(getVariableName()); 129 inst.setSlotNumber(slot); 130 inst.setRequiredType(getRequiredType()); 131 ExpressionTool.makeParentReferences(inst); 132 fixupBinding(inst); 133 return inst; 134 } 135 } 136 137 138 142 143 public SequenceType getRequiredType() { 144 if (requiredType!=null) { 145 return requiredType; 146 } else { 147 return SequenceType.ANY_SEQUENCE; 148 } 149 } 150 151 } 152 153 | Popular Tags |