001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xsl.java;
030:
031: import com.caucho.java.JavaWriter;
032: import com.caucho.xml.QName;
033: import com.caucho.xpath.pattern.AbstractPattern;
034: import com.caucho.xsl.Sort;
035: import com.caucho.xsl.XslParseException;
036:
037: import java.util.ArrayList;
038:
039: /**
040: * Represents any XSL node from the stylesheet.
041: */
042: public class XslApplyTemplates extends XslNode {
043: private String _select;
044: private String _mode;
045:
046: private ArrayList<XslSort> _sorts = new ArrayList<XslSort>();
047:
048: /**
049: * Returns the tag name.
050: */
051: public String getTagName() {
052: return "xsl:apply-templates";
053: }
054:
055: /**
056: * Adds an attribute.
057: */
058: public void addAttribute(QName name, String value)
059: throws XslParseException {
060: if (name.getName().equals("select")) {
061: _select = value;
062: } else if (name.getName().equals("mode")) {
063: _mode = value;
064: } else
065: super .addAttribute(name, value);
066: }
067:
068: /**
069: * Ends the attributes.
070: */
071: public void endAttributes() throws XslParseException {
072: }
073:
074: /**
075: * Adds a child node.
076: */
077: public void addChild(XslNode node) throws XslParseException {
078: if (node instanceof XslSort) {
079: _sorts.add((XslSort) node);
080: } else
081: super .addChild(node);
082: }
083:
084: /**
085: * Generates the code for the tag
086: *
087: * @param out the output writer for the generated java.
088: */
089: public void generate(JavaWriter out) throws Exception {
090: AbstractPattern selectPattern = null;
091:
092: if (_select != null) {
093: selectPattern = parseSelect(_select);
094: }
095:
096: Sort[] sort = null;
097:
098: if (_sorts.size() > 0) {
099: sort = new Sort[_sorts.size()];
100:
101: for (int i = 0; i < _sorts.size(); i++)
102: sort[i] = _sorts.get(i).generateSort();
103: }
104:
105: if (sort != null && selectPattern == null) {
106: // selectPattern = parseSelect("*", node);
107: selectPattern = parseSelect("*");
108: }
109:
110: pushCall(out);
111:
112: generateChildren(out);
113:
114: printApplyTemplates(out, selectPattern, _mode, sort);
115:
116: popCall(out);
117: }
118:
119: /**
120: * Prints code for xsl:apply-templates
121: *
122: * @param select the select pattern
123: * @param mode the template mode
124: * @param sort the sort expressions
125: */
126: private void printApplyTemplates(JavaWriter out,
127: AbstractPattern select, String mode, Sort[] sort)
128: throws Exception {
129: int min = 0;
130: int max = Integer.MAX_VALUE;
131:
132: String applyName = "applyNode" + _gen.getModeName(mode);
133: String env = "_xsl_arg" + _gen.getCallDepth();
134:
135: if (select == null && sort == null) {
136: out.println("for (Node _xsl_node = node.getFirstChild();");
137: out.println(" _xsl_node != null;");
138: out
139: .println(" _xsl_node = _xsl_node.getNextSibling()) {");
140: out.println(" " + env + ".setSelect(node, null);");
141: out.println(" " + env + ".setCurrentNode(_xsl_node);");
142: out.println(" " + applyName + "(out, _xsl_node, " + env
143: + ", " + min + ", " + max + ");");
144: out.println("}");
145: } else if (sort == null) {
146: int oldSelectDepth = _gen.getSelectDepth();
147:
148: String name = printSelectBegin(out, select, false, null);
149:
150: out.println(env + ".setSelect(node, _select_patterns["
151: + _gen.addSelect(select) + "]);");
152: out.println(env + ".setCurrentNode(" + name + ");");
153:
154: out.println(applyName + "(out, " + name + ", " + env + ", "
155: + min + ", " + max + ");");
156:
157: for (; _gen.getSelectDepth() > oldSelectDepth; _gen
158: .popSelectDepth()) {
159: out.popDepth();
160: out.println("}");
161: }
162: } else {
163: int sortIndex = _gen.addSort(sort);
164:
165: out.println("{");
166: out.pushDepth();
167: out.println("ArrayList _xsl_list = xslSort(node, env"
168: + ", _select_patterns[" + _gen.addSelect(select)
169: + "]" + ", _xsl_sorts[" + sortIndex + "]);");
170: out.println(env + ".setContextSize(_xsl_list.size());");
171: out
172: .println("for (int _xsl_i = 0; _xsl_i < _xsl_list.size(); _xsl_i++) {");
173: out
174: .println(" " + env
175: + ".setContextPosition(_xsl_i + 1);");
176: out.println(" " + applyName
177: + "(out, (Node) _xsl_list.get(_xsl_i)" + ", " + env
178: + ", " + min + ", " + max + ");");
179: out.println("}");
180: out.popDepth();
181: out.println("}");
182: }
183: }
184:
185: protected void popScope(JavaWriter out) throws Exception {
186: }
187: }
|