KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > style > XSLForEach


1 package com.icl.saxon.style;
2 import com.icl.saxon.tree.AttributeCollection;
3 import com.icl.saxon.*;
4 import com.icl.saxon.expr.*;
5 import javax.xml.transform.*;
6
7 import com.icl.saxon.trace.TraceListener;
8 import com.icl.saxon.om.NodeInfo;
9 import com.icl.saxon.om.NodeEnumeration;
10
11 /**
12 * Handler for xsl:for-each elements in stylesheet.<BR>
13 */

14
15 public class XSLForEach extends StyleElement {
16
17     Expression select = null;
18
19     /**
20     * Determine whether this node is an instruction.
21     * @return true - it is an instruction
22     */

23
24     public boolean isInstruction() {
25         return true;
26     }
27
28     /**
29     * Determine whether this type of element is allowed to contain a template-body
30     * @return true: yes, it may contain a template-body
31     */

32
33     public boolean mayContainTemplateBody() {
34         return true;
35     }
36
37     public void prepareAttributes() throws TransformerConfigurationException {
38
39         StandardNames sn = getStandardNames();
40         AttributeCollection atts = getAttributeList();
41         
42         String JavaDoc selectAtt = null;
43         
44         for (int a=0; a<atts.getLength(); a++) {
45             int nc = atts.getNameCode(a);
46             int f = nc & 0xfffff;
47             if (f==sn.SELECT) {
48                 selectAtt = atts.getValue(a);
49             } else {
50                 checkUnknownAttribute(nc);
51             }
52         }
53         
54         if (selectAtt==null) {
55             reportAbsence("select");
56         } else {
57             select = makeExpression(selectAtt);
58         }
59
60     }
61
62     public void validate() throws TransformerConfigurationException {
63         checkWithinTemplate();
64         select = handleSortKeys(select);
65     }
66
67     public void process(Context context) throws TransformerException
68     {
69         XSLTemplate saveCurrent = context.getCurrentTemplate();
70         context.setCurrentTemplate(null);
71         NodeEnumeration selection = select.enumerate(context, false);
72         if (!(selection instanceof LastPositionFinder)) {
73             selection = new LookaheadEnumerator(selection);
74         }
75  
76         Context c = context.newContext();
77         c.setLastPositionFinder((LastPositionFinder)selection);
78         int position = 1;
79
80         if (context.getController().isTracing()) {
81             TraceListener listener = context.getController().getTraceListener();
82             while(selection.hasMoreElements()) {
83                 NodeInfo node = selection.nextElement();
84                 c.setPosition(position++);
85                 c.setCurrentNode(node);
86                 c.setContextNode(node);
87                 listener.enterSource(null, c);
88                 processChildren(c);
89                 listener.leaveSource(null, c);
90                 context.setReturnValue(c.getReturnValue());
91             }
92         } else {
93             while(selection.hasMoreElements()) {
94                 NodeInfo node = selection.nextElement();
95                 c.setPosition(position++);
96                 c.setCurrentNode(node);
97                 c.setContextNode(node);
98                 processChildren(c);
99                 context.setReturnValue(c.getReturnValue());
100             }
101         }
102         context.setCurrentTemplate(saveCurrent);
103     }
104
105
106 }
107
108 //
109
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
110
// you may not use this file except in compliance with the License. You may obtain a copy of the
111
// License at http://www.mozilla.org/MPL/
112
//
113
// Software distributed under the License is distributed on an "AS IS" basis,
114
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
115
// See the License for the specific language governing rights and limitations under the License.
116
//
117
// The Original Code is: all this file.
118
//
119
// The Initial Developer of the Original Code is
120
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
121
//
122
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
123
//
124
// Contributor(s): none.
125
//
126
Popular Tags