001: package com.opensymphony.webwork.components;
002:
003: import com.opensymphony.xwork.util.OgnlValueStack;
004: import com.opensymphony.util.TextUtils;
005:
006: import java.io.Writer;
007: import java.io.IOException;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: /**
013: * <!-- START SNIPPET: javadoc -->
014: *
015: * Used to get the property of a <i>value</i>, which will default to the top of
016: * the stack if none is specified.
017: *
018: * <!-- END SNIPPET: javadoc -->
019: *
020: * <p/>
021: *
022: *
023: * <!-- START SNIPPET: params -->
024: *
025: * <ul>
026: * <li>default (String) - The default value to be used if <u>value</u> attribute is null</li>
027: * <li>escape (Boolean) - Escape HTML. Default to true</li>
028: * <li>value (Object) - value to be displayed</li>
029: * </ul>
030: *
031: * <!-- END SNIPPET: params -->
032: *
033: *
034: * <pre>
035: * <!-- START SNIPPET: example -->
036: *
037: * <ww:push value="myBean">
038: * <!-- Example 1: -->
039: * <ww:property value="myBeanProperty" />
040: *
041: * <!-- Example 2: -->
042: * <ww:property value="myBeanProperty" default="a default value" />
043: * </ww:push>
044: *
045: * <!-- END SNIPPET: example -->
046: * </pre>
047: *
048: * <pre>
049: * <!-- START SNIPPET: exampledescription -->
050: *
051: * Example 1 prints the result of myBean's getMyBeanProperty() method.
052: * Example 2 prints the result of myBean's getMyBeanProperty() method and if it is null, print 'a default value' instead.
053: *
054: * <!-- END SNIPPET: exampledescription -->
055: * </pre>
056: *
057: *
058: * <pre>
059: * <!-- START SNIPPET: i18nExample -->
060: *
061: * <ww:property value="getText('some.key')" />
062: *
063: * <!-- END SNIPPET: i18nExample -->
064: * </pre>
065: *
066: *
067: * @author Patrick Lightbody
068: * @author Cameron Braid
069: * @author Mathias Bogaert
070: * @author tm_jee
071: * @author Rene Gielen
072: * @version $Revision: 2468 $
073: *
074: * @ww.tag name="property" tld-body-content="empty" tld-tag-class="com.opensymphony.webwork.views.jsp.PropertyTag"
075: * description="Print out expression which evaluates against the stack"
076: */
077: public class Property extends Component {
078: private static final Log LOG = LogFactory.getLog(Property.class);
079:
080: public Property(OgnlValueStack stack) {
081: super (stack);
082: }
083:
084: private String defaultValue;
085: private String value;
086: private boolean escape = true;
087:
088: /**
089: * The default value to be used if <u>value</u> attribute is null
090: * @ww.tagattribute required="false" type="String"
091: */
092: public void setDefault(String defaultValue) {
093: this .defaultValue = defaultValue;
094: }
095:
096: /**
097: * Whether to escape HTML
098: * @ww.tagattribute required="false" type="Boolean" default="true"
099: */
100: public void setEscape(boolean escape) {
101: this .escape = escape;
102: }
103:
104: /**
105: * value to be displayed
106: * @ww.tagattribute required="false" type="Object" default="<top of stack>"
107: */
108: public void setValue(String value) {
109: this .value = value;
110: }
111:
112: public boolean start(Writer writer) {
113: boolean result = super .start(writer);
114:
115: String actualValue = null;
116:
117: if (value == null) {
118: value = "top";
119: } else if (altSyntax()) {
120: // the same logic as with findValue(String)
121: // if value start with %{ and end with }, just cut it off!
122: if (value.startsWith("%{") && value.endsWith("}")) {
123: value = value.substring(2, value.length() - 1);
124: }
125: }
126:
127: // exception: don't call findString(), since we don't want the
128: // expression parsed in this one case. it really
129: // doesn't make sense, in fact.
130: actualValue = (String) getStack()
131: .findValue(value, String.class);
132:
133: try {
134: if (actualValue != null) {
135: writer.write(prepare(actualValue));
136: } else if (defaultValue != null) {
137: writer.write(prepare(defaultValue));
138: }
139: } catch (IOException e) {
140: LOG.info("Could not print out value '" + value + "'", e);
141: }
142:
143: return result;
144: }
145:
146: private String prepare(String value) {
147: if (escape) {
148: return TextUtils.htmlEncode(value);
149: } else {
150: return value;
151: }
152: }
153: }
|