View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/MessageTag.java,v 1.16 2006/01/13 13:38:51 hkollmann Exp $
3    * $Revision: 1.16 $
4    * $Date: 2006/01/13 13:38:51 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.taglib;
25  
26  import org.dbforms.util.MessageResources;
27  
28  import java.util.Locale;
29  import java.util.StringTokenizer;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.jsp.JspException;
33  
34  
35  
36  /***
37   * 2002-09-23 HKK: Extented to support parameters
38   */
39  public class MessageTag extends AbstractScriptHandlerTag
40     implements javax.servlet.jsp.tagext.TryCatchFinally {
41     private String key   = null;
42     private String param = null;
43  
44     /***
45      * DOCUMENT ME!
46      *
47      * @param newKey DOCUMENT ME!
48      */
49     public void setKey(String newKey) {
50        key = newKey;
51     }
52  
53  
54     /***
55      * DOCUMENT ME!
56      *
57      * @return DOCUMENT ME!
58      */
59     public String getKey() {
60        return key;
61     }
62  
63  
64     /***
65      * DOCUMENT ME!
66      *
67      * @param newParam DOCUMENT ME!
68      */
69     public void setParam(String newParam) {
70        param = newParam;
71     }
72  
73  
74     /***
75      * DOCUMENT ME!
76      *
77      * @return DOCUMENT ME!
78      */
79     public String getParam() {
80        return param;
81     }
82  
83  
84     /***
85      * DOCUMENT ME!
86      *
87      * @return DOCUMENT ME!
88      *
89      * @throws JspException DOCUMENT ME!
90      */
91     public int doEndTag() throws JspException {
92        if (getKey() != null) {
93           Locale locale = MessageResources.getLocale((HttpServletRequest) pageContext
94                                                      .getRequest());
95           String message;
96  
97           if ((param == null) || (param.length() == 0)) {
98              message = MessageResources.getMessage(getKey(), locale);
99           } else {
100             message = MessageResources.getMessage(getKey(), locale,
101                                                   splitString(param, ","));
102          }
103 
104          try {
105             if (message != null) {
106                pageContext.getOut()
107                           .write(message);
108             } else {
109                pageContext.getOut()
110                           .write(getKey());
111 
112                if (param != null) {
113                   pageContext.getOut()
114                              .write("&nbsp;");
115                   pageContext.getOut()
116                              .write(param);
117                }
118             }
119          } catch (java.io.IOException ioe) {
120             throw new JspException("IO Error: " + ioe.getMessage());
121          }
122       }
123 
124       return EVAL_PAGE;
125    }
126 
127 
128    /***
129     * DOCUMENT ME!
130     */
131    public void doFinally() {
132       key   = null;
133       param = null;
134    }
135 
136 
137    /***
138     * DOCUMENT ME!
139     *
140     * @return DOCUMENT ME!
141     *
142     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
143     */
144    public int doStartTag() throws javax.servlet.jsp.JspException {
145       return SKIP_BODY;
146    }
147 
148 
149    private String[] splitString(String str,
150                                 String delimeter) {
151       StringTokenizer st     = new StringTokenizer(str, delimeter);
152       int             i      = 0;
153       String[]        result = new String[st.countTokens()];
154 
155       while (st.hasMoreTokens()) {
156          result[i] = st.nextToken();
157          i++;
158       }
159 
160       return result;
161    }
162 }