Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LocalizedMessage.java « i18n « bouncycastle « org « jdk1.2 « main « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c1c4c58a15dd2f67b45747a716b3f883f283ed57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package org.bouncycastle.i18n;

import org.bouncycastle.i18n.filter.Filter;
import org.bouncycastle.i18n.filter.TrustedInput;
import org.bouncycastle.i18n.filter.UntrustedInput;
import org.bouncycastle.i18n.filter.UntrustedUrlInput;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.Format;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.TimeZone;

public class LocalizedMessage 
{
    protected static final int NO_FILTER = 0;
    protected static final int FILTER = 1;
    protected static final int FILTER_URL = 2;

    protected String id;
    protected String resource;
    
    // ISO-8859-1 is the default encoding
    public static final String DEFAULT_ENCODING = "ISO-8859-1";
    protected String encoding = DEFAULT_ENCODING;
    
    protected FilteredArguments arguments;
    protected FilteredArguments extraArgs = null;
    
    protected Filter filter = null;
    
    protected ClassLoader loader = null;
    
    /**
     * Constructs a new LocalizedMessage using <code>resource</code> as the base name for the 
     * RessourceBundle and <code>id</code> as the message bundle id the resource file. 
     * @param resource base name of the resource file 
     * @param id the id of the corresponding bundle in the resource file
     * @throws NullPointerException if <code>resource</code> or <code>id</code> is <code>null</code>
     */
    public LocalizedMessage(String resource,String id) throws NullPointerException
    {
        if (resource == null || id == null)
        {
            throw new NullPointerException();
        }
        this.id = id;
        this.resource = resource;
        arguments = new FilteredArguments();
    }
    
    /**
     * Constructs a new LocalizedMessage using <code>resource</code> as the base name for the 
     * RessourceBundle and <code>id</code> as the message bundle id the resource file. 
     * @param resource base name of the resource file 
     * @param id the id of the corresponding bundle in the resource file
     * @param encoding the encoding of the resource file
     * @throws NullPointerException if <code>resource</code> or <code>id</code> is <code>null</code>
     * @throws UnsupportedEncodingException if the encoding is not supported
     */
    public LocalizedMessage(String resource,String id, String encoding) throws NullPointerException, UnsupportedEncodingException
    {
        if (resource == null || id == null)
        {
            throw new NullPointerException();
        }
        this.id = id;
        this.resource = resource;
        arguments = new FilteredArguments();
        this.encoding = encoding;
    }
    
    /**
     * Constructs a new LocalizedMessage using <code>resource</code> as the base name for the 
     * RessourceBundle and <code>id</code> as the message bundle id the resource file. 
     * @param resource base name of the resource file 
     * @param id the id of the corresponding bundle in the resource file
     * @param arguments an array containing the arguments for the message
     * @throws NullPointerException if <code>resource</code> or <code>id</code> is <code>null</code>
     */
    public LocalizedMessage(String resource, String id, Object[] arguments) throws NullPointerException
    {
        if (resource == null || id == null || arguments == null)
        {
            throw new NullPointerException();
        }
        this.id = id;
        this.resource = resource;
        this.arguments = new FilteredArguments(arguments);
    }
    
    /**
     * Constructs a new LocalizedMessage using <code>resource</code> as the base name for the 
     * RessourceBundle and <code>id</code> as the message bundle id the resource file. 
     * @param resource base name of the resource file 
     * @param id the id of the corresponding bundle in the resource file
     * @param encoding the encoding of the resource file
     * @param arguments an array containing the arguments for the message
     * @throws NullPointerException if <code>resource</code> or <code>id</code> is <code>null</code>
     * @throws UnsupportedEncodingException if the encoding is not supported
     */
    public LocalizedMessage(String resource, String id, String encoding, Object[] arguments) throws NullPointerException, UnsupportedEncodingException
    {
        if (resource == null || id == null || arguments == null)
        {
            throw new NullPointerException();
        }
        this.id = id;
        this.resource = resource;
        this.arguments = new FilteredArguments(arguments);
        this.encoding = encoding;
    }
    
