Package org.exolab.castor.xml

Examples of org.exolab.castor.xml.ValidationException


        // -- check name
        if (_parent != null && _parent.getStructureType() != Structure.SCHEMA) {
            if (getName() != null) {
                String err = "Only top-level complexTypes can be named.";
                err += getName() + "is not a valid complexType.";
                throw new ValidationException(err);
            }
        }
        // -- check attributes
        _attributes.validate();

        // -- check content model
        Enumeration enumeration = _contentModel.enumerate();
        while (enumeration.hasMoreElements()) {
            ((Structure) enumeration.nextElement()).validate();
        }

        // -- make sure baseType is accessible
        XMLType type = getBaseType();
        if ((type == null) && (_baseType != null)) {
            String error = "The base type '" + _baseType + "' was not found.";
            throw new ValidationException(error);
        }
        if (type != null) {
            if (type.getStructureType() == Structure.SIMPLE_TYPE) {
                if (_restricted) {
                    String name = getName();
                    if (name == null) {
                        name = "anonymous-complexType-for-element: ";
                        if (_parent != null) {
                            // -- parent should be an element if name is null,
                            // but
                            // -- we'll check the type to be on the safe side
                            if (_parent.getStructureType() == Structure.ELEMENT) {
                                name += ((ElementDecl) _parent).getName();
                            } else {
                                name += _parent.toString();
                            }
                        }
                    }
                    String err = "complexType: " + name;
                    err += "; A complex type cannot be a restriction"
                            + " of a simpleType:";
                    err += type.getName();
                    throw new ValidationException(err);
                }
            } else if (type.getStructureType() == Structure.COMPLEX_TYPE) {

                if (!_complexContent) {
                    // we are now sure that the base is a ComplexType
                    // but is the base of this complexType a simpleType? (see
                    // 4.3.3->simpleContent->content type)
                    if (((ComplexType) type).getContentType().getType() != ContentType.SIMPLE) {
                        String name = getName();
                        if (name == null) {
                            name = "anonymous-complexType-for-element: ";
                            if (_parent != null) {
                                // -- parent should be an element if name is
                                // null, but
                                // -- we'll check the type to be on the safe
                                // side
                                if (_parent.getStructureType() == Structure.ELEMENT) {
                                    name += ((ElementDecl) _parent).getName();
                                } else {
                                    name += _parent.toString();
                                }
                            }
                        }
                        String err = "complexType: " + name;
                        err += "; When a complexType is a restriction of simpleContent the base type"
                                + " must be a complexType whose base is also simpleContent.";
                        throw new ValidationException(err);
                    }
                }
            }
        }
View Full Code Here


    public void validate()
        throws ValidationException
    {
        if ((_attributeRef == null) && (_name == null))  {
            String err = "<attribute> is missing required 'name' attribute.";
            throw new ValidationException(err);
        }

         if (_attributeRef != null) {
            if (_schema.getAttribute(_attributeRef) == null) {
                String err = "<attribute ref=\"" + _attributeRef + "\"> "+
                    "is not resolvable.";
                throw new ValidationException(err);
            }
            return;
        }
       
    } //-- validate
View Full Code Here

            // populate the JNDI configuration with the default values for
            // the connector
            config.setJndiConfiguration(
                    JndiConfigurationFactory.create(config));
        } else if (config.getJndiConfiguration() == null) {
            throw new ValidationException(
                    "JndiConfiguration must be provided when "
                    + "ServerConfiguration/embeddedJNDI is false");
        }
        return config;
    }
View Full Code Here

        else if (_fields.size() < 1) {
            err = "There must be at least one 'field' in an "
                + "identity constraint.";
        }
       
        if (err != null) throw new ValidationException(err);
       
    } //-- validate
