001: /*
002: * $Id: Property.java 497654 2007-01-19 00:21:57Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import java.io.IOException;
024: import java.io.Writer;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.struts2.views.annotations.StrutsTag;
029: import org.apache.struts2.views.annotations.StrutsTagAttribute;
030:
031: import com.opensymphony.xwork2.util.ValueStack;
032: import com.opensymphony.xwork2.util.TextUtils;
033:
034: /**
035: * <!-- START SNIPPET: javadoc -->
036: *
037: * Used to get the property of a <i>value</i>, which will default to the top of
038: * the stack if none is specified.
039: *
040: * <!-- END SNIPPET: javadoc -->
041: *
042: * <p/>
043: *
044: *
045: * <!-- START SNIPPET: params -->
046: *
047: * <ul>
048: * <li>default (String) - The default value to be used if <u>value</u> attribute is null</li>
049: * <li>escape (Boolean) - Escape HTML. Default to true</li>
050: * <li>value (Object) - value to be displayed</li>
051: * </ul>
052: *
053: * <!-- END SNIPPET: params -->
054: *
055: *
056: * <pre>
057: * <!-- START SNIPPET: example -->
058: *
059: * <s:push value="myBean">
060: * <!-- Example 1: -->
061: * <s:property value="myBeanProperty" />
062: *
063: * <!-- Example 2: -->TextUtils
064: * <s:property value="myBeanProperty" default="a default value" />
065: * </s:push>
066: *
067: * <!-- END SNIPPET: example -->
068: * </pre>
069: *
070: * <pre>
071: * <!-- START SNIPPET: exampledescription -->
072: *
073: * Example 1 prints the result of myBean's getMyBeanProperty() method.
074: * Example 2 prints the result of myBean's getMyBeanProperty() method and if it is null, print 'a default value' instead.
075: *
076: * <!-- END SNIPPET: exampledescription -->
077: * </pre>
078: *
079: *
080: * <pre>
081: * <!-- START SNIPPET: i18nExample -->
082: *
083: * <s:property value="getText('some.key')" />
084: *
085: * <!-- END SNIPPET: i18nExample -->
086: * </pre>
087: *
088: */
089: @StrutsTag(name="property",tldBodyContent="empty",tldTagClass="org.apache.struts2.views.jsp.PropertyTag",description="Print out expression which evaluates against the stack")
090: public class Property extends Component {
091: private static final Log LOG = LogFactory.getLog(Property.class);
092:
093: public Property(ValueStack stack) {
094: super (stack);
095: }
096:
097: private String defaultValue;
098: private String value;
099: private boolean escape = true;
100:
101: @StrutsTagAttribute(description="The default value to be used if value attribute is null")
102: public void setDefault(String defaultValue) {
103: this .defaultValue = defaultValue;
104: }
105:
106: @StrutsTagAttribute(description=" Whether to escape HTML",type="Boolean",defaultValue="true")
107: public void setEscape(boolean escape) {
108: this .escape = escape;
109: }
110:
111: @StrutsTagAttribute(description="Value to be displayed",type="Object",defaultValue="<top of stack>")
112: public void setValue(String value) {
113: this .value = value;
114: }
115:
116: public boolean start(Writer writer) {
117: boolean result = super .start(writer);
118:
119: String actualValue = null;
120:
121: if (value == null) {
122: value = "top";
123: } else if (altSyntax()) {
124: // the same logic as with findValue(String)
125: // if value start with %{ and end with }, just cut it off!
126: if (value.startsWith("%{") && value.endsWith("}")) {
127: value = value.substring(2, value.length() - 1);
128: }
129: }
130:
131: // exception: don't call findString(), since we don't want the
132: // expression parsed in this one case. it really
133: // doesn't make sense, in fact.
134: actualValue = (String) getStack()
135: .findValue(value, String.class);
136:
137: try {
138: if (actualValue != null) {
139: writer.write(prepare(actualValue));
140: } else if (defaultValue != null) {
141: writer.write(prepare(defaultValue));
142: }
143: } catch (IOException e) {
144: LOG.info("Could not print out value '" + value + "'", e);
145: }
146:
147: return result;
148: }
149:
150: private String prepare(String value) {
151: if (escape) {
152: return TextUtils.htmlEncode(value);
153: } else {
154: return value;
155: }
156: }
157: }
|