    /**
     * Reads the entry <code>id + "." + key</code> from the resource file and returns a 
     * formated message for the given Locale and TimeZone.
     * @param key second part of the entry id
     * @param loc the used {@link Locale}
     * @param timezone the used {@link TimeZone}
     * @return a Strng containing the localized message
     * @throws MissingEntryException if the resource file is not available or the entry does not exist.
     */
    public String getEntry(String key,Locale loc, TimeZone timezone) throws MissingEntryException
    {
        String entry = id;
        if (key != null)
        {
            entry += "." + key;
        }
        
        try
        {
            ResourceBundle bundle;
            if (loader == null)
            {
                bundle = ResourceBundle.getBundle(resource,loc);
            }
            else
            {
                bundle = ResourceBundle.getBundle(resource, loc);
            }
            String result = bundle.getString(entry);
            if (!encoding.equals(DEFAULT_ENCODING))
            {
                result = new String(result.getBytes(DEFAULT_ENCODING), encoding);
            }
            if (!arguments.isEmpty())
            {
                result = formatWithTimeZone(result,arguments.getFilteredArgs(loc),loc,timezone);
            }
            result = addExtraArgs(result, loc);
            return result;
        }
        catch (MissingResourceException mre)
        {
            throw new MissingEntryException("Can't find entry " + entry + " in resource file " + resource + ".",
                    resource,
                    entry,
                    loc,
                    loader != null ? loader : this.getClassLoader()); 
        }
        catch (UnsupportedEncodingException use)
        {
            // should never occur - cause we already test this in the constructor
            throw new RuntimeException(use.toString());
        }
    }
    
    protected String formatWithTimeZone(
            String template,
            Object[] arguments, 
            Locale locale,
            TimeZone timezone) 
    {
        MessageFormat mf = new MessageFormat(" ");
        mf.setLocale(locale);
        mf.applyPattern(template);
        if (!timezone.equals(TimeZone.getDefault())) 
        {
            Format[] formats = mf.getFormats();
            for (int i = 0; i < formats.length; i++) 
            {
                if (formats[i] instanceof DateFormat) 
                {
                    DateFormat temp = (DateFormat) formats[i];
                    temp.setTimeZone(timezone);
                    mf.setFormat(i,temp);
                }
            }
        }
        return mf.format(arguments);
    }
    
    protected String addExtraArgs(String msg, Locale locale)
    {
        if (extraArgs != null)
        {
            StringBuffer sb = new StringBuffer(msg);
            Object[] filteredArgs = extraArgs.getFilteredArgs(locale);
            for (int i = 0; i < filteredArgs.length; i++)
            {
                sb.append(filteredArgs[i]);
            }
            msg = sb.toString();
        }
        return msg;
    }
    
    /**
     * Sets the {@link Filter} that is used to filter the arguments of this message
     * @param filter the {@link Filter} to use. <code>null</code> to disable filtering.
     */
    public void setFilter(Filter filter)
    {
        arguments.setFilter(filter);
        if (extraArgs != null)
        {
            extraArgs.setFilter(filter);
        }
        this.filter = filter;
    }
    
    /**
     * Returns the current filter.
     * @return the current filter
     */
    public Filter getFilter()
    {
        return filter;
    }
    
    /**
     * Set the {@link ClassLoader} which loads the resource files. If it is set to <code>null</code>
     * then the default {@link ClassLoader} is used. 
     * @param loader the {@link ClassLoader} which loads the resource files
     */
    public void setClassLoader(ClassLoader loader)
    {
        this.loader = loader;
    }
    
    /**
     * Returns the {@link ClassLoader} which loads the resource files or <code>null</code>
     * if the default ClassLoader is used.
     * @return the {@link ClassLoader} which loads the resource files
     */
    public ClassLoader getClassLoader()
    {
        return loader;
    }
    
    /**
     * Returns the id of the message in the resource bundle.
     * @return the id of the message
     */
    public String getId()
    {
        return id;
    }
    
    /**
     * Returns the name of the resource bundle for this message
     * @return name of the resource file
     */
    public String getResource()
    {
        return resource;
    }
    
    /**
     * Returns an <code>Object[]</code> containing the message arguments.
     * @return the message arguments
     */
    public Object[] getArguments()
    {
        return arguments.getArguments();
    }
    
