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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp.java;
031:
032: import com.caucho.jsp.JspParseException;
033: import com.caucho.vfs.WriteStream;
034: import com.caucho.xml.QName;
035:
036: import java.io.IOException;
037: import javax.servlet.jsp.jstl.core.*;
038:
039: /**
040: * Generates code for the fmt:setBundle bundle.
041: *
042: * <p>Set bundle looks up the correct localization context based on
043: * a <code>basename</code> and the current <code>pageContext</code>.
044: */
045: public class JstlFmtBundle extends JstlNode {
046: private static final QName BASENAME = new QName("basename");
047: private static final QName VAR = new QName("var");
048: private static final QName SCOPE = new QName("scope");
049: private static final QName PREFIX = new QName("prefix");
050:
051: private String _basename;
052: private JspAttribute _basenameAttr;
053:
054: private String _var;
055: private String _scope;
056:
057: private String _prefix;
058: private JspAttribute _prefixAttr;
059:
060: /**
061: * Adds an attribute.
062: */
063: public void addAttribute(QName name, String value)
064: throws JspParseException {
065: if (BASENAME.equals(name))
066: _basename = value;
067: else if (VAR.equals(name))
068: _var = value;
069: else if (SCOPE.equals(name))
070: _scope = value;
071: else if (PREFIX.equals(name))
072: _prefix = value;
073: else
074: throw error(L.l("`{0}' is an unknown attribute for <{1}>.",
075: name.getName(), getTagName()));
076: }
077:
078: /**
079: * Returns true if the tag has scripting values.
080: */
081: public boolean hasScripting() {
082: return (super .hasScripting() || hasScripting(_prefix) || hasScripting(_prefixAttr));
083: }
084:
085: /**
086: * Returns the prefix.
087: */
088: public String getPrefixCode() {
089: if (_prefix != null)
090: return "\"" + _prefix + "\" + ";
091: else
092: return "";
093: }
094:
095: /**
096: * Generates the XML text representation for the tag validation.
097: *
098: * @param os write stream to the generated XML.
099: */
100: public void printXml(WriteStream os) throws IOException {
101: os.print("<fmt:bundle");
102:
103: if (_basename != null)
104: os.print(" basename=\"" + xmlText(_basename) + "\"");
105:
106: if (_var != null)
107: os.print(" var=\"" + xmlText(_var) + "\"");
108:
109: if (_scope != null)
110: os.print(" scope=\"" + xmlText(_scope) + "\"");
111:
112: if (_prefix != null)
113: os.print(" prefix=\"" + xmlText(_prefix) + "\"");
114:
115: os.print(">");
116:
117: printXmlChildren(os);
118:
119: os.print("</fmt:bundle>");
120: }
121:
122: /**
123: * Generates the code for the fmt:bundle tag.
124: */
125: public void generate(JspJavaWriter out) throws Exception {
126: if (_basename == null && _basenameAttr == null)
127: throw error(L.l(
128: "required attribute `basename' missing from `{0}'",
129: getTagName()));
130:
131: String basenameExpr;
132:
133: if (_basenameAttr != null)
134: basenameExpr = _basenameAttr.generateValue();
135: else
136: basenameExpr = generateValue(String.class, _basename);
137:
138: String oldVar = "_caucho_bundle_" + _gen.uniqueId();
139:
140: out.print("Object " + oldVar
141: + " = pageContext.putAttribute(\"caucho.bundle\", ");
142: out.println("pageContext.getBundle(" + basenameExpr + "));");
143:
144: String var = _var;
145: if (var == null)
146: var = Config.FMT_LOCALIZATION_CONTEXT;
147:
148: /*
149: if (var != null) {
150: out.print("pageContext.putAttribute(\"");
151: out.printJavaString(var);
152: out.println("\", pageContext.getAttribute(\"caucho.bundle\"));");
153: }
154: */
155:
156: String oldPrefixVar = "_caucho_bundle_prefix_"
157: + _gen.uniqueId();
158:
159: if (_prefix != null) {
160: out
161: .print("Object "
162: + oldPrefixVar
163: + " = pageContext.putAttribute(\"caucho.bundle.prefix\", ");
164: out.print(generateValue(String.class, _prefix));
165: out.println(");");
166: } else
167: out
168: .println("Object "
169: + oldPrefixVar
170: + " = pageContext.putAttribute(\"caucho.bundle.prefix\", null);");
171:
172: out.println("try {");
173: out.pushDepth();
174:
175: generateChildren(out);
176:
177: out.popDepth();
178: out.println("} finally {");
179: out.println(" pageContext.setAttribute(\"caucho.bundle\", "
180: + oldVar + ");");
181: out
182: .println(" pageContext.setAttribute(\"caucho.bundle.prefix\", "
183: + oldPrefixVar + ");");
184: /*
185: if (hasPrefix) {
186: out.println(" _caucho_bundle_prefix = " + oldPrefix + ";");
187: }
188: */
189:
190: out.println("}");
191: }
192: }
|