1 package net.sf.saxon.style; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.expr.ExpressionTool; 4 import net.sf.saxon.instruct.Choose; 5 import net.sf.saxon.instruct.Executable; 6 import net.sf.saxon.om.AttributeCollection; 7 import net.sf.saxon.om.Axis; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.type.ItemType; 10 import net.sf.saxon.value.Value; 11 12 13 18 19 public class XSLIf extends StyleElement { 20 21 private Expression test; 22 23 27 28 public boolean isInstruction() { 29 return true; 30 } 31 32 37 38 protected ItemType getReturnedItemType() { 39 return getCommonChildItemType(); 40 } 41 42 46 47 public boolean mayContainSequenceConstructor() { 48 return true; 49 } 50 51 public void prepareAttributes() throws XPathException { 52 53 String testAtt=null; 54 55 AttributeCollection atts = getAttributeList(); 56 57 for (int a=0; a<atts.getLength(); a++) { 58 int nc = atts.getNameCode(a); 59 String f = getNamePool().getClarkName(nc); 60 if (f==StandardNames.TEST) { 61 testAtt = atts.getValue(a); 62 } else { 63 checkUnknownAttribute(nc); 64 } 65 } 66 67 if (testAtt==null) { 68 reportAbsence("test"); 69 } else { 70 test = makeExpression(testAtt); 71 } 72 } 73 74 public void validate() throws XPathException { 75 checkWithinTemplate(); 76 test = typeCheck("test", test); 77 } 78 79 82 83 public void markTailCalls() { 84 StyleElement last = getLastChildInstruction(); 85 if (last != null) { 86 last.markTailCalls(); 87 } 88 } 89 90 public Expression compile(Executable exec) throws XPathException { 91 if (test instanceof Value) { 92 try { 95 if (test.effectiveBooleanValue(null)) { 96 return compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true); 97 } else { 102 return null; 103 } 104 } catch (XPathException err) { 105 } 107 } 108 109 Expression action = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true); 110 if (action == null) { 111 return null; 112 } 113 Expression[] conditions = {test}; 114 Expression[] actions = {action}; 115 116 Choose inst = new Choose(conditions, actions); 117 ExpressionTool.makeParentReferences(inst); 118 return inst; 119 } 120 121 122 } 123 | Popular Tags |