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.jsp.java;
030:
031: import com.caucho.jsp.JspParseException;
032: import com.caucho.vfs.WriteStream;
033: import com.caucho.xml.QName;
034:
035: import java.io.IOException;
036: import java.util.ArrayList;
037:
038: public class JstlFmtMessage extends JstlNode {
039: private static final QName KEY = new QName("key");
040: private static final QName BUNDLE = new QName("bundle");
041: private static final QName VAR = new QName("var");
042: private static final QName SCOPE = new QName("scope");
043:
044: private String _key;
045: private JspAttribute _keyAttr;
046:
047: private String _bundle;
048: private JspAttribute _bundleAttr;
049:
050: private String _var;
051: private String _scope;
052:
053: private ArrayList<JstlFmtParam> _params = new ArrayList<JstlFmtParam>();
054:
055: /**
056: * Adds an attribute.
057: */
058: public void addAttribute(QName name, String value)
059: throws JspParseException {
060: if (KEY.equals(name))
061: _key = value;
062: else if (BUNDLE.equals(name))
063: _bundle = value;
064: else if (VAR.equals(name))
065: _var = value;
066: else if (SCOPE.equals(name))
067: _scope = value;
068: else
069: throw error(L.l("`{0}' is an unknown attribute for <{1}>.",
070: name.getName(), getTagName()));
071: }
072:
073: /**
074: * Adds an attribute.
075: */
076: public void addAttribute(QName name, JspAttribute value)
077: throws JspParseException {
078: if (KEY.equals(name))
079: _keyAttr = value;
080: else if (BUNDLE.equals(name))
081: _bundleAttr = value;
082: else
083: throw error(L.l(
084: "`{0}' is an unsupported jsp:attribute for <{1}>.",
085: name.getName(), getTagName()));
086: }
087:
088: /**
089: * Adds a child element.
090: */
091: public void addChild(JspNode node) throws JspParseException {
092: if (node instanceof JstlFmtParam)
093: _params.add((JstlFmtParam) node);
094: else
095: super .addChild(node);
096: }
097:
098: /**
099: * 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) throws IOException {
104: os.print("<fmt:message");
105:
106: if (_key != null) {
107: os.print(" key=\"" + xmlText(_key) + "\"");
108: }
109:
110: if (_bundle != null)
111: os.print(" bundle=\"" + xmlText(_bundle) + "\"");
112:
113: if (_var != null)
114: os.print(" var=\"" + _var + "\"");
115:
116: if (_scope != null)
117: os.print(" scope=\"" + _scope + "\"");
118:
119: os.print(">");
120:
121: for (int i = 0; i < _params.size(); i++)
122: _params.get(i).printXml(os);
123:
124: printXmlChildren(os);
125:
126: os.print("</fmt:message>");
127: }
128:
129: /**
130: * Generates the code for the fmt:message tag.
131: */
132: public void generatePrologue(JspJavaWriter out) throws Exception {
133: for (int i = 0; i < _params.size(); i++) {
134: _params.get(i).generatePrologue(out);
135: }
136: }
137:
138: /**
139: * Generates the code for the fmt:message tag.
140: */
141: public void generate(JspJavaWriter out) throws Exception {
142: String keyExpr = null;
143:
144: if (_key != null) {
145: keyExpr = generateValue(String.class, _key);
146: } else if (_keyAttr != null) {
147: keyExpr = _keyAttr.generateValue();
148: } else if (isChildrenStatic()) {
149: keyExpr = '"' + escapeJavaString(getStaticText().trim()) + '"';
150: } else {
151: out.println("out = pageContext.pushBody();");
152:
153: generateChildren(out);
154:
155: keyExpr = "_caucho_var_" + _gen.uniqueId();
156:
157: out
158: .println("String "
159: + keyExpr
160: + " = ((com.caucho.jsp.BodyContentImpl) out).getTrimString();");
161:
162: out.println("out = pageContext.popBody();");
163: }
164:
165: // see if there's a fmt:bundle with a prefix
166:
167: String prefix = "";
168: JspNode node;
169:
170: // jsp/1c51, jsp/1c54
171: /*
172: for (node = getParent(); node != null; node = node.getParent()) {
173: if (node instanceof JstlFmtBundle) {
174: prefix = ((JstlFmtBundle) node).getPrefixCode();
175: break;
176: }
177: }
178: */
179:
180: String paramVar = "null";
181:
182: if (_params.size() > 0) {
183: paramVar = "_caucho_param_" + _gen.uniqueId();
184:
185: out.println("Object []" + paramVar + " = new Object["
186: + _params.size() + "];");
187:
188: for (int i = 0; i < _params.size(); i++) {
189: JstlFmtParam param = _params.get(i);
190:
191: param.generateSet(out, paramVar + "[" + i + "]");
192:
193: /*
194: String value = param.getAttribute("value");
195:
196: if (! value.equals("")) {
197: int paramIndex = _gen.addExpr(value);
198:
199: out.println(paramVar + "[" + i + "] = _caucho_expr_" + paramIndex + ".evalObject(pageContext);");
200: }
201: else {
202: out.println("out = pageContext.pushBody();");
203:
204: _gen.generateChildren(param.getFirstChild());
205:
206: out.println(paramVar + "[" + i + "] = ((com.caucho.jsp.BodyContentImpl) out).getTrimString();");
207:
208: out.println("out = pageContext.popBody();");
209: }
210: */
211: }
212: }
213:
214: if (_var != null) {
215: String value;
216:
217: if (_bundleAttr != null) {
218: value = ("pageContext.getLocalizedMessage("
219: + _bundleAttr.generateValue() + ", " + prefix
220: + keyExpr + ", " + paramVar + ", null)");
221: } else if (_bundle != null) {
222: String bundleExpr = generateValue(Object.class, _bundle);
223:
224: value = ("pageContext.getLocalizedMessage("
225: + bundleExpr + ", " + prefix + keyExpr + ", "
226: + paramVar + ", null)");
227: } else
228: value = ("pageContext.getLocalizedMessage(null, "
229: + prefix + keyExpr + ", " + paramVar + ", null)");
230:
231: generateSetOrRemove(out, _var, _scope, value);
232: } else {
233: if (_bundleAttr != null) {
234: String bundleExpr = _bundleAttr.generateValue();
235:
236: out
237: .println("out.print(pageContext.getLocalizedMessage("
238: + bundleExpr
239: + ", "
240: + prefix
241: + keyExpr
242: + ", " + paramVar + ", null));");
243:
244: } else if (_bundle != null) {
245: String bundleExpr = generateValue(Object.class, _bundle);
246:
247: out
248: .println("out.print(pageContext.getLocalizedMessage("
249: + bundleExpr
250: + ", "
251: + prefix
252: + keyExpr
253: + ", " + paramVar + ", null));");
254:
255: } else
256: out
257: .println("out.print(pageContext.getLocalizedMessage(null, "
258: + prefix
259: + keyExpr
260: + ", "
261: + paramVar
262: + ", null));");
263: }
264: }
265: }
|