View Javadoc

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