Package org.exolab.castor.xml

Examples of org.exolab.castor.xml.XMLFieldDescriptor


     */
    public XMLFieldDescriptor getFieldDescriptor(final String name, final String namespace,
            final NodeType nodeType) {
        if (nodeType == null || nodeType == NodeType.Element) {
            for (int i = 0; i < _elements.length; i++) {
                XMLFieldDescriptor desc = _elements[i];
                if (desc != null && desc.matches(name, namespace)) {
                    return desc;
                }
            }
        }
        return null;
View Full Code Here


     */
    public XMLFieldDescriptor getFieldDescriptor(final String name,
            final String namespace, final NodeType nodeType) {
        if (nodeType == null || nodeType == NodeType.Element) {
            for (int i = 0; i < _elements.length; i++) {
                XMLFieldDescriptor desc = _elements[i];
                if (desc != null && desc.matches(name)) {
                    return desc;
                }
            }
        }
        return null;
View Full Code Here

     * @return the descriptor that was removed from the list.
     */
    public XMLFieldDescriptor remove(int index) {

        if ((index < 0) || (index > elementCount)) return null;
        XMLFieldDescriptor desc = elements[index];
        shiftDown(index+1);
        --elementCount;
        return desc;
    } //-- remove
View Full Code Here

        if ((obj == null) || (!(obj instanceof XMLFieldDescriptor))) {
            return false;
        }

        XMLFieldDescriptor descriptor = (XMLFieldDescriptor) obj;

        // check field names
        if (!getFieldName().equals(descriptor.getFieldName())) {
            return false;
        }

        // check field types
        if (!getFieldType().equals(descriptor.getFieldType())) {
            return false;
        }

        // check field handler
        FieldHandler tmpHandler = descriptor.getHandler();
        if (getHandler() == null) {
            return (tmpHandler == null);
        } else if (tmpHandler == null) {
            return false;
        }
View Full Code Here

         
          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++;
View Full Code Here

     *         available.
     */
    public XMLFieldDescriptor getFieldDescriptor(String name, String namespace,
            NodeType nodeType) {
        boolean wild = ((nodeType == null) || _introspected);
        XMLFieldDescriptor result = null;

        XMLFieldDescriptor[] attributes = _attArray;
        XMLFieldDescriptor[] elements   = _elemArray;

        // TODO: clean up location patch
        String location = null;
        if (name != null) {
            int idx = name.lastIndexOf('/');
            if (idx >= 0) {
                location = name.substring(0, idx);
                name = name.substring(idx+1);
            }
        }

        if (wild || (nodeType == NodeType.Element)) {

            if (elements == null) elements = getElementArray();

//            if (_compositor == SEQUENCE && sequenceOfElements.size() > 0) {
//                XMLFieldDescriptor elementDescriptor = (XMLFieldDescriptor) sequenceOfElements.get(sequenceElementCount);
//                String elementName = elementDescriptor.getXMLName();
//                if (!elementName.equals(name)) {
//                    throw new IllegalArgumentException ("Element with name " + name + " passed to type " + getXMLName() + " in incorrect order; expected TODO.");
//                } else {
//                    sequenceElementCount++;
//                }
//            }
           
            for (int i = 0; i < elements.length; i++) {
                XMLFieldDescriptor desc = elements[i];
               
                if (desc == null) continue;

                if (location != null) {
                    if (!location.equals(desc.getLocationPath()))
                        continue;
                }

                if (desc.matches(name)) {
                      if (!desc.matches(WILDCARD)) {
                          // currently, a 'null' namespace value indicates that the XML artifact in question
                          // either is namespace-less or is part of the default namespace; in both cases,
                          // we currently can not perform namespace comparison.
                          // TODO[wguttmn, 20071205]: add code to handle default namespace declarations rather than checking for null
                          if (namespace == null
                                  || ResolveHelpers.namespaceEquals(namespace, desc.getNameSpaceURI())) {
                              return desc;
                          }
                      } else {
                          //--  possible wildcard match check for
                          //--  exact match (we need to extend the
                          //--  the descriptor interface to handle this
                          if (name.equals(desc.getXMLName()))
                              return desc;
                          //-- assume wild-card match
                          result = desc;
                      }
                }

                //handle container
                if ( desc.isContainer() ) {
                    XMLClassDescriptor xcd = (XMLClassDescriptor)desc.getClassDescriptor();
                    //prevent endless loop
                    if (xcd != this) {
                        //is it in this class descriptor?
                        if (xcd.getFieldDescriptor(name, namespace, NodeType.Element) != null) {
                            result = desc;
                            break;
                        }
                    }
                }//container
            }

            if (result != null)
                return result;
        }

        //-- handle attributes
        if (wild || (nodeType == NodeType.Attribute))
        {
            if (attributes == null) attributes = getAttributeArray();
            for (int i = 0; i < attributes.length; i++) {
                XMLFieldDescriptor desc = attributes[i];
                if (desc == null) continue;
                if (desc.matches(name)) {
                    return desc;
                }
            }
        }

        //-- handle namespace node
        if (nodeType == NodeType.Namespace) {
            if (attributes == null) attributes = getAttributeArray();
            for (int i = 0; i < attributes.length; i++) {
                if (attributes[i] == null) continue;
                if (attributes[i].getNodeType() == NodeType.Namespace) {
                    return attributes[i];
                }
            }
        }

        // To handle container object, we need to check if an attribute of a
        // container field match this attribute
        if (nodeType == NodeType.Attribute) {
            if (elements == null) elements = getElementArray();
            for (int i = 0; i < elements.length; i++) {
                XMLFieldDescriptor desc = elements[i];
                if (desc.isContainer()) {
                    XMLClassDescriptor xcd = (XMLClassDescriptor)desc.getClassDescriptor();
                    //prevent endless loop
                    if (xcd != this) {
                        //is it in this class descriptor?
                        XMLFieldDescriptor temp = xcd.getFieldDescriptor(name, namespace, NodeType.Attribute);
                        if (temp != null) {
                            return desc;
                        }
                    }
                }
View Full Code Here

    public void sortDescriptors() {

        //-- handle attributes
        XMLFieldDescriptor[] descriptors = getAttributeArray();
        for (int i = 0; i < descriptors.length; i++) {
            XMLFieldDescriptor fieldDesc = descriptors[i];
            switch (fieldDesc.getNodeType().getType()) {
                case NodeType.ELEMENT:
                    _elements.add(fieldDesc);
                    _attributes.remove(fieldDesc);
                    _attArray = null;
                    break;
                case NodeType.TEXT:
                    _attributes.remove(fieldDesc);
                    _attArray = null;
                    break;
                default:
                    break;
            }
        }

        //-- handle elements
        descriptors = getElementArray();
        for (int i = 0; i < descriptors.length; i++) {
            XMLFieldDescriptor fieldDesc = descriptors[i];
            switch (fieldDesc.getNodeType().getType()) {
                case NodeType.NAMESPACE:
                case NodeType.ATTRIBUTE:
                    _attributes.add(fieldDesc);
                    _elements.remove(fieldDesc);
                    _elemArray = null;
View Full Code Here

            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]);
                }
                //-- handle content, not affected by choice
                if (contentDescriptor != null) {
                    validateField(object, context, contentDescriptor);
                }
                break;
            //-- Currently SEQUENCE is handled the same as all
            case SEQUENCE:
            //-- ALL
            default:


                //-- handle elements
                for (int i = 0; i < localElements.length; i++) {
                    final XMLFieldDescriptor fieldDescriptor = localElements[i];
                    if (fieldDescriptor == null) {
                        continue;
                    }
                    validateField(object, context, fieldDescriptor);
                }
View Full Code Here

        boolean result = false;
        boolean hasValue = false;
        XMLFieldDescriptor[] fields = null;
        int i = 0;
        //1--direct look up for a field
        XMLFieldDescriptor fieldDesc = this.getFieldDescriptor(name, namespace, NodeType.Element);
        if (fieldDesc == null)
           fieldDesc = this.getFieldDescriptor(name, namespace, NodeType.Attribute);

        //if the descriptor is still null, the field can't be in stored in this classDescriptor
        if (fieldDesc == null)
            return false;

        Object tempObject;
        //3--The descriptor is multivalued
        if (fieldDesc.isMultivalued()) {
            //-- check size
            FieldValidator validator = fieldDesc.getValidator();
            if (validator != null) {
                if (validator.getMaxOccurs() < 0) {
                   result = true;
                }
                 else {
                    // count current objects and add 1
                    tempObject = fieldDesc.getHandler().getValue(object);
                    int current = Array.getLength(tempObject);
                    int newTotal = current + 1;
                    result = (newTotal <= validator.getMaxOccurs());
                 }
            }
            else {
                //-- not created by source generator...assume unbounded
                result = true;
            }
        }
        //3-- the fieldDescriptor is a container
        else if (fieldDesc.isContainer()) {
            tempObject = fieldDesc.getHandler().getValue(object);
            //if the object is not yet instantiated, we return true
            if (tempObject == null)
                result = true;
            else
                result = ((XMLClassDescriptor)fieldDesc.getClassDescriptor()).canAccept(name, namespace, tempObject);
        }
        //4-- Check if the value is set or not
        else {
            FieldHandler handler = fieldDesc.getHandler();

            boolean checkPrimitiveValue = true;
            if (handler instanceof AbstractFieldHandler) {
                hasValue = ((AbstractFieldHandler)handler).hasValue( object );
                //-- poor man's check for a generated handler, since
                //-- all generated handlers extend XMLFieldHandler, however
                //-- this doesn't guarantee that that handler is indeed
                //-- a generated handler, it does however mean that
                //-- the handler definately didn't come from the
                //-- MappingLoader.
                //checkPrimitiveValue = (!(handler instanceof XMLFieldHandler));
            }
            else {
                hasValue = (handler.getValue(object) != null);
            }

            //-- patch for primitives, we should check
            //-- for the has-method somehow...but this
            //-- is good enough for now. This may break
            //-- some non-Castor-generated code with
            //-- primitive values that have been set
            //-- with the same as the defaults
            if (hasValue &&
                checkPrimitiveValue &&
                fieldDesc.getFieldType().isPrimitive())
            {
                if (isDefaultPrimitiveValue(handler.getValue( object ))) {
                    hasValue = false;
                }
            }
            //-- end patch
            result = !hasValue;
        }

        //5--if there is no value and the _compositor is CHOICE
        //--we have to check to see if another value has not been set
        if (result && (_compositor == CHOICE)
            && (fieldDesc.getNodeType() == NodeType.Element) ) {
            fields = getElementArray();
            i = 0;
            while (result && i<fields.length) {
                XMLFieldDescriptor desc = fields[i];
                if (desc != fieldDesc && (object!=null) ) {
                    Object tempObj = desc.getHandler().getValue(object);
                    hasValue = (tempObj != null);
                    if (hasValue) {
                        result = false;
                        //special case for array
                        if (tempObj.getClass().isArray()) {
View Full Code Here

            final String namespace, final NodeType nodeType) {
       
        boolean wild = (nodeType == null);
       
        if (wild || (nodeType == NodeType.Element)) {
            XMLFieldDescriptor desc = null;
            for (int i = 0; i < _elementDescriptors.length; i++) {
                desc = _elementDescriptors[i];
                if (desc == null) {
                    continue;
                }
                if (desc.matches(name, namespace)) {
                    return desc;
                }
            }
        }
       
        if (wild || (nodeType == NodeType.Attribute)) {
            XMLFieldDescriptor desc = null;
            for (int i = 0; i < _attributeDescriptors.length; i++) {
                desc = _attributeDescriptors[i];
                if (desc == null) {
                    continue;
                }
                if (desc.matches(name, namespace)) {
                    return desc;
                }
            }
        }
       
View Full Code Here

TOP

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

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.