KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > language > LanguageCategory


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20
21 package com.sslexplorer.language;
22
23
24
25 import java.io.IOException JavaDoc;
26
27 import java.io.InputStream JavaDoc;
28
29 import java.net.URL JavaDoc;
30
31 import java.util.Properties JavaDoc;
32
33
34
35 /**
36
37  * Encapsulates an I18N category. A single category maps to a single
38
39  * resources file in a particular name space. For example, the resource
40
41  * /com/sslexplorer/agent/ApplicationResources.properties maps to the category
42
43  * with the ID of <i>com.sslexplorer.agent</i>.
44
45  * <p>
46
47  * Each category must provide have an ID as a dotted namespace,
48
49  * the base URL the resource came from (i.e. the jar or directory
50
51  * URL), a path for the resource inside the base resource, a name
52
53  * description and location.
54
55  * <p>
56
57  * The name and description elements will be in <b>English</b> as they
58
59  * are loaded from the default English translation that is included with
60
61  * the core. If you need localised category names you will have to
62
63  * retrieve then yourself from the appropriate message bundle.
64
65  * <p>
66
67  * The location determines what component of SSL-Explorer the
68
69  * category is used in. It may currently be one of
70
71  * {@link #LOCATION_CORE}, {@link #LOCATION_AGENT} or
72
73  * {@link #LOCATION_LAUNCHER}.
74
75  * <p>
76
77  * The categories available are determined by scanning the
78
79  * classpath for all default english ApplicationResources.properties
80
81  * resources. Use {@link LanguagePackManager#getCategories()} to retrieve
82
83  * this list of all available categories.
84
85  *
86
87  * @author Brett Smith <a HREF="mailto:brett@3sp.com">&lt;brett@3sp.com&gt;</a>
88
89  */

