Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/opencms/flex/util/CmsMessages.java


1   /*
2    * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/flex/util/CmsMessages.java,v $
3    * Date   : $Date: 2003/03/28 19:51:03 $
4    * Version: $Revision: 1.6 $
5    *
6    * This library is part of OpenCms -
7    * the Open Source Content Mananagement System
8    *
9    * Copyright (C) 2002 - 2003 Alkacon Software (http://www.alkacon.com)
10   *
11   * This library is free software; you can redistribute it and/or
12   * modify it under the terms of the GNU Lesser General Public
13   * License as published by the Free Software Foundation; either
14   * version 2.1 of the License, or (at your option) any later version.
15   *
16   * This library is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19   * Lesser General Public License for more details.
20   *
21   * For further information about Alkacon Software, please see the
22   * company website: http://www.alkacon.com
23   *
24   * For further information about OpenCms, please see the
25   * project website: http://www.opencms.org
26   * 
27   * You should have received a copy of the GNU Lesser General Public
28   * License along with this library; if not, write to the Free Software
29   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30   */
31   
32  package com.opencms.flex.util;
33  
34  import java.text.DateFormat;
35  import java.util.Date;
36  import java.util.Locale;
37  import java.util.MissingResourceException;
38  import java.util.ResourceBundle;
39  
40  /**
41   * Reads localized resource Strings from a <code>java.util.ResourceBundle</code> 
42   * and provides convenience methods to access the Strings from a template.<p>
43   * 
44   * This class is to be used from JSP templates. Because of that, throwing of 
45   * exceptions related to the access of the resource bundle are suppressed
46   * so that a template always execute. The class provides an {@link #isInitialized()} method
47   * that can be checked to see if the instance was properly initialized.
48   * 
49   * @author  Alexander Kandzior (a.kandzior@alkacon.com)
50   * @version $Revision: 1.6 $
51   * 
52   * @since 5.0 beta 2
53   */
54  public final class CmsMessages extends Object {  
55         
56      // member variables
57      private ResourceBundle m_bundle; 
58      private Locale m_locale;
59      
60         
61      /**
62       * Constructor for the messages with an initialized <code>java.util.Locale</code>.
63       * 
64       * @param baseName the base ResourceBundle name
65       * @param theLocale the locale to use, eg. "de", "en" etc.
66       */
67      public CmsMessages( String baseName, Locale locale ) {
68          try {
69              m_locale = locale;
70              m_bundle = ResourceBundle.getBundle( baseName, m_locale );        
71          } catch (MissingResourceException e) {
72              m_bundle = null;
73          }
74      }  
75      
76      /**
77       * Constructor for the messages with a language string.<p>
78       * 
79       * The <code>language</code> is a 2 letter language ISO code, e.g. <code>"EN"</code>.<p>
80       * 
81       * The Locale for the messages will be created like this:<br>
82       * <code>new Locale(language, "", "")</code>.<p>
83       * 
84       * @param baseName the base ResourceBundle name
85       * @param language ISO language indentificator for the locale of the bundle
86       */
87      public CmsMessages( String baseName, String language ) {
88          this(baseName, language, "", "");      
89      }
90  
91      /**
92       * Constructor for the messages with language and country code strings.<p>
93       * 
94       * The <code>language</code> is a 2 letter language ISO code, e.g. <code>"EN"</code>.
95       * The <code>country</code> is a 2 letter country ISO code, e.g. <code>"us"</code>.<p>
96       * 
97       * The Locale for the messages will be created like this:<br>
98       * <code>new Locale(language, country, "")</code>.
99       * 
100      * @param baseName the base ResourceBundle name
101      * @param language ISO language indentificator for the locale of the bundle
102      * @param country ISO 2 letter country code for the locale of the bundle 
103      */
104     public CmsMessages( String baseName, String language, String country ) {
105         this(baseName, language, country, "");              
106     }
107     
108     /**
109      * Constructor for the messages with language, country code and variant strings.<p>
110      * 
111      * The <code>language</code> is a 2 letter language ISO code, e.g. <code>"EN"</code>.
112      * The <code>country</code> is a 2 letter country ISO code, e.g. <code>"us"</code>.
113      * The <code>variant</code> is a vendor or browser-specific code, e.g. <code>"POSIX"</code>.<p>
114      * 
115      * The Locale for the messages will be created like this:<br>
116      * <code>new Locale(language, country, variant)</code>.
117      * 
118      * @param baseName the base ResourceBundle name
119      * @param language language indentificator for the locale of the bundle
120      * @param country 2 letter country code for the locale of the bundle 
121      * @param variant a vendor or browser-specific variant code
122      */    
123     public CmsMessages( String baseName, String language, String country, String variant ) {
124         this(baseName, new Locale(language, country, variant));
125     }
126             
127     /**
128      * Checks if the bundle was properly initialized.
129      * 
130      * @return <code>true</code> if bundle was initialized, <code>false</code> otherwise
131      */
132     public boolean isInitialized() {
133         return (m_bundle != null);
134     }
135         
136     /**
137      * Gets the localized resource string for a given message key.<p>
138      * 
139      * If the key was not found in the bundle, the return value is
140      * <code>"??? " + keyName + " ???"</code>. This will also be returned 
141      * if the bundle was not properly initialized first.
142      * 
143      * @param keyName the key for the desired string 
144      * @return the resource string for the given key 
145      */
146     public String key( String keyName ) {   
147         try {            
148             if (m_bundle != null) return m_bundle.getString( keyName );
149         } catch (MissingResourceException e) {
150             // not found, return warning
151         }
152         return "??? " + keyName + " ???";
153     }    
154     
155     /**
156      * Directly calls the getString(String) method of the wrapped ResourceBundle.<p>
157      * 
158      * If you use this this class on a template, you should consider using 
159      * the {@link #key(String)} method to get the value from the ResourceBundle because it
160      * handles the exception for you in a convenient way. 
161      * 
162      * @param keyName the key  
163      * @return the resource string for the given key
164      * @throws MissingResourceException in case the key is not found of the bundle is not initialized
165      */
166     public String getString( String keyName ) throws MissingResourceException {              
167         if (m_bundle != null) return m_bundle.getString( keyName );
168         else throw new MissingResourceException("ResourceBundle not initialized", this.getClass().getName(), keyName);
169     }       
170 
171     /**
172      * Returns a formatted date.<p>
173      * 
174      * @param timestamp the date timestamp to format
175      * @return a formatted date
176      */
177     public String getDate( long timestamp ) {
178         return getDate(new Date(timestamp));
179     }
180     
181     /**
182      * Returns a formatted date.<p>
183      * 
184      * @param date the date to format 
185      * @return a formatted date
186      */    
187     public String getDate(Date date) {
188         return getDate(date, DateFormat.SHORT);
189     }
190     
191     /**
192      * Returns a formatted date.<p>
193      * 
194      * @param date the date to format 
195      * @param style the style to format the date with 
196      * @return a formatted date
197      * @see java.text.DateFormat
198      */      
199     public String getDate(Date date, int style) {
200         DateFormat df = DateFormat.getDateInstance(style, m_locale);
201         return df.format(date);
202     }
203     
204     /**
205      * Returns a formatted date with a time.<p>
206      * 
207      * @param timestamp the date timestamp to format
208      * @return a formatted date with a time
209      */    
210     public String getDateTime( long timestamp ) {
211         return getDateTime(new Date(timestamp));
212     }
213     
214     /**
215      * Returns a formatted date with a time.<p>
216      * 
217      * @param date the date to format 
218      * @return a formatted date with a time
219      */     
220     public String getDateTime(Date date) {
221         return getDateTime(date, DateFormat.SHORT);
222     }
223     
224     /**
225      * Returns a formatted date with a time.<p>
226      * 
227      * @param date the date to format 
228      * @param style the style to format the date with 
229      * @return a formatted date with a time
230      * @see java.text.DateFormat 
231      */     
232     public String getDateTime(Date date, int style) {
233         DateFormat df = DateFormat.getDateInstance(style, m_locale);
234         DateFormat tf = DateFormat.getTimeInstance(style, m_locale);
235         return df.format(date) + " " + tf.format(date);
236     }    
237 }