static void validateContent()

in xmlschema-walker/src/main/java/org/apache/ws/commons/schema/docpath/XmlSchemaElementValidator.java [173:236]


    static void validateContent(XmlSchemaStateMachineNode state, String elementContent,
                                NamespaceContext nsContext) throws ValidationException {

        if ((state == null) || (nsContext == null)
            || !state.getNodeType().equals(XmlSchemaStateMachineNode.Type.ELEMENT)) {
            throw new ValidationException("Niether state nor nsContext can be null, and state must be of an "
                                          + "SchemaStateMachineNode.Type.ELEMENT node, not "
                                          + ((state == null) ? null : state.getNodeType()));
        }

        final QName elemQName = state.getElement().getQName();
        final XmlSchemaTypeInfo elemType = state.getElementType();
        final XmlSchemaElement element = state.getElement();

        if (elementContent != null) {
            elementContent = elementContent.trim();
        }

        switch (elemType.getType()) {
        case COMPLEX: {
            if (!elemType.isMixed() && (elementContent != null) && (elementContent.length() > 0)) {

                /*
                 * If a type is COMPLEX, then it either is a mixed type or it
                 * only has elements as children. Likewise, if the text is not
                 * null or empty, and the type is not mixed, then element
                 * content is where it is not expected.
                 */
                throw new ValidationException(elemQName
                                              + " is a non-mixed complex type, therefore there should"
                                              + " not be any content between the tags, like \""
                                              + elementContent + "\".");
            }
            break;
        }
        case ATOMIC:
        case LIST:
        case UNION: {
            if ((elementContent == null) || (elementContent.length() == 0)) {
                if (state.getElement().isNillable()) {
                    // Null is a perfectly valid state.
                    return;
                } else {
                    elementContent = element.getDefaultValue();
                    if (elementContent == null) {
                        elementContent = element.getFixedValue();
                    }
                    if (elementContent == null) {
                        throw new ValidationException(
                                                      "Element "
                                                          + elemQName
                                                          + " has no content, no default value, and no fixed value,"
                                                          + " but is of type " + elemType.getType() + ".");
                    }
                }
            }
            validateType(elemQName.toString(), elementContent, elemType, nsContext);
            break;
        }
        default:
            throw new IllegalStateException(elemQName + " has an unrecognized content type of "
                                            + elemType.getType() + ".");
        }
    }