Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
84   284   46   4.2
42   189   0.55   10
20     2.3  
2    
 
 
  ITrackerResourceBundle       Line # 35 84 46 0% 0.0
  ITrackerResourceBundle.DirtyKey       Line # 282 0 0 - -1.0
 
No Tests
 
1    /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10    * the Free Software Foundation; either version 2 of the License, or
11    * (at your option) any later version.
12    *
13    * This program is distributed in the hope that it will be useful,
14    * but WITHOUT ANY WARRANTY; without even the implied warranty of
15    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16    * GNU General Public License for more details.
17    */
18   
19    package org.itracker.core.resources;
20   
21    import java.util.Collections;
22    import java.util.Enumeration;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Locale;
26    import java.util.MissingResourceException;
27    import java.util.ResourceBundle;
28    import java.util.Set;
29    import java.util.TreeSet;
30   
31    import org.apache.log4j.Logger;
32    import org.itracker.model.Language;
33    import org.itracker.services.exceptions.ITrackerDirtyResourceException;
34   
 
35    public class ITrackerResourceBundle extends ResourceBundle {
36   
37    private static final Logger log = Logger
38    .getLogger(ITrackerResourceBundle.class);
39    private final HashMap<String, Object> data = new HashMap<String, Object>();
40    /**
41    * TODO should dataArray be re-factored out?
42    */
43    private Object[][] dataArray = null;
44    private ResourceBundle propertiesBundle;
45   
 
46  0 toggle static ResourceBundle loadBundle() {
47  0 return new ITrackerResourceBundle();
48    }
49   
 
50  0 toggle static ResourceBundle loadBundle(Locale locale) {
51  0 return new ITrackerResourceBundle(locale);
52    }
53   
 
54  0 toggle static ResourceBundle loadBundle(Locale locale, Object[][] data) {
55  0 return new ITrackerResourceBundle(locale, data);
56    }
57   
 
58  0 toggle static ResourceBundle loadBundle(Locale locale, List<Language> items) {
59  0 return new ITrackerResourceBundle(locale, items);
60    }
61   
 
62  0 toggle private ITrackerResourceBundle() {
63  0 super.setParent(ResourceBundle.getBundle(
64    ITrackerResources.RESOURCE_BUNDLE_NAME, new Locale(
65    ITrackerResources.getDefaultLocale())));
66    }
67   
68    /**
69    * @param locale
70    */
 
71  0 toggle private ITrackerResourceBundle(Locale locale) {
72  0 if (null == locale) {
73  0 locale = ITrackerResources.getLocale(ITrackerResources
74    .getDefaultLocale());
75    }
76  0 this.propertiesBundle = ResourceBundle.getBundle(
77    ITrackerResources.RESOURCE_BUNDLE_NAME, locale);
78   
79  0 if (!locale.equals(ITrackerResources
80    .getLocale(ITrackerResources.BASE_LOCALE))) {
81  0 if (locale.getCountry().length() > 0) {
82  0 setParent(ITrackerResources.getBundle(new Locale(locale
83    .getLanguage())));
84  0 } else if (locale.getLanguage().length() > 0) {
85  0 setParent(ITrackerResources.getBundle(ITrackerResources
86    .getLocale(ITrackerResources.BASE_LOCALE)));
87    }
88    }
89   
90    }
91   
 
92  0 toggle public static ResourceBundle getBundle() {
93  0 return ITrackerResources.getBundle();
94    }
95   
 
96  0 toggle public static ResourceBundle getBundle(Locale locale) {
97  0 return ITrackerResources.getBundle(locale);
98    }
99   
100    /**
101    * @param locale
102    * @param data
103    * @deprecated used still for testing
104    */
 
105  0 toggle public ITrackerResourceBundle(Locale locale, Object[][] data) {
106  0 this(locale);
107  0 setContents(data);
108    }
109   
110    /**
111    * @param locale
112    * @param items
113    */
 
114  0 toggle private ITrackerResourceBundle(Locale locale, List<Language> items) {
115  0 this(locale);
116  0 setContents(items);
117    }
118   
119    /**
120    *
121    * @return should be private or removed
122    * @deprecated
123    */
 
124  0 toggle public Object[][] getContents() {
125    // Only load the array if it is requested for some reason.
126  0 if (dataArray == null) {
127  0 int i = 0;
128  0 Object[][] newData = new Object[2][data.size()];
129  0 Enumeration<String> keys = getKeys();
130  0 while (keys.hasMoreElements()) {
131  0 newData[0][i] = keys.nextElement();
132  0 newData[1][i] = data.get(newData[0][i]);
133    }
134   
135  0 this.dataArray = newData;
136    }
137   
138  0 return dataArray.clone();
139    }
140   
141    /**
142    * @deprecated should be private
143    * @param content
144    */
 
145  0 toggle public void setContents(List<Language> content) {
146  0 if (content != null) {
147  0 synchronized (data) {
148  0 data.clear();
149  0 this.dataArray = null;
150  0 for (int i = 0; i < content.size(); i++) {
151  0 data.put(content.get(i).getResourceKey(), content.get(i)
152    .getResourceValue());
153    }
154    }
155    }
156    }
157   
158    /**
159    * @deprecated
160    * @param content
161    * should be private
162    */
 
163  0 toggle private void setContents(Object[][] content) {
164  0 if (content != null && content.length == 2
165    && content[0].length == content[1].length) {
166  0 synchronized (data) {
167  0 data.clear();
168  0 this.dataArray = null;
169  0 for (int i = 0; i < content[0].length; i++) {
170  0 data.put((String) content[0][i], content[1][i]);
171    }
172    }
173    }
174    }
175   
 
176  0 toggle @Override
177    public Locale getLocale() {
178  0 Locale l = super.getLocale();
179  0 if (null == l && null != propertiesBundle) {
180  0 l = propertiesBundle.getLocale();
181    }
182  0 return l;
183    }
184   
 
185  0 toggle public boolean isDirty(String key) {
186  0 try {
187  0 handleGetObject(key);
188    } catch (ITrackerDirtyResourceException exception) {
189  0 return true;
190    }
191  0 return false;
192    }
193   
194    // public void updateValue(String key, Object value) {
195    // synchronized (data) {
196    // data.put(key, value);
197    // }
198    // }
199   
 
200  0 toggle public void updateValue(String key, String value) {
201  0 synchronized (data) {
202  0 data.put(key, value);
203  0 this.dataArray = null;
204    }
205    }
206   
 
207  0 toggle public void updateValue(Language model) {
208  0 if (model != null) {
209  0 synchronized (data) {
210  0 data.put(model.getResourceKey(), model.getResourceValue());
211  0 this.dataArray = null;
212    }
213    }
214    }
215   
 
216  0 toggle public void removeValue(String key, boolean markDirty) {
217  0 if (key != null) {
218  0 synchronized (data) {
219  0 if (markDirty) {
220  0 data.put(key, new DirtyKey() {
221    });
222    } else {
223  0 data.remove(key);
224    }
225  0 this.dataArray = null;
226    }
227    }
228    }
229   
230    /**
231    * Implementation of ResourceBundle.handleGetObject. Returns the request key
232    * from the internal data map.
233    */
 
234  0 toggle public final Object handleGetObject(String key) {
235  0 Object value = data.get(key);
236  0 if (value instanceof DirtyKey) {
237  0 throw new ITrackerDirtyResourceException(
238    "The requested key has been marked dirty.",
239    "ITrackerResourceBundle_" + getLocale(), key);
240    }
241  0 if (null == value) {
242  0 try {
243  0 value = propertiesBundle.getObject(key);
244   
245    // log.debug("handleGetObject2: "
246    // + key + "=" + value);
247    } catch (MissingResourceException e) {
248  0 if (log.isDebugEnabled()) {
249  0 log.debug("handleGetObject: " + key, e);
250    }
251    }
252    }
253  0 return value;
254    }
255   
256    /**
257    * Implementation of ResourceBundle.getKeys. Since it returns an
258    * enumeration, It creates a new Set, and returns that collections
259    * enumerator.
260    */
 
261  0 toggle public Enumeration<String> getKeys() {
262  0 Set<String> set = new TreeSet<String>(data.keySet());
263  0 if (null != parent) {
264  0 Enumeration<String> keys = parent.getKeys();
265  0 String key;
266  0 while (keys.hasMoreElements()) {
267  0 key = keys.nextElement();
268  0 set.add(key);
269    }
270    }
271  0 if (null != propertiesBundle) {
272  0 Enumeration<String> keys = propertiesBundle.getKeys();
273  0 String key;
274  0 while (keys.hasMoreElements()) {
275  0 key = keys.nextElement();
276  0 set.add(key);
277    }
278    }
279  0 return Collections.enumeration(set);
280    }
281   
 
282    public static interface DirtyKey {
283    }
284    }