| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.TryCatch; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.om.AttributeCollection; |
| 13 |
import net.sf.saxon.om.AxisInfo; |
| 14 |
import net.sf.saxon.om.NodeInfo; |
| 15 |
import net.sf.saxon.pattern.QNameTest; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.List; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class XSLTry extends StyleElement { |
| 32 |
|
| 33 |
private Expression select; |
| 34 |
private boolean rollbackOutput = true; |
| 35 |
private List<QNameTest> catchTests = new ArrayList<>(); |
| 36 |
private List<Expression> catchExprs = new ArrayList<>(); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public boolean isInstruction() { |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public boolean mayContainSequenceConstructor() { |
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
public void prepareAttributes() throws XPathException { |
| 59 |
|
| 60 |
String selectAtt = null; |
| 61 |
String rollbackOutputAtt = null; |
| 62 |
|
| 63 |
AttributeCollection atts = getAttributeList(); |
| 64 |
|
| 65 |
for (int a = 0; a < atts.getLength(); a++) { |
| 66 |
String f = atts.getNodeName(a).getDisplayName(); |
| 67 |
switch (f) { |
| 68 |
case "select": |
| 69 |
selectAtt = atts.getValue(a); |
| 70 |
select = makeExpression(selectAtt, a); |
| 71 |
break; |
| 72 |
case "rollback-output": |
| 73 |
rollbackOutputAtt = atts.getValue(a); |
| 74 |
break; |
| 75 |
default: |
| 76 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if (rollbackOutputAtt != null) { |
| 82 |
rollbackOutput = processBooleanAttribute("rollback-output", rollbackOutputAtt); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
protected boolean isPermittedChild(StyleElement child) { |
| 87 |
return child instanceof XSLCatch; |
| 88 |
} |
| 89 |
|
| 90 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 91 |
select = typeCheck("select", select); |
| 92 |
boolean foundCatch = false; |
| 93 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 94 |
NodeInfo kid; |
| 95 |
while ((kid = kids.next()) != null) { |
| 96 |
if (kid instanceof XSLCatch) { |
| 97 |
foundCatch = true; |
| 98 |
} else if (kid instanceof XSLFallback) { |
| 99 |
|
| 100 |
} else { |
| 101 |
if (foundCatch) { |
| 102 |
compileError("xsl:catch elements must come after all other children of xsl:try (excepting xsl:fallback)", "XTSE0010"); |
| 103 |
} |
| 104 |
if (select != null) { |
| 105 |
compileError("An " + getDisplayName() + " element with a select attribute must be empty", "XTSE3140"); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
if (!foundCatch) { |
| 110 |
compileError("xsl:try must have at least one xsl:catch child element", "XTSE0010"); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 115 |
Expression content = compileSequenceConstructor(exec, decl, true); |
| 116 |
if (select == null) { |
| 117 |
select = content; |
| 118 |
} |
| 119 |
TryCatch expr = new TryCatch(select); |
| 120 |
for (int i = 0; i < catchTests.size(); i++) { |
| 121 |
expr.addCatchExpression(catchTests.get(i), catchExprs.get(i)); |
| 122 |
} |
| 123 |
expr.setRollbackOutput(rollbackOutput); |
| 124 |
return expr; |
| 125 |
} |
| 126 |
|
| 127 |
public void addCatchClause(QNameTest nameTest, Expression catchExpr) { |
| 128 |
catchTests.add(nameTest); |
| 129 |
catchExprs.add(catchExpr); |
| 130 |
} |
| 131 |
|
| 132 |
} |