View Full Code Here

    public void checkDescriptorForCorrectOrderWithinSequence(
            final XMLFieldDescriptor elementDescriptor, UnmarshalState parentState, String xmlName) throws ValidationException {
        if (_compositor == SEQUENCE && sequenceOfElements.size() > 0) {
         
          if (parentState.expectedIndex == sequenceOfElements.size() ) {
            throw new ValidationException ("Element with name " + xmlName + " passed to type " + getXMLName() + " in incorrect order; It is not allowed to be the last element of this sequence!");
          }
         
          XMLFieldDescriptor expectedElementDescriptor = (XMLFieldDescriptor) sequenceOfElements.get(parentState.expectedIndex);
           
            String expectedElementName = expectedElementDescriptor.getXMLName();
            String elementName = xmlName;

            boolean anyNode = expectedElementDescriptor.getFieldName().equals("_anyObject") && expectedElementName == null;
           
            // choices
            if (!anyNode && expectedElementDescriptor.getXMLName().equals("-error-if-this-is-used-")) {
             
              // find possible names
              ArrayList possibleNames = new ArrayList();
              fillPossibleNames(possibleNames, expectedElementDescriptor);
             
              // check name
              if (!possibleNames.contains(elementName)) {
                if (!expectedElementDescriptor.isRequired()) {
                   parentState.expectedIndex++;
                         checkDescriptorForCorrectOrderWithinSequence(elementDescriptor, parentState, xmlName);
                } else {
                  throw new ValidationException ("Element with name " + elementName + " passed to type " + getXMLName() + " in incorrect order; expected element has to be member of the expected choice.");
                }
              } else {
                parentState.expectedIndex++;
              }
                return;
            }

            // multi valued flag
            if (expectedElementDescriptor.isMultivalued() && !parentState.withinMultivaluedElement) {
              parentState.withinMultivaluedElement = true;
            }
           
            if (!anyNode && !(expectedElementName).equals(elementName)) {
               
                // handle substitution groups !!!
                List substitutes =  expectedElementDescriptor.getSubstitutes();
                if (substitutes != null && !substitutes.isEmpty()) {
                    if (substitutes.contains(elementName)) {
                        if (!parentState.withinMultivaluedElement) {
                            parentState.expectedIndex++;
                        }
                        return;
                    }
                }
                // handle multi-valued fields
                if (expectedElementDescriptor.isMultivalued()) {
                  parentState.withinMultivaluedElement = false;
                    parentState.expectedIndex++;
                    checkDescriptorForCorrectOrderWithinSequence(elementDescriptor, parentState, xmlName);
                    return;
                }
                // handle required fields
                if (expectedElementDescriptor.isRequired()) {
                    throw new ValidationException ("Element with name " + elementName + " passed to type " + getXMLName() + " in incorrect order; expected element with name '" + expectedElementName + "' or any other optional element declared prior to it.");
                }
               
                // non required field, proceed until next required field
                parentState.expectedIndex++;
                checkDescriptorForCorrectOrderWithinSequence(elementDescriptor, parentState, xmlName);
View Full Code Here

     * @param context the ValidationContext
     */
    public void validate(Object object, ValidationContext context)
    throws ValidationException {
        if (object == null) {
            throw new ValidationException("Cannot validate a null object.");
        }
        Class a = getJavaClass();
        ClassLoader acl = a.getClassLoader();
        Class b = object.getClass();
        ClassLoader bcl = b.getClassLoader();
        if (!getJavaClass().isAssignableFrom(object.getClass())) {
            String err = "The given object is not an instance of the class"
                + " described by this ClassDecriptor.";
            throw new ValidationException(err);
        }

        //-- DEBUG
        //System.out.println("Validating class: " + object.getClass().getName());
        //-- /DEBUG

        XMLFieldDescriptor[] localElements = getElementArray();
        XMLFieldDescriptor[] localAttributes = getAttributeArray();

        if (_extends != null) {

            //-- cascade call for validation
            if (_extends instanceof XMLClassDescriptorImpl) {
                ((XMLClassDescriptorImpl) _extends).validate(object, context);
            } else {
                TypeValidator baseValidator = _extends.getValidator();
                if (baseValidator != null) {
                    baseValidator.validate(object, context);
                }
            }

            //-- get local element descriptors by filtering out inherited ones
            XMLFieldDescriptor[] inheritedElements   = _extends.getElementDescriptors();
            XMLFieldDescriptor[] allElements = localElements;
            localElements = new XMLFieldDescriptor[allElements.length - inheritedElements.length];
            int localIdx = 0;
            for (int i = 0; i < allElements.length; i++) {
                XMLFieldDescriptor desc = allElements[i];
                boolean isInherited = false;
                for (int idx = 0; idx < inheritedElements.length; idx++) {
                    if (inheritedElements[idx].equals(desc)) {
                        isInherited = true;
                        break;
                    }
                }
                if (!isInherited) {
                    localElements[localIdx] = desc;
                    ++localIdx;
                }
            }

            //-- get local attribute descriptors by filtering out inherited ones
            XMLFieldDescriptor[] inheritedAttributes = _extends.getAttributeDescriptors();
            XMLFieldDescriptor[] allAttributes = localAttributes;
            localAttributes =
                new XMLFieldDescriptor[allAttributes.length - inheritedAttributes.length];
            localIdx = 0;
            for (int i = 0; i < allAttributes.length; i++) {
                XMLFieldDescriptor desc = allAttributes[i];
                boolean isInherited = false;
                for (int idx = 0; idx < inheritedAttributes.length; idx++) {
                    if (inheritedAttributes[idx].equals(desc)) {
                        isInherited = true;
                        break;
                    }
                }
                if (!isInherited) {
                    localAttributes[localIdx] = desc;
                    ++localIdx;
                }
            }

        }

        switch (_compositor) {

            case CHOICE:

                boolean found = false;
                boolean hasLocalDescs = (localElements.length > 0);
                XMLFieldDescriptor fieldDesc = null;
                //-- handle elements, affected by choice
                for (int i = 0; i < localElements.length; i++) {
                    XMLFieldDescriptor desc = localElements[i];

                    if (desc == null) {
                        continue;
                    }

                    FieldHandler handler = desc.getHandler();

                    if (handler.getValue(object) != null) {
                         //Special case if we have a Vector, an ArrayList
                         //or an Array --> need to check if it is not empty
                         if (desc.isMultivalued()) {
                             Object temp = handler.getValue(object);
                             //-- optimize this?
                             if (Array.getLength(temp) == 0) {
                                  temp = null;
                                  continue;
                             }
                             temp = null;
                         }

                        if (found) {
                            String err = null;
                            if (desc.isContainer()) {
                                err = "The group '" + desc.getFieldName();
                                err += "' cannot exist at the same time that ";
                                if (fieldDesc.isContainer()) {
                                    err += "the group '" + fieldDesc.getFieldName();
                                } else {
                                    err += "the element '" + fieldDesc.getXMLName();
                                }
                                err += "' also exists.";
                            } else {
                                 err = "The element '" + desc.getXMLName();
                                 err += "' cannot exist at the same time that ";
                                 err += "element '" + fieldDesc.getXMLName() + "' also exists.";
                            }
                            throw new ValidationException(err);
                        }
                        found = true;
                        fieldDesc = desc;

                        FieldValidator fieldValidator = desc.getValidator();
                        if (fieldValidator != null) {
                            fieldValidator.validate(object, context);
                        }
                    }
                }

                // if all elements are mandatory, print the grammar that the choice
                // must match.
                if ((!found) && (hasLocalDescs))  {
                    StringBuffer buffer = new StringBuffer(40);
                    boolean existsOptionalElement = false;
                    buffer.append('(');
                    String sep = " | ";
                    for (int i = 0; i < localElements.length; i++) {
                        XMLFieldDescriptor  desc = localElements[i];
                        if (desc == null) {
                            continue;
                        }

                        FieldValidator fieldValidator = desc.getValidator();
                        if (fieldValidator.getMinOccurs() == 0) {
                            existsOptionalElement = true;
                            break;
                        }
                        buffer.append(sep);
                        buffer.append(desc.getXMLName());
                    }
                    buffer.append(')');
                    if (!existsOptionalElement) {
                        String err = "In the choice contained in <" + this.getXMLName()
                                     + ">, at least one of these elements must appear:\n"
                                     + buffer.toString();
                        throw new ValidationException(err);
                    }
                }
                //-- handle attributes, not affected by choice
                for (int i = 0; i < localAttributes.length; i++) {
                    validateField(object, context, localAttributes[i]);
View Full Code Here

    public void validate(final String value, final ValidationContext context)
                                                    throws ValidationException {
        if (value == null) {
            if (_required && !isNillable()) {
                String err = "this is a required field and cannot be null.";
                throw new ValidationException(err);
            }
            return;
        }

        if (_fixed != null && !_fixed.equals(value)) {
            String err = "strings of this type must be equal to the fixed value of " + _fixed;
            throw new ValidationException(err);
        }

        int len = value.length();

        if (_length > 0 && len != _length) {
            String err = "Strings of this type must have a length of " + _length + " characters";
            throw new ValidationException(err);
        }

        if (_minLength > 0 && len < _minLength) {
            String err = "Strings of this type must have a minimum length of " + _minLength
                    + " characters";
            throw new ValidationException(err);
        }

        if (_maxLength >= 0 && len > _maxLength) {
            String err = "Strings of this type must have a maximum length of " + _maxLength
                    + " characters";
            throw new ValidationException(err);
        }

        if (hasPattern()) {
            super.validate(value, context);
        }
View Full Code Here

    public void validate(final Object object, final ValidationContext context)
                                                    throws ValidationException {
        if (object == null) {
            if (_required) {
                String err = "this is a required field and cannot be null.";
                throw new ValidationException(err);
            }
            return;
        }

        validate(object.toString(), context);
View Full Code Here

     */
    public void validate(final long i, final ValidationContext context)
                                                    throws ValidationException {
        if (_useFixed && i != _fixed) {
            String err = "long " + i + " is not equal to the fixed value: " + _fixed;
            throw new ValidationException(err);
        }

        if (_useMin && i < _min) {
            String err = "long " + i + " is less than the minimum allowed value: " + _min;
            throw new ValidationException(err);
        }

        if (_useMax && i > _max) {
            String err = "long " + i + " is greater than the maximum allowed value: " + _max;
            throw new ValidationException(err);
        }

        if (_totalDigits != -1) {
            int length = Long.toString(i).length();
            if (i < 0) {
                length--;
            }
            if (length > _totalDigits) {
                String err = "long " + i + " has too many digits -- must have "
                        + _totalDigits + " digits or fewer.";
                throw new ValidationException(err);
            }
        }

        if (hasPattern()) {
            super.validate(Long.toString(i), context);
View Full Code Here

     */
    public void validate(final Object object, final ValidationContext context)
                                                    throws ValidationException {
        if (object == null) {
            String err = "IntegerValidator cannot validate a null object.";
            throw new ValidationException(err);
        }

        long value = 0;
        try {
            value = ((Long) object).longValue();
        } catch (Exception ex) {
            String lenientProperty = context.getInternalContext()
                .getStringProperty(XMLProperties.LENIENT_INTEGER_VALIDATION);
            if (Boolean.valueOf(lenientProperty).booleanValue()) {
                try {
                    value = ((Integer) object).longValue();
                } catch (Exception e) {
                    String err = "Expecting a Long/Integer, received instead: ";
                    err += object.getClass().getName();
                    throw new ValidationException(err);
                }
            } else {
                String err = "Expecting an Long, received instead: ";
                err += object.getClass().getName();
                throw new ValidationException(err);
            }
        }
        validate(value, context);
    }
View Full Code Here

TOP

Related Classes of org.exolab.castor.xml.ValidationException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.