001: package com.opensymphony.webwork.components;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.util.ArrayList;
006: import java.util.Collections;
007: import java.util.Iterator;
008: import java.util.List;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import com.opensymphony.util.TextUtils;
014: import com.opensymphony.xwork.TextProvider;
015: import com.opensymphony.xwork.util.OgnlValueStack;
016:
017: /**
018: * <!-- START SNIPPET: javadoc -->
019: * Render a I18n text message.
020: *
021: * <p/>
022: *
023: * The message must be in a resource bundle
024: * with the same name as the action that it is associated with. In practice
025: * this means that you should create a properties file in the same package
026: * as your Java class with the same name as your class, but with .properties
027: * extension.
028: *
029: * <p/>
030: *
031: * If the named message is not found, then the body of the tag will be used as default message.
032: * If no body is used, then the name of the message will be used.
033: *
034: * <!-- END SNIPPET: javadoc -->
035: *
036: *
037: *
038: * <!-- START SNIPPET: params -->
039: *
040: * <ul>
041: * <li>name* (String) - the i18n message key</li>
042: * </ul>
043: *
044: * <!-- END SNIPPET: params -->
045: *
046: * <p/>
047: *
048: * Example:
049: * <pre>
050: * <!-- START SNIPPET: exdescription -->
051: *
052: * Accessing messages from a given bundle (the i18n Shop example bundle in the first example) and using bundle defined through ww in the second example.</p>
053: *
054: * <!-- END SNIPPET: exdescription -->
055: * </pre>
056: *
057: * <pre>
058: * <!-- START SNIPPET: example -->
059: *
060: * <!-- First Example -->
061: * <ww:i18n name="webwork.action.test.i18n.Shop">
062: * <ww:text name="main.title"/>
063: * </ww:i18n>
064: *
065: * <!-- Second Example -->
066: * <ww:text name="main.title" />
067: *
068: * <!-- END SNIPPET: example -->
069: * </pre>
070: *
071: *
072: * <pre>
073: * <!-- START SNIPPET: i18nExample -->
074: *
075: * <-- Third Example -->
076: * <ww:text name="some.key" />
077: *
078: * <-- Fourth Example -->
079: * <a:text name="some.invalid.key" >
080: * The Default Message That Will Be Displayed
081: * </a:text>
082: *
083: * <!-- END SNIPPET: i18nExample -->
084: * </pre>
085: *
086: *
087: * @author Jason Carreira
088: * @author Patrick Lightbody
089: * @author Rene Gielen
090: * @author tm_jee
091: * @version $Revision: 2586 $
092: * @since 2.2
093: *
094: * @see Param
095: *
096: * @ww.tag name="text" tld-body-content="JSP" tld-tag-class="com.opensymphony.webwork.views.jsp.TextTag"
097: * description="Render a I18n text message."
098: */
099: public class Text extends Component implements Param.UnnamedParametric {
100: private static final Log LOG = LogFactory.getLog(Text.class);
101:
102: protected List values = Collections.EMPTY_LIST;
103: protected String actualName;
104: protected String name;
105:
106: public Text(OgnlValueStack stack) {
107: super (stack);
108: }
109:
110: /**
111: * Name of resource property to fetch
112: * @ww.tagattribute required="true"
113: */
114: public void setName(String name) {
115: this .name = name;
116: }
117:
118: public boolean usesBody() {
119: // overriding this to true such that EVAL_BODY_BUFFERED is return and
120: // bodyContent will be valid hence, text between start & end tag will
121: // be honoured as default message (WW-1268)
122: return true;
123: }
124:
125: public boolean end(Writer writer, String body) {
126: actualName = findString(name, "name",
127: "You must specify the i18n key. Example: welcome.header");
128: String defaultMessage;
129: if (TextUtils.stringSet(body)) {
130: defaultMessage = body;
131: } else {
132: defaultMessage = actualName;
133: }
134: String msg = null;
135: OgnlValueStack stack = getStack();
136:
137: for (Iterator iterator = getStack().getRoot().iterator(); iterator
138: .hasNext();) {
139: Object o = iterator.next();
140:
141: if (o instanceof TextProvider) {
142: TextProvider tp = (TextProvider) o;
143: msg = tp.getText(actualName, defaultMessage, values,
144: stack);
145:
146: break;
147: }
148: }
149:
150: if (msg != null) {
151: try {
152: if (getId() == null) {
153: writer.write(msg);
154: } else {
155: stack.getContext().put(getId(), msg);
156: }
157: } catch (IOException e) {
158: LOG.error("Could not write out Text tag", e);
159: }
160: }
161:
162: return super .end(writer, "");
163: }
164:
165: public void addParameter(String key, Object value) {
166: addParameter(value);
167: }
168:
169: public void addParameter(Object value) {
170: if (values.isEmpty()) {
171: values = new ArrayList(4);
172: }
173:
174: values.add(value);
175: }
176: }
|