1 /*
2 * Copyright (c) 2001 - 2005 ivata limited.
3 * All rights reserved.
4 * ---------------------------------------------------------
5 * ivata groupware may be redistributed under the GNU General Public
6 * License as published by the Free Software Foundation;
7 * version 2 of the License.
8 *
9 * These programs are free software; you can redistribute them and/or
10 * modify them under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; version 2 of the License.
12 *
13 * These programs are distributed in the hope that they will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * See the GNU General Public License in the file LICENSE.txt for more
18 * details.
19 *
20 * If you would like a copy of the GNU General Public License write to
21 *
22 * Free Software Foundation, Inc.
23 * 59 Temple Place - Suite 330
24 * Boston, MA 02111-1307, USA.
25 *
26 *
27 * To arrange commercial support and licensing, contact ivata at
28 * http://www.ivata.com/contact.jsp
29 * ---------------------------------------------------------
30 * $Log: ErrorsTag.java,v $
31 * Revision 1.1 2005/04/11 14:56:37 colinmacleod
32 * Added base tag, link tag and rewrite tag to
33 * change the host name via environment entries.
34 * Added errors tag to handle errors even when
35 * the message is not found in the application
36 * resources.
37 *
38 * ---------------------------------------------------------
39 */
40 package com.ivata.mask.web.tag.html;
41
42 import java.util.Iterator;
43
44 import javax.servlet.jsp.JspException;
45
46 import org.apache.struts.action.ActionMessage;
47 import org.apache.struts.action.ActionMessages;
48 import org.apache.struts.taglib.TagUtils;
49
50 import com.ivata.mask.util.StringHandling;
51
52 /***
53 * The standard <strong>Struts</strong> errors tag does not output anything if
54 * the error message is missing. This override puts out the message key in this
55 * case.
56 *
57 * @since ivata groupware 0.11 (2005-Mar-29)
58 * @author Colin MacLeod
59 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
60 * @version $Revision: 1.1 $
61 */
62 public class ErrorsTag extends org.apache.struts.taglib.html.ErrorsTag {
63 /***
64 * Overridden from the <strong>Struts</strong> tag to output error messages
65 * even if there is no message.
66 *
67 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
68 * @return always returns <code>EVAL_BODY_INCLUDE</code>.
69 * @throws JspException If there is any problem writing out the messages.
70 */
71 public int doStartTag() throws JspException {
72 TagUtils tagUtils = TagUtils.getInstance();
73 ActionMessages errors = tagUtils.getActionMessages(pageContext, name);
74
75 if ((errors == null)
76 || errors.isEmpty()) {
77 return EVAL_BODY_INCLUDE;
78 }
79 StringBuffer buffer = new StringBuffer();
80
81 // first put out the header
82 String header =
83 tagUtils.message(
84 pageContext,
85 bundle,
86 locale,
87 "errors.header");
88 buffer.append(StringHandling.getNotNull(header, ""));
89
90 // now go thro' each error message in turn
91 Iterator errorMessageIterator;
92 if (property == null) {
93 errorMessageIterator = errors.get();
94 } else {
95 errorMessageIterator = errors.get(property);
96 }
97 while (errorMessageIterator.hasNext()) {
98 // before each error message, there is (possibly) a prefix message
99 String prefix =
100 tagUtils.message(
101 pageContext,
102 bundle,
103 locale,
104 "errors.prefix");
105 buffer.append(StringHandling.getNotNull(prefix, ""));
106
107 // now put out the error message itself - if null, just put out
108 // the error key
109 ActionMessage actionMessage = (ActionMessage)
110 errorMessageIterator.next();
111 String errorMessage =
112 TagUtils.getInstance().message(
113 pageContext,
114 bundle,
115 locale,
116 actionMessage.getKey(),
117 actionMessage.getValues());
118 buffer.append(StringHandling.getNotNull(errorMessage,
119 "["
120 + actionMessage.getKey()
121 + "("
122 + actionMessage.getValues()
123 + ")]"));
124 // after each error message, there is (possibly) a suffix message
125 String suffix =
126 tagUtils.message(
127 pageContext,
128 bundle,
129 locale,
130 "errors.suffix");
131 buffer.append(StringHandling.getNotNull(suffix, ""));
132 }
133 // finally put out the footer
134 String footer =
135 tagUtils.message(
136 pageContext,
137 bundle,
138 locale,
139 "errors.footer");
140 buffer.append(StringHandling.getNotNull(footer, ""));
141
142 tagUtils.write(pageContext, buffer.toString());
143 return EVAL_BODY_INCLUDE;
144 }
145 }