View Javadoc

1   /*
2    * Copyright (c) 2009 QOS.ch All rights reserved.
3    * 
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   * 
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   * 
14   * THE SOFTWARE IS PROVIDED "AS  IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package ch.qos.cal10n.verifier;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Locale;
28  import java.util.ResourceBundle;
29  import java.util.Set;
30  
31  import ch.qos.cal10n.util.AnnotationExtractor;
32  import ch.qos.cal10n.verifier.Cal10nError.ErrorType;
33  
34  /**
35   * Given an enum class, verify that the resource bundles corresponding to a
36   * given locale contains the correct codes.
37   * 
38   * @author Ceki Gulcu
39   */
40  public class MessageCodeVerifier implements IMessageCodeVerifier {
41  
42    Class<? extends Enum<?>> enumType;
43    String enumTypeAsStr;
44    
45    public MessageCodeVerifier(Class<? extends Enum<?>> enumClass) {
46      this.enumType = enumClass;
47      this.enumTypeAsStr = enumClass.getName();
48    }
49  
50    @SuppressWarnings("unchecked")
51    public MessageCodeVerifier(String enumTypeAsStr) {
52      this.enumTypeAsStr = enumTypeAsStr;
53      String errMsg = "Failed to find enum class [" + enumTypeAsStr + "]";
54      try {
55        this.enumType = (Class<? extends Enum<?>>) Class.forName(enumTypeAsStr);
56      } catch (ClassNotFoundException e) {
57        throw new IllegalStateException(errMsg, e);
58      } catch (NoClassDefFoundError e) {
59        throw new IllegalStateException(errMsg, e);
60      }
61    }
62  
63    
64    /* (non-Javadoc)
65     * @see ch.qos.cai10n.verifier.IIMessageCodeVerifier#getEnumType()
66     */
67    public Class<? extends Enum<?>> getEnumType() {
68      return enumType;
69    }
70  
71    /* (non-Javadoc)
72     * @see ch.qos.cal10n.verifier.IIMessageCodeVerifier#getEnumTypeAsStr()
73     */
74    public String getEnumTypeAsStr() {
75      return enumTypeAsStr;
76    }
77  
78    /* (non-Javadoc)
79     * @see ch.qos.cal10n.verifier.IIMessageCodeVerifier#verify(java.util.Locale)
80     */
81    public List<Cal10nError> verify(Locale locale) {
82      List<Cal10nError> errorList = new ArrayList<Cal10nError>();
83  
84      String resouceBundleName = AnnotationExtractor
85          .getResourceBundleName(enumType);
86  
87      if (resouceBundleName == null) {
88        errorList.add(new Cal10nError(ErrorType.MISSING_RBN_ANNOTATION, "",
89            enumType, locale, ""));
90        // no point in continuing
91        return errorList;
92      }
93  
94      ResourceBundle rb = ResourceBundle.getBundle(resouceBundleName, locale);
95  
96      ErrorFactory errorFactory = new ErrorFactory(enumType, locale,
97          resouceBundleName);
98  
99      if (rb == null) {
100       errorList.add(errorFactory.buildError(ErrorType.FAILED_TO_FIND_RB, ""));
101     }
102 
103     Set<String> rbKeySet = rb.keySet();
104     if (rbKeySet.size() == 0) {
105       errorList.add(errorFactory.buildError(ErrorType.EMPTY_RB, ""));
106     }
107 
108     Enum<?>[] enumArray = enumType.getEnumConstants();
109     if (enumArray == null || enumArray.length == 0) {
110       errorList.add(errorFactory.buildError(ErrorType.EMPTY_ENUM, ""));
111     }
112 
113     if (errorList.size() != 0) {
114       return errorList;
115     }
116 
117     for (Enum<?> e : enumArray) {
118       String enumKey = e.toString();
119       if (rbKeySet.contains(enumKey)) {
120         rbKeySet.remove(enumKey);
121       } else {
122         errorList.add(errorFactory.buildError(ErrorType.ABSENT_IN_RB, enumKey));
123       }
124     }
125 
126     for (String rbKey : rbKeySet) {
127       errorList.add(errorFactory.buildError(ErrorType.ABSENT_IN_ENUM, rbKey));
128     }
129     return errorList;
130   }
131 
132   /* (non-Javadoc)
133    * @see ch.qos.cal10n.verifier.IIMessageCodeVerifier#typeIsolatedVerify(java.util.Locale)
134    */
135   public List<String> typeIsolatedVerify(Locale locale) {
136     List<Cal10nError> errorList = verify(locale);
137     List<String> strList = new ArrayList<String>();
138     for (Cal10nError error : errorList) {
139       strList.add(error.toString());
140     }
141     return strList;
142   }
143 
144   /***
145    * Verify all declared locales in one step.
146    */
147   public List<Cal10nError> verifyAllLocales() {
148     List<Cal10nError> errorList = new ArrayList<Cal10nError>();
149     
150     String[] localeNameArray = getLocaleNames();
151     
152     if (localeNameArray == null || localeNameArray.length == 0) {
153       String errMsg = "Missing @LocaleNames annotation in enum type ["
154           + enumTypeAsStr + "]";
155       throw new IllegalStateException(errMsg);
156     }
157     for (String localeName : localeNameArray) {
158       Locale locale = new Locale(localeName);
159       List<Cal10nError> tmpList = verify(locale);
160       errorList.addAll(tmpList);
161     }
162     
163     return errorList;
164   }
165 
166   
167   /* (non-Javadoc)
168    * @see ch.qos.cal10n.verifier.IIMessageCodeVerifier#getLocaleNames()
169    */
170   public String[] getLocaleNames() {
171     String[] localeNameArray = AnnotationExtractor.getLocaleNames(enumType);
172     return localeNameArray;
173   }
174   
175   public String getResourceBundleName() {
176     String rbName = AnnotationExtractor.getResourceBundleName(enumType);
177     return rbName;
178   }
179 
180   
181 
182 }