KickJava   Java API By Example, From Geeks To Geeks.

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


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.Executable;
5 import net.sf.saxon.instruct.ForEach;
6 import net.sf.saxon.om.AttributeCollection;
7 import net.sf.saxon.om.Axis;
8 import net.sf.saxon.sort.SortExpression;
9 import net.sf.saxon.sort.SortKeyDefinition;
10 import net.sf.saxon.trans.XPathException;
11 import net.sf.saxon.type.ItemType;
12 import net.sf.saxon.value.EmptySequence;
13
14
15 /**
16 * Handler for xsl:for-each elements in stylesheet. <br>
17 */

18
19 public class XSLForEach extends StyleElement {
20
21     Expression select = null;
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      * Specify that xsl:sort is a permitted child
34      */

35
36     protected boolean isPermittedChild(StyleElement child) {
37         return (child instanceof XSLSort);
38     }
39
40     /**
41      * Determine the type of item returned by this instruction (only relevant if
42      * it is an instruction).
43      * @return the item type returned
44      */

45
46     protected ItemType getReturnedItemType() {
47         return getCommonChildItemType();
48     }
49
50     /**
51     * Determine whether this type of element is allowed to contain a template-body
52     * @return true: yes, it may contain a template-body
53     */

54
55     public boolean mayContainSequenceConstructor() {
56         return true;
57     }
58
59     public void prepareAttributes() throws XPathException {
60
61         AttributeCollection atts = getAttributeList();
62
63         String JavaDoc selectAtt = null;
64
65         for (int a=0; a<atts.getLength(); a++) {
66             int nc = atts.getNameCode(a);
67             String JavaDoc f = getNamePool().getClarkName(nc);
68             if (f==StandardNames.SELECT) {
69                 selectAtt = atts.getValue(a);
70             } else {
71                 checkUnknownAttribute(nc);
72             }
73         }
74
75         if (selectAtt==null) {
76             reportAbsence("select");
77         } else {
78             select = makeExpression(selectAtt);
79         }
80
81     }
82
83     public void validate() throws XPathException {
84         checkWithinTemplate();
85         checkSortComesFirst(false);
86         select = typeCheck("select", select);
87     }
88
89     public Expression compile(Executable exec) throws XPathException {
90         SortKeyDefinition[] sortKeys = makeSortKeys();
91         Expression sortedSequence = select;
92         if (sortKeys != null) {
93             sortedSequence = new SortExpression(select, sortKeys);
94             ExpressionTool.makeParentReferences(sortedSequence);
95         }
96
97         Expression block = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true);
98         if (block == null) {
99             // body of for-each is empty: it's a no-op.
100
return EmptySequence.getInstance();
101         }
102         try {
103             ForEach inst = new ForEach(sortedSequence, block.simplify(getStaticContext()));
104             ExpressionTool.makeParentReferences(inst);
105             return inst;
106         } catch (XPathException err) {
107             compileError(err);
108             return null;
109         }
110     }
111
112
113 }
114
115 //
116
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
117
// you may not use this file except in compliance with the License. You may obtain a copy of the
118
// License at http://www.mozilla.org/MPL/
119
//
120
// Software distributed under the License is distributed on an "AS IS" basis,
121
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
122
// See the License for the specific language governing rights and limitations under the License.
123
//
124
// The Original Code is: all this file.
125
//
126
// The Initial Developer of the Original Code is Michael H. Kay.
127
//
128
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
129
//
130
// Contributor(s): none.
131
//
132
Popular Tags