Debian Codegraph @

jdbm1/src/main/java/jdbm/I18n.java

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. 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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20
21 package jdbm;
22
23
24 import java.text.MessageFormat;
25 import java.util.MissingResourceException;
26 import java.util.ResourceBundle;
27
28
29 /**
30 * Provides i18n handling of error codes.
31 * About formatting see also {@link MessageFormat}
32 *
33 * @author <a href="mailto:[email protected]">Apache Directory Project</a>
34 */
35 public enum I18n
36 {
37 ERR_513("ERR_513"),
38 ERR_514("ERR_514"),
39 ERR_515("ERR_515"),
40 ERR_516("ERR_516"),
41 ERR_517("ERR_517"),
42 ERR_518("ERR_518"),
43 ERR_519("ERR_519"),
44 // ERR_520( "ERR_520" ),
45 // ERR_521( "ERR_521" ),
46 ERR_522("ERR_522"),
47 ERR_523("ERR_523"),
48 ERR_524("ERR_524"),
49 ERR_525("ERR_525"),
50 ERR_526("ERR_526"),
51 ERR_527("ERR_527"),
52 ERR_528("ERR_528"),
53 ERR_529("ERR_529"),
54 // ERR_530( "ERR_530" ),
55 ERR_531("ERR_531"),
56 ERR_532("ERR_532"),
57 ERR_533("ERR_533"),
58 ERR_534("ERR_534"),
59 ERR_535("ERR_535"),
60 ERR_536("ERR_536"),
61 ERR_537("ERR_537"),
62 ERR_538("ERR_538"),
63 ERR_539_BAD_BLOCK_ID("ERR_539_BAD_BLOCK_ID"),
64 ERR_540("ERR_540"),
65 ERR_541("ERR_541"),
66 ERR_542("ERR_542"),
67 ERR_543("ERR_543"),
68 ERR_544("ERR_544"),
69 ERR_545("ERR_545"),
70 ERR_546("ERR_546"),
71 ERR_547("ERR_547"),
72 ERR_548("ERR_548"),
73 ERR_549("ERR_549"),
74 ERR_550("ERR_550"),
75 ERR_551("ERR_551"),
76 ERR_552("ERR_552"),
77 ERR_553("ERR_553"),
78 ERR_554("ERR_554"),
79 ERR_555("ERR_555"),
80 ERR_556("ERR_556"),
81 ERR_557("ERR_557"),
82 ERR_558("ERR_558"),
83 ERR_559("ERR_559"),
84 ERR_560("ERR_560"),
85 ERR_561("ERR_561"),
86 ERR_562("ERR_562"),
87 ERR_563("ERR_563"),
88 ERR_564("ERR_564"),
89 ERR_565("ERR_565"),
90 ERR_566("ERR_566"),
91 ERR_567("ERR_567");
92
93 private static ResourceBundle errBundle = ResourceBundle
94 .getBundle( "jdbm.errors" );
95
96 private final static ResourceBundle msgBundle = ResourceBundle
97 .getBundle( "jdbm/messages" );
98
99 /** The error code */
100 private String errorCode;
101
102
103 /**
104 * Creates a new instance of I18n.
105 */
106 private I18n( String errorCode )
107 {
108 this.errorCode = errorCode;
109 }
110
111
112 /**
113 * @return the errorCode
114 */
115 public String getErrorCode()
116 {
117 return errorCode;
118 }
119
120
121 /**
122 *
123 * Translate an error code with argument(s)
124 *
125 * @param err The error code
126 * @param args The argument(s)
127 * @return The translate error code
128 */
129 public static String err( I18n err, Object... args )
130 {
131 try
132 {
133 return err + " " + MessageFormat.format( errBundle.getString( err.getErrorCode() ), args );
134 }
135 catch ( Exception e )
136 {
137 StringBuffer sb = new StringBuffer();
138 boolean comma = false;
139
140 for ( Object obj : args )
141 {
142 if ( comma )
143 {
144 sb.append( "," );
145 }
146 else
147 {
148 comma = true;
149 }
150
151 sb.append( obj );
152 }
153
154 return err + " (" + sb.toString() + ")";
155 }
156 }
157
158
159 /**
160 *
161 * Translate a message with argument(s)
162 *
163 * @param msg The message
164 * @param args The argument(s)
165 * @return The translated message
166 */
167 public static String msg( String msg, Object... args )
168 {
169 try
170 {
171 return MessageFormat.format( msgBundle.getString( msg ), args );
172 }
173 catch ( MissingResourceException mre )
174 {
175 try
176 {
177 return MessageFormat.format( msg, args );
178 }
179 catch ( Exception e )
180 {
181 StringBuffer sb = new StringBuffer();
182 boolean comma = false;
183
184 for ( Object obj : args )
185 {
186 if ( comma )
187 {
188 sb.append( "," );
189 }
190 else
191 {
192 comma = true;
193 }
194
195 sb.append( obj );
196 }
197
198 return msg + " (" + sb.toString() + ")";
199 }
200 }
201 }
202 }