View Javadoc

1   /*
2    * $Id: MessagesPresentTag.java 471754 2006-11-06 14:55:09Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts.taglib.logic;
22  
23  import org.apache.struts.Globals;
24  import org.apache.struts.action.ActionMessages;
25  import org.apache.struts.taglib.TagUtils;
26  
27  import javax.servlet.jsp.JspException;
28  
29  import java.util.Iterator;
30  
31  /**
32   * Evalute to <code>true</code> if an <code>ActionMessages</code> class or a
33   * class that can be converted to an <code>ActionMessages</code> class is in
34   * request scope under the specified key and there is at least one message in
35   * the class or for the property specified.
36   *
37   * @version $Rev: 471754 $ $Date: 2005-11-19 12:36:20 -0500 (Sat, 19 Nov 2005)
38   *          $
39   * @since Struts 1.1
40   */
41  public class MessagesPresentTag extends ConditionalTagBase {
42      /**
43       * If this is set to 'true', then the <code>Globals.MESSAGE_KEY</code>
44       * will be used to retrieve the messages from scope.
45       */
46      protected String message = null;
47  
48      public MessagesPresentTag() {
49          name = Globals.ERROR_KEY;
50      }
51  
52      public String getMessage() {
53          return (this.message);
54      }
55  
56      public void setMessage(String message) {
57          this.message = message;
58      }
59  
60      /**
61       * Evaluate the condition that is being tested by this particular tag, and
62       * return <code>true</code> if the nested body content of this tag should
63       * be evaluated, or <code>false</code> if it should be skipped.
64       *
65       * @throws JspException if a JSP exception occurs
66       */
67      protected boolean condition()
68          throws JspException {
69          return (condition(true));
70      }
71  
72      /**
73       * Evaluate the condition that is being tested by this particular tag, and
74       * return <code>true</code> if there is at least one message in the class
75       * or for the property specified.
76       *
77       * @param desired Desired outcome for a true result
78       * @throws JspException if a JSP exception occurs
79       */
80      protected boolean condition(boolean desired)
81          throws JspException {
82          ActionMessages am = null;
83  
84          String key = name;
85  
86          if ((message != null) && "true".equalsIgnoreCase(message)) {
87              key = Globals.MESSAGE_KEY;
88          }
89  
90          try {
91              am = TagUtils.getInstance().getActionMessages(pageContext, key);
92          } catch (JspException e) {
93              TagUtils.getInstance().saveException(pageContext, e);
94              throw e;
95          }
96  
97          Iterator iterator = (property == null) ? am.get() : am.get(property);
98  
99          return (iterator.hasNext() == desired);
100     }
101 
102     /**
103      * Release all allocated resources.
104      */
105     public void release() {
106         super.release();
107         name = Globals.ERROR_KEY;
108         message = null;
109     }
110 }