Package org.exolab.castor.xml.util

Examples of org.exolab.castor.xml.util.XMLFieldDescriptorImpl


*/
public class CategoryDescriptor extends BaseHarnessDescriptor {
    public CategoryDescriptor() {
        _xmlName = "category";
        _nsPrefix = "jtf";
        XMLFieldDescriptorImpl desc = null;
        XMLFieldHandler handler = null;
        //-- initialize attribute descriptors
       
        _attributeDescriptors = new XMLFieldDescriptorImpl[2];
        //-- _name
        desc = new XMLFieldDescriptorImpl(String.class, "_name", "name", NodeType.Attribute);
        desc.setImmutable(true);
        desc.setHandler(new XMLFieldHandler() {
            public Object getValue(final Object object) throws IllegalStateException {
                Category target = (Category) object;
                return target.getName();
            }
            public void setValue(final Object object, final Object value)
            throws IllegalStateException, IllegalArgumentException {
                try {
                    Category target = (Category) object;
                    target.setName((String) value);
                } catch (Exception ex) {
                    throw new IllegalStateException(ex.toString());
                }
            }
            public Object newInstance(final Object parent) {
                return null;
            }
        });
        _attributeDescriptors[0] = desc;
       
        //-- _name
        desc = new XMLFieldDescriptorImpl(String.class, "_className", "class", NodeType.Attribute);
        desc.setImmutable(true);
        desc.setHandler(new XMLFieldHandler() {
            public Object getValue(final Object object) throws IllegalStateException {
                Category target = (Category) object;
                return target.getClassName();
            }
            public void setValue(final Object object, final Object value)
            throws IllegalStateException, IllegalArgumentException {
                try {
                    Category target = (Category) object;
                    target.setClassName((String) value);
                } catch (Exception ex) {
                    throw new IllegalStateException(ex.toString());
                }
            }
            public Object newInstance(final Object parent) {
                return null;
            }
        });
        _attributeDescriptors[1] = desc;

        //-- initialize element descriptors
       
        _elementDescriptors = new XMLFieldDescriptorImpl[3];
        //-- _description
        desc = new XMLFieldDescriptorImpl(
                String.class, "_description", "description", NodeType.Element);
        desc.setImmutable(true);
        handler = (new XMLFieldHandler() {
            public Object getValue(final Object object) throws IllegalStateException {
                Category target = (Category) object;
                return target.getDescription();
            }
            public void setValue(final Object object, final Object value)
            throws IllegalStateException, IllegalArgumentException {
                try {
                    Category target = (Category) object;
                    target.setDescription((String) value);
                } catch (Exception ex) {
                    throw new IllegalStateException(ex.toString());
                }
            }
            public Object newInstance(final Object parent) {
                return null;
            }
        });
        desc.setHandler(handler);
        desc.setMultivalued(false);
        _elementDescriptors[0] = desc;
               
        //-- _case
        desc = new XMLFieldDescriptorImpl(Case.class, "_case", "case", NodeType.Element);
        desc.setImmutable(false);
        handler = (new XMLFieldHandler() {
            public Object getValue(final Object object) throws IllegalStateException {
                Category target = (Category) object;
                return target.getCase();
            }
            public void setValue(final Object object, final Object value)
            throws IllegalStateException, IllegalArgumentException {
                try {
                    Category target = (Category) object;
                    target.addCase((Case) value);
                } catch (Exception ex) {
                    throw new IllegalStateException(ex.toString());
                }
            }
            public Object newInstance(final Object parent) {
                return null;
            }
        });
        desc.setHandler(handler);
        desc.setMultivalued(true);
        _elementDescriptors[1] = desc;

        //-- _object
        desc = (new XMLFieldDescriptorImpl(
                Object.class, "_object", (String) null, NodeType.Element) {
            public boolean matches(final String xmlName) {
                return true;
            }
        });
        desc.setImmutable(false);
        handler = (new XMLFieldHandler() {
            public Object getValue(final Object object) throws IllegalStateException {
                Category target = (Category) object;
                return target.getObject();
            }
            public void setValue(final Object object, final Object value)
            throws IllegalStateException, IllegalArgumentException {
                try {
                    Category target = (Category) object;
                    target.setObject(value);
                } catch (Exception ex) {
                    throw new IllegalStateException(ex.toString());
                }
            }
            public Object newInstance(final Object parent) {
                return null;
            }
        });
        desc.setHandler(handler);
        desc.setMultivalued(false);
        _elementDescriptors[2] = desc;
    }
View Full Code Here


