com.haulmont.chile.core.datatypes.Datatype.parse()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(140)

本文整理了Java中com.haulmont.chile.core.datatypes.Datatype.parse()方法的一些代码示例,展示了Datatype.parse()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Datatype.parse()方法的具体详情如下:
包路径:com.haulmont.chile.core.datatypes.Datatype
类名称:Datatype
方法名:parse

Datatype.parse介绍

[英]Parses value from String. During the parsing process, you can throw ValueConversionExceptioninstead of ParseException.
[中]从字符串中解析值。在解析过程中,可以抛出ValueConversionException而不是ParseException。

代码示例

代码示例来源:origin: com.haulmont.cuba/cuba-global

@Override
  public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    String formattedDate = json.getAsJsonPrimitive().getAsString();
    try {
      return Strings.isNullOrEmpty(formattedDate) ? null : dateDatatype.parse(formattedDate);
    } catch (ParseException e) {
      throw new EntitySerializationException("Cannot parse date " + formattedDate);
    }
  }
}

代码示例来源:origin: com.haulmont.cuba/cuba-rest-api

private static Object parseByDatatype(String value, Class<?> type) throws ParseException {
  Datatype datatype = Datatypes.getNN(type);
  return datatype.parse(value);
}

代码示例来源:origin: com.haulmont.reports/reports-core

protected Object readSimpleProperty(JsonReader in, Class<?> propertyType) throws IOException {
  String value = in.nextString();
  Object parsedValue = null;
  try {
    Datatype<?> datatype = Datatypes.get(propertyType);
    if (datatype != null) {
      parsedValue = datatype.parse(value);
    }
    return parsedValue;
  } catch (ParseException e) {
    throw new RuntimeException(
        format("An error occurred while parsing property. Class [%s]. Value [%s].", propertyType, value), e);
  }
}

代码示例来源:origin: com.haulmont.cuba/cuba-rest-api

/**
 * Parses string value into specific type
 *
 * @param value    value to parse
 * @param typeName Datatype name
 * @return parsed object
 */
public static Object parseByTypename(String value, String typeName) {
  Datatype datatype = Datatypes.get(typeName);
  try {
    return datatype.parse(value);
  } catch (ParseException e) {
    throw new IllegalArgumentException(String.format("Cannot parse specified parameter of type '%s'", typeName), e);
  }
}

代码示例来源:origin: com.haulmont.cuba/cuba-global

/**
   * Parse Long using {@code integerFormat} string specified in the main message pack.
   * @return Long value or null if a blank string is provided
   */
  @Nullable
  public Long parseLong(String str) throws ParseException {
    return datatypeRegistry.getNN(Long.class).parse(str, uss.getLocale());
  }
}

代码示例来源:origin: com.haulmont.cuba/cuba-global

/**
 * Parse Date (time without date) using {@code timeFormat} string specified in the main message pack.
 * @return Date value or null if a blank string is provided
 */
@Nullable
public Date parseTime(String str) throws ParseException {
  return datatypeRegistry.getNN(Time.class).parse(str, uss.getLocale());
}

代码示例来源:origin: com.haulmont.cuba/cuba-global

/**
 * Parse BigDecimal using {@code decimalFormat} string specified in the main message pack.
 * @return BigDecimal value or null if a blank string is provided
 */
@Nullable
public BigDecimal parseBigDecimal(String str) throws ParseException {
  return datatypeRegistry.getNN(BigDecimal.class).parse(str, uss.getLocale());
}

代码示例来源:origin: com.haulmont.cuba/cuba-global

/**
 * Parse Integer using {@code integerFormat} string specified in the main message pack.
 * @return Integer value or null if a blank string is provided
 */
@Nullable
public Integer parseInteger(String str) throws ParseException {
  return datatypeRegistry.getNN(Integer.class).parse(str, uss.getLocale());
}

代码示例来源:origin: com.haulmont.cuba/cuba-global

