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:
038: /**
039: * Generates code for the fmt:setBundle tag.
040: *
041: * <p>Set bundle looks up the correct localization context based on
042: * a <code>basename</code> and the current <code>pageContext</code>.
043: */
044: public class JstlFmtSetBundle extends JstlNode {
045: private static final QName BASENAME = new QName("basename");
046: private static final QName VAR = new QName("var");
047: private static final QName SCOPE = new QName("scope");
048:
049: private String _basename;
050: private JspAttribute _basenameAttr;
051:
052: private String _var = "javax.servlet.jsp.jstl.fmt.localizationContext";
053: private String _scope;
054:
055: /**
056: * Adds an attribute.
057: */
058: public void addAttribute(QName name, String value)
059: throws JspParseException {
060: if (BASENAME.equals(name))
061: _basename = value;
062: else if (VAR.equals(name))
063: _var = value;
064: else if (SCOPE.equals(name))
065: _scope = value;
066: else
067: throw error(L.l("'{0}' is an unknown attribute for <{1}>.",
068: name.getName(), getTagName()));
069: }
070:
071: /**
072: * Adds an attribute.
073: */
074: public void addAttribute(QName name, JspAttribute value)
075: throws JspParseException {
076: if (BASENAME.equals(name))
077: _basenameAttr = value;
078: else
079: throw error(L.l(
080: "'{0}' is an unsupported jsp:attribute for <{1}>.",
081: name.getName(), getTagName()));
082: }
083:
084: /**
085: * Generates the XML text representation for the tag validation.
086: *
087: * @param os write stream to the generated XML.
088: */
089: public void printXml(WriteStream os) throws IOException {
090: os.print("<fmt:bundle");
091:
092: if (_basename != null)
093: os.print(" basename=\"" + xmlText(_basename) + "\"");
094:
095: if (_var != null)
096: os.print(" var=\"" + xmlText(_var) + "\"");
097:
098: if (_scope != null)
099: os.print(" scope=\"" + xmlText(_scope) + "\"");
100:
101: os.print(">");
102:
103: printXmlChildren(os);
104:
105: os.print("</fmt:bundle>");
106: }
107:
108: /**
109: * Generates the code for the c:out tag.
110: */
111: public void generate(JspJavaWriter out) throws Exception {
112: if (_basename == null && _basenameAttr == null)
113: throw error(L.l(
114: "required attribute 'basename' missing from '{0}'",
115: getTagName()));
116:
117: String basenameExpr;
118:
119: if (_basenameAttr != null)
120: basenameExpr = _basenameAttr.generateValue();
121: else
122: basenameExpr = generateValue(String.class, _basename);
123:
124: String value = "pageContext.getBundle(" + basenameExpr + ")";
125:
126: generateSetNotNull(out, _var, _scope, value);
127: }
128: }
|