90
91 public class LanguageCategory {
92
93     
94
95     /**
96
97      * The message key to use to retrieve the category name
98
99      */

100
101     public final static String JavaDoc NAME_KEY = "sslexplorer.resourceBundle.name";
102
103     
104
105     /**
106
107      * The message key to use to retrieve the category description
108
109      */

110
111     public final static String JavaDoc DESCRIPTION_KEY = "sslexplorer.resourceBundle.description";
112
113     
114
115     /**
116
117      * The message key to use to retrieve the category location
118
119      */

120
121     public final static String JavaDoc LOCATION_KEY = "sslexplorer.resourceBundle.location";
122
123     
124
125     /**
126
127      * The message key to use to retrieve the extension id
128
129      */

130
131     public final static String JavaDoc EXTENSION_ID_KEY = "sslexplorer.resourceBundle.extensionId";
132
133     
134
135     /**
136
137      * Language category is in the core
138
139      */

140
141     public final static String JavaDoc EXTENSION_CORE = "core";
142
143     
144
145     /**
146
147      * Language category is for the core
148
149      */

150
151     public final static int LOCATION_CORE = 0;
152
153     
154
155     /**
156
157      * Language category is for the agent
158
159      */

160
161     public final static int LOCATION_AGENT = 1;
162
163     
164
165     /**
166
167      * Language category is for the launcher
168
169      */

170
171     public final static int LOCATION_LAUNCHER = 2;
172
173     
174
175     // Private instance variables
176

177     private String JavaDoc id;
178
179     private String JavaDoc name;
180
181     private String JavaDoc description;
182
183     private String JavaDoc extensionId;
184
185     private URL JavaDoc base;
186
187     private String JavaDoc path;
188
189     private int location;
190
191     
192
193     /**
194
195      * Constructor for creating a category given a stream in property file format
196
197      * (name/value pairs) that contains the name, description and location properties.
198
199      *
200
201      * @param in input stream for property file
202
203      * @param base URL of resource that contains category (i.e. .jar or directory file URL)
204
205      * @param path path of category (path separated by / characters)
206
207      * @param id id as a dotted namespace
208
209      * @throws IOException if stream cannot be read
210
211      */

212
213     public LanguageCategory(InputStream JavaDoc in, URL JavaDoc base, String JavaDoc path, String JavaDoc id) throws IOException JavaDoc {
214
215         Properties JavaDoc resources = new Properties JavaDoc();
216
217         resources.load(in);
218
219         name = resources.getProperty(LanguageCategory.NAME_KEY, id);
220
221         description = resources.getProperty(LanguageCategory.DESCRIPTION_KEY, id);
222
223         extensionId = resources.getProperty(LanguageCategory.EXTENSION_ID_KEY, EXTENSION_CORE);
224
225         try {
226
227             location = Integer.parseInt(resources.getProperty(LanguageCategory.LOCATION_KEY, String.valueOf(LanguageCategory.LOCATION_CORE)));
228
229         }
230
231         catch(NumberFormatException JavaDoc nfe) {
232
233             location = LanguageCategory.LOCATION_CORE;
234
235         }
236
237         this.base = base;
238
239         this.path = path;
240
241         this.id = id;
242
243     }
244
245     
246
247     /**
248
249      * Constructor
250
251      *
252
253      * @param base URL of resource that contains category (i.e. .jar or directory file URL)
254
255      * @param path path of category (path separated by / characters)
256
257      * @param id id as a dotted namespace
258
259      * @param name name of category
260
261      * @param description description of category
262
263      * @param extensionId id of the category's extension
264
265      * @param location location of category. May be one of {@link #LOCATION_CORE}, {@link #LOCATION_AGENT} or {@link #LOCATION_LAUNCHER}.
266
267      */

268
269     public LanguageCategory(URL JavaDoc base, String JavaDoc path, String JavaDoc id, String JavaDoc name, String JavaDoc description, String JavaDoc extensionId, int location) {
270
271         super();
272
273         this.base = base;
274
275         this.path = path;
276
277         this.id = id;
278
279         this.name = name;
280
281         this.description = description;
282
283         this.extensionId = extensionId;
284
285         this.location = location;
286
287     }
288
289     
290
291     /**
292
293      * Get the location this category is to be used. This may
294
295      * be one of {@link #LOCATION_CORE}, {@link #LOCATION_AGENT} or {@link #LOCATION_LAUNCHER}.
296
297      *
298
299      * @return location
300
301      */

302
303     public int getLocation() {
304
305         return location;
306
307     }
308
309     
310
311     /**
312
313      * Set the location this category is to be used. This may
314
315      * be one of {@link #LOCATION_CORE}, {@link #LOCATION_AGENT} or {@link #LOCATION_LAUNCHER}.
316
317      *
318
319      * @param location location
320
321      * @throws IllegalArgumentException on invalid location
322
323      */

324
325     public void setLocation(int location) {
326
327         if(location == LOCATION_CORE || location == LOCATION_AGENT || location == LOCATION_LAUNCHER) {
328
329             this.location = location;
330
331         }
332
333         else {
334
335             throw new IllegalArgumentException JavaDoc("Illegal location: "+ location);
336
337         }
338
339     }
340
341     
342
343     /**
344
345      * Get the <b>English</b> description of this category.
346
347      *
348
349      * @return english category description
350
351      */

352
353     public String JavaDoc getDescription() {
354
355         return description;
356
357     }
358
359     
360
361     /**
362
363      * Set the <b>English</b> description of this category.
364
365      *
366
367      * @param description english category description
368
369      */

370
371     public void setDescription(String JavaDoc description) {
372
373         this.description = description;
374
375     }
376
377     
378
379     /**
380
381      * Get the ID of this category. This will be in dotted
382
383      * namespace format. For example, <i>com.sslexplorer.agent</i>.
384
385      *
386
387      * @return category ID
388
389      */

390
391     public String JavaDoc getId() {
392
393         return id;
394
395     }
396
397
398
399     /**
400
401      * Set the ID of this category. This will be in dotted
402
403      * namespace format. For example, <i>com.sslexplorer.agent</i>.
404
405      *
406
407      * @param id category ID
408
409      */

410
411     public void setId(String JavaDoc id) {
412
413         this.id = id;
414
415     }
416
417     
418
419     /**
420
421      * Get the <b>English</b> name of this category.
422
423      *
424
425      * @return english category name
426
427      */

428
429     public String JavaDoc getName() {
430
431         return name;
432
433     }
434
435     
436
437     /**
438
439      * Set the <b>English</b> name of this category.
440
441      *
442
443      * @param name english category name
444
445      */

446
447     public void setName(String JavaDoc name) {
448
449         this.name = name;
450
451     }
452
453
454
455     /**
456
457      * Get the URL of the resource this category was discovered
458
459      * in. This will likely either be a file URL pointing to a
460
461      * JAR or a file URL pointing to a directory.
462
463      *
464
465      * @return base URL of resource category discovered in
466
467      */

468
469     public URL JavaDoc getBase() {
470
471         return base;
472
473     }
474
475
476
477     /**
478
479      * Set the URL of the resource this category was discovered
480
481      * in. This will likely either be a file URL pointing to a
482
483      * JAR or a file URL pointing to a directory.
484
485      *
486
487      * @param base base URL of resource category discovered in
488
489      */

490
491     public void setBase(URL JavaDoc base) {
492
493         this.base = base;
494
495     }
496
497
498
499     /**
500
501      * Get the path of the resource inside the base resource this
502
503      * category was discovered in. For example, a category with
504
505      * an ID of com.sslexplorer.agent would have been found in
506
507      * /com/sslexplorer/agent.
508
509      *
510
511      * @return path
512
513      */

514
515     public String JavaDoc getPath() {
516
517         return path;
518
519     }
520
521
522
523
524
525     /**
526
527      * Set the path of the resource inside the base resource this
528
529      * category was discovered in. For example, a category with
530
531      * an ID of com.sslexplorer.agent would have been found in
532
533      * /com/sslexplorer/agent.
534
535      *
536
537      * @param path path
538
539      */

540
541     public void setPath(String JavaDoc path) {
542
543         this.path = path;
544
545     }
546
547
548
549     
550
551     /**
552
553      * Get the id of the extension of this
554
555      * category was discovered in. For example, a category with extensionId
556
557      * sslexplorer-enterprise-core would have been found in
558
559      * the core extension.
560
561      *
562
563      * @return extensionId
564
565      */

566
567     public String JavaDoc getExtensionId() {
568
569         return extensionId;
570
571     }
572
573
574
575     
576
577     /**
578
579      * Set the id of the extension of this
580
581      * category was discovered in. For example, a category with extensionId
582
583      * sslexplorer-enterprise-core would have been found in
584
585      * the core extension.
586
587      *
588
589      * @param extensionId extension id
590
591      */

592
593     public void setExtensionId(String JavaDoc extensionId) {
594
595         this.extensionId = extensionId;
596
597     }
598
599
600
601     
602
603 }
604
605
Popular Tags