KickJava   Java API By Example, From Geeks To Geeks.

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


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.jsp.java;
30
31 import com.caucho.jsp.JspParseException;
32 import com.caucho.vfs.WriteStream;
33 import com.caucho.xml.QName;
34
35 import java.io.IOException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 public class JstlFmtMessage extends JstlNode {
39   private static final QName KEY = new QName("key");
40   private static final QName BUNDLE = new QName("bundle");
41   private static final QName VAR = new QName("var");
42   private static final QName SCOPE = new QName("scope");
43   
44   private String JavaDoc _key;
45   private JspAttribute _keyAttr;
46   
47   private String JavaDoc _bundle;
48   private JspAttribute _bundleAttr;
49   
50   private String JavaDoc _var;
51   private String JavaDoc _scope;
52
53   private ArrayList JavaDoc<JstlFmtParam> _params = new ArrayList JavaDoc<JstlFmtParam>();
54   
55   /**
56    * Adds an attribute.
57    */

58   public void addAttribute(QName name, String JavaDoc value)
59     throws JspParseException
60   {
61     if (KEY.equals(name))
62       _key = value;
63     else if (BUNDLE.equals(name))
64       _bundle = value;
65     else if (VAR.equals(name))
66       _var = value;
67     else if (SCOPE.equals(name))
68       _scope = value;
69     else
70       throw error(L.l("`{0}' is an unknown attribute for <{1}>.",
71                       name.getName(), getTagName()));
72   }
73   
74   /**
75    * Adds an attribute.
76    */

77   public void addAttribute(QName name, JspAttribute value)
78     throws JspParseException
79   {
80     if (KEY.equals(name))
81       _keyAttr = value;
82     else if (BUNDLE.equals(name))
83       _bundleAttr = value;
84     else
85       throw error(L.l("`{0}' is an unsupported jsp:attribute for <{1}>.",
86                       name.getName(), getTagName()));
87   }
88
89   /**
90    * Adds a child element.
91    */

92   public void addChild(JspNode node)
93     throws JspParseException
94   {
95     if (node instanceof JstlFmtParam)
96       _params.add((JstlFmtParam) node);
97     else
98       super.addChild(node);
99   }
100
101   /**
102    * Generates the XML text representation for the tag validation.
103    *
104    * @param os write stream to the generated XML.
105    */

106   public void printXml(WriteStream os)
107     throws IOException JavaDoc
108   {
109     os.print("<fmt:message");
110
111     if (_key != null) {
112       os.print(" key=\"" + xmlText(_key) + "\"");
113     }
114
115     if (_bundle != null)
116       os.print(" bundle=\"" + xmlText(_bundle) + "\"");
117     
118     if (_var != null)
119       os.print(" var=\"" + _var + "\"");
120     
121     if (_scope != null)
122       os.print(" scope=\"" + _scope + "\"");
123
124     os.print(">");
125
126     for (int i = 0; i < _params.size(); i++)
127       _params.get(i).printXml(os);
128
129     printXmlChildren(os);
130
131     os.print("</fmt:message>");
132   }
133
134   /**
135    * Generates the code for the fmt:message tag.
136    */

137   public void generatePrologue(JspJavaWriter out)
138     throws Exception JavaDoc
139   {
140     for (int i = 0; i < _params.size(); i++) {
141       _params.get(i).generatePrologue(out);
142     }
143   }
144
145   /**
146    * Generates the code for the fmt:message tag.
147    */

148   public void generate(JspJavaWriter out)
149     throws Exception JavaDoc
150   {
151     String JavaDoc keyExpr = null;
152
153     if (_key != null) {
154       keyExpr = generateValue(String JavaDoc.class, _key);
155     }
156     else if (_keyAttr != null) {
157       keyExpr = _keyAttr.generateValue();
158     }
159     else if (isChildrenStatic()) {
160       keyExpr = '"' + escapeJavaString(getStaticText().trim()) + '"';
161     }
162     else {
163       out.println("out = pageContext.pushBody();");
164
165       generateChildren(out);
166
167       keyExpr = "_caucho_var_" + _gen.uniqueId();
168
169       out.println("String " + keyExpr + " = ((com.caucho.jsp.BodyContentImpl) out).getTrimString();");
170
171       out.println("out = pageContext.popBody();");
172     }
173
174     // see if there's a fmt:bundle with a prefix
175

176     String JavaDoc prefix = "";
177     JspNode node;
178
179     // jsp/1c51, jsp/1c54
180
/*
181     for (node = getParent(); node != null; node = node.getParent()) {
182       if (node instanceof JstlFmtBundle) {
183         prefix = ((JstlFmtBundle) node).getPrefixCode();
184     break;
185       }
186     }
187     */

188
189     String JavaDoc paramVar = "null";
190
191     if (_params.size() > 0) {
192       paramVar = "_caucho_param_" + _gen.uniqueId();
193
194       out.println("Object []" + paramVar + " = new Object[" + _params.size() + "];");
195
196       for (int i = 0; i < _params.size(); i++) {
197         JstlFmtParam param = _params.get(i);
198
199         param.generateSet(out, paramVar + "[" + i + "]");
200
201         /*
202         String value = param.getAttribute("value");
203
204         if (! value.equals("")) {
205           int paramIndex = _gen.addExpr(value);
206
207           out.println(paramVar + "[" + i + "] = _caucho_expr_" + paramIndex + ".evalObject(pageContext);");
208         }
209         else {
210           out.println("out = pageContext.pushBody();");
211
212           _gen.generateChildren(param.getFirstChild());
213
214           out.println(paramVar + "[" + i + "] = ((com.caucho.jsp.BodyContentImpl) out).getTrimString();");
215
216           out.println("out = pageContext.popBody();");
217         }
218         */

219       }
220     }
221
222     if (_var != null) {
223       String JavaDoc value;
224
225       if (_bundleAttr != null) {
226         value = ("pageContext.getLocalizedMessage(" +
227          _bundleAttr.generateValue() + ", " +
228                  prefix + keyExpr + ", " + paramVar + ", null)");
229       }
230       else if (_bundle != null) {
231         String JavaDoc bundleExpr = generateValue(Object JavaDoc.class, _bundle);
232       
233         value = ("pageContext.getLocalizedMessage(" + bundleExpr + ", " +
234                  prefix + keyExpr + ", " + paramVar + ", null)");
235       }
236       else
237         value = ("pageContext.getLocalizedMessage(null, " + prefix + keyExpr + ", " +
238                  paramVar + ", null)");
239   
240       generateSetOrRemove(out, _var, _scope, value);
241     }
242     else {
243       if (_bundleAttr != null) {
244         String JavaDoc bundleExpr = _bundleAttr.generateValue();
245       
246         out.println("out.print(pageContext.getLocalizedMessage(" + bundleExpr + ", " + prefix + keyExpr + ", " + paramVar + ", null));");
247
248       }
249       else if (_bundle != null) {
250         String JavaDoc bundleExpr = generateValue(Object JavaDoc.class, _bundle);
251       
252         out.println("out.print(pageContext.getLocalizedMessage(" + bundleExpr + ", " + prefix + keyExpr + ", " + paramVar + ", null));");
253
254       }
255       else
256         out.println("out.print(pageContext.getLocalizedMessage(null, " + prefix + keyExpr + ", " + paramVar + ", null));");
257     }
258   }
259 }
260
Popular Tags