KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > XSLIf


1 package net.sf.saxon.style;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.ExpressionTool;
4 import net.sf.saxon.instruct.Choose;
5 import net.sf.saxon.instruct.Executable;
6 import net.sf.saxon.om.AttributeCollection;
7 import net.sf.saxon.om.Axis;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.type.ItemType;
10 import net.sf.saxon.value.Value;
11
12
13 /**
14 * Handler for xsl:if elements in stylesheet. <br>
15 * The xsl:if element has a mandatory attribute test, a boolean expression.
16 * The content is output if the test condition is true.
17 */

18
19 public class XSLIf extends StyleElement {
20
21     private Expression test;
22
23     /**
24     * Determine whether this node is an instruction.
25     * @return true - it is an instruction
26     */

27
28     public boolean isInstruction() {
29         return true;
30     }
31
32     /**
33      * Determine the type of item returned by this instruction (only relevant if
34      * it is an instruction).
35      * @return the item type returned
36      */

37
38     protected ItemType getReturnedItemType() {
39         return getCommonChildItemType();
40     }
41
42     /**
43     * Determine whether this type of element is allowed to contain a template-body
44     * @return true: yes, it may contain a template-body
45     */

46
47     public boolean mayContainSequenceConstructor() {
48         return true;
49     }
50
51     public void prepareAttributes() throws XPathException {
52
53         String JavaDoc testAtt=null;
54
55         AttributeCollection atts = getAttributeList();
56
57         for (int a=0; a<atts.getLength(); a++) {
58             int nc = atts.getNameCode(a);
59             String JavaDoc f = getNamePool().getClarkName(nc);
60             if (f==StandardNames.TEST) {
61                 testAtt = atts.getValue(a);
62             } else {
63                 checkUnknownAttribute(nc);
64             }
65         }
66
67         if (testAtt==null) {
68             reportAbsence("test");
69         } else {
70             test = makeExpression(testAtt);
71         }
72     }
73
74     public void validate() throws XPathException {
75         checkWithinTemplate();
76         test = typeCheck("test", test);
77     }
78
79     /**
80     * Mark tail-recursive calls on stylesheet functions. For most instructions, this does nothing.
81     */

82
83     public void markTailCalls() {
84         StyleElement last = getLastChildInstruction();
85         if (last != null) {
86             last.markTailCalls();
87         }
88     }
89
90     public Expression compile(Executable exec) throws XPathException {
91         if (test instanceof Value) {
92             // condition known statically, so we only need compile the code if true.
93
// This can happen with expressions such as test="function-available('abc')".
94
try {
95                 if (test.effectiveBooleanValue(null)) {
96                     return compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true);
97 // Block block = new Block();
98
// block.setLocationId(allocateLocationId(getSystemId(), getLineNumber()));
99
// compileChildren(exec, block, true);
100
// return block.simplify(getStaticContext());
101
} else {
102                     return null;
103                 }
104             } catch (XPathException err) {
105                 // can't happen, but if it does then we'll fall through to non-optimizing case
106
}
107         }
108
109         Expression action = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true);
110         if (action == null) {
111             return null;
112         }
113         Expression[] conditions = {test};
114         Expression[] actions = {action};
115
116         Choose inst = new Choose(conditions, actions);
117         ExpressionTool.makeParentReferences(inst);
118         return inst;
119     }
120
121
122 }
123 //
124
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
125
// you may not use this file except in compliance with the License. You may obtain a copy of the
126
// License at http://www.mozilla.org/MPL/
127
//
128
// Software distributed under the License is distributed on an "AS IS" basis,
129
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
130
// See the License for the specific language governing rights and limitations under the License.
131
//
132
// The Original Code is: all this file.
133
//
134
// The Initial Developer of the Original Code is Michael H. Kay.
135
//
136
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
137
//
138
// Contributor(s): none.
139
//
140
Popular Tags