1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package ch.qos.cal10n.verifier;
24
25 import java.text.MessageFormat;
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.ResourceBundle;
32 import java.util.Set;
33
34 import ch.qos.cal10n.Cal10nConstants;
35 import ch.qos.cal10n.util.AnnotationExtractor;
36 import ch.qos.cal10n.util.CAL10NResourceBundleFinder;
37 import ch.qos.cal10n.util.MiscUtil;
38 import ch.qos.cal10n.verifier.Cal10nError.ErrorType;
39
40
41
42
43
44
45
46 public class MessageKeyVerifier implements IMessageKeyVerifier {
47
48 Class<? extends Enum<?>> enumType;
49 String enumTypeAsStr;
50
51 public MessageKeyVerifier(Class<? extends Enum<?>> enumClass) {
52 this.enumType = enumClass;
53 this.enumTypeAsStr = enumClass.getName();
54 }
55
56 @SuppressWarnings("unchecked")
57 public MessageKeyVerifier(String enumTypeAsStr) {
58 this.enumTypeAsStr = enumTypeAsStr;
59 String errMsg = "Failed to find enum class [" + enumTypeAsStr + "]";
60 try {
61 this.enumType = (Class<? extends Enum<?>>) Class.forName(enumTypeAsStr);
62 } catch (ClassNotFoundException e) {
63 throw new IllegalStateException(errMsg, e);
64 } catch (NoClassDefFoundError e) {
65 throw new IllegalStateException(errMsg, e);
66 }
67 }
68
69 public Class<? extends Enum<?>> getEnumType() {
70 return enumType;
71 }
72
73 public String getEnumTypeAsStr() {
74 return enumTypeAsStr;
75 }
76
77 public List<Cal10nError> verify(Locale locale) {
78 List<Cal10nError> errorList = new ArrayList<Cal10nError>();
79
80 String baseName = AnnotationExtractor.getBaseName(enumType);
81
82 if (baseName == null) {
83 errorList.add(new Cal10nError(ErrorType.MISSING_BN_ANNOTATION, "",
84 enumType, locale, ""));
85
86 return errorList;
87 }
88
89 String charset = AnnotationExtractor.getCharset(enumType, Locale.FRENCH);
90 ResourceBundle rb = CAL10NResourceBundleFinder.getBundle(this.getClass()
91 .getClassLoader(), baseName, locale, charset);
92
93 ErrorFactory errorFactory = new ErrorFactory(enumType, locale, baseName);
94
95 if (rb == null) {
96 errorList.add(errorFactory.buildError(ErrorType.FAILED_TO_FIND_RB, ""));
97
98 return errorList;
99 }
100
101 Set<String> rbKeySet = buildKeySetFromEnumeration(rb.getKeys());
102
103 if (rbKeySet.size() == 0) {
104 errorList.add(errorFactory.buildError(ErrorType.EMPTY_RB, ""));
105 }
106
107 Enum<?>[] enumArray = enumType.getEnumConstants();
108 if (enumArray == null || enumArray.length == 0) {
109 errorList.add(errorFactory.buildError(ErrorType.EMPTY_ENUM, ""));
110 }
111
112 if (errorList.size() != 0) {
113 return errorList;
114 }
115
116 for (Enum<?> e : enumArray) {
117 String enumKey = e.toString();
118 if (rbKeySet.contains(enumKey)) {
119 rbKeySet.remove(enumKey);
120 } else {
121 errorList.add(errorFactory.buildError(ErrorType.ABSENT_IN_RB, enumKey));
122 }
123 }
124
125 for (String rbKey : rbKeySet) {
126 errorList.add(errorFactory.buildError(ErrorType.ABSENT_IN_ENUM, rbKey));
127 }
128 return errorList;
129 }
130
131 private Set<String> buildKeySetFromEnumeration(Enumeration<String> e) {
132 Set<String> set = new HashSet<String>();
133 while (e.hasMoreElements()) {
134 String s = e.nextElement();
135 set.add(s);
136 }
137 return set;
138 }
139
140 public List<String> typeIsolatedVerify(Locale locale) {
141 List<Cal10nError> errorList = verify(locale);
142 List<String> strList = new ArrayList<String>();
143 for (Cal10nError error : errorList) {
144 strList.add(error.toString());
145 }
146 return strList;
147 }
148
149
150
151
152 public List<Cal10nError> verifyAllLocales() {
153 List<Cal10nError> errorList = new ArrayList<Cal10nError>();
154
155 String[] localeNameArray = getLocaleNames();
156
157 if (localeNameArray == null || localeNameArray.length == 0) {
158 String errMsg = MessageFormat.format(Cal10nConstants.MISSING_LD_ANNOTATION_MESSAGE, enumTypeAsStr);
159 throw new IllegalStateException(errMsg);
160 }
161 for (String localeName : localeNameArray) {
162 Locale locale = MiscUtil.toLocale(localeName);
163 List<Cal10nError> tmpList = verify(locale);
164 errorList.addAll(tmpList);
165 }
166
167 return errorList;
168 }
169
170 public String[] getLocaleNames() {
171 String[] localeNameArray = AnnotationExtractor.getLocaleNames(enumType);
172 return localeNameArray;
173 }
174
175 public String getBaseName() {
176 String rbName = AnnotationExtractor.getBaseName(enumType);
177 return rbName;
178 }
179
180 }