KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > java > JstlFmtBundle


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp.java;
31
32 import com.caucho.jsp.JspParseException;
33 import com.caucho.vfs.WriteStream;
34 import com.caucho.xml.QName;
35
36 import java.io.IOException JavaDoc;
37
38 /**
39  * Generates code for the fmt:setBundle bundle.
40  *
41  * <p>Set bundle looks up the correct localization context based on
42  * a <code>basename</code> and the current <code>pageContext</code>.
43  */

44 public class JstlFmtBundle extends JstlNode {
45   private static final QName BASENAME = new QName("basename");
46   private static final QName VAR = new QName("var");
47   private static final QName SCOPE = new QName("scope");
48   private static final QName PREFIX = new QName("prefix");
49   
50   private String JavaDoc _basename;
51   private JspAttribute _basenameAttr;
52   
53   private String JavaDoc _var;
54   private String JavaDoc _scope;
55
56   private String JavaDoc _prefix;
57   private JspAttribute _prefixAttr;
58   
59   /**
60    * Adds an attribute.
61    */

62   public void addAttribute(QName name, String JavaDoc value)
63     throws JspParseException
64   {
65     if (BASENAME.equals(name))
66       _basename = value;
67     else if (VAR.equals(name))
68       _var = value;
69     else if (SCOPE.equals(name))
70       _scope = value;
71     else if (PREFIX.equals(name))
72       _prefix = value;
73     else
74       throw error(L.l("`{0}' is an unknown attribute for <{1}>.",
75                       name.getName(), getTagName()));
76   }
77
78   /**
79    * Returns true if the tag has scripting values.
80    */

81   public boolean hasScripting()
82   {
83     return (super.hasScripting() ||
84         hasScripting(_prefix) || hasScripting(_prefixAttr));
85   }
86
87   /**
88    * Returns the prefix.
89    */

90   public String JavaDoc getPrefixCode()
91   {
92     if (_prefix != null)
93       return "\"" + _prefix + "\" + ";
94     else
95       return "";
96   }
97
98   /**
99    * Generates the XML text representation for the tag validation.
100    *
101    * @param os write stream to the generated XML.
102    */

103   public void printXml(WriteStream os)
104     throws IOException JavaDoc
105   {
106     os.print("<fmt:bundle");
107
108     if (_basename != null)
109       os.print(" basename=\"" + xmlText(_basename) + "\"");
110     
111     if (_var != null)
112       os.print(" var=\"" + xmlText(_var) + "\"");
113     
114     if (_scope != null)
115       os.print(" scope=\"" + xmlText(_scope) + "\"");
116     
117     if (_prefix != null)
118       os.print(" prefix=\"" + xmlText(_prefix) + "\"");
119
120     os.print(">");
121
122     printXmlChildren(os);
123
124     os.print("</fmt:bundle>");
125   }
126
127   /**
128    * Generates the code for the fmt:bundle tag.
129    */

130   public void generate(JspJavaWriter out)
131     throws Exception JavaDoc
132   {
133     if (_basename == null && _basenameAttr == null)
134       throw error(L.l("required attribute `basename' missing from `{0}'",
135                       getTagName()));
136
137     String JavaDoc basenameExpr;
138
139     if (_basenameAttr != null)
140       basenameExpr = _basenameAttr.generateValue();
141     else
142       basenameExpr = generateValue(String JavaDoc.class, _basename);
143
144     String JavaDoc oldVar = "_caucho_bundle_" + _gen.uniqueId();
145     
146     out.print("Object " + oldVar + " = pageContext.putAttribute(\"caucho.bundle\", ");
147     out.println("pageContext.getBundle(" + basenameExpr + "));");
148
149     String JavaDoc oldPrefixVar = "_caucho_bundle_prefix_" + _gen.uniqueId();
150
151     if (_prefix != null)
152       out.println("Object " + oldPrefixVar + " = pageContext.putAttribute(\"caucho.bundle.prefix\", \"" + _prefix + "\");");
153     else
154       out.println("Object " + oldPrefixVar + " = pageContext.putAttribute(\"caucho.bundle.prefix\", null);");
155
156     /*
157     boolean hasPrefix = false;
158     Node node;
159     for (node = elt;
160          node instanceof Element;
161          node = node.getParentNode()) {
162       TagInfo tag = null;//_gen.getTag((Element) node);
163
164       if (tag == null)
165         continue;
166
167       String urn = tag.getTagLibrary().getReliableURN();
168       if (urn == null)
169         urn = tag.getTagLibrary().getURI();
170       
171       if ("bundle".equals(node.getLocalName()) &&
172           _gen.JSTL_FMT_URI.equals(urn) &&
173           ! ((Element) node).getAttribute("prefix").equals(""))
174         hasPrefix = true;
175     }
176
177     String oldPrefix = null;
178     
179     if (hasPrefix)
180       oldPrefix = "_caucho_bundle_prefix_" + _gen.uniqueId();
181     
182     String prefix = elt.getAttribute("prefix");
183     if (hasPrefix && prefix.equals("")) {
184       out.println("String " + oldPrefix + " = _caucho_bundle_prefix;");
185       out.println("_caucho_bundle_prefix = \"\";");
186     }
187     else if (hasPrefix) {
188       int bundleIndex = _gen.addExpr(prefix);
189       
190       out.println("String " + oldPrefix + " = _caucho_bundle_prefix;");
191       out.println("_caucho_bundle_prefix = _caucho_expr_" + bundleIndex + ".evalString(_jsp_env);");
192     }
193     */

194
195     out.println("try {");
196     out.pushDepth();
197
198     generateChildren(out);
199
200     out.popDepth();
201     out.println("} finally {");
202     out.println(" pageContext.setAttribute(\"caucho.bundle\", " + oldVar + ");");
203     out.println(" pageContext.setAttribute(\"caucho.bundle.prefix\", " + oldPrefixVar + ");");
204     /*
205     if (hasPrefix) {
206       out.println(" _caucho_bundle_prefix = " + oldPrefix + ";");
207     }
208     */

209     
210     out.println("}");
211   }
212 }
213
Popular Tags