/**
 * Parse Date (date without time) using {@code dateFormat} string specified in the main message pack.
 * @return Date value or null if a blank string is provided
 */
@Nullable
public Date parseDate(String str) throws ParseException {
  return datatypeRegistry.getNN(java.sql.Date.class).parse(str, uss.getLocale());
}

代码示例来源:origin: com.haulmont.cuba/cuba-global

/**
 * Parse Double using {@code doubleFormat} string specified in the main message pack.
 * @return Double value or null if a blank string is provided
 */
@Nullable
public Double parseDouble(String str) throws ParseException {
  return datatypeRegistry.getNN(Double.class).parse(str, uss.getLocale());
}

代码示例来源:origin: com.haulmont.cuba/cuba-global

/**
 * Parse Boolean using {@code trueString} and {@code falseString} strings specified in the main message pack.
 * @return Boolean value or null if a blank string is provided
 */
@Nullable
public Boolean parseBoolean(String str) throws ParseException {
  return datatypeRegistry.getNN(Boolean.class).parse(str, uss.getLocale());
}

代码示例来源:origin: com.haulmont.cuba/cuba-global

protected Object readSimpleProperty(JsonElement valueElement, Datatype propertyType) {
  String value = valueElement.getAsString();
  if (value == null) return null;
  try {
    Class javaClass = propertyType.getJavaClass();
    if (BigDecimal.class.isAssignableFrom(javaClass)) {
      return valueElement.getAsBigDecimal();
    } else if (Long.class.isAssignableFrom(javaClass)) {
      return valueElement.getAsLong();
    } else if (Integer.class.isAssignableFrom(javaClass)) {
      return valueElement.getAsInt();
    } else if (Double.class.isAssignableFrom(javaClass)) {
      return valueElement.getAsDouble();
    }
    return propertyType.parse(value);
  } catch (ParseException e) {
    throw new EntitySerializationException(String.format("An error occurred while parsing property. Type [%s]. Value [%s].", propertyType, value), e);
  }
}

代码示例来源:origin: com.haulmont.cuba/cuba-global

protected Object getXmlAnnotationAttributeValue(String value, String className, String datatypeName) {
  if (className == null && datatypeName == null)
    return inferMetaAnnotationType(value);
  if (className != null) {
    Class aClass = ReflectionHelper.getClass(className);
    if (aClass.isEnum()) {
      //noinspection unchecked
      return Enum.valueOf(aClass, value);
    } else {
      throw new UnsupportedOperationException("Class " + className + "  is not Enum");
    }
  } else {
    try {
      return datatypes.get(datatypeName).parse(value);
    } catch (ParseException e) {
      throw new RuntimeException("Unable to parse XML meta-annotation value", e);
    }
  }
}

代码示例来源:origin: com.haulmont.charts/charts-web

@Nullable
protected Object getItemId(CollectionDatasource datasource, String itemIdString) {
  MetaProperty pkProp = metadata.getTools().getPrimaryKeyProperty(datasource.getMetaClass());
  if (pkProp != null) {
    Datatype<Object> datatype = pkProp.getRange().asDatatype();
    try {
      return datatype.parse(itemIdString);
    } catch (ParseException e) {
      throw new RuntimeException("Error parsing item ID", e);
    }
  }
  return null;
}

代码示例来源:origin: com.haulmont.cuba/cuba-gui

protected void loadEntityId(Element element, InstanceLoader<Entity> loader) {
  String entityIdStr = element.attributeValue("entityId");
  if (Strings.isNullOrEmpty(entityIdStr)) {
    return;
  }
  MetaProperty property = metadataTools.getPrimaryKeyProperty(loader.getContainer().getEntityMetaClass());
  if (property == null) {
    throw new IllegalStateException("Cannot determine id property for " + loader.getContainer().getEntityMetaClass());
  }
  if (property.getRange().isDatatype()) {
    try {
      Object value = property.getRange().asDatatype().parse(entityIdStr);
      loader.setEntityId(value);
    } catch (ParseException e) {
      throw new RuntimeException("Error parsing entityId for " + loader, e);
    }
  } else {
    throw new IllegalStateException("Cannot assign id to " + loader + " because the entity has a composite PK");
  }
}

