コード例 #1
0
 /**
  * State/Province Bean processing function.
  *
  * @param in Java bean containing the raw stateprovince as String
  * @param out Java bean containing the stateprovince (from controlled vocabulary) as String
  * @param params Will be ignored so use null
  * @param result Optional ProcessingResult
  */
 @Override
 public void processBean(
     Object in, Object out, Map<String, Object> params, ProcessingResult result) {
   try {
     String textStateProvince = (String) PropertyUtils.getSimpleProperty(in, stateProvinceName);
     StateProvinceEnum stateProvinceEnum = process(textStateProvince, result);
     String stateProvinceStr = null;
     if (stateProvinceEnum == null) {
       switch (errorHandlingMode) {
         case USE_ORIGINAL:
           stateProvinceStr = textStateProvince;
           break;
         case USE_NULL:
           stateProvinceStr = null;
           break;
         case USE_EMPTY:
           stateProvinceStr = "";
           break;
         default:
           stateProvinceStr = null;
           break;
       }
     } else {
       stateProvinceStr = stateProvinceEnum.getName();
     }
     PropertyUtils.setSimpleProperty(out, stateProvinceName, stateProvinceStr);
   } catch (IllegalAccessException e) {
     logger.error("Bean access error", e);
   } catch (InvocationTargetException e) {
     logger.error("Bean access error", e);
   } catch (NoSuchMethodException e) {
     logger.error("Bean access error", e);
   }
 }
コード例 #2
0
  /**
   * NoSuchElementException could be thrown if something is wrong with the dictionary file or the
   * StateProvinceEnum implementation. NullPointerException will be thrown if the dictionary can not
   * be found.
   *
   * @param targetCountry used to retrieve the dictionary file
   * @param stateProvince class of the concrete enum
   * @param stateProvinceName name of the field holding the stateprovince value
   * @param errorHandlingMode
   */
  public StateProvinceProcessor(
      Country targetCountry,
      Class<T> stateProvinceClass,
      String stateProvinceName,
      ErrorHandlingModeEnum errorHandlingMode) {
    super(
        false,
        new InputStream[] {
          StateProvinceProcessor.class.getResourceAsStream(
              "/dictionaries/geography/"
                  + targetCountry.getIso2LetterCode()
                  + "_StateProvinceName.txt")
        });
    this.associatedCountry = targetCountry;
    this.errorHandlingMode = errorHandlingMode;
    this.stateProvinceName = stateProvinceName;
    try {
      // make sure we can find the fromCode(String) method for this enum
      fromCodeMethod = stateProvinceClass.getMethod("fromCode", String.class);
    } catch (SecurityException e) {
      fromCodeMethod = null;
    } catch (NoSuchMethodException e) {
      fromCodeMethod = null;
    }

    StateProvinceEnum[] statesProvinces = stateProvinceClass.getEnumConstants();
    if (statesProvinces == null || statesProvinces.length <= 0 || fromCodeMethod == null) {
      String errorText =
          "No well-formed StateProvinceEnum found for country "
              + targetCountry.getTitle()
              + " and Class "
              + stateProvinceClass;
      logger.error(errorText);
      throw new NoSuchElementException(errorText);
    }

    for (StateProvinceEnum cp : statesProvinces) {
      add(cp.getName(), cp.getCode());
      add(cp.getCode(), cp.getCode());
    }

    // always a default Locale
    setLocale(Locale.ENGLISH);
  }