View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.struts2.jasper.compiler;
19  
20  import java.text.MessageFormat;
21  import java.util.MissingResourceException;
22  import java.util.ResourceBundle;
23  
24  /***
25   * Class responsible for converting error codes to corresponding localized
26   * error messages.
27   *
28   * @author Jan Luehe
29   */
30  public class Localizer {
31  
32      private static ResourceBundle bundle = null;
33  
34      static {
35          try {
36              bundle = ResourceBundle.getBundle(
37                      "org.apache.struts2.jasper.resources.LocalStrings");
38          } catch (Throwable t) {
39              t.printStackTrace();
40          }
41      }
42  
43      /*
44       * Returns the localized error message corresponding to the given error
45       * code.
46       *
47       * If the given error code is not defined in the resource bundle for
48       * localized error messages, it is used as the error message.
49       *
50       * @param errCode Error code to localize
51       * 
52       * @return Localized error message
53       */
54      public static String getMessage(String errCode) {
55          String errMsg = errCode;
56          try {
57              errMsg = bundle.getString(errCode);
58          } catch (MissingResourceException e) {
59          }
60          return errMsg;
61      }
62  
63      /* 
64       * Returns the localized error message corresponding to the given error
65       * code.
66       *
67       * If the given error code is not defined in the resource bundle for
68       * localized error messages, it is used as the error message.
69       *
70       * @param errCode Error code to localize
71       * @param arg Argument for parametric replacement
72       *
73       * @return Localized error message
74       */
75      public static String getMessage(String errCode, String arg) {
76          return getMessage(errCode, new Object[]{arg});
77      }
78  
79      /* 
80       * Returns the localized error message corresponding to the given error
81       * code.
82       *
83       * If the given error code is not defined in the resource bundle for
84       * localized error messages, it is used as the error message.
85       *
86       * @param errCode Error code to localize
87       * @param arg1 First argument for parametric replacement
88       * @param arg2 Second argument for parametric replacement
89       *
90       * @return Localized error message
91       */
92      public static String getMessage(String errCode, String arg1, String arg2) {
93          return getMessage(errCode, new Object[]{arg1, arg2});
94      }
95  
96      /* 
97      * Returns the localized error message corresponding to the given error
98      * code.
99      *
100     * If the given error code is not defined in the resource bundle for
101     * localized error messages, it is used as the error message.
102     *
103     * @param errCode Error code to localize
104     * @param arg1 First argument for parametric replacement
105     * @param arg2 Second argument for parametric replacement
106     * @param arg3 Third argument for parametric replacement
107     *
108     * @return Localized error message
109     */
110     public static String getMessage(String errCode, String arg1, String arg2,
111                                     String arg3) {
112         return getMessage(errCode, new Object[]{arg1, arg2, arg3});
113     }
114 
115     /* 
116      * Returns the localized error message corresponding to the given error
117      * code.
118      *
119      * If the given error code is not defined in the resource bundle for
120      * localized error messages, it is used as the error message.
121      *
122      * @param errCode Error code to localize
123      * @param arg1 First argument for parametric replacement
124      * @param arg2 Second argument for parametric replacement
125      * @param arg3 Third argument for parametric replacement
126      * @param arg4 Fourth argument for parametric replacement
127      *
128      * @return Localized error message
129      */
130     public static String getMessage(String errCode, String arg1, String arg2,
131                                     String arg3, String arg4) {
132         return getMessage(errCode, new Object[]{arg1, arg2, arg3, arg4});
133     }
134 
135     /*
136      * Returns the localized error message corresponding to the given error
137      * code.
138      *
139      * If the given error code is not defined in the resource bundle for
140      * localized error messages, it is used as the error message.
141      *
142      * @param errCode Error code to localize
143      * @param args Arguments for parametric replacement
144      *
145      * @return Localized error message
146      */
147     public static String getMessage(String errCode, Object[] args) {
148         String errMsg = errCode;
149         try {
150             errMsg = bundle.getString(errCode);
151             if (args != null) {
152                 MessageFormat formatter = new MessageFormat(errMsg);
153                 errMsg = formatter.format(args);
154             }
155         } catch (MissingResourceException e) {
156         }
157 
158         return errMsg;
159     }
160 }