代码示例来源:origin: com.haulmont.cuba/cuba-global

/**
 * Parse Date (date and time) using {@code dateTimeFormat} string specified in the main message pack.
 * <p>Takes into account time zone if it is set for the current user session.</p>
 * @return Date value or null if a blank string is provided
 */
@Nullable
public Date parseDateTime(String str) throws ParseException {
  TimeZone timeZone = uss.getUserSession().getTimeZone();
  Datatype<Date> datatype = datatypeRegistry.getNN(Date.class);
  if (datatype instanceof DateTimeDatatype) {
    return ((DateTimeDatatype) datatype).parse(str, uss.getLocale(), timeZone);
  }
  return datatype.parse(str, uss.getLocale());
}

代码示例来源:origin: com.haulmont.bpm/bpm-gui

/**
 * Sets the value from the {@code formParam} to the field. Before setting is performed the value is
 * parsed or if an UEL expression is stored in {@code formParam} this expression is evaluated.
 */
protected void setFieldValue(Field field, ProcFormParam formParam, Datatype datatype, String actExecutionId) {
  if (!Strings.isNullOrEmpty(formParam.getValue())) {
    try {
      Object value;
      if (isExpression(formParam.getValue())) {
        value = processRuntimeService.evaluateExpression(formParam.getValue(), actExecutionId);
      } else {
        value = datatype.parse(formParam.getValue());
      }
      field.setValue(value);
    } catch (ParseException e) {
      throw new BpmException("Error when parsing process form parameter value", e);
    }
  }
}

代码示例来源:origin: com.haulmont.cuba/cuba-gui

@Override
  public void commitAndClose() {
    SessionAttribute item = datasource.getItem();
    if (item.getStringValue() != null) {
      Datatype dt = Datatypes.get(item.getDatatype());
      try {
        Object object = dt.parse(item.getStringValue());
        item.setStringValue(object == null ? "" : object.toString());
      } catch (IllegalArgumentException | ParseException e) {
        showNotification(getMessage("unableToParseValue"), NotificationType.ERROR);
        return;
      }
    }
    super.commitAndClose();
  }
}

代码示例来源:origin: com.haulmont.cuba/cuba-rest-api

protected Object parseValue(MetaProperty metaProperty, String stringValue) throws RestFilterParseException {
  if (metaProperty.getRange().isDatatype()) {
    try {
      return metaProperty.getRange().asDatatype().parse(stringValue);
    } catch (ParseException e) {
      throw new RestFilterParseException("Cannot parse property value: " + stringValue, e);
    }
  } else if (metaProperty.getRange().isEnum()) {
    try {
      return Enum.valueOf((Class<Enum>) metaProperty.getJavaType(), stringValue);
    } catch (IllegalArgumentException e) {
      throw new RestFilterParseException("Cannot parse enum value: " + stringValue, e);
    }
  }
  throw new RestFilterParseException("Cannot parse the condition value: " + stringValue);
}

代码示例来源:origin: com.haulmont.cuba/cuba-gui

protected Object loadEntity(String id) {
  Metadata metadata = AppBeans.get(Metadata.class);
  MetaProperty pkProp = metadata.getTools().getPrimaryKeyProperty(metadata.getClassNN(javaClass));
  Object objectId = null;
  if (pkProp != null) {
    Datatype<Object> datatype = pkProp.getRange().asDatatype();
    try {
      objectId = datatype.parse(id);
    } catch (ParseException e) {
      throw new RuntimeException("Error parsing entity ID", e);
    }
  }
  LoadContext ctx = new LoadContext(javaClass).setId(objectId);
  DataService dataService = AppBeans.get(DataService.NAME);
  return dataService.load(ctx);
}

相关文章