KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > TextTag


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jsp;
6
7 import com.opensymphony.webwork.views.jsp.ParamTag;
8 import com.opensymphony.webwork.views.jsp.WebWorkBodyTagSupport;
9 import com.opensymphony.xwork.TextProvider;
10 import com.opensymphony.xwork.util.OgnlValueStack;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 import javax.servlet.jsp.JspException JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21
22 /**
23  * Access a i18n-ized message. The message must be in a resource bundle
24  * with the same name as the action that it is associated with. In practice
25  * this means that you should create a properties file in the same package
26  * as your Java class with the same name as your class, but with .properties
27  * extension.
28  * <p/>
29  * See examples for further info on how to use.
30  * <p/>
31  * If the named message is not found, then the body of the tag will be used as default message.
32  * If no body is used, then the name of the message will be used.
33  *
34  * @author Jason Carreira
35  */

36 public class TextTag extends WebWorkBodyTagSupport implements ParamTag.UnnamedParametric {
37     //~ Static fields/initializers /////////////////////////////////////////////
38

39     private static final Log LOG = LogFactory.getLog(TextTag.class);
40
41     //~ Instance fields ////////////////////////////////////////////////////////
42

43     protected String JavaDoc value0Attr;
44     protected String JavaDoc value1Attr;
45     protected String JavaDoc value2Attr;
46     protected String JavaDoc value3Attr;
47     List JavaDoc values;
48     String JavaDoc actualName;
49     String JavaDoc nameAttr;
50
51     //~ Methods ////////////////////////////////////////////////////////////////
52

53     public void setName(String JavaDoc name) {
54         this.nameAttr = name;
55     }
56
57     public Map JavaDoc getParameters() {
58         return null;
59     }
60
61     public void setValue0(String JavaDoc aName) {
62         LOG.warn("The value attributes of TextTag are deprecated.");
63         value0Attr = aName;
64     }
65
66     public void setValue1(String JavaDoc aName) {
67         LOG.warn("The value attributes of TextTag are deprecated.");
68         value1Attr = aName;
69     }
70
71     public void setValue2(String JavaDoc aName) {
72         LOG.warn("The value attributes of TextTag are deprecated.");
73         value2Attr = aName;
74     }
75
76     public void setValue3(String JavaDoc aName) {
77         LOG.warn("The value attributes of TextTag are deprecated.");
78         value3Attr = aName;
79     }
80
81     public void addParameter(String JavaDoc key, Object JavaDoc value) {
82         addParameter(value);
83     }
84
85     public void addParameter(Object JavaDoc value) {
86         if (value == null) {
87             return;
88         }
89
90         if (values == null) {
91             values = new ArrayList JavaDoc();
92         }
93
94         values.add(value);
95     }
96
97     public int doEndTag() throws JspException JavaDoc {
98         actualName = (String JavaDoc) findString(nameAttr);
99
100         // Add tag attribute values
101
// These can be used to parameterize the i18n-ized message
102
if (value0Attr != null) {
103             addParameter(findValue(value0Attr));
104         }
105
106         if (value1Attr != null) {
107             addParameter(findValue(value1Attr));
108         }
109
110         if (value2Attr != null) {
111             addParameter(findValue(value2Attr));
112         }
113
114         if (value3Attr != null) {
115             addParameter(findValue(value3Attr));
116         }
117
118         String JavaDoc defaultMessage;
119
120         if ((bodyContent != null) && (bodyContent.getString().trim().length() > 0)) {
121             defaultMessage = bodyContent.getString().trim();
122         } else {
123             defaultMessage = actualName;
124         }
125
126         String JavaDoc msg = null;
127         OgnlValueStack stack = getStack();
128
129         for (Iterator JavaDoc iterator = getStack().getRoot().iterator();
130             iterator.hasNext();) {
131             Object JavaDoc o = iterator.next();
132
133             if (o instanceof TextProvider) {
134                 TextProvider tp = (TextProvider) o;
135                 msg = tp.getText(actualName, defaultMessage, values, stack);
136
137                 break;
138             }
139         }
140
141         if (msg != null) {
142             try {
143                 if (getId() == null) {
144                     pageContext.getOut().write(msg);
145                 } else {
146                     stack.getContext().put(getId(), msg);
147                 }
148             } catch (IOException JavaDoc e) {
149                 throw new JspException JavaDoc(e);
150             }
151         }
152
153         return EVAL_PAGE;
154     }
155
156     public int doStartTag() throws JspException JavaDoc {
157         values = null;
158
159         return super.doStartTag();
160     }
161 }
162
Popular Tags