Debian Codegraph @

net/sf/saxon/style/XSLCatch.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.Expression;
11 import net.sf.saxon.om.*;
12 import net.sf.saxon.pattern.*;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.type.Type;
15
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.StringTokenizer;
19
20 /**
21 * Handler for xsl:catch elements in stylesheet.
22 * The xsl:catch element has a mandatory attribute errors, a space-separated list of NameTests defining
23 * which errors are caught. If an error is caught, the value to be returned may be defined either by a select
24 * attribute or by the content of the saxon:catch element.
25 */
26
27 public class XSLCatch extends StyleElement {
28
29 private Expression select;
30 private QNameTest nameTest;
31
32 /**
33 * Determine whether this node is an instruction.
34 *
35 * @return true - it is an instruction
36 */
37
38 public boolean isInstruction() {
39 return false;
40 }
41
42 /**
43 * Determine whether this type of element is allowed to contain a template-body
44 *
45 * @return true: yes, it may contain a template-body
46 */
47
48 public boolean mayContainSequenceConstructor() {
49 return true;
50 }
51
52 /**
53 * Ask whether variables declared in an "uncle" element are visible.
54 *
55 * @return true for all elements except xsl:fallback and saxon:catch
56 */
57
58 protected boolean seesAvuncularVariables() {
59 return false;
60 }
61
62 public void prepareAttributes() throws XPathException {
63
64 String selectAtt = null;
65 String errorAtt = null;
66
67 AttributeCollection atts = getAttributeList();
68
69 for (int a = 0; a < atts.getLength(); a++) {
70 String f = atts.getQName(a);
71 if (f.equals("select")) {
72 selectAtt = atts.getValue(a);
73 select = makeExpression(selectAtt, a);
74 } else if (f.equals("errors")) {
75 errorAtt = atts.getValue(a);
76 } else {
77 checkUnknownAttribute(atts.getNodeName(a));
78 }
79 }
80
81 if (errorAtt == null) {
82 // default is "catch all errors"
83 nameTest = AnyNodeTest.getInstance(); // for error recovery
84 } else {
85 List<QNameTest> tests = parseNameTests(errorAtt);
86 if (tests.size() == 0) {
87 compileError("xsl:catch/@errors must not be empty");
88 }
89 if (tests.size() == 1) {
90 nameTest = tests.get(0);
91 } else {
92 nameTest = new UnionQNameTest(tests);
93 }
94 }
95 }
96
97 /**
98 * Parse an attribute consisting of a space-separated sequence of NameTests
99 *
100 * @param elements the value of the attribute
101 * @return the sequence of NameTests
102 * @throws XPathException in the event of a syntax error
103 */
104
105 private List<QNameTest> parseNameTests(String elements) throws XPathException {
106 List<QNameTest> result = new ArrayList<QNameTest>();
107 StringTokenizer st = new StringTokenizer(elements, " \t\n\r", false);
108 while (st.hasMoreTokens()) {
109 String s = st.nextToken();
110 QNameTest nt;
111 if (s.equals("*")) {
112 nt = AnyNodeTest.getInstance();
113 result.add(nt);
114
115 } else if (s.endsWith(":*")) {
116 if (s.length() == 2) {
117 compileError("No prefix before ':*'");
118 result.add(AnyNodeTest.getInstance());
119 }
120 String prefix = s.substring(0, s.length() - 2);
121 String uri = getURIForPrefix(prefix, false);
122 nt = new NamespaceTest(
123 getNamePool(),
124 Type.ELEMENT,
125 uri);
126 result.add(nt);
127 } else if (s.startsWith("*:")) {
128 if (s.length() == 2) {
129 compileErrorInAttribute("No local name after '*:'", "XTSE0010", "errors");
130 result.add(AnyNodeTest.getInstance());
131 }
132 String localname = s.substring(2);
133 nt = new LocalNameTest(
134 getNamePool(),
135 Type.ELEMENT,
136 localname);
137 result.add(nt);
138 } else {
139 String prefix;
140 String localName;
141 String uri;
142 try {
143 String[] parts = NameChecker.getQNameParts(s);
144 prefix = parts[0];
145 if (parts[0].equals("")) {
146 uri = "";
147 } else {
148 uri = getURIForPrefix(prefix, false);
149 if (uri == null) {
150 undeclaredNamespaceError(prefix, "XTSE0280", "errors");
151 result.add(AnyNodeTest.getInstance());
152 break;
153 }
154 }
155 localName = parts[1];
156 } catch (QNameException err) {
157 compileErrorInAttribute("Error code " + s + " is not a valid QName", "XTSE0280", "errors");
158 result.add(AnyNodeTest.getInstance());
159 break;
160 }
161 NamePool target = getNamePool();
162 int nameCode = target.allocateFingerprint(uri, localName);
163 nt = new NameTest(Type.ELEMENT, nameCode, getNamePool());
164 result.add(nt);
165 }
166 }
167 return result;
168 }
169
170 public void validate(ComponentDeclaration decl) throws XPathException {
171 select = typeCheck("select", select);
172 if (select != null && hasChildNodes()) {
173 compileError("An xsl:catch element with a select attribute must be empty", "XTSE3150");
174 }
175 if (!(getParent() instanceof XSLTry)) {
176 compileError("xsl:catch may appear only as a child of xsl:try");
177 }
178 }
179
180 /*@Nullable*/
181 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
182 if (select == null) {
183 select = compileSequenceConstructor(exec, decl, true);
184 }
185 ((XSLTry) getParent()).addCatchClause(nameTest, select);
186 return null;
187 }
188
189 }