    //--------------------/

    public CaseDescriptor() {
        _xmlName = "Case";
        _nsPrefix = "jtf";
        XMLFieldDescriptorImpl desc = null;
        //-- initialize attribute descriptors
       
        _attributeDescriptors = new XMLFieldDescriptorImpl[1];

        //-- _class
        desc = new XMLFieldDescriptorImpl(
                java.lang.String.class, "_className", "class", NodeType.Attribute);
        desc.setImmutable(true);
        desc.setHandler(new XMLFieldHandler() {
            public Object getValue(final Object object) throws IllegalStateException {
                Case target = (Case) object;
                return target.getClassName();
            }
            public void setValue(final Object object, final Object value)
View Full Code Here

*/
public class HarnessDescriptor extends BaseHarnessDescriptor {
    public HarnessDescriptor() {
        _xmlName = "harness";
        _nsPrefix = "jtf";
        XMLFieldDescriptorImpl desc = null;
        XMLFieldHandler handler = null;
        //-- initialize attribute descriptors
       
        _attributeDescriptors = new XMLFieldDescriptorImpl[1];
        //-- _name
        desc = new XMLFieldDescriptorImpl(
                String.class, "_name", "name", NodeType.Attribute);
        desc.setImmutable(true);
        desc.setHandler(new XMLFieldHandler() {
            public Object getValue(final Object object) throws IllegalStateException {
                Harness target = (Harness) object;
                return target.getName();
            }
            public void setValue(final Object object, final Object value)
            throws IllegalStateException, IllegalArgumentException {
                try {
                    Harness target = (Harness) object;
                    target.setName((String) value);
                } catch (Exception ex) {
                    throw new IllegalStateException(ex.toString());
                }
            }
            public Object newInstance(final Object parent) {
                return null;
            }
        });
        _attributeDescriptors[0] = desc;
       
        //-- initialize element descriptors
       
        _elementDescriptors = new XMLFieldDescriptorImpl[2];
        //-- _description
        desc = new XMLFieldDescriptorImpl(
                String.class, "_description", "description", NodeType.Element);
        desc.setImmutable(true);
        handler = (new XMLFieldHandler() {
            public Object getValue(final Object object) throws IllegalStateException {
                Harness target = (Harness) object;
                return target.getDescription();
            }
            public void setValue(final Object object, final Object value)
            throws IllegalStateException, IllegalArgumentException {
                try {
                    Harness target = (Harness) object;
                    target.setDescription((String) value);
                } catch (Exception ex) {
                    throw new IllegalStateException(ex.toString());
                }
            }
            public Object newInstance(final Object parent) {
                return null;
            }
        });
        desc.setHandler(handler);
        desc.setMultivalued(false);
        _elementDescriptors[0] = desc;
       
        //-- _category
        desc = new XMLFieldDescriptorImpl(
                Category.class, "_category", "category", NodeType.Element);
        handler = (new XMLFieldHandler() {
            public Object getValue(final Object object) throws IllegalStateException {
                Harness target = (Harness) object;
                return target.getCategory();
            }
            public void setValue(final Object object, final Object value)
            throws IllegalStateException, IllegalArgumentException {
                try {
                    Harness target = (Harness) object;
                    target.setCategory((Category) value);
                } catch (Exception ex) {
                    throw new IllegalStateException(ex.toString());
                }
            }
            public Object newInstance(final Object parent) {
                return new Category();
            }
        });
        desc.setHandler(handler);
        desc.setMultivalued(true);
        _elementDescriptors[1] = desc;
    }
View Full Code Here

        FieldDescriptor        fieldDesc;
        FieldMappingCollectionType         colType  = fieldMap.getCollection();
        String                 xmlName  = null;
        NodeType               nodeType = null;
        String                 match    = null;
        XMLFieldDescriptorImpl xmlDesc;
        boolean                isReference = false;
        boolean                isXMLTransient = false;

        //-- handle special case for HashMap/Hashtable
        if ((fieldMap.getType() == null) && (colType != null)) {
            if ((colType == FieldMappingCollectionType.HASHTABLE) ||
                (colType == FieldMappingCollectionType.MAP) ||
                (colType == FieldMappingCollectionType.SORTEDMAP))
            {
                fieldMap.setType(MapItem.class.getName());
            }
        }

        // Create an XML field descriptor
        fieldDesc = super.createFieldDesc( javaClass, fieldMap );

        BindXml xml = fieldMap.getBindXml();

        boolean deriveNameByClass = false;

        if (xml != null) {
            //-- xml name
            xmlName = xml.getName();

            //-- node type
            if ( xml.getNode() != null )
                nodeType = NodeType.getNodeType( xml.getNode().toString() );

            //-- matches
            match = xml.getMatches();

            //-- reference
            isReference = xml.getReference();

            //-- XML transient
            isXMLTransient = xml.getTransient();

            //-- autonaming
            BindXmlAutoNamingType autoName = xml.getAutoNaming();
            if (autoName != null) {
                deriveNameByClass = (autoName == BindXmlAutoNamingType.DERIVEBYCLASS);
            }

        }

        //-- transient
        //-- XXXX -> if it's transient we probably shouldn't do all
        //-- XXXX -> the unecessary work
        isXMLTransient = isXMLTransient || fieldDesc.isTransient();

        //--

        //-- handle QName for xmlName
        String namespace = null;
        if ((xmlName != null) && (xmlName.length() > 0)){
            if (xmlName.charAt(0) == '{') {
                int idx = xmlName.indexOf('}');
                if (idx < 0) {
                    throw new MappingException("Invalid QName: " + xmlName);
                }
                namespace = xmlName.substring(1, idx);
                xmlName = xmlName.substring(idx+1);
            }
            else if (xmlName.startsWith(XML_PREFIX)) {
                namespace = Namespaces.XML_NAMESPACE;
                xmlName = xmlName.substring(4);
            }
        }

        if (nodeType == null) {
            if (isPrimitive(javaClass))
                nodeType = getInternalContext().getPrimitiveNodeType();
            else
                nodeType = NodeType.Element;
        }

        //-- Create XML name if necessary. Note if name is to be derived
        //-- by class..we just make sure we set the name to null...
        //-- the Marshaller does this during runtime. This allows
        //-- Collections to be handled properly.
        if ((!deriveNameByClass) && ((xmlName == null) && (match == null)))
        {
            xmlName = getInternalContext().getXMLNaming().toXMLName( fieldDesc.getFieldName() );
            match = xmlName + ' ' + fieldDesc.getFieldName();
        }

        xmlDesc = new XMLFieldDescriptorImpl( fieldDesc, xmlName, nodeType, getInternalContext().getPrimitiveNodeType() );
       
        if (xmlDesc.getHandler() != null && xmlDesc.getHandler() instanceof AbstractFieldHandler) {
            AbstractFieldHandler handler = (AbstractFieldHandler) xmlDesc.getHandler();
            handler.setFieldDescriptor(xmlDesc);
        }

        //-- transient?
        xmlDesc.setTransient(isXMLTransient);

        //--set a default fieldValidator
        xmlDesc.setValidator(new FieldValidator());

        //-- enable use parent namespace if explicit one doesn't exist
        xmlDesc.setUseParentsNamespace(true);

        //-- If deriveNameByClass we need to reset the name to
        //-- null because XMLFieldDescriptorImpl tries to be smart
        //-- and automatically creates the name.
        if (deriveNameByClass) {
            xmlDesc.setXMLName(null);
        }

        //-- namespace
        if (namespace != null) {
            xmlDesc.setNameSpaceURI(namespace);
        }

        //-- matches
        if (match != null) {
            xmlDesc.setMatches(match);
            //-- special fix for xml-name since XMLFieldDescriptorImpl
            //-- will create a default name based off the field name
            if (xmlName == null) xmlDesc.setXMLName(null);
        }

        //-- reference
        xmlDesc.setReference(isReference);
        if (isReference) {
          if (colType == null) {
            FieldValidator fieldValidator = new FieldValidator();
            fieldValidator.setValidator(new IdRefValidator());
            xmlDesc.setValidator(fieldValidator);
          } else {
            // TODO handle other cases
          }
        }

        xmlDesc.setContainer(fieldMap.getContainer());
       
        xmlDesc.setNillable(fieldMap.isNillable());

        if (xml != null) {

            //-- has class descriptor for type specified
            if (xml.getClassMapping() != null) {
                ClassDescriptor cd = createClassDescriptor(xml.getClassMapping());
                xmlDesc.setClassDescriptor(cd);
            }

            //-- has location path?
            if (xml.getLocation() != null) {
                xmlDesc.setLocationPath(xml.getLocation());
            }
            //is the value type needs specific handling
            //such as QName or NCName support?
            String xmlType = xml.getType();
            xmlDesc.setSchemaType(xmlType);
            xmlDesc.setQNamePrefix(xml.getQNamePrefix());
            TypeValidator validator = null;
            if (NCNAME.equals(xmlType)) {
                validator = new NameValidator(XMLConstants.NAME_TYPE_NCNAME);
                xmlDesc.setValidator(new FieldValidator(validator));
            }

            //-- special properties?
            Property[] props = xml.getProperty();
            if ((props != null) && (props.length > 0)) {
                for (int pIdx = 0; pIdx < props.length; pIdx++) {
                    Property prop = props[pIdx];
                    xmlDesc.setXMLProperty(prop.getName(), prop.getValue());
                }
            }
        }

        //-- Get collection type
        if (colType == null) {
            //-- just in case user forgot to use collection="..."
            //-- in the mapping file
            Class type = fieldDesc.getFieldType();
            if (type != null && CollectionHandlers.hasHandler(type)) {
                String typeName = CollectionHandlers.getCollectionName(type);
                colType = FieldMappingCollectionType.valueOf(typeName);
            }
        }

        //-- isMapped item
        if (colType != null) {
            if ((colType == FieldMappingCollectionType.HASHTABLE) ||
                (colType == FieldMappingCollectionType.MAP) ||
                (colType == FieldMappingCollectionType.SORTEDMAP))
            {
                //-- Make sure user is not using an addMethod
                //-- before setting the mapped field to true.
                String methodName = fieldMap.getSetMethod();
                if (methodName != null) {
                    if (!methodName.startsWith("add")) {
                        xmlDesc.setMapped(true);
                    }
                }
                else xmlDesc.setMapped(true);
            }


            //-- special NodeType.Namespace handling
            //-- prevent FieldHandlerImpl from using CollectionHandler
            //-- during calls to #getValue
            if ((nodeType == NodeType.Namespace) || (xmlDesc.isMapped())) {
                Object handler = xmlDesc.getHandler();
                if (handler instanceof FieldHandlerImpl) {
                    FieldHandlerImpl handlerImpl = (FieldHandlerImpl)handler;
                    handlerImpl.setConvertFrom(new IdentityConvertor());
                }
            }
            //-- wrap collection in element?
            if (nodeType == NodeType.Element) {
                if (fieldMap.hasContainer() && (!fieldMap.getContainer())) {
                    xmlDesc = wrapCollection(xmlDesc);
                }
            }
        }

        //-- is Type-Safe Enumeration?
        //-- This is not very clean, we should have a way
        //-- to specify something is a type-safe enumeration
        //-- without having to guess.
        else if ((!isReference) && (!isXMLTransient)) {
            Class fieldType = xmlDesc.getFieldType();
            if (!isPrimitive(fieldType)) {
                //-- make sure no default constructor
                Constructor cons = null;
                try {
                    cons = fieldType.getConstructor(EMPTY_ARGS);
                    if (!Modifier.isPublic(cons.getModifiers())) {
                        cons = null;
                    }
                }
                catch(NoSuchMethodException nsmx) {
                    //-- Do nothing
                }
                try {
                    if (cons == null) {
                        //-- make sure a valueOf factory method
                        //-- exists and no user specified handler exists
                        Method method = fieldType.getMethod(VALUE_OF, STRING_ARG);
                        Class returnType = method.getReturnType();
                        if ((returnType != null) && fieldType.isAssignableFrom(returnType)) {
                            if (fieldMap.getHandler() == null) {
                                //-- Use EnumFieldHandler
                                //-- mapping loader now supports a basic EnumFieldHandler
                                //-- for xml we simply need to make sure the toString()
                                //-- method is called during getValue()
                                //FieldHandler handler = xmlDesc.getHandler();
                                //handler = new EnumFieldHandler(fieldType, handler);

                                FieldHandler handler = new ToStringFieldHandler(fieldType, xmlDesc.getHandler());

                                xmlDesc.setHandler(handler);
                                xmlDesc.setImmutable(true);
                            }
                        }
                    }
                }
                catch(NoSuchMethodException nsmx) {
                    //-- Do nothing
                }
            }
        }

        //-- constructor argument?
        String setter = fieldMap.getSetMethod();
        if (setter != null) {
            if (setter.startsWith("%")) {
                int index = 0;
                setter = setter.substring(1);
                index = Integer.parseInt(setter);
                if ((index < 1) || (index > 9)) {
                    throw new MappingException("mapper.invalidParameterIndex", setter);
                }
                //-- adjust index to base zero
                --index;
                xmlDesc.setConstructorArgumentIndex(index);
            }
        }

        return xmlDesc;
    }
View Full Code Here

        //-- Make new class descriptor for the field that
        //-- will represent the container element <e>
        Class type = ContainerElement.class;
        XMLClassDescriptorImpl classDesc = new XMLClassDescriptorImpl(type);
        //-- make copy of fieldDesc and add it to our new class descriptor
        XMLFieldDescriptorImpl newFieldDesc
            = new XMLFieldDescriptorImpl(fieldDesc,
                                         fieldDesc.getXMLName(),
                                         fieldDesc.getNodeType(),
                                         getInternalContext().getPrimitiveNodeType());
        //-- nullify xmlName so that auto-naming will be enabled,
        //-- we can't do this in the constructor because
        //-- XMLFieldDescriptorImpl will create a default one.
        newFieldDesc.setXMLName(null);
        newFieldDesc.setMatches("*");

        //-- add the field descriptor to our new class descriptor
        classDesc.addFieldDescriptor(newFieldDesc);
        //-- reassociate the orignal class descriptor (for 'c')
        // of fieldDesc with our new classDesc
        fieldDesc.setClassDescriptor(classDesc);

        //-- wrap the field handler in a special container field
        //-- handler that will actually do the delgation work
        FieldHandler handler = new ContainerFieldHandler(fieldDesc.getHandler());
        newFieldDesc.setHandler(handler);
        fieldDesc.setHandler(handler);

        //-- Change fieldType of original field descriptor and
        //-- return new descriptor
        return new XMLContainerElementFieldDescriptor(fieldDesc, getInternalContext().getPrimitiveNodeType());
View Full Code Here

        super();

        _xmlName = xmlName;

        // -- Create FieldDescriptor
        _desc = new XMLFieldDescriptorImpl(Object.class, "item", _xmlName,
                NodeType.Element);

        _desc.setMultivalued(true);
        _desc.setMatches("*");
        _desc.setHandler(new XMLFieldHandler() {
View Full Code Here

        super();

        _xmlName = xmlName;

        //-- Create FieldDescriptor
        XMLFieldDescriptorImpl desc
            = new XMLFieldDescriptorImpl(Object.class, "item", _xmlName, NodeType.Element);

        desc.setMultivalued(true);
        desc.setMatches("*");
        desc.setHandler(new XMLFieldHandler() {

            /**
             * {@inheritDoc}
             */
            public Object getValue(final Object object) throws IllegalStateException {
View Full Code Here

     */
    public EnumerationDescriptor() {
        super(java.util.Enumeration.class);
        //-- create element descriptor

        XMLFieldDescriptorImpl desc = new XMLFieldDescriptorImpl(Object.class,
                "_elements", null, NodeType.Element);

        FieldHandler handler = (new XMLFieldHandler() {

            /**
             * {@inheritDoc}
             */
            public Object getValue(final Object object) throws IllegalStateException {
                return object;
            }

            /**
             * {@inheritDoc}
             */
            public void setValue(final Object object, final Object value)
                throws IllegalStateException, IllegalArgumentException {
                //-- do nothing since enumerations are immutable.
            }

            /**
             * {@inheritDoc}
             */
            public Object newInstance(final Object parent) {
                //-- not used
                return null;
            }
        });
        desc.setHandler(handler);
        desc.setMultivalued(true);

        addFieldDescriptor(desc);
    } //-- EnumerationDescriptor()
View Full Code Here

        }

        _javaClass = array.getComponentType();

        //-- create element descriptor
        XMLFieldDescriptorImpl desc = new XMLFieldDescriptorImpl(_javaClass,
                "_elements", null, NodeType.Element);
        FieldHandler handler = (new XMLFieldHandler() {

            /**
             * {@inheritDoc}
             */
            public Object getValue(final Object object) throws IllegalStateException {
                return object;
            }

            /**
             * {@inheritDoc}
             */
            public void setValue(final Object object, final Object value)
                        throws IllegalStateException, IllegalArgumentException {
                //-- do nothing
            }

            /**
             * {@inheritDoc}
             */
            public Object newInstance(final Object parent) {
                //-- not used
                return null;
            }
        });
        desc.setHandler(handler);
        desc.setMultivalued(true);

        _elements[0] = desc;
        _fields[0] = desc;
    } //-- RootArrayDescriptor()
View Full Code Here

            catch (MappingException mx) {
                throw new MarshalException(mx);
            }


            XMLFieldDescriptorImpl fieldDesc
                = createFieldDescriptor(type, methodSet.fieldName, xmlName);

            if (isCollection) {
                fieldDesc.setMultivalued(true);
                fieldDesc.setNodeType(NodeType.Element);
            }

            //-- check for instances of java.util.Date
            if (java.util.Date.class.isAssignableFrom(type)) {
                //handler = new DateFieldHandler(handler);
                if (!customHandler) {
                    dateDescriptors.add(fieldDesc);
                }
            }

            fieldDesc.setHandler(handler);
           
            //-- enable use parent namespace if explicit one doesn't exist
            fieldDesc.setUseParentsNamespace(true);

            //-- Wrap collections?
            if (isCollection && _wrapCollectionsInContainer) {
                String fieldName = COLLECTION_WRAPPER_PREFIX + methodSet.fieldName;
                //-- If we have a field 'c' that is a collection and
                //-- we want to wrap that field in an element <e>, we
                //-- need to create a field descriptor for
                //-- an object that represents the element <e> and
                //-- acts as a go-between from the parent of 'c'
                //-- denoted as P(c) and 'c' itself
                //
                //   object model: P(c) -> c
                //   xml : <p><e><c></e><p>

                //-- Make new class descriptor for the field that
                //-- will represent the container element <e>
                Class cType = ContainerElement.class;
                XMLClassDescriptorImpl containerClassDesc = new XMLClassDescriptorImpl(cType);

                //-- add the field descriptor to our new class descriptor
                containerClassDesc.addFieldDescriptor(fieldDesc);
                //-- nullify xmlName so that auto-naming will be enabled,
                //-- we can't do this in the constructor because
                //-- XMLFieldDescriptorImpl will create a default one.
                fieldDesc.setXMLName(null);
                fieldDesc.setMatches("*");

                //-- wrap the field handler in a special container field
                //-- handler that will actually do the delegation work
                FieldHandler cHandler = new ContainerFieldHandler(handler);
                fieldDesc.setHandler(cHandler);

                fieldDesc = createFieldDescriptor(cType, fieldName, xmlName);
                fieldDesc.setClassDescriptor(containerClassDesc);
                fieldDesc.setHandler(cHandler);

                //-- enable use parent namespace if explicit one doesn't exist
                fieldDesc.setUseParentsNamespace(true);
               
            }
            //-- add FieldDescriptor to ClassDescriptor
            classDesc.addFieldDescriptor(fieldDesc);


        } //-- end of method loop

        //-- If we didn't find any methods we can try
        //-- direct field access
        if (methodCount == 0) {

            Field[] fields = c.getFields();
            Hashtable descriptors = new Hashtable();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];

                Class owner = field.getDeclaringClass();

                //-- ignore fields from super-class, that will be
                //-- introspected separately, if necessary
                if (owner != c) {
                    //-- if declaring class is anything but
                    //-- an interface, than just continue,
                    //-- the field comes from a super class
                    //-- (e.g. java.lang.Object)
                    if (!owner.isInterface()) continue;

                    //-- owner is an interface, is it an
                    //-- interface this class implements
                    //-- or a parent class?
                    if (interfaces.length > 0) {
                        boolean found = false;
                        for (int count = 0; count < interfaces.length; count++) {
                            if (interfaces[count] == owner) {
                                found = true;
                                break;
                            }
                        }
                        if (!found) continue;
                    }
                }

                //-- make sure field is not transient or static final
                int modifiers = field.getModifiers();
                if (Modifier.isTransient(modifiers)) continue;
                if (Modifier.isFinal(modifiers) &&
                    Modifier.isStatic(modifiers))
                    continue;

                Class type = field.getType();



                if (!isDescriptable(type)) continue;

                //-- Built-in support for JDK 1.1 Collections
                //-- we need to a pluggable interface for
                //-- JDK 1.2+
                boolean isCollection = isCollection(type);


                TypeInfo typeInfo = null;
                CollectionHandler colHandler = null;

                //-- If the type is a collection and there is no add method,
                //-- then we obtain a CollectionHandler
                if (isCollection) {

                    try {
                        colHandler = CollectionHandlers.getHandler(type);
                    }
                    catch(MappingException mx) {
                        //-- No CollectionHandler available, continue
                        //-- without one...
                    }

                    //-- Find component type
                    if (type.isArray()) {
                        //-- Byte arrays are handled as a special case
                        //-- so don't use CollectionHandler
                        if (type.getComponentType() == Byte.TYPE) {
                            colHandler = null;
                        }
                        else type = type.getComponentType();

                    }
                }

                String fieldName = field.getName();
                String xmlName = _xmlNaming.toXMLName(fieldName);

                //-- Create FieldHandler first, before the XMLFieldDescriptor
                //-- in case we need to use a custom handler

                typeInfo = new TypeInfo(type, null, null, false, null, colHandler);

                FieldHandler handler = null;
                boolean customHandler = false;
                try {
                    handler = new FieldHandlerImpl(field, typeInfo);

                    //-- handle Hashtable/Map
                    if (isCollection && _saveMapKeys && isMapCollection(type)) {
                        ((FieldHandlerImpl)handler).setConvertFrom(new IdentityConvertor());
                    }

                    //-- look for GeneralizedFieldHandler
                    FieldHandlerFactory factory = getHandlerFactory(type);
                    if (factory != null) {
                        GeneralizedFieldHandler gfh = factory.createFieldHandler(type);
                        if (gfh != null) {
                            gfh.setFieldHandler(handler);
                            handler = gfh;
                            customHandler = true;
                            //-- swap type with the type specified by the
                            //-- custom field handler
                            if (gfh.getFieldType() != null) {
                                type = gfh.getFieldType();
                            }
                        }
                    }
                }
                catch (MappingException mx) {
                    throw new MarshalException(mx);
                }

                XMLFieldDescriptorImpl fieldDesc =
                        createFieldDescriptor(type, fieldName, xmlName);

                if (isCollection) {
                    fieldDesc.setNodeType(NodeType.Element);
                    fieldDesc.setMultivalued(true);
                }
                descriptors.put(xmlName, fieldDesc);
                classDesc.addFieldDescriptor(fieldDesc);
                fieldDesc.setHandler(handler);

                //-- enable use parent namespace if explicit one doesn't exist
                fieldDesc.setUseParentsNamespace(true);

                //-- check for instances of java.util.Date
                if (java.util.Date.class.isAssignableFrom(type)) {
                    if (!customHandler) {
                        dateDescriptors.add(fieldDesc);
                    }
                }

            }
        } //-- end of direct field access


        //-- A temporary fix for java.util.Date
        if (dateDescriptors != null) {
            for (int i = 0; i < dateDescriptors.size(); i++) {
                XMLFieldDescriptorImpl fieldDesc =
                    (XMLFieldDescriptorImpl) dateDescriptors.get(i);
                FieldHandler handler = fieldDesc.getHandler();
                fieldDesc.setImmutable(true);
                DateFieldHandler dfh = new DateFieldHandler(handler);

                //-- patch for java.sql.Date
                Class type = fieldDesc.getFieldType();
                if (java.sql.Date.class.isAssignableFrom(type)) {
                    dfh.setUseSQLDate(true);
                }
                fieldDesc.setHandler(dfh);
            }
        }

        //-- Add reference to superclass...if necessary
        if ((superClass != null) &&
View Full Code Here

TOP

Related Classes of org.exolab.castor.xml.util.XMLFieldDescriptorImpl

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.