| 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.GroundedValue; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class XSLIf extends StyleElement { |
| 25 |
|
| 26 |
private Expression test; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public boolean isInstruction() { |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public boolean mayContainSequenceConstructor() { |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
public void prepareAttributes() throws XPathException { |
| 49 |
test = prepareTestAttribute(this); |
| 50 |
if (test == null) { |
| 51 |
reportAbsence("test"); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static Expression prepareTestAttribute(StyleElement se) throws XPathException { |
| 64 |
|
| 65 |
String testAtt = null; |
| 66 |
|
| 67 |
AttributeCollection atts = se.getAttributeList(); |
| 68 |
|
| 69 |
for (int a = 0; a < atts.getLength(); a++) { |
| 70 |
String f = atts.getQName(a); |
| 71 |
if (f.equals("test")) { |
| 72 |
testAtt = atts.getValue(a); |
| 73 |
} else { |
| 74 |
se.checkUnknownAttribute(atts.getNodeName(a)); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if (testAtt == null) { |
| 79 |
return null; |
| 80 |
} else { |
| 81 |
return se.makeExpression(testAtt, atts.getIndex("", "test")); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 86 |
test = typeCheck("test", test); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public boolean markTailCalls() { |
| 94 |
StyleElement last = getLastChildInstruction(); |
| 95 |
return last != null && last.markTailCalls(); |
| 96 |
} |
| 97 |
|
| 98 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 99 |
if (test instanceof Literal) { |
| 100 |
GroundedValue testVal = ((Literal) test).getValue(); |
| 101 |
|
| 102 |
|
| 103 |
try { |
| 104 |
if (testVal.effectiveBooleanValue()) { |
| 105 |
return compileSequenceConstructor(exec, decl, true); |
| 106 |
} else { |
| 107 |
return null; |
| 108 |
} |
| 109 |
} catch (XPathException err) { |
| 110 |
|
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
Expression action = compileSequenceConstructor(exec, decl, true); |
| 115 |
if (action == null) { |
| 116 |
return null; |
| 117 |
} |
| 118 |
Expression[] conditions = {test}; |
| 119 |
Expression[] actions = {action}; |
| 120 |
|
| 121 |
Choose choose = new Choose(conditions, actions); |
| 122 |
choose.setInstruction(true); |
| 123 |
return choose; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
} |