View Javadoc

1   package org.codehaus.mojo.tomcat;
2   
3   /*
4    * Copyright 2005 Mark Hobson.
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  
19  import java.text.MessageFormat;
20  import java.util.MissingResourceException;
21  import java.util.ResourceBundle;
22  
23  import org.apache.maven.plugin.AbstractMojo;
24  
25  /**
26   * Abstract goal that provides i18n support.
27   * 
28   * @author Mark Hobson <markhobson@gmail.com>
29   * @version $Id: AbstractI18NMojo.java 6588 2008-03-28 12:22:57Z bentmann $
30   */
31  public abstract class AbstractI18NMojo
32      extends AbstractMojo
33  {
34      // ----------------------------------------------------------------------
35      // Fields
36      // ----------------------------------------------------------------------
37  
38      /**
39       * The plugin messages.
40       */
41      private ResourceBundle messages;
42  
43      // ----------------------------------------------------------------------
44      // Constructors
45      // ----------------------------------------------------------------------
46  
47      /**
48       * Creates a new <code>AbstractI18NMojo</code>.
49       */
50      public AbstractI18NMojo()
51      {
52          String packageName = getClass().getPackage().getName();
53  
54          messages = ResourceBundle.getBundle( packageName + ".messages" );
55      }
56  
57      // ----------------------------------------------------------------------
58      // Protected Methods
59      // ----------------------------------------------------------------------
60  
61      /**
62       * Gets the message for the given key from this packages resource bundle.
63       * 
64       * @param key the key for the required message
65       * @return the message
66       */
67      protected String getMessage( String key )
68      {
69          try
70          {
71              return messages.getString( key );
72          }
73          catch ( NullPointerException exception )
74          {
75              return "???" + key + "???";
76          }
77          catch ( MissingResourceException exception )
78          {
79              return "???" + key + "???";
80          }
81          catch ( ClassCastException exception )
82          {
83              return "???" + key + "???";
84          }
85      }
86  
87      /**
88       * Gets the message for the given key from this packages resource bundle
89       * and formats it with the given parameter.
90       * 
91       * @param key the key for the required message
92       * @param param the parameter to be used to format the message with
93       * @return the formatted message
94       */
95      protected String getMessage( String key, Object param )
96      {
97          return MessageFormat.format( getMessage( key ), new Object[] { param } );
98      }
99  
100     /**
101      * Gets the message for the given key from this packages resource bundle
102      * and formats it with the given parameters.
103      * 
104      * @param key the key for the required message
105      * @param param1 the first parameter to be used to format the message with
106      * @param param2 the second parameter to be used to format the message with
107      * @return the formatted message
108      */
109     protected String getMessage( String key, Object param1, Object param2 )
110     {
111         return MessageFormat.format( getMessage( key ), new Object[] { param1, param2 } );
112     }
113 
114     /**
115      * Gets the message for the given key from this packages resource bundle
116      * and formats it with the given parameters.
117      * 
118      * @param key the key for the required message
119      * @param params the parameters to be used to format the message with
120      * @return the formatted message
121      */
122     protected String getMessage( String key, Object[] params )
123     {
124         return MessageFormat.format( getMessage( key ), params );
125     }
126 }