KickJava   Java API By Example, From Geeks To Geeks.

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


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.xsl.XslParseException;
34
35 /**
36  * Represents an xsl:param node from the stylesheet.
37  */

38 public class XslParam extends XslNode implements XslTopNode {
39   private String JavaDoc _name;
40   private String JavaDoc _select;
41   private String JavaDoc _as;
42   private String JavaDoc _require;
43
44   private boolean _isGlobal;
45
46   /**
47    * returns the tag name.
48    */

49   public String JavaDoc getTagName()
50   {
51     return "xsl:param";
52   }
53   
54   /**
55    * Returns the param name.
56    */

57   public String JavaDoc getName()
58   {
59     return _name;
60   }
61
62   /**
63    * Sets the parent.
64    */

65   public void setParent(XslNode parent)
66   {
67     super.setParent(parent);
68
69     if (parent instanceof XslStylesheet)
70       _isGlobal = true;
71   }
72
73   /**
74    * Set true for global param.
75    */

76   public void setGlobal(boolean isGlobal)
77   {
78     _isGlobal = true;
79   }
80   
81   /**
82    * Adds an attribute.
83    */

84   public void addAttribute(QName name, String JavaDoc value)
85     throws XslParseException
86   {
87     if (name.getName().equals("name"))
88       _name = value;
89     else if (name.getName().equals("select"))
90       _select = value;
91     else if (name.getName().equals("as"))
92       _as = value;
93     else if (name.getName().equals("require"))
94       _require = value;
95     else
96       super.addAttribute(name, value);
97   }
98
99   /**
100    * Ends the attributes.
101    */

102   public void endAttributes()
103     throws XslParseException
104   {
105     if (_name == null)
106       throw error(L.l("xsl:param needs a 'name' attribute."));
107
108     if (_isGlobal)
109       _gen.addGlobalParameter(_name);
110   }
111
112   /**
113    * Generates the code for the tag
114    *
115    * @param out the output writer for the generated java.
116    */

117   public void generate(JavaWriter out)
118     throws Exception JavaDoc
119   {
120     /*
121     print("_xsl_arg" + _callDepth + ".addVar(\"" + name + "\", ");
122     generateString(value, '+', elt);
123     println(");");
124     */

125     
126     if (_isGlobal) {
127       out.println("if (out.getParameter(\"" + _name + "\") != null)");
128       out.println(" env.setGlobal(\"" + _name + "\", out.getParameter(\"" + _name + "\"));");
129       out.println("else {");
130       out.pushDepth();
131     }
132
133     if (_select != null) {
134       if (_isGlobal) {
135     out.print("env.setGlobal(\"" + _name + "\", ");
136     out.println("_exprs[" + addExpr(_select) + "].evalObject(node, env));");
137       }
138       else {
139     out.print("_exprs[" + addExpr(_select) + "]");
140     out.println(".addParam(env, \"" + _name + "\", " +
141             "node, env);");
142       }
143     }
144     else if (hasChildren()) {
145       out.println("if (env.getVar(\"" + _name + "\") == null) {");
146       out.pushDepth();
147       
148       String JavaDoc id = "frag" + _gen.generateId();
149       
150       out.println("XMLWriter " + id + " = out.pushFragment();");
151
152       generateChildren(out);
153
154       if (_isGlobal)
155     out.print("env.setGlobal(\"");
156       else
157     out.print("env.addVar(\"");
158       
159       out.printJavaString(_name);
160       out.println("\", out.popFragment(" + id + "));");
161       
162       out.popDepth();
163       out.println("}");
164     }
165     
166     if (_isGlobal) {
167       out.popDepth();
168       out.println("}");
169     }
170   }
171 }
172
Popular Tags