AnnotationExtractor xref

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  package ch.qos.cal10n.util;
    23
    24  import ch.qos.cal10n.BaseName;
    25  import ch.qos.cal10n.Locale;
    26  import ch.qos.cal10n.LocaleData;
    27
    28  /**
    29   *
    30   * @author Ceki G&uuml;lc&uuml;
    31   *
    32   */
    33  public class AnnotationExtractor {
    34
    35    static public <E extends Enum<?>> String getBaseName(Class<E> enumClass) {
    36      BaseName rbnAnnotation = (BaseName) enumClass.getAnnotation(BaseName.class);
    37      if (rbnAnnotation == null) {
    38        return null;
    39      }
    40      return rbnAnnotation.value();
    41    }
    42
    43    static public <E extends Enum<?>> String[] getLocaleNames(Class<E> enumClass) {
    44      Locale[] localeDataArray = getLocaleData(enumClass);
    45
    46      if (localeDataArray == null) {
    47        return null;
    48      }
    49
    50      String[] names = new String[localeDataArray.length];
    51      for (int i = 0; i < localeDataArray.length; i++) {
    52        names[i] = localeDataArray[i].value();
    53      }
    54      return names;
    55    }
    56
    57    static public <E extends Enum<?>> Locale[] getLocaleData(Class<E> enumClass) {
    58      LocaleData localeDataArrayAnnotation = (LocaleData) enumClass
    59          .getAnnotation(LocaleData.class);
    60      if (localeDataArrayAnnotation == null) {
    61        return null;
    62      }
    63      return localeDataArrayAnnotation.value();
    64    }
    65
    66    public static String getCharset(Class<?> enumClass,
    67        java.util.Locale juLocale) {
    68      LocaleData localeDataArrayAnnotation = (LocaleData) enumClass
    69          .getAnnotation(LocaleData.class);
    70      if (localeDataArrayAnnotation == null) {
    71        return "";
    72      }
    73
    74      String defaultCharset = localeDataArrayAnnotation.defaultCharset();
    75
    76      Locale  la = findLocaleAnnotation(juLocale, localeDataArrayAnnotation);
    77      String localeCharset = null;
    78      if(la != null) {
    79        localeCharset = la.charset();
    80      }
    81      if(!isEmttyString(localeCharset)) {
    82        return localeCharset;
    83      }
    84
    85      return defaultCharset;
    86    }
    87
    88    static Locale findLocaleAnnotation(java.util.Locale julocale, LocaleData localeDataArrayAnnotation) {
    89      Locale[] localeAnnotationArray = localeDataArrayAnnotation.value();
    90      if(localeAnnotationArray == null) {
    91        return null;
    92      }
    93      for(Locale la: localeAnnotationArray) {
    94        if(la.value().equals(julocale.toString())) {
    95          return la;
    96        }
    97      }
    98
    99      return null;
    100   }
    101
    102   static boolean isEmttyString(String s) {
    103     return s == null || s.length() == 0;
    104   }
    105 }