KickJava   Java API By Example, From Geeks To Geeks.

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


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 any XSL node from the stylesheet.
41  */

42 public class XslApplyTemplates extends XslNode {
43   private String JavaDoc _select;
44   private String JavaDoc _mode;
45
46   private ArrayList JavaDoc<XslSort> _sorts = new ArrayList JavaDoc<XslSort>();
47
48   /**
49    * Returns the tag name.
50    */

51   public String JavaDoc getTagName()
52   {
53     return "xsl:apply-templates";
54   }
55
56   /**
57    * Adds an attribute.
58    */

59   public void addAttribute(QName name, String JavaDoc value)
60     throws XslParseException
61   {
62     if (name.getName().equals("select")) {
63       _select = value;
64     }
65     else if (name.getName().equals("mode")) {
66       _mode = value;
67     }
68     else
69       super.addAttribute(name, value);
70   }
71
72   /**
73    * Ends the attributes.
74    */

75   public void endAttributes()
76     throws XslParseException
77   {
78   }
79
80   /**
81    * Adds a child node.
82    */

83   public void addChild(XslNode node)
84     throws XslParseException
85   {
86     if (node instanceof XslSort) {
87       _sorts.add((XslSort) node);
88     }
89     else
90       super.addChild(node);
91   }
92
93   /**
94    * Generates the code for the tag
95    *
96    * @param out the output writer for the generated java.
97    */

98   public void generate(JavaWriter out)
99     throws Exception JavaDoc
100   {
101     AbstractPattern selectPattern = null;
102     
103     if (_select != null) {
104       selectPattern = parseSelect(_select);
105     }
106
107     Sort []sort = null;
108
109     if (_sorts.size() > 0) {
110       sort = new Sort[_sorts.size()];
111
112       for (int i = 0; i < _sorts.size(); i++)
113     sort[i] = _sorts.get(i).generateSort();
114     }
115     
116
117     if (sort != null && selectPattern == null) {
118       // selectPattern = parseSelect("*", node);
119
selectPattern = parseSelect("*");
120     }
121
122     pushCall(out);
123     
124     generateChildren(out);
125     
126     printApplyTemplates(out, selectPattern, _mode, sort);
127     
128     popCall(out);
129   }
130
131   /**
132    * Prints code for xsl:apply-templates
133    *
134    * @param select the select pattern
135    * @param mode the template mode
136    * @param sort the sort expressions
137    */

138   private void printApplyTemplates(JavaWriter out,
139                    AbstractPattern select,
140                    String JavaDoc mode,
141                    Sort []sort)
142     throws Exception JavaDoc
143   {
144     int min = 0;
145     int max = Integer.MAX_VALUE;
146
147     String JavaDoc applyName = "applyNode" + _gen.getModeName(mode);
148     String JavaDoc env = "_xsl_arg" + _gen.getCallDepth();
149
150     if (select == null && sort == null) {
151       out.println("for (Node _xsl_node = node.getFirstChild();");
152       out.println(" _xsl_node != null;");
153       out.println(" _xsl_node = _xsl_node.getNextSibling()) {");
154       out.println(" " + env + ".setSelect(node, null);");
155       out.println(" " + env + ".setCurrentNode(_xsl_node);");
156       out.println(" " + applyName + "(out, _xsl_node, " + env + ", " +
157           min + ", " + max + ");");
158       out.println("}");
159     }
160     else if (sort == null) {
161       int oldSelectDepth = _gen.getSelectDepth();
162
163       String JavaDoc name = printSelectBegin(out, select, false, null);
164
165       out.println(env + ".setSelect(node, _select_patterns[" +
166           _gen.addSelect(select) + "]);");
167       out.println(env + ".setCurrentNode(" + name + ");");
168
169       out.println(applyName + "(out, " + name + ", " + env + ", " +
170           min + ", " + max + ");");
171
172       for (; _gen.getSelectDepth() > oldSelectDepth; _gen.popSelectDepth()) {
173         out.popDepth();
174         out.println("}");
175       }
176     }
177     else {
178       int sortIndex = _gen.addSort(sort);
179       
180       out.println("{");
181       out.pushDepth();
182       out.println("ArrayList _xsl_list = xslSort(node, env" +
183           ", _select_patterns[" + _gen.addSelect(select) + "]" +
184           ", _xsl_sorts[" + sortIndex + "]);");
185       out.println(env + ".setContextSize(_xsl_list.size());");
186       out.println("for (int _xsl_i = 0; _xsl_i < _xsl_list.size(); _xsl_i++) {");
187       out.println(" " + env + ".setContextPosition(_xsl_i + 1);");
188       out.println(" " + applyName + "(out, (Node) _xsl_list.get(_xsl_i)" +
189           ", " + env + ", " + min + ", " + max + ");");
190       out.println("}");
191       out.popDepth();
192       out.println("}");
193     }
194   }
195
196   protected void popScope(JavaWriter out)
197     throws Exception JavaDoc
198   {
199   }
200 }
201
Popular Tags