Debian Codegraph @

net/sf/saxon/style/XSLAssert.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.expr.Literal;
12 import net.sf.saxon.expr.StringLiteral;
13 import net.sf.saxon.expr.instruct.Block;
14 import net.sf.saxon.expr.instruct.Choose;
15 import net.sf.saxon.expr.instruct.Message;
16 import net.sf.saxon.functions.SystemFunction;
17 import net.sf.saxon.om.AttributeCollection;
18 import net.sf.saxon.om.StructuredQName;
19 import net.sf.saxon.trans.XPathException;
20
21
22 /**
23 * An xsl:assert element in an XSLT 3.0 stylesheet.
24 */
25
26 public final class XSLAssert extends StyleElement {
27
28 private Expression test = null;
29 private Expression select = null;
30 private Expression errorCode = null;
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 true;
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 public void prepareAttributes() {
53
54 String testAtt = null;
55 String selectAtt = null;
56 String errorCodeAtt = null;
57 AttributeCollection atts = getAttributeList();
58
59 for (int a = 0; a < atts.getLength(); a++) {
60 String f = atts.getQName(a);
61 switch (f) {
62 case "test":
63 testAtt = atts.getValue(a);
64 test = makeExpression(testAtt, a);
65 break;
66 case "select":
67 selectAtt = atts.getValue(a);
68 select = makeExpression(selectAtt, a);
69 break;
70 case "error-code":
71 errorCodeAtt = atts.getValue(a);
72 errorCode = makeAttributeValueTemplate(errorCodeAtt, a);
73 break;
74 default:
75 checkUnknownAttribute(atts.getNodeName(a));
76 break;
77 }
78 }
79
80 if (testAtt == null) {
81 reportAbsence("test");
82 }
83
84 }
85
86 public void validate(ComponentDeclaration decl) throws XPathException {
87 select = typeCheck("select", select);
88 test = typeCheck("test", test);
89 if (errorCode == null) {
90 errorCode = new StringLiteral("Q{http://www.w3.org/2005/xqt-errors}XTMM9001");
91 } else {
92 errorCode = typeCheck("error-code", errorCode);
93 }
94 }
95
96 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
97 if (exec.getCompilerInfo().isAssertionsEnabled()) {
98 Expression b = compileSequenceConstructor(exec, decl, true);
99 if (b != null) {
100 if (select == null) {
101 select = b;
102 } else {
103 select = Block.makeBlock(select, b);
104 select.setLocation(
105 allocateLocation());
106 }
107 }
108 if (select == null) {
109 select = new StringLiteral("xsl:message (no content)");
110 }
111 if (errorCode instanceof StringLiteral) {
112 // resolve any QName prefix now
113 String code = ((StringLiteral) errorCode).getStringValue();
114 if (code.contains(":") && !code.startsWith("Q{")) {
115 StructuredQName name = makeQName(code);
116 errorCode = new StringLiteral(name.getEQName());
117 }
118 }
119 Message msg = new Message(select, new StringLiteral("yes"), errorCode);
120 msg.setIsAssert(true);
121 if (!(errorCode instanceof StringLiteral)) {
122 // evaluation of the error code may need the namespace context
123 msg.setRetainedStaticContext(makeRetainedStaticContext());
124 }
125 Expression condition = SystemFunction.makeCall("not", test.getRetainedStaticContext(), test);
126
127 return new Choose(new Expression[]{condition}, new Expression[]{msg});
128 } else {
129 // assertions are disabled (the default)
130 return Literal.makeEmptySequence();
131 }
132 }
133
134 }