KickJava   Java API By Example, From Geeks To Geeks.

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


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 tag.
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 JstlFmtSetBundle 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   
49   private String JavaDoc _basename;
50   private JspAttribute _basenameAttr;
51   
52   private String JavaDoc _var = "javax.servlet.jsp.jstl.fmt.localizationContext";
53   private String JavaDoc _scope;
54   
55   /**
56    * Adds an attribute.
57    */

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

75   public void addAttribute(QName name, JspAttribute value)
76     throws JspParseException
77   {
78     if (BASENAME.equals(name))
79       _basenameAttr = value;
80     else
81       throw error(L.l("`{0}' is an unsupported jsp:attribute for <{1}>.",
82                       name.getName(), getTagName()));
83   }
84
85   /**
86    * Generates the XML text representation for the tag validation.
87    *
88    * @param os write stream to the generated XML.
89    */

90   public void printXml(WriteStream os)
91     throws IOException JavaDoc
92   {
93     os.print("<fmt:bundle");
94
95     if (_basename != null)
96       os.print(" basename=\"" + xmlText(_basename) + "\"");
97     
98     if (_var != null)
99       os.print(" var=\"" + xmlText(_var) + "\"");
100     
101     if (_scope != null)
102       os.print(" scope=\"" + xmlText(_scope) + "\"");
103
104     os.print(">");
105
106     printXmlChildren(os);
107
108     os.print("</fmt:bundle>");
109   }
110
111   /**
112    * Generates the code for the c:out tag.
113    */

114   public void generate(JspJavaWriter out)
115     throws Exception JavaDoc
116   {
117     if (_basename == null && _basenameAttr == null)
118       throw error(L.l("required attribute `basename' missing from `{0}'",
119                       getTagName()));
120
121     String JavaDoc basenameExpr;
122
123     if (_basenameAttr != null)
124       basenameExpr = _basenameAttr.generateValue();
125     else
126       basenameExpr = generateValue(String JavaDoc.class, _basename);
127
128     String JavaDoc value = "pageContext.getBundle(" + basenameExpr + ")";
129
130     generateSetNotNull(out, _var, _scope, value);
131   }
132 }
133
Popular Tags