/**
   * 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;
  }
Esempio n. 2
0
  /**
   * Finds a value in the given {@code operationObject} whose key matches this attribute's {@link
   * #getName() name} and validates it using this attribute's {@link #getValidator() validator}.
   *
   * @param operationObject model node of type {@link ModelType#OBJECT}, typically representing an
   *     operation request
   * @return the value
   * @throws OperationFailedException if the value is not valid
   */
  public ModelNode validateOperation(final ModelNode operationObject)
      throws OperationFailedException {

    ModelNode node = new ModelNode();
    if (operationObject.has(name)) {
      node.set(operationObject.get(name));
    }
    if (isAllowExpression() && node.getType() == ModelType.STRING) {
      node = ParseUtils.parsePossibleExpression(node.asString());
    }
    if (!node.isDefined() && defaultValue.isDefined()) {
      validator.validateParameter(name, defaultValue);
    } else {
      validator.validateParameter(name, node);
    }

    return node;
  }
Esempio n. 3
0
 protected void readDomainElementAttributes_1_3(
     XMLExtendedStreamReader reader, Namespace expectedNs, ModelNode address, List<ModelNode> list)
     throws XMLStreamException {
   final int count = reader.getAttributeCount();
   for (int i = 0; i < count; i++) {
     Namespace ns = Namespace.forUri(reader.getAttributeNamespace(i));
     switch (ns) {
       case XML_SCHEMA_INSTANCE:
         {
           switch (Attribute.forName(reader.getAttributeLocalName(i))) {
             case SCHEMA_LOCATION:
               {
                 parseSchemaLocations(reader, address, list, i);
                 break;
               }
             case NO_NAMESPACE_SCHEMA_LOCATION:
               {
                 // todo, jeez
                 break;
               }
             default:
               {
                 throw unexpectedAttribute(reader, i);
               }
           }
           break;
         }
       default:
         switch (Attribute.forName(reader.getAttributeLocalName(i))) {
           case NAME:
             ModelNode op = new ModelNode();
             op.get(OP).set(WRITE_ATTRIBUTE_OPERATION);
             op.get(NAME).set(NAME);
             op.get(VALUE).set(ParseUtils.parsePossibleExpression(reader.getAttributeValue(i)));
             list.add(op);
             break;
           default:
             throw unexpectedAttribute(reader, i);
         }
     }
   }
 }