| 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.Literal; |
| 12 |
import net.sf.saxon.expr.instruct.Choose; |
| 13 |
import net.sf.saxon.om.AttributeCollection; |
| 14 |
import net.sf.saxon.om.AxisInfo; |
| 15 |
import net.sf.saxon.om.NodeInfo; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 18 |
import net.sf.saxon.value.BooleanValue; |
| 19 |
|
| 20 |
import java.util.Arrays; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class XSLChoose extends StyleElement { |
| 27 |
|
| 28 |
private StyleElement otherwise; |
| 29 |
private int numberOfWhens = 0; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public boolean isInstruction() { |
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
public void prepareAttributes() throws XPathException { |
| 43 |
AttributeCollection atts = getAttributeList(); |
| 44 |
for (int a = 0; a < atts.getLength(); a++) { |
| 45 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 50 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 51 |
while (true) { |
| 52 |
NodeInfo curr = kids.next(); |
| 53 |
if (curr == null) { |
| 54 |
break; |
| 55 |
} |
| 56 |
if (curr instanceof XSLWhen) { |
| 57 |
if (otherwise != null) { |
| 58 |
otherwise.compileError("xsl:otherwise must come last", "XTSE0010"); |
| 59 |
} |
| 60 |
numberOfWhens++; |
| 61 |
} else if (curr instanceof XSLOtherwise) { |
| 62 |
if (otherwise != null) { |
| 63 |
((XSLOtherwise) curr).compileError("Only one xsl:otherwise is allowed in an xsl:choose", "XTSE0010"); |
| 64 |
} else { |
| 65 |
otherwise = (StyleElement) curr; |
| 66 |
} |
| 67 |
} else if (curr instanceof StyleElement) { |
| 68 |
((StyleElement) curr).compileError("Only xsl:when and xsl:otherwise are allowed here", "XTSE0010"); |
| 69 |
} else { |
| 70 |
compileError("Only xsl:when and xsl:otherwise are allowed within xsl:choose", "XTSE0010"); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if (numberOfWhens == 0) { |
| 75 |
compileError("xsl:choose must contain at least one xsl:when", "XTSE0010"); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public boolean markTailCalls() { |
| 84 |
boolean found = false; |
| 85 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 86 |
while (true) { |
| 87 |
NodeInfo curr = kids.next(); |
| 88 |
if (curr == null) { |
| 89 |
return found; |
| 90 |
} |
| 91 |
if (curr instanceof StyleElement) { |
| 92 |
found |= ((StyleElement) curr).markTailCalls(); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 100 |
|
| 101 |
int entries = numberOfWhens + (otherwise == null ? 0 : 1); |
| 102 |
Expression[] conditions = new Expression[entries]; |
| 103 |
Expression[] actions = new Expression[entries]; |
| 104 |
|
| 105 |
int w = 0; |
| 106 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 107 |
while (true) { |
| 108 |
NodeInfo curr = kids.next(); |
| 109 |
if (curr == null) { |
| 110 |
break; |
| 111 |
} |
| 112 |
if (curr instanceof XSLWhen) { |
| 113 |
conditions[w] = ((XSLWhen) curr).getCondition(); |
| 114 |
Expression b = ((XSLWhen) curr).compileSequenceConstructor(exec, decl, true); |
| 115 |
if (b == null) { |
| 116 |
b = Literal.makeEmptySequence(); |
| 117 |
} |
| 118 |
try { |
| 119 |
b = b.simplify(); |
| 120 |
actions[w] = b; |
| 121 |
} catch (XPathException e) { |
| 122 |
compileError(e); |
| 123 |
} |
| 124 |
|
| 125 |
if (getCompilation().getCompilerInfo().isCompileWithTracing()) { |
| 126 |
actions[w] = makeTraceInstruction((XSLWhen) curr, actions[w]); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
if (conditions[w] instanceof Literal && ((Literal) conditions[w]).getValue() instanceof BooleanValue) { |
| 131 |
if (((BooleanValue) ((Literal) conditions[w]).getValue()).getBooleanValue()) { |
| 132 |
|
| 133 |
entries = w + 1; |
| 134 |
break; |
| 135 |
} else { |
| 136 |
|
| 137 |
w--; |
| 138 |
entries--; |
| 139 |
} |
| 140 |
} |
| 141 |
w++; |
| 142 |
} else if (curr instanceof XSLOtherwise) { |
| 143 |
Expression otherwise = Literal.makeLiteral(BooleanValue.TRUE); |
| 144 |
otherwise.setRetainedStaticContext(makeRetainedStaticContext()); |
| 145 |
conditions[w] = otherwise; |
| 146 |
Expression b = ((XSLOtherwise) curr).compileSequenceConstructor(exec, decl, true); |
| 147 |
if (b == null) { |
| 148 |
b = Literal.makeEmptySequence(); |
| 149 |
b.setRetainedStaticContext(makeRetainedStaticContext()); |
| 150 |
} |
| 151 |
try { |
| 152 |
b = b.simplify(); |
| 153 |
actions[w] = b; |
| 154 |
} catch (XPathException e) { |
| 155 |
compileError(e); |
| 156 |
} |
| 157 |
if (getCompilation().getCompilerInfo().isCompileWithTracing()) { |
| 158 |
actions[w] = makeTraceInstruction((XSLOtherwise) curr, actions[w]); |
| 159 |
} |
| 160 |
w++; |
| 161 |
} else { |
| 162 |
|
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if (conditions.length != entries) { |
| 167 |
|
| 168 |
if (entries == 0) { |
| 169 |
return null; |
| 170 |
} |
| 171 |
if (entries == 1 && (conditions[0] instanceof Literal) && |
| 172 |
((Literal) conditions[0]).getValue() instanceof BooleanValue) { |
| 173 |
if (((BooleanValue) ((Literal) conditions[0]).getValue()).getBooleanValue()) { |
| 174 |
|
| 175 |
return actions[0]; |
| 176 |
} else { |
| 177 |
|
| 178 |
return null; |
| 179 |
} |
| 180 |
} |
| 181 |
conditions = Arrays.copyOf(conditions, entries); |
| 182 |
actions = Arrays.copyOf(actions, entries); |
| 183 |
} |
| 184 |
|
| 185 |
Choose choose = new Choose(conditions, actions); |
| 186 |
choose.setInstruction(true); |
| 187 |
return choose; |
| 188 |
} |
| 189 |
|
| 190 |
} |
| 191 |
|