KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xsl > java > XslForEach


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xsl.java;
30
31 import com.caucho.java.JavaWriter;
32 import com.caucho.xml.QName;
33 import com.caucho.xpath.pattern.AbstractPattern;
34 import com.caucho.xsl.Sort;
35 import com.caucho.xsl.XslParseException;
36
37 import java.util.ArrayList JavaDoc;
38
39 /**
40  * Represents the xsl:for-each element.
41  */

42 public class XslForEach extends XslNode {
43   private String JavaDoc _select;
44
45   private ArrayList JavaDoc<XslSort> _sorts = new ArrayList JavaDoc<XslSort>();
46
47   /**
48    * Adds an attribute.
49    */

50   public void addAttribute(QName name, String JavaDoc value)
51     throws XslParseException
52   {
53     if (name.getName().equals("select")) {
54       _select = value;
55     }
56     else
57       super.addAttribute(name, value);
58   }
59
60   /**
61    * Ends the attributes.
62    */

63   public void endAttributes()
64     throws XslParseException
65   {
66     /*
67     if (_select == null)
68       throw error(L.l("<xsl:for-each> requires a 'select' attribute."));
69     */

70   }
71
72   /**
73    * Adds a child node.
74    */

75   public void addChild(XslNode node)
76     throws XslParseException
77   {
78     if (node instanceof XslSort) {
79       _sorts.add((XslSort) node);
80     }
81     else
82       super.addChild(node);
83   }
84
85   /**
86    * Generates the code for the tag
87    *
88    * @param out the output writer for the generated java.
89    */

90   public void generate(JavaWriter out)
91     throws Exception JavaDoc
92   {
93     if (_sorts.size() != 0) {
94       printForEachSort(out);
95       return;
96     }
97     
98     out.println("{");
99     out.pushDepth();
100
101     AbstractPattern select = parseSelect(_select);
102     
103     boolean hasExprEnv = ! allowJavaSelect(select);
104
105     int id = _gen.generateId();
106     
107     String JavaDoc sel = "_xsl_sel" + id;
108     String JavaDoc oldCxt = "_xsl_cxt" + id;
109     String JavaDoc oldCur = "_xsl_cur" + id;
110     String JavaDoc oldSel = "_xsl_old_sel" + id;
111     String JavaDoc oldEnv = "_xsl_env" + id;
112     String JavaDoc oldSize = "_xsl_old_size" + id;
113
114     out.println("com.caucho.xpath.pattern.AbstractPattern " + sel + ";");
115     out.print(sel + " = _select_patterns[");
116     out.print(_gen.addSelect(select));
117     out.println("];");
118     out.println("Node " + oldCxt + " = env.getContextNode();");
119     out.println("Node " + oldCur + " = env.getCurrentNode();");
120     
121     if (! hasExprEnv) {
122       out.println("AbstractPattern " + oldSel + " = env.setSelect(node, " + sel + ");");
123       out.println("int " + oldSize + " = env.setContextSize(0);");
124     }
125     
126     
127     // String pos = "_xsl_pos" + unique++;
128
String JavaDoc iter = "_xsl_iter" + _gen.generateId();
129
130     int oldSelectDepth = _gen.getSelectDepth();
131     
132     // println("int " + pos + " = 0;");
133

134     boolean hasEnv = false;
135     
136     if (allowJavaSelect(select)) {
137       out.println("ExprEnvironment " + oldEnv + " = env.setExprEnv(null);");
138       
139       String JavaDoc ptr = printSelectBegin(out, select, true, null);
140
141       _gen.pushLoop();
142       out.println("Node " + _gen.getElement() + " = node;");
143       out.println("node = " + ptr + ";");
144     }
145     else {
146       out.print("NodeIterator " + iter + " = " + sel);
147       out.println(".select(node, " + getEnv() + ");");
148       out.println("ExprEnvironment " + oldEnv + " = env.setExprEnv(" + iter + ");");
149       out.println("while (" + iter + ".hasNext()) {");
150       out.pushDepth();
151       _gen.setSelectDepth(_gen.getSelectDepth() + 1);
152       
153       _gen.pushLoop();
154       
155       out.println("Node " + _gen.getElement() + " = node;");
156       out.println("node = " + iter + ".nextNode();");
157       
158     }
159     out.println("env.setCurrentNode(node);");
160     
161     // println(pos + "++;");
162

163     // String oldPos = currentPos;
164
// currentPos = pos;
165

166     AbstractPattern oldNodeListContext = _gen.getNodeListContext();
167     _gen.setNodeListContext(parseMatch(_select));
168
169     generateChildren(out);
170
171     _gen.setNodeListContext(oldNodeListContext);
172     
173     // currentPos = oldPos;
174

175     out.println("node = " + _gen.getElement() + ";");
176     out.println("env.setCurrentNode(" + oldCur + ");");
177
178     int selectDepth = _gen.getSelectDepth();
179     
180     for (; selectDepth > oldSelectDepth; selectDepth--) {
181       out.popDepth();
182       out.println("}");
183     }
184     _gen.setSelectDepth(oldSelectDepth);
185     
186     out.println("env.setExprEnv(" + oldEnv + ");");
187     
188     if (! hasExprEnv) {
189       out.println("env.setSelect(" + oldCxt + ", " + oldSel + ");");
190       out.println("env.setContextSize(" + oldSize + ");");
191     //println("env.setCurrentNode(node);");
192
}
193     
194     out.popDepth();
195     out.println("}");
196
197     _gen.popLoop();
198   }
199   
200   /*
201   public void generate(JavaWriter out)
202     throws Exception
203   {
204     Sort []sort = new Sort[_sorts.size()];
205
206     for (int i = 0; i < _sorts.size(); i++)
207       sort[i] = _sorts.get(i).generateSort();
208     
209     out.println("{");
210     out.pushDepth();
211
212     AbstractPattern select = parseSelect(_select);
213     
214     boolean hasExprEnv = ! allowJavaSelect(select);
215
216     int id = _gen.generateId();
217     
218     String sel = "_xsl_sel" + id;
219     String oldCxt = "_xsl_cxt" + id;
220     String oldCur = "_xsl_cur" + id;
221     String oldSel = "_xsl_old_sel" + id;
222     String oldEnv = "_xsl_env" + id;
223     
224     out.println("env.setCurrentNode(node);");
225     String pos = "_xsl_pos" + _gen.generateId();
226     String list = "_xsl_list" + _gen.generateId();
227
228     int sortIndex = _gen.addSort(sort);
229     
230     println("Node " + oldCxt + " = env.getContextNode();");
231     println("Node " + oldCur + " = env.getCurrentNode();");
232     
233     out.println("ArrayList " + list +
234         " = xslSort(node, env" +
235         ", _select_patterns[" + _gen.addSelect(select) + "]" +
236         ", _xsl_sorts[" + sortIndex + "]);");
237     out.println("env.setContextSize(" + list + ".size());");
238     out.println("for (int " + pos + " = 1; " + pos +
239         " <= " + list + ".size(); " + pos + "++) {");
240     _gen.pushLoop();
241     out.pushDepth();
242     out.println("Node " + _gen.getElement() + " = node;");
243     out.println("node = (Node) " + list + ".get(" + pos + " - 1);");
244
245     String oldPos = _gen.getCurrentPosition();
246     _gen.setCurrentPosition(pos);
247     
248     out.println("env.setPosition(" + pos + ");");
249     
250     AbstractPattern oldNodeListContext = _gen.getNodeListContext();
251     _gen.setNodeListContext(parseMatch(_select));
252
253     generateChildren(out);
254
255     _gen.setCurrentPosition(oldPos);
256
257     _gen.setNodeListContext(oldNodeListContext);
258     
259     out.println("node = " + _gen.getElement() + ";");
260     
261     out.popDepth();
262     out.println("}");
263     _gen.popLoop();
264     out.popDepth();
265     out.println("}");
266   }
267   */

268
269   public void printForEachSort(JavaWriter out)
270     throws Exception JavaDoc
271   {
272     Sort []sort = new Sort[_sorts.size()];
273
274     for (int i = 0; i < _sorts.size(); i++)
275       sort[i] = _sorts.get(i).generateSort();
276     
277     out.println("{");
278     out.pushDepth();
279
280     AbstractPattern select = parseSelect(_select);
281     
282     boolean hasExprEnv = ! allowJavaSelect(select);
283
284     int id = _gen.generateId();
285     
286     String JavaDoc sel = "_xsl_sel" + id;
287     String JavaDoc oldCxt = "_xsl_cxt" + id;
288     String JavaDoc oldCur = "_xsl_cur" + id;
289     String JavaDoc oldSel = "_xsl_old_sel" + id;
290     String JavaDoc oldEnv = "_xsl_env" + id;
291     
292     out.println("env.setCurrentNode(node);");
293     String JavaDoc pos = "_xsl_pos" + _gen.generateId();
294     String JavaDoc list = "_xsl_list" + _gen.generateId();
295
296     int sortIndex = _gen.addSort(sort);
297     
298     out.println("Node " + oldCxt + " = env.getContextNode();");
299     out.println("Node " + oldCur + " = env.getCurrentNode();");
300     
301     out.println("ArrayList " + list +
302         " = xslSort(node, env" +
303         ", _select_patterns[" + _gen.addSelect(select) + "]" +
304         ", _xsl_sorts[" + sortIndex + "]);");
305     out.println("env.setContextSize(" + list + ".size());");
306     out.println("for (int " + pos + " = 1; " + pos +
307         " <= " + list + ".size(); " + pos + "++) {");
308     _gen.pushLoop();
309     out.pushDepth();
310     out.println("Node " + _gen.getElement() + " = node;");
311     out.println("node = (Node) " + list + ".get(" + pos + " - 1);");
312
313     String JavaDoc oldPos = _gen.getCurrentPosition();
314     _gen.setCurrentPosition(pos);
315     
316     out.println("env.setPosition(" + pos + ");");
317     
318     AbstractPattern oldNodeListContext = _gen.getNodeListContext();
319     _gen.setNodeListContext(parseMatch(_select));
320
321     generateChildren(out);
322
323     _gen.setCurrentPosition(oldPos);
324
325     _gen.setNodeListContext(oldNodeListContext);
326     
327     out.println("node = " + _gen.getElement() + ";");
328     
329     out.popDepth();
330     out.println("}");
331     _gen.popLoop();
332     out.popDepth();
333     out.println("}");
334   }
335 }
336
Popular Tags