/**
   * Creates and returns a {@link org.jboss.dmr.ModelNode} using the given {@code value} after first
   * validating the node against {@link #getValidator() this object's validator}.
   *
   * <p>If {@code value} is {@code null} and a {@link #getDefaultValue() default value} is
   * available, the value of that default value will be used.
   *
   * @param value the value. Will be {@link String#trim() trimmed} before use if not {@code null}.
   * @param location current location of the parser's {@link javax.xml.stream.XMLStreamReader}. Used
   *     for any exception message
   * @return {@code ModelNode} representing the parsed value
   * @throws javax.xml.stream.XMLStreamException if {@code value} is not valid
   */
  public ModelNode parse(final String value, final Location location) throws XMLStreamException {

    final String trimmed = value == null ? null : value.trim();
    ModelNode node;
    if (trimmed != null) {
      if (isAllowExpression()) {
        node = ParseUtils.parsePossibleExpression(trimmed);
      } else {
        node = new ModelNode().set(trimmed);
      }
      if (node.getType() != ModelType.EXPRESSION) {
        // Convert the string to the expected type
        switch (getType()) {
          case BIG_DECIMAL:
            node.set(node.asBigDecimal());
            break;
          case BIG_INTEGER:
            node.set(node.asBigInteger());
            break;
          case BOOLEAN:
            node.set(node.asBoolean());
            break;
          case BYTES:
            node.set(node.asBytes());
            break;
          case DOUBLE:
            node.set(node.asDouble());
            break;
          case INT:
            node.set(node.asInt());
            break;
          case LONG:
            node.set(node.asLong());
            break;
        }
      }
    } else if (getDefaultValue().isDefined()) {
      node = new ModelNode().set(getDefaultValue());
    } else {
      node = new ModelNode();
    }

    try {
      getValidator().validateParameter(getXmlName(), node);
    } catch (OperationFailedException e) {
      throw new XMLStreamException(e.getFailureDescription().toString(), location);
    }

    return node;
  }
Exemple #2
0
 public static ModelNode parseAttributeValue(
     final String value, final boolean isExpressionAllowed, final ModelType attributeType) {
   final String trimmed = value == null ? null : value.trim();
   ModelNode node;
   if (trimmed != null) {
     if (isExpressionAllowed && isExpression(trimmed)) {
       node = new ModelNode(new ValueExpression(trimmed));
     } else {
       if (attributeType == STRING || attributeType == PROPERTY) {
         node = new ModelNode().set(value);
       } else {
         node = new ModelNode().set(trimmed);
       }
     }
     if (node.getType() != ModelType.EXPRESSION) {
       // Convert the string to the expected type
       switch (attributeType) {
         case BIG_DECIMAL:
           node.set(node.asBigDecimal());
           break;
         case BIG_INTEGER:
           node.set(node.asBigInteger());
           break;
         case BOOLEAN:
           node.set(node.asBoolean());
           break;
         case BYTES:
           node.set(node.asBytes());
           break;
         case DOUBLE:
           node.set(node.asDouble());
           break;
         case INT:
           node.set(node.asInt());
           break;
         case LONG:
           node.set(node.asLong());
           break;
       }
     }
   } else {
     node = new ModelNode();
   }
   return node;
 }