KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > tags > BundleMessagesTag


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20
21 package com.sslexplorer.core.tags;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24
25 import org.apache.struts.Globals;
26 import org.apache.struts.action.ActionMessage;
27 import org.apache.struts.action.ActionMessages;
28 import org.apache.struts.taglib.TagUtils;
29 import org.apache.struts.taglib.html.MessagesTag;
30
31 import com.sslexplorer.core.BundleActionMessage;
32
33 /**
34  * Extension of the standard <bean:message> tag that understands {@link com.sslexplorer.core.BundleActionMessage}
35  * objects as well as the usual {@link org.apache.struts.action.ActionMessage} objects.
36  * <p>
37  * <i>Bundle Action Messages</i> differe from the usual objects in that the key of the
38  * message bundle that contains the message text may be specified by the contructor of
39  * the object instead of in the JSP tags.
40  *
41  * @author Brett Smith <a HREF="mailto:brett@3sp.com">&lt;brett@3sp.com&gt;</a>
42  */

43 public class BundleMessagesTag extends MessagesTag {
44
45     /* (non-Javadoc)
46      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
47      */

48     public int doStartTag() throws JspException JavaDoc {
49         processed = false;
50         ActionMessages messages = null;
51         String JavaDoc name = this.name;
52         if (message != null && "true".equalsIgnoreCase(message)) {
53             name = Globals.MESSAGE_KEY;
54         }
55         try {
56             messages = TagUtils.getInstance().getActionMessages(pageContext, name);
57         } catch (JspException JavaDoc e) {
58             TagUtils.getInstance().saveException(pageContext, e);
59             throw e;
60         }
61         this.iterator = (property == null) ? messages.get() : messages.get(property);
62         if (!this.iterator.hasNext()) {
63             return SKIP_BODY;
64         }
65         ActionMessage report = (ActionMessage) this.iterator.next();
66         String JavaDoc actualBundle = report instanceof BundleActionMessage ? ((BundleActionMessage)report).getBundle() :bundle;
67         String JavaDoc msg =
68             TagUtils.getInstance().message(
69                 pageContext,
70                 actualBundle,
71                 locale,
72                 report.getKey(),
73                 report.getValues());
74         if (msg == null) {
75             pageContext.setAttribute(id, "Could not locate resource with key " + report.getKey() + " in bundle " + actualBundle );
76         } else {
77             pageContext.setAttribute(id, msg);
78         }
79         if (header != null && header.length() > 0) {
80             String JavaDoc headerMessage =
81                 TagUtils.getInstance().message(pageContext, bundle, locale, header);
82
83             if (headerMessage != null) {
84                 TagUtils.getInstance().write(pageContext, headerMessage);
85             }
86         }
87         processed = true;
88         return (EVAL_BODY_BUFFERED);
89     }
90
91     /* (non-Javadoc)
92      * @see javax.servlet.jsp.tagext.IterationTag#doAfterBody()
93      */

94     public int doAfterBody() throws JspException JavaDoc {
95         if (bodyContent != null) {
96             TagUtils.getInstance().writePrevious(pageContext, bodyContent.getString());
97             bodyContent.clearBody();
98         }
99         if (iterator.hasNext()) {
100             ActionMessage report = (ActionMessage) iterator.next();
101             String JavaDoc msg =
102                 TagUtils.getInstance().message(
103                     pageContext,
104                     report instanceof BundleActionMessage ? ((BundleActionMessage)report).getBundle() :bundle,
105                     locale,
106                     report.getKey(),
107                     report.getValues());
108
109            if (msg == null) {
110                pageContext.removeAttribute(id);
111            } else {
112                pageContext.setAttribute(id, msg);
113            }
114
115            return (EVAL_BODY_BUFFERED);
116         } else {
117            return (SKIP_BODY);
118         }
119
120     }
121 }
122
Popular Tags