Inspector | Message | Severity | Location |
---|---|---|---|
Java Inspector 006 | Missing default case for switch statement | 1 | 86:41 |
Java Inspector 006 | Missing default case for switch statement | 1 | 149:41 |
Java Inspector 006 | Missing default case for switch statement | 1 | 219:41 |
Java Inspector 048 | Copyrights information should be present in each file. | 1 | |
Java Inspector 049 | Use a Collection instead of arrays Object[] | 2 | 74:17 |
Java Inspector 089 | Constructor is not properly documented | 2 | 53:9 |
Java Inspector 089 | Undocumented parameter clazz | 2 | 53:9 |
Java Inspector 089 | Method is not properly documented | 2 | 64:9 |
Java Inspector 089 | Method return value is not properly documented | 2 | 64:9 |
Java Inspector 089 | Method is not properly documented | 2 | 73:9 |
Java Inspector 089 | Parameter profile is not documented | 2 | 73:9 |
Java Inspector 089 | Undocumented parameter locale | 2 | 73:9 |
Java Inspector 089 | Parameter extension is not documented | 2 | 73:9 |
Java Inspector 089 | Method return value is not properly documented | 2 | 73:9 |
Java Inspector 089 | Undocumented method | 2 | 78:25 |
Java Inspector 089 | Method is not properly documented | 2 | 128:9 |
Java Inspector 089 | Method return value is not properly documented | 2 | 128:9 |
Java Inspector 089 | Method is not properly documented | 2 | 137:9 |
Java Inspector 089 | Parameter profile is not documented | 2 | 137:9 |
Java Inspector 089 | Undocumented parameter locale | 2 | 137:9 |
Java Inspector 089 | Parameter extension is not documented | 2 | 137:9 |
Java Inspector 089 | Method return value is not properly documented | 2 | 137:9 |
Java Inspector 089 | Undocumented method | 2 | 141:25 |
Java Inspector 089 | Method is not properly documented | 2 | 205:9 |
Java Inspector 089 | Parameter profile is not documented | 2 | 205:9 |
Java Inspector 089 | Undocumented parameter locale | 2 | 205:9 |
Java Inspector 089 | Parameter extension is not documented | 2 | 205:9 |
Java Inspector 089 | Method return value is not properly documented | 2 | 205:9 |
Java Inspector 089 | Undocumented method | 2 | 211:25 |
Java Inspector 089 | Undocumented method | 2 | 261:25 |
Java Inspector 024 | Avoid hardwired character literals | 3 | 81:87 |
Java Inspector 024 | Avoid hardwired character literals | 3 | 81:91 |
Java Inspector 024 | Avoid hardwired character literals | 3 | 144:87 |
Java Inspector 024 | Avoid hardwired character literals | 3 | 144:91 |
Java Inspector 024 | Avoid hardwired character literals | 3 | 214:87 |
Java Inspector 024 | Avoid hardwired character literals | 3 | 214:91 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 80:49 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 96:54 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 99:54 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 143:49 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 159:54 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 162:54 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 213:49 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 229:54 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 232:54 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 83:58 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 88:66 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 91:66 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 93:74 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 97:66 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 104:58 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 151:66 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 154:66 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 156:74 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 160:66 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 167:58 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 184:97 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 221:66 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 224:66 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 226:74 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 230:66 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 237:58 |
Java Inspector 051 | It is good practice to call in any case super() in a constructor. | 3 | 53:9 |
1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.util;
24
25import java.io.IOException;
26import java.io.InputStream;
27import java.util.ArrayList;
28import java.util.Collection;
29import java.util.Iterator;
30import java.util.Locale;
31import java.util.Map;
32import java.util.Properties;
33
34import biz.hammurapi.config.ConfigurationException;
35import biz.hammurapi.config.Context;
36import biz.hammurapi.config.DomConfigFactory;
37import biz.hammurapi.config.RuntimeConfigurationException;
38
39/**
40 * Utility class which searches class hierarchy for a resource.
41 * Resource shall be named after class name, profile name is separated from
42 * class name by ! and locale separated from class name/profile by _.
43 * @author Pavel Vlasov
44 * @revision $Revision$
45 */
46public class ClassResourceLoader {
47 private Visitable visitable;
48 private Class clazz;
49
50 /**
51 *
52 */
53 public ClassResourceLoader(Class clazz) {
54 this.visitable=new ClassHierarchyVisitable(clazz);
55 this.clazz=clazz;
56 }
57
58 /**
59 *
60 * @param profile Profile, can be null
61 * @param extension Extension, can be null
62 * @return
63 */
64 public InputStream getResourceAsStream(String profile, String extension) {
65 return getResourceAsStream(profile, Locale.getDefault(), extension);
66 }
67
68 /**
69 * @param profile
70 * @param extension
71 * @return
72 */
73 public InputStream getResourceAsStream(final String profile, final Locale locale, final String extension) {
74 final InputStream[] ret={null};
75 visitable.accept(new Visitor() {
76 Locale actualLocale = locale==null ? Locale.getDefault() : locale;
77
78 public boolean visit(Object target) {
79 Class currentClass=(Class) target;
80 for (int i=0; i<4; i++) {
81 String variant=currentClass.getName().replace('.','/');
82 if (profile!=null) {
83 variant+="!"+profile;
84 }
85
86 switch (i) {
87 case 0:
88 variant+="_"+actualLocale;
89 break;
90 case 1:
91 variant+="_"+actualLocale.getLanguage();
92 if (actualLocale.getCountry().length()!=0) {
93 variant+="_"+actualLocale.getCountry();
94 }
95 break;
96 case 2:
97 variant+="_"+actualLocale.getLanguage();
98 break;
99 case 3:
100 break;
101 }
102
103 if (extension!=null) {
104 variant+="."+extension;
105 }
106
107
108 ret[0]=clazz.getClassLoader().getResourceAsStream(variant);
109 if (ret[0]!=null) {
110 return false;
111 }
112 }
113
114 return true;
115 }
116
117 });
118
119 return ret[0];
120 }
121
122 /**
123 *
124 * @param profile Profile, can be null
125 * @param extension Extension, can be null
126 * @return
127 */
128 public Properties getProperties(String profile, String extension) {
129 return getProperties(profile, Locale.getDefault(), extension);
130 }
131
132 /**
133 * @param profile
134 * @param extension
135 * @return
136 */
137 public Properties getProperties(final String profile, final Locale locale, final String extension) {
138 final Properties ret=new Properties();
139 visitable.accept(new Visitor() {
140
141 public boolean visit(Object target) {
142 Class currentClass=(Class) target;
143 for (int i=0; i<4; i++) {
144 String variant=currentClass.getName().replace('.','/');
145 if (profile!=null) {
146 variant+=profile;
147 }
148
149 switch (i) {
150 case 0:
151 variant+="_"+locale;
152 break;
153 case 1:
154 variant+="_"+locale.getLanguage();
155 if (locale.getCountry().length()!=0) {
156 variant+="_"+locale.getCountry();
157 }
158 break;
159 case 2:
160 variant+="_"+locale.getLanguage();
161 break;
162 case 3:
163 break;
164 }
165
166 if (extension!=null) {
167 variant+="."+extension;
168 }
169
170
171 InputStream s=clazz.getClassLoader().getResourceAsStream(variant);
172 if (s!=null) {
173 Properties cp=new Properties();
174 try {
175 cp.load(s);
176 Iterator it=cp.entrySet().iterator();
177 while (it.hasNext()) {
178 Map.Entry entry=(Map.Entry) it.next();
179 if (!ret.containsKey(entry.getKey())) {
180 ret.setProperty((String) entry.getKey(), (String) entry.getValue());
181 }
182 }
183 } catch (IOException e) {
184 throw new RuntimeConfigurationException("Cannot load properties from resource "+variant, e);
185 }
186
187 break;
188 }
189 }
190
191
192 return true;
193 }
194
195 });
196
197 return ret;
198 }
199
200 /**
201 * @param profile
202 * @param extension
203 * @return
204 */
205 public Context getContext(final String profile, Locale locale, final String extension) {
206 final Locale actualLocale = locale==null ? Locale.getDefault() : locale;
207 final Collection contexts=new ArrayList();
208 final DomConfigFactory dcf=new DomConfigFactory();
209 visitable.accept(new Visitor() {
210
211 public boolean visit(Object target) {
212 Class currentClass=(Class) target;
213 for (int i=0; i<4; i++) {
214 String variant=currentClass.getName().replace('.','/');
215 if (profile!=null) {
216 variant+=profile;
217 }
218
219 switch (i) {
220 case 0:
221 variant+="_"+actualLocale;
222 break;
223 case 1:
224 variant+="_"+actualLocale.getLanguage();
225 if (actualLocale.getCountry().length()!=0) {
226 variant+="_"+actualLocale.getCountry();
227 }
228 break;
229 case 2:
230 variant+="_"+actualLocale.getLanguage();
231 break;
232 case 3:
233 break;
234 }
235
236 if (extension!=null) {
237 variant+="."+extension;
238 }
239
240
241 InputStream s=clazz.getClassLoader().getResourceAsStream(variant);
242 if (s!=null) {
243 try {
244 contexts.add(dcf.create(s, null));
245 } catch (ConfigurationException e) {
246 throw new RuntimeConfigurationException(e);
247 } catch (IOException e) {
248 throw new RuntimeConfigurationException(e);
249 }
250 break;
251 }
252 }
253
254 return true;
255 }
256
257 });
258
259 return contexts.isEmpty() ? null : new Context() {
260
261 public Object get(String name) {
262 Iterator it=contexts.iterator();
263 while (it.hasNext()) {
264 Object ret=((Context) it.next()).get(name);
265 if (ret!=null) {
266 return ret;
267 }
268 }
269 return null;
270 }
271
272 };
273 }
274}
275