KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > gui > resource > ResourceManager


1 /*
2
3    Copyright 2000-2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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.batik.util.gui.resource;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.MissingResourceException JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 /**
27  * This class offers convenience methods to decode
28  * resource bundle entries
29  *
30  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
31  * @version $Id: ResourceManager.java,v 1.4 2004/08/18 07:15:56 vhardy Exp $
32  */

33 public class ResourceManager {
34     /**
35      * The managed resource bundle
36      */

37     protected ResourceBundle JavaDoc bundle;
38
39     /**
40      * Creates a new resource manager
41      * @param rb a resource bundle
42      */

43     public ResourceManager(ResourceBundle JavaDoc rb) {
44     bundle = rb;
45     }
46
47     /**
48      * Returns the string that is mapped with the given key
49      * @param key a key in the resource bundle
50      * @throws MissingResourceException if key is not the name of a resource
51      */

52     public String JavaDoc getString(String JavaDoc key)
53     throws MissingResourceException JavaDoc {
54     return bundle.getString(key);
55     }
56
57     /**
58      * Returns the tokens that compose the string mapped
59      * with the given key. Delimiters (" \t\n\r\f") are not returned.
60      * @param key a key of the resource bundle
61      * @throws MissingResourceException if key is not the name of a resource
62      */

63     public List JavaDoc getStringList(String JavaDoc key)
64     throws MissingResourceException JavaDoc {
65         return getStringList(key, " \t\n\r\f", false);
66     }
67     /**
68      * Returns the tokens that compose the string mapped
69      * with the given key. Delimiters are not returned.
70      * @param key a key of the resource bundle
71      * @param delim the delimiters of the tokens
72      * @throws MissingResourceException if key is not the name of a resource
73      */

74     public List JavaDoc getStringList(String JavaDoc key, String JavaDoc delim)
75     throws MissingResourceException JavaDoc {
76         return getStringList(key, delim, false);
77     }
78
79     /**
80      * Returns the tokens that compose the string mapped
81      * with the given key
82      * @param key a key of the resource bundle
83      * @param delim the delimiters of the tokens
84      * @param returnDelims if true, the delimiters are returned in the list
85      * @throws MissingResourceException if key is not the name of a resource
86      */

87     public List JavaDoc getStringList(String JavaDoc key, String JavaDoc delim, boolean returnDelims)
88     throws MissingResourceException JavaDoc {
89         List JavaDoc result = new ArrayList JavaDoc();
90         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(getString(key),
91                                                      delim,
92                                                      returnDelims);
93         while (st.hasMoreTokens()) {
94             result.add(st.nextToken());
95         }
96         return result;
97     }
98
99     /**
100      * Returns the boolean mapped with the given key
101      * @param key a key of the resource bundle
102      * @throws MissingResourceException if key is not the name of a resource
103      * @throws ResourceFormatException if the resource is malformed
104      */

105     public boolean getBoolean(String JavaDoc key)
106     throws MissingResourceException JavaDoc, ResourceFormatException {
107     String JavaDoc b = getString(key);
108
109     if (b.equals("true")) {
110         return true;
111     } else if (b.equals("false")) {
112         return false;
113     } else {
114         throw new ResourceFormatException("Malformed boolean",
115                                               bundle.getClass().getName(),
116                                               key);
117     }
118     }
119
120     /**
121      * Returns the integer mapped with the given string
122      * @param key a key of the resource bundle
123      * @throws MissingResourceException if key is not the name of a resource
124      * @throws ResourceFormatException if the resource is malformed
125      */

126     public int getInteger(String JavaDoc key)
127     throws MissingResourceException JavaDoc, ResourceFormatException {
128     String JavaDoc i = getString(key);
129     
130     try {
131         return Integer.parseInt(i);
132     } catch (NumberFormatException JavaDoc e) {
133         throw new ResourceFormatException("Malformed integer",
134                                               bundle.getClass().getName(),
135                                               key);
136     }
137     }
138
139     public int getCharacter(String JavaDoc key)
140         throws MissingResourceException JavaDoc, ResourceFormatException {
141         String JavaDoc s = getString(key);
142         
143         if(s == null || s.length() == 0){
144             throw new ResourceFormatException("Malformed character",
145                                               bundle.getClass().getName(),
146                                               key);
147         }
148
149         return s.charAt(0);
150     }
151
152 }
153
Popular Tags