EMMA Coverage Report (generated Thu Jan 20 11:39:44 EST 2011)
[all classes][com.hammurapi.common]

COVERAGE SUMMARY FOR SOURCE FILE [ClassResourceLoader.java]

nameclass, %method, %block, %line, %
ClassResourceLoader.java0%   (0/3)0%   (0/11)0%   (0/428)0%   (0/65)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ClassResourceLoader0%   (0/1)0%   (0/7)0%   (0/75)0%   (0/15)
ClassResourceLoader (Class, String, ClassLoader): void 0%   (0/1)0%   (0/20)0%   (0/5)
access$0 (ClassResourceLoader): String 0%   (0/1)0%   (0/3)0%   (0/1)
access$1 (ClassResourceLoader): ClassLoader 0%   (0/1)0%   (0/3)0%   (0/1)
getProperties (String, Locale, String): Properties 0%   (0/1)0%   (0/18)0%   (0/3)
getProperties (String, String): Properties 0%   (0/1)0%   (0/6)0%   (0/1)
getResource (String, Locale, String): URL 0%   (0/1)0%   (0/19)0%   (0/3)
getResource (String, String): URL 0%   (0/1)0%   (0/6)0%   (0/1)
     
class ClassResourceLoader$10%   (0/1)0%   (0/2)0%   (0/159)0%   (0/24)
ClassResourceLoader$1 (ClassResourceLoader, Locale, String, String, URL []): ... 0%   (0/1)0%   (0/22)0%   (0/3)
visit (Class): boolean 0%   (0/1)0%   (0/137)0%   (0/21)
     
class ClassResourceLoader$20%   (0/1)0%   (0/2)0%   (0/194)0%   (0/29)
ClassResourceLoader$2 (ClassResourceLoader, String, Locale, String, Propertie... 0%   (0/1)0%   (0/18)0%   (0/2)
visit (Class): boolean 0%   (0/1)0%   (0/176)0%   (0/27)

1/*
2@license.text@
3 */
4package com.hammurapi.common;
5 
6import java.io.IOException;
7import java.io.InputStream;
8import java.net.URL;
9import java.util.Locale;
10import java.util.Map;
11import java.util.Properties;
12 
13 
14/**
15 * Utility class which searches class hierarchy for a resource.
16 * Resource shall be named after class name, profile name is separated from
17 * class name by ! and locale separated from class name/profile by _.
18 * @author Pavel Vlasov
19 * @revision $Revision$
20 */
21public class ClassResourceLoader {
22        private Visitable<Class<?>> visitable;
23        private String prefix;
24        private ClassLoader classLoader;
25        
26        public ClassResourceLoader(Class<?> clazz, String prefix, ClassLoader classLoader) {
27                this.visitable=new ClassHierarchyVisitable(clazz);
28                this.classLoader = classLoader==null ? clazz.getClassLoader() : classLoader;
29                this.prefix = prefix;
30        }
31        
32        /**
33         * 
34         * @param profile Profile, can be null
35         * @param extension Extension, can be null
36         * @return
37         */
38        public URL getResource(String profile, String extension) {
39                return getResource(profile, Locale.getDefault(), extension);
40        }
41 
42        /**
43         * @param profile
44         * @param extension
45         * @return
46         */
47        public URL getResource(final String profile, final Locale locale, final String extension) {                
48                final URL[] ret={null};
49                visitable.accept(new Visitor<Class<?>>() {
50                        Locale actualLocale = locale==null ? Locale.getDefault() : locale;
51 
52                        public boolean visit(Class<?> currentClass) {
53                                for (int i=0; i<4; i++) {
54                                        String variant=currentClass.getName().replace('.','/');
55                                        if (prefix!=null) {
56                                                variant = prefix + variant;
57                                        }
58                                        if (profile!=null) {
59                                                variant+="!"+profile;
60                                        }
61                                        
62                                        switch (i) {
63                                                case 0:
64                                                        variant+="_"+actualLocale;
65                                                        break;
66                                                case 1:
67                                                        variant+="_"+actualLocale.getLanguage();
68                                                        if (actualLocale.getCountry().length()!=0) {
69                                                                variant+="_"+actualLocale.getCountry();
70                                                        }
71                                                        break;
72                                                case 2:
73                                                        variant+="_"+actualLocale.getLanguage();
74                                                        break;
75                                                case 3:
76                                                        break;                                                        
77                                        }
78                                        
79                                        if (extension!=null) {
80                                                variant+="."+extension;
81                                        }
82                                        
83                                        
84                                        ret[0]=classLoader.getResource(variant);
85                                        if (ret[0]!=null) {
86                                                return false;
87                                        }
88                                }
89                                                                                                                        
90                                return true;
91                        }
92                        
93                });
94                
95                return ret[0];
96        }
97 
98        /**
99         * Collects properties from class hierarchy.  
100         * @param profile Profile, can be null
101         * @param extension Extension, can be null
102         * @return
103         */
104        public Properties getProperties(String profile, String extension) {
105                return getProperties(profile, Locale.getDefault(), extension);
106        }
107 
108        /**
109         * Collects properties from class hierarchy.  
110         * @param profile
111         * @param extension
112         * @return
113         */
114        public Properties getProperties(final String profile, final Locale locale, final String extension) {
115                final Properties ret=new Properties();
116                visitable.accept(new Visitor<Class<?>>() {
117 
118                        public boolean visit(Class<?> currentClass) {
119                                for (int i=0; i<4; i++) {
120                                        String variant=currentClass.getName().replace('.','/');
121                                        if (prefix!=null) {
122                                                variant = prefix + variant;
123                                        }
124                                        if (profile!=null) {
125                                                variant+=profile;
126                                        }
127                                        
128                                        switch (i) {
129                                                case 0:
130                                                        variant+="_"+locale;
131                                                        break;
132                                                case 1:
133                                                        variant+="_"+locale.getLanguage();
134                                                        if (locale.getCountry().length()!=0) {
135                                                                variant+="_"+locale.getCountry();
136                                                        }
137                                                        break;
138                                                case 2:
139                                                        variant+="_"+locale.getLanguage();
140                                                        break;
141                                                case 3:
142                                                        break;                                                        
143                                        }
144                                        
145                                        if (extension!=null) {
146                                                variant+="."+extension;
147                                        }
148                                        
149                                        
150                                        InputStream s=classLoader.getResourceAsStream(variant);
151                                        if (s!=null) {
152                                                Properties cp=new Properties();
153                                                try {
154                                                        cp.load(s);
155                                                        for (Map.Entry<Object, Object> entry: cp.entrySet()) {
156                                                                if (!ret.containsKey(entry.getKey())) {
157                                                                        ret.setProperty((String) entry.getKey(), (String) entry.getValue());
158                                                                }
159                                                        }
160                                                } catch (IOException e) {
161                                                        throw new RuntimeException("Cannot load properties from resource "+variant, e);
162                                                }
163                                                
164                                                break;
165                                        }
166                                }
167                                
168                                                                                        
169                                return true;
170                        }
171                        
172                });
173                
174                return ret;
175        }
176        
177}

[all classes][com.hammurapi.common]
EMMA 2.0.5312 EclEmma Fix 2 (C) Vladimir Roubtsov