001: /*
002: * $Id: Push.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.Writer;
024:
025: import org.apache.struts2.views.annotations.StrutsTag;
026: import org.apache.struts2.views.annotations.StrutsTagAttribute;
027:
028: import com.opensymphony.xwork2.util.ValueStack;
029:
030: /**
031: * <!-- START SNIPPET: javadoc -->
032: * <p>Push value on stack for simplified usage.</p>
033: * <!-- END SNIPPET: javadoc -->
034: *
035: * <!-- START SNIPPET: params -->
036: * <ul>
037: * <li>value* (Object) - value to be pushed into the top of the stack</li>
038: * </ul>
039: * <!-- END SNIPPET: params -->
040: *
041: *
042: * <p/> <b>Examples</b>
043: * <pre>
044: * <!-- START SNIPPET: example1 -->
045: * <s:push value="user">
046: * <s:propery value="firstName" />
047: * <s:propery value="lastName" />
048: * </s:push>
049: * <!-- END SNIPPET: example1 -->
050: * </pre>
051: *
052: * <!-- START SNIPPET: example1description -->
053: * Pushed user into the stack, and hence property tag could access user's properties
054: * (firstName, lastName etc) since user is not at the top of the stack
055: * <!-- END SNIPPET: example1description -->
056: *
057: * <pre>
058: * <!-- START SNIPPET: example2 -->
059: * <s:push value="myObject"> ----- (1)
060: * <s:bean name="jp.SomeBean" id="myBean"/> ----- (2)
061: * <s:param name="myParam" value="top"/> ----- (3)
062: * </s:bean>
063: * </s:push>
064: * <!-- END SNIPPET: example2 -->
065: * </pre>
066: *
067: * <pre>
068: * <!-- START SNIPPET: example2description -->
069: * when in (1), myObject is at the top of the stack
070: * when in (2), jp.SomeBean is in the top of stack, also in stack's context with key myBean
071: * when in (3), top will get the jp.SomeBean instance
072: * <!-- END SNIPPET: example2description -->
073: * </pre>
074: *
075: * <pre>
076: * <!-- START SNIPPET: example3 -->
077: * <s:push value="myObject"> ---(A)
078: * <s:bean name="jp.SomeBean" id="myBean"/> ---(B)
079: * <s:param name="myParam" value="top.mySomeOtherValue"/> ---(C)
080: * </s:bean>
081: * </s:push>
082: * <!-- END SNIPPET: example3 -->
083: * </pre>
084: *
085: * <pre>
086: * <!-- START SNIPPET: example3description -->
087: * when in (A), myObject is at the top of the stack
088: * when in (B), jp.SomeBean is at the top of the stack, also in context with key myBean
089: * when in (C), top refers to jp.SomeBean instance. so top.mySomeOtherValue would invoke SomeBean's mySomeOtherValue() method
090: * <!-- END SNIPPET: example3description -->
091: * </pre>
092: *
093: * <pre>
094: * <!-- START SNIPPET: example4 -->
095: * <s:push value="myObject"> ---- (i)
096: * <s:bean name="jp.SomeBean" id="myBean"/> ---- (ii)
097: * <s:param name="myParam" value="[1].top"/> -----(iii)
098: * </s:bean>
099: * </s:push>
100: * <!-- END SNIPPET: example4 -->
101: * </pre>
102: *
103: * <pre>
104: * <!-- START SNIPPET: example4description -->
105: * when in (i), myObject is at the top of the stack
106: * when in (ii), jp.SomeBean is at the top of the stack, followed by myObject
107: * when in (iii), [1].top will returned top of the cut of stack starting from myObject, namely myObject itself
108: * <!-- END SNIPPET: example4description -->
109: * </pre>
110: *
111: */
112: @StrutsTag(name="push",tldTagClass="org.apache.struts2.views.jsp.PushTag",description="Push value on stack for simplified usage.")
113: public class Push extends Component {
114: protected String value;
115: protected boolean pushed;
116:
117: public Push(ValueStack stack) {
118: super (stack);
119: }
120:
121: public boolean start(Writer writer) {
122: boolean result = super .start(writer);
123:
124: ValueStack stack = getStack();
125:
126: if (stack != null) {
127: stack
128: .push(findValue(value, "value",
129: "You must specify a value to push on the stack. Example: person"));
130: pushed = true;
131: } else {
132: pushed = false; // need to ensure push is assigned, otherwise we may have a leftover value
133: }
134:
135: return result;
136: }
137:
138: public boolean end(Writer writer, String body) {
139: ValueStack stack = getStack();
140:
141: if (pushed && (stack != null)) {
142: stack.pop();
143: }
144:
145: return super .end(writer, body);
146: }
147:
148: @StrutsTagAttribute(description="Value to push on stack",required=true)
149: public void setValue(String value) {
150: this.value = value;
151: }
152: }
|