001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.jasper.compiler;
019:
020: import java.text.MessageFormat;
021: import java.util.MissingResourceException;
022: import java.util.ResourceBundle;
023:
024: /**
025: * Class responsible for converting error codes to corresponding localized
026: * error messages.
027: *
028: * @author Jan Luehe
029: */
030: public class Localizer {
031:
032: private static ResourceBundle bundle = null;
033:
034: static {
035: try {
036: bundle = ResourceBundle
037: .getBundle("org.apache.jasper.resources.LocalStrings");
038: } catch (Throwable t) {
039: t.printStackTrace();
040: }
041: }
042:
043: /*
044: * Returns the localized error message corresponding to the given error
045: * code.
046: *
047: * If the given error code is not defined in the resource bundle for
048: * localized error messages, it is used as the error message.
049: *
050: * @param errCode Error code to localize
051: *
052: * @return Localized error message
053: */
054: public static String getMessage(String errCode) {
055: String errMsg = errCode;
056: try {
057: errMsg = bundle.getString(errCode);
058: } catch (MissingResourceException e) {
059: }
060: return errMsg;
061: }
062:
063: /*
064: * Returns the localized error message corresponding to the given error
065: * code.
066: *
067: * If the given error code is not defined in the resource bundle for
068: * localized error messages, it is used as the error message.
069: *
070: * @param errCode Error code to localize
071: * @param arg Argument for parametric replacement
072: *
073: * @return Localized error message
074: */
075: public static String getMessage(String errCode, String arg) {
076: return getMessage(errCode, new Object[] { arg });
077: }
078:
079: /*
080: * Returns the localized error message corresponding to the given error
081: * code.
082: *
083: * If the given error code is not defined in the resource bundle for
084: * localized error messages, it is used as the error message.
085: *
086: * @param errCode Error code to localize
087: * @param arg1 First argument for parametric replacement
088: * @param arg2 Second argument for parametric replacement
089: *
090: * @return Localized error message
091: */
092: public static String getMessage(String errCode, String arg1,
093: String arg2) {
094: return getMessage(errCode, new Object[] { arg1, arg2 });
095: }
096:
097: /*
098: * Returns the localized error message corresponding to the given error
099: * code.
100: *
101: * If the given error code is not defined in the resource bundle for
102: * localized error messages, it is used as the error message.
103: *
104: * @param errCode Error code to localize
105: * @param arg1 First argument for parametric replacement
106: * @param arg2 Second argument for parametric replacement
107: * @param arg3 Third argument for parametric replacement
108: *
109: * @return Localized error message
110: */
111: public static String getMessage(String errCode, String arg1,
112: String arg2, String arg3) {
113: return getMessage(errCode, new Object[] { arg1, arg2, arg3 });
114: }
115:
116: /*
117: * Returns the localized error message corresponding to the given error
118: * code.
119: *
120: * If the given error code is not defined in the resource bundle for
121: * localized error messages, it is used as the error message.
122: *
123: * @param errCode Error code to localize
124: * @param arg1 First argument for parametric replacement
125: * @param arg2 Second argument for parametric replacement
126: * @param arg3 Third argument for parametric replacement
127: * @param arg4 Fourth argument for parametric replacement
128: *
129: * @return Localized error message
130: */
131: public static String getMessage(String errCode, String arg1,
132: String arg2, String arg3, String arg4) {
133: return getMessage(errCode, new Object[] { arg1, arg2, arg3,
134: arg4 });
135: }
136:
137: /*
138: * Returns the localized error message corresponding to the given error
139: * code.
140: *
141: * If the given error code is not defined in the resource bundle for
142: * localized error messages, it is used as the error message.
143: *
144: * @param errCode Error code to localize
145: * @param args Arguments for parametric replacement
146: *
147: * @return Localized error message
148: */
149: public static String getMessage(String errCode, Object[] args) {
150: String errMsg = errCode;
151: try {
152: errMsg = bundle.getString(errCode);
153: if (args != null) {
154: MessageFormat formatter = new MessageFormat(errMsg);
155: errMsg = formatter.format(args);
156: }
157: } catch (MissingResourceException e) {
158: }
159:
160: return errMsg;
161: }
162: }
|