Debian Codegraph @

net/sf/saxon/style/XSLTry.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * Handler for xsl:try elements in stylesheet.
25 * The xsl:try element contains a sequence constructor or a select expression,
26 * which defines the expression to be evaluated, and it may contain one or more
27 * xsl:catch elements, which define the value to be returned in the event of
28 * dynamic errors.
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 * Determine whether this node is an instruction.
40 *
41 * @return true - it is an instruction
42 */
43
44 public boolean isInstruction() {
45 return true;
46 }
47
48 /**
49 * Determine whether this type of element is allowed to contain a sequence constructor
50 *
51 * @return true: yes, it may contain a sequence constructor
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 // no action;
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 }