Debian Codegraph @

net/sf/saxon/style/XSLIf.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.instruct.Choose;
13 import net.sf.saxon.om.AttributeCollection;
14 import net.sf.saxon.om.GroundedValue;
15 import net.sf.saxon.trans.XPathException;
16
17
18 /**
19 * Handler for xsl:if elements in stylesheet. <br>
20 * The xsl:if element has a mandatory attribute test, a boolean expression.
21 * The content is output if the test condition is true.
22 */
23
24 public class XSLIf extends StyleElement {
25
26 /*@Nullable*/ private Expression test;
27
28 /**
29 * Determine whether this node is an instruction.
30 *
31 * @return true - it is an instruction
32 */
33
34 public boolean isInstruction() {
35 return true;
36 }
37
38 /**
39 * Determine whether this type of element is allowed to contain a template-body
40 *
41 * @return true: yes, it may contain a template-body
42 */
43
44 public boolean mayContainSequenceConstructor() {
45 return true;
46 }
47
48 public void prepareAttributes() throws XPathException {
49 test = prepareTestAttribute(this);
50 if (test == null) {
51 reportAbsence("test");
52 }
53 }
54
55 /**
56 * Process all the attributes, for an element where the only permitted attribute is "test"
57 *
58 * @param se the containing element
59 * @return the expression represented by the test attribute, or null if the attribute is absent
60 * @throws XPathException if an error is encountered
61 */
62
63 public static Expression prepareTestAttribute(StyleElement se) throws XPathException {
64
65 String testAtt = null;
66
67 AttributeCollection atts = se.getAttributeList();
68
69 for (int a = 0; a < atts.getLength(); a++) {
70 String f = atts.getQName(a);
71 if (f.equals("test")) {
72 testAtt = atts.getValue(a);
73 } else {
74 se.checkUnknownAttribute(atts.getNodeName(a));
75 }
76 }
77
78 if (testAtt == null) {
79 return null;
80 } else {
81 return se.makeExpression(testAtt, atts.getIndex("", "test"));
82 }
83 }
84
85 public void validate(ComponentDeclaration decl) throws XPathException {
86 test = typeCheck("test", test);
87 }
88
89 /**
90 * Mark tail-recursive calls on stylesheet functions. For most instructions, this does nothing.
91 */
92
93 public boolean markTailCalls() {
94 StyleElement last = getLastChildInstruction();
95 return last != null && last.markTailCalls();
96 }
97
98 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
99 if (test instanceof Literal) {
100 GroundedValue testVal = ((Literal) test).getValue();
101 // condition known statically, so we only need compile the code if true.
102 // This can happen with expressions such as test="function-available('abc')".
103 try {
104 if (testVal.effectiveBooleanValue()) {
105 return compileSequenceConstructor(exec, decl, true);
106 } else {
107 return null;
108 }
109 } catch (XPathException err) {
110 // fall through to non-optimizing case
111 }
112 }
113
114 Expression action = compileSequenceConstructor(exec, decl, true);
115 if (action == null) {
116 return null;
117 }
118 Expression[] conditions = {test};
119 Expression[] actions = {action};
120
121 Choose choose = new Choose(conditions, actions);
122 choose.setInstruction(true);
123 return choose;
124 }
125
126
127 }