    /**
     * 
     * @param extraArg
     */
    public void setExtraArgument(Object extraArg)
    {
        setExtraArguments(new Object[] {extraArg});
    }
    
    /**
     * 
     * @param extraArgs
     */
    public void setExtraArguments(Object[] extraArgs)
    {
        if (extraArgs != null)
        {
            this.extraArgs = new FilteredArguments(extraArgs);
            this.extraArgs.setFilter(filter);
        }
        else
        {
            this.extraArgs = null;
        }
    }
    
    /**
     * 
     * @return
     */
    public Object[] getExtraArgs()
    {
        return (extraArgs == null) ? null : extraArgs.getArguments();
    }
    
    protected class FilteredArguments
    {
        
        protected Filter filter = null;
        
        protected boolean[] isLocaleSpecific;
        protected int[] argFilterType;
        protected Object[] arguments;
        protected Object[] unpackedArgs;
        protected Object[] filteredArgs;
        
        FilteredArguments()
        {
            this(new Object[0]);
        }
        
        FilteredArguments(Object[] args)
        {
            this.arguments = args;
            this.unpackedArgs = new Object[args.length];
            this.filteredArgs = new Object[args.length];
            this.isLocaleSpecific = new boolean[args.length];
            this.argFilterType = new int[args.length];
            for (int i = 0; i < args.length; i++)
            {
                if (args[i] instanceof TrustedInput)
                {
                    this.unpackedArgs[i] = ((TrustedInput) args[i]).getInput();
                    argFilterType[i] = NO_FILTER;
                }
                else if (args[i] instanceof UntrustedInput)
                {
                    this.unpackedArgs[i] = ((UntrustedInput) args[i]).getInput();
                    if (args[i] instanceof UntrustedUrlInput)
                    {
                        argFilterType[i] = FILTER_URL;
                    }
                    else
                    {
                        argFilterType[i] = FILTER;
                    }
                }
                else
                {
                    this.unpackedArgs[i] = args[i];
                    argFilterType[i] = FILTER;
                }
                
                // locale specific
                this.isLocaleSpecific[i] = (this.unpackedArgs[i] instanceof LocaleString);
            }
        }
        
        public boolean isEmpty()
        {
            return unpackedArgs.length == 0;
        }
        
        public Object[] getArguments()
        {
            return arguments;
        }
        
        public Object[] getFilteredArgs(Locale locale)
        {
            Object[] result = new Object[unpackedArgs.length];
            for (int i = 0; i < unpackedArgs.length; i++)
            {
                Object arg;
                if (filteredArgs[i] != null)
                {
                    arg = filteredArgs[i];
                }
                else
                {
                    arg = unpackedArgs[i];
                    if (isLocaleSpecific[i])
                    {
                        // get locale
                        arg = ((LocaleString) arg).getLocaleString(locale);
                        arg = filter(argFilterType[i], arg);
                    }
                    else
                    {
                        arg = filter(argFilterType[i], arg);
                        filteredArgs[i] = arg;
                    }
                }
                result[i] = arg;
            }
            return result;
        }
        
        private Object filter(int type, Object obj)
        {
            if (filter != null)
            {
                Object o = (null == obj) ? "null" : obj;
                switch (type)
                {
                case NO_FILTER:
                    return o;
                case FILTER:
                    return filter.doFilter(o.toString());
                case FILTER_URL:
                    return filter.doFilterUrl(o.toString());
                default:
                    return null;
                }
            }
            else
            {
                return obj;
            }
        }

        public Filter getFilter()
        {
            return filter;
        }

        public void setFilter(Filter filter)
        {
            if (filter != this.filter)
            {
                for (int i = 0; i < unpackedArgs.length; i++)
                {
                    filteredArgs[i] = null;
                }
            }
            this.filter = filter;
        }
        
    }
    
    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("Resource: \"").append(resource);
        sb.append("\" Id: \"").append(id).append("\"");
        sb.append(" Arguments: ").append(arguments.getArguments().length).append(" normal, ")
        .append(extraArgs.getArguments().length).append(" extra");
        sb.append(" Encoding: ").append(encoding);
        sb.append(" ClassLoader: ").append(loader);
        return sb.toString();
    }

}