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.xsl.XslParseException;
034:
035: /**
036: * Represents an xsl:param node from the stylesheet.
037: */
038: public class XslParam extends XslNode implements XslTopNode {
039: private String _name;
040: private String _select;
041: private String _as;
042: private String _require;
043:
044: private boolean _isGlobal;
045:
046: /**
047: * returns the tag name.
048: */
049: public String getTagName() {
050: return "xsl:param";
051: }
052:
053: /**
054: * Returns the param name.
055: */
056: public String getName() {
057: return _name;
058: }
059:
060: /**
061: * Sets the parent.
062: */
063: public void setParent(XslNode parent) {
064: super .setParent(parent);
065:
066: if (parent instanceof XslStylesheet)
067: _isGlobal = true;
068: }
069:
070: /**
071: * Set true for global param.
072: */
073: public void setGlobal(boolean isGlobal) {
074: _isGlobal = true;
075: }
076:
077: /**
078: * Adds an attribute.
079: */
080: public void addAttribute(QName name, String value)
081: throws XslParseException {
082: if (name.getName().equals("name"))
083: _name = value;
084: else if (name.getName().equals("select"))
085: _select = value;
086: else if (name.getName().equals("as"))
087: _as = value;
088: else if (name.getName().equals("require"))
089: _require = value;
090: else
091: super .addAttribute(name, value);
092: }
093:
094: /**
095: * Ends the attributes.
096: */
097: public void endAttributes() throws XslParseException {
098: if (_name == null)
099: throw error(L.l("xsl:param needs a 'name' attribute."));
100:
101: if (_isGlobal)
102: _gen.addGlobalParameter(_name);
103: }
104:
105: /**
106: * Generates the code for the tag
107: *
108: * @param out the output writer for the generated java.
109: */
110: public void generate(JavaWriter out) throws Exception {
111: /*
112: print("_xsl_arg" + _callDepth + ".addVar(\"" + name + "\", ");
113: generateString(value, '+', elt);
114: println(");");
115: */
116:
117: if (_isGlobal) {
118: out.println("if (out.getParameter(\"" + _name
119: + "\") != null)");
120: out.println(" env.setGlobal(\"" + _name
121: + "\", out.getParameter(\"" + _name + "\"));");
122: out.println("else {");
123: out.pushDepth();
124: }
125:
126: if (_select != null) {
127: if (_isGlobal) {
128: out.print("env.setGlobal(\"" + _name + "\", ");
129: out.println("_exprs[" + addExpr(_select)
130: + "].evalObject(node, env));");
131: } else {
132: out.print("_exprs[" + addExpr(_select) + "]");
133: out.println(".addParam(env, \"" + _name + "\", "
134: + "node, env);");
135: }
136: } else if (hasChildren()) {
137: out.println("if (env.getVar(\"" + _name + "\") == null) {");
138: out.pushDepth();
139:
140: String id = "frag" + _gen.generateId();
141:
142: out.println("XMLWriter " + id + " = out.pushFragment();");
143:
144: generateChildren(out);
145:
146: if (_isGlobal)
147: out.print("env.setGlobal(\"");
148: else
149: out.print("env.addVar(\"");
150:
151: out.printJavaString(_name);
152: out.println("\", out.popFragment(" + id + "));");
153:
154: out.popDepth();
155: out.println("}");
156: }
157:
158: if (_isGlobal) {
159: out.popDepth();
160: out.println("}");
161: }
162: }
163: }
|