| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.instruct.CallTemplate; |
| 12 |
import net.sf.saxon.expr.instruct.NamedTemplate; |
| 13 |
import net.sf.saxon.lib.NamespaceConstant; |
| 14 |
import net.sf.saxon.om.*; |
| 15 |
import net.sf.saxon.trans.Err; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 18 |
import net.sf.saxon.type.Type; |
| 19 |
import net.sf.saxon.value.SequenceType; |
| 20 |
import net.sf.saxon.value.Whitespace; |
| 21 |
|
| 22 |
import java.util.List; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class XSLCallTemplate extends StyleElement { |
| 29 |
|
| 30 |
private static StructuredQName ERROR_TEMPLATE_NAME = |
| 31 |
new StructuredQName("saxon", NamespaceConstant.SAXON, "error-template"); |
| 32 |
|
| 33 |
private StructuredQName calledTemplateName; |
| 34 |
private NamedTemplate template = null; |
| 35 |
private boolean useTailRecursion = false; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public boolean isInstruction() { |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
public void prepareAttributes() throws XPathException { |
| 48 |
|
| 49 |
AttributeCollection atts = getAttributeList(); |
| 50 |
|
| 51 |
String nameAttribute = null; |
| 52 |
|
| 53 |
for (int a = 0; a < atts.getLength(); a++) { |
| 54 |
String f = atts.getQName(a); |
| 55 |
if (f.equals("name")) { |
| 56 |
nameAttribute = Whitespace.trim(atts.getValue(a)); |
| 57 |
} else { |
| 58 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if (nameAttribute == null) { |
| 63 |
calledTemplateName = ERROR_TEMPLATE_NAME; |
| 64 |
reportAbsence("name"); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
try { |
| 69 |
calledTemplateName = makeQName(nameAttribute); |
| 70 |
} catch (XPathException err) { |
| 71 |
calledTemplateName = ERROR_TEMPLATE_NAME; |
| 72 |
compileErrorInAttribute(err.getMessage(), err.getErrorCodeLocalPart(), "name"); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 77 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 78 |
while (true) { |
| 79 |
NodeInfo child = kids.next(); |
| 80 |
if (child == null) { |
| 81 |
break; |
| 82 |
} |
| 83 |
if (child instanceof XSLWithParam) { |
| 84 |
|
| 85 |
} else if (child instanceof XSLFallback && mayContainFallback()) { |
| 86 |
|
| 87 |
} else if (child.getNodeKind() == Type.TEXT) { |
| 88 |
|
| 89 |
if (!Whitespace.isWhite(child.getStringValueCS())) { |
| 90 |
compileError("No character data is allowed within xsl:call-template", "XTSE0010"); |
| 91 |
} |
| 92 |
} else { |
| 93 |
compileError("Child element " + Err.wrap(child.getDisplayName(), Err.ELEMENT) + |
| 94 |
" is not allowed as a child of xsl:call-template", "XTSE0010"); |
| 95 |
} |
| 96 |
} |
| 97 |
if (!calledTemplateName.equals(ERROR_TEMPLATE_NAME)) { |
| 98 |
template = findTemplate(calledTemplateName); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
public void postValidate() throws XPathException { |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if (template != null) { |
| 107 |
checkParams(); |
| 108 |
} else { |
| 109 |
throw new AssertionError("Target template not known"); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
private void checkParams() throws XPathException { |
| 114 |
List<NamedTemplate.LocalParamInfo> declaredParams = template.getLocalParamDetails(); |
| 115 |
for (NamedTemplate.LocalParamInfo param : declaredParams) { |
| 116 |
if (param.isRequired && !param.isTunnel) { |
| 117 |
AxisIterator actualParams = iterateAxis(AxisInfo.CHILD); |
| 118 |
boolean ok = false; |
| 119 |
NodeInfo withParam; |
| 120 |
while ((withParam = actualParams.next()) != null) { |
| 121 |
if (withParam instanceof XSLWithParam && |
| 122 |
((XSLWithParam) withParam).getVariableQName().equals(param.name)) { |
| 123 |
ok = true; |
| 124 |
break; |
| 125 |
} |
| 126 |
} |
| 127 |
if (!ok) { |
| 128 |
compileError("No value supplied for required parameter " + |
| 129 |
Err.wrap(param.name.getDisplayName(), Err.VARIABLE), "XTSE0690"); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
AxisIterator actualParams = iterateAxis(AxisInfo.CHILD); |
| 138 |
NodeInfo w; |
| 139 |
while ((w = actualParams.next()) != null) { |
| 140 |
if (w instanceof XSLWithParam && !((XSLWithParam) w).isTunnelParam()) { |
| 141 |
XSLWithParam withParam = (XSLWithParam) w; |
| 142 |
boolean ok = false; |
| 143 |
for (NamedTemplate.LocalParamInfo param : declaredParams) { |
| 144 |
if (param.name.equals(withParam.getVariableQName()) && !param.isTunnel) { |
| 145 |
|
| 146 |
ok = true; |
| 147 |
SequenceType required = param.requiredType; |
| 148 |
withParam.checkAgainstRequiredType(required); |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
if (!ok && !xPath10ModeIsEnabled()) { |
| 153 |
compileError("Parameter " + |
| 154 |
withParam.getVariableQName().getDisplayName() + |
| 155 |
" is not declared in the called template", "XTSE0680"); |
| 156 |
|
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
private NamedTemplate findTemplate(StructuredQName templateName) throws XPathException { |
| 163 |
PrincipalStylesheetModule pack = getPrincipalStylesheetModule(); |
| 164 |
NamedTemplate template = pack.getNamedTemplate(templateName); |
| 165 |
if (template == null) { |
| 166 |
if (templateName.hasURI(NamespaceConstant.XSLT) && templateName.getLocalPart().equals("original")) { |
| 167 |
|
| 168 |
return (NamedTemplate) getXslOriginal(StandardNames.XSL_TEMPLATE); |
| 169 |
} |
| 170 |
compileError("Cannot find a template named " + calledTemplateName, "XTSE0650"); |
| 171 |
} |
| 172 |
return template; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public boolean markTailCalls() { |
| 181 |
useTailRecursion = true; |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 187 |
if (template == null) { |
| 188 |
return null; |
| 189 |
} |
| 190 |
|
| 191 |
CallTemplate call = new CallTemplate(template, calledTemplateName, useTailRecursion, isWithinDeclaredStreamableConstruct()); |
| 192 |
call.setLocation(allocateLocation()); |
| 193 |
call.setActualParameters( |
| 194 |
getWithParamInstructions(call, exec, decl, false), |
| 195 |
getWithParamInstructions(call, exec, decl, true)); |
| 196 |
return call; |
| 197 |
} |
| 198 |
|
| 199 |
} |
| 200 |
|