예제 #1
0
  /**
   * Converts a primitive type value, this implementation only converts an EEnum to an Enum value.
   *
   * @param value the value to convert
   * @param eDataType its EDataType
   * @return the converted value
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  protected Object convertEAttributeValue(final Object value, final EDataType eDataType) {
    if (value instanceof Enum<?>) {
      final EDataType enumDataType = getDataTypeOrBaseType(eDataType);
      Check.isInstanceOf(enumDataType, EEnum.class);
      final ModelPackage modelPackage =
          ModelResolver.getInstance().getModelPackage(enumDataType.getEPackage().getNsURI());
      final Class<? extends Enum> enumClass =
          (Class<? extends Enum>) modelPackage.getEClassifierClass(enumDataType);
      return Enum.valueOf(enumClass, ((Enum<?>) value).name().toUpperCase(Locale.ENGLISH));
    } else if (value instanceof EEnumLiteral) {
      final EDataType enumDataType = getDataTypeOrBaseType(eDataType);
      Check.isInstanceOf(enumDataType, EEnum.class);
      final EEnumLiteral eeNumLiteral = (EEnumLiteral) value;
      final ModelPackage modelPackage =
          ModelResolver.getInstance().getModelPackage(enumDataType.getEPackage().getNsURI());
      if (modelPackage == null) {
        // dynamic model
        return eeNumLiteral;
      }
      final Class<? extends Enum> enumClass =
          (Class<? extends Enum>) modelPackage.getEClassifierClass(enumDataType);
      return Enum.valueOf(enumClass, eeNumLiteral.getName().toUpperCase(Locale.ENGLISH));
    }

    // convert these to a Date always
    if (value instanceof XMLGregorianCalendar) {
      final XMLGregorianCalendar xmlCalendar = (XMLGregorianCalendar) value;
      final Date date = xmlCalendar.toGregorianCalendar().getTime();
      return date;
    }

    return value;
  }
예제 #2
0
  /**
   * Converts a primitive type value. For EEnum: this implementation only converts an Enum to an
   * EEnum value.
   *
   * @param value the value to convert
   * @param eDataType its EDataType
   * @return the converted value
   */
  protected Object convertEAttributeValue(final Object value, final EDataType eDataType) {
    if (value instanceof Enum<?>) {
      final EDataType enumDataType = getDataTypeOrBaseType(eDataType);
      Check.isInstanceOf(enumDataType, EEnum.class);
      final EEnum eeNum = (EEnum) enumDataType;
      for (final EEnumLiteral enumLiteral : eeNum.getELiterals()) {
        // the code generation template uppercases enum
        if (enumLiteral.getName().toUpperCase(Locale.ENGLISH).equals(((Enum<?>) value).name())) {
          return enumLiteral.getInstance();
        }
      }
    }

    if (value instanceof Date && eDataType == XMLTypePackage.eINSTANCE.getDate()) {
      final Date date = (Date) value;
      final XMLCalendar xmlCalendar = new XMLCalendar(date, XMLCalendar.DATE);
      final Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      xmlCalendar.clear();
      xmlCalendar.setYear(calendar.get(Calendar.YEAR));
      xmlCalendar.setMonth(1 + calendar.get(Calendar.MONTH));
      xmlCalendar.setDay(calendar.get(Calendar.DATE));
      // note xmlcalendar expects minutes, calendar gives millis
      xmlCalendar.setTimezone(calendar.get(Calendar.ZONE_OFFSET) / 60000);
      return xmlCalendar;
    }

    if (value instanceof Date && eDataType == XMLTypePackage.eINSTANCE.getDateTime()) {
      final Date date = (Date) value;
      return new XMLCalendar(date, XMLCalendar.DATETIME);
    }

    return value;
  }