Package org.exolab.castor.xml.util

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


        //   xml : <p><e><c></e><p>

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


        //--------------------------/
        //- handle complex objects -/
        //--------------------------/

        XMLClassDescriptorImpl classDesc
            = new IntrospectedXMLClassDescriptor(c);

        Method[] methods = c.getMethods();
        List      dateDescriptors = new ArrayList(3);
        Hashtable methodSets      = new Hashtable();

        int methodCount = 0;

        Class superClass = c.getSuperclass();
        Class[] interfaces = c.getInterfaces();



        //-- create method sets
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];

            Class owner = method.getDeclaringClass();

            //-- ignore methods 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;
                }
            }
            else {
                //-- look for overloaded methods
                if (superClass != null) {
                    Class[] args = method.getParameterTypes();
                    String name = method.getName();
                    Method tmpMethod = null;
                    try {
                        tmpMethod = superClass.getMethod(name, args);
                    }
                    catch(NoSuchMethodException nsme) {
                        //-- do nothing
                    }
                    if (tmpMethod != null) continue;
                }
            }


            //-- if method is static...ignore
            if ((method.getModifiers() & Modifier.STATIC) != 0) continue;

            String methodName = method.getName();

            //-- read methods
            if (methodName.startsWith(JavaNaming.METHOD_PREFIX_GET)) {
                if (method.getParameterTypes().length != 0) continue;
                //-- disable direct field access
                ++methodCount;
                //-- make sure return type is "descriptable"
                //-- and not null
                Class type = method.getReturnType();
                if (type == null) continue;
                if (!isDescriptable(type)) continue;

                //-- caclulate name from Method name
                String fieldName = methodName.substring(3);
                fieldName = _javaNaming.toJavaMemberName(fieldName);

                MethodSet methodSet = (MethodSet)methodSets.get(fieldName);
                if (methodSet == null) {
                    methodSet = new MethodSet(fieldName);
                    methodSets.put(fieldName, methodSet);
                }
                methodSet.get = method;
            }
            else if (methodName.startsWith(JavaNaming.METHOD_PREFIX_IS)) {
                if (method.getParameterTypes().length != 0) continue;
                //-- make sure type is not null, and a boolean
                Class type = method.getReturnType();
                if (type == null) continue;
                if (type.isPrimitive()) {
                    if (type != Boolean.TYPE) continue;
                }
                else {
                    if (type != Boolean.class) continue;
                }
                //-- disable direct field access
                ++methodCount;
                //-- caclulate name from Method name
                String fieldName = methodName.substring(JavaNaming.METHOD_PREFIX_IS.length());
                fieldName = _javaNaming.toJavaMemberName(fieldName);

                MethodSet methodSet = (MethodSet)methodSets.get(fieldName);
                if (methodSet == null) {
                    methodSet = new MethodSet(fieldName);
                    methodSets.put(fieldName, methodSet);
                }
                methodSet.get = method;
            }
            //-----------------------------------/
            //-- write methods (collection item)
            else if (methodName.startsWith(JavaNaming.METHOD_PREFIX_ADD)) {
                if (method.getParameterTypes().length != 1) continue;
                //-- disable direct field access
                ++methodCount;
                //-- make sure parameter type is "descriptable"
                if (!isDescriptable(method.getParameterTypes()[0])) continue;
                //-- caclulate name from Method name
                String fieldName = methodName.substring(3);
                fieldName = _javaNaming.toJavaMemberName(fieldName);
                MethodSet methodSet = (MethodSet) methodSets.get(fieldName);
                if (methodSet == null) {
                    methodSet = new MethodSet(fieldName);
                    methodSets.put(fieldName, methodSet);
                }
                methodSet.add = method;
            }
            //-- write method (singleton or collection)
            else if (methodName.startsWith(JavaNaming.METHOD_PREFIX_SET)) {
                if (method.getParameterTypes().length != 1) continue;
                //-- disable direct field access
                ++methodCount;
                //-- make sure parameter type is "descriptable"
                if (!isDescriptable(method.getParameterTypes()[0])) continue;
                //-- caclulate name from Method name
                String fieldName = methodName.substring(3);
                fieldName = _javaNaming.toJavaMemberName(fieldName);
                MethodSet methodSet = (MethodSet) methodSets.get(fieldName);
                if (methodSet == null) {
                    methodSet = new MethodSet(fieldName);
                    methodSets.put(fieldName, methodSet);
                }
                methodSet.set = method;
            }
            else if (methodName.startsWith(JavaNaming.METHOD_PREFIX_CREATE)) {
                if (method.getParameterTypes().length != 0) continue;
                Class type = method.getReturnType();
                //-- make sure return type is "descriptable"
                //-- and not null
                if (!isDescriptable(type)) continue;
                //-- caclulate name from Method name
                String fieldName = methodName.substring(JavaNaming.METHOD_PREFIX_CREATE.length());
                fieldName = _javaNaming.toJavaMemberName(fieldName);
                MethodSet methodSet = (MethodSet) methodSets.get(fieldName);
                if (methodSet == null) {
                    methodSet = new MethodSet(fieldName);
                    methodSets.put(fieldName, methodSet);
                }
                methodSet.create = method;
            }
        } //-- end create method sets


        //-- Loop Through MethodSets and create
        //-- descriptors
        Enumeration enumeration = methodSets.elements();

        while (enumeration.hasMoreElements()) {

            MethodSet methodSet = (MethodSet) enumeration.nextElement();

            //-- create XMLFieldDescriptor
            String xmlName = _xmlNaming.toXMLName(methodSet.fieldName);

            boolean isCollection = false;

            //-- calculate class type
            //-- 1st check for add-method, then set or get method
            Class type = null;
            if (methodSet.add != null) {
                type = methodSet.add.getParameterTypes()[0];
                isCollection = true;
            }

            //-- if there was no add method, use get/set methods
            //-- to calculate type.
            if (type == null) {
                if (methodSet.get != null) {
                    type = methodSet.get.getReturnType();
                }
                else if (methodSet.set != null) {
                    type = methodSet.set.getParameterTypes()[0];
                }
                else {
                    //-- if we make it here, the only method found
                    //-- was a create method, which is useless by itself.
                    continue;
                }
            }

            //-- Handle Collections
            isCollection = (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 && (methodSet.add == null)) {

                try {
                    colHandler = CollectionHandlers.getHandler(type);
                }
                catch(MappingException mx) {
                    //-- No collection handler available,
                    //-- proceed anyway...
                }

                //-- 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();
                }
            }

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

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

            FieldHandler handler = null;
            boolean customHandler = false;
            try {
                handler = new FieldHandlerImpl(methodSet.fieldName,
                                                null,
                                                null,
                                                methodSet.get,
                                                methodSet.set,
                                                typeInfo);
                //-- clean up
                if (methodSet.add != null)
                    ((FieldHandlerImpl)handler).setAddMethod(methodSet.add);

                if (methodSet.create != null)
                    ((FieldHandlerImpl)handler).setCreateMethod(methodSet.create);

                //-- 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, 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("*");
 
View Full Code Here

            //-- reset classDesc
            classDesc = oldClassDesc;
           
            //-- isWrapper?
            if (isWrapper) {
                state.classDesc = new XMLClassDescriptorImpl(ContainerElement.class, name);
                state.wrapper = true;
                if (LOG.isDebugEnabled()) {
                  LOG.debug("wrapper-element: " + name);
                }
                //-- process attributes
View Full Code Here

       
        //--------------------------/
        //- handle complex objects -/
        //--------------------------/
       
        XMLClassDescriptorImpl classDesc
            = new IntrospectedXMLClassDescriptor(c);
       
        Method[] methods = c.getMethods();
        List      dateDescriptors = new List(3);
        Hashtable methodSets      = new Hashtable();
       
        int methodCount = 0;
       
        Class superClass = c.getSuperclass();
        Class[] interfaces = c.getInterfaces();
       
       
       
        //-- create method sets
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
           
            Class owner = method.getDeclaringClass();
           
            //-- ignore methods 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;
                }
            }
            else {
                //-- look for overloaded methods
                if (superClass != null) {
                    Class[] args = method.getParameterTypes();
                    String name = method.getName();
                    Method tmpMethod = null;
                    try {
                        tmpMethod = superClass.getMethod(name, args);
                    }
                    catch(NoSuchMethodException nsme) {
                        //-- do nothing
                    }
                    if (tmpMethod != null) continue;
                }
            }
           
           
            //-- if method is static...ignore
            if ((method.getModifiers() & Modifier.STATIC) != 0) continue;
           
            String methodName = method.getName();
           
            //-- read methods
            if (methodName.startsWith(GET)) {
                if (method.getParameterTypes().length != 0) continue;
                //-- disable direct field access
                ++methodCount;
                //-- make sure return type is "descriptable"
                //-- and not null
                Class type = method.getReturnType();
                if (type == null) continue;
                if (!isDescriptable(type)) continue;
               
                //-- caclulate name from Method name
                String fieldName = methodName.substring(3);
                fieldName = JavaNaming.toJavaMemberName(fieldName);
               
                MethodSet methodSet = (MethodSet)methodSets.get(fieldName);
                if (methodSet == null) {
                    methodSet = new MethodSet(fieldName);
                    methodSets.put(fieldName, methodSet);
                }
                methodSet.get = method;
            }
            else if (methodName.startsWith(IS)) {
                if (method.getParameterTypes().length != 0) continue;
                //-- make sure type is not null, and a boolean
                Class type = method.getReturnType();
                if (type == null) continue;
                if (type.isPrimitive()) {
                    if (type != Boolean.TYPE) continue;
                }
                else {
                    if (type != Boolean.class) continue;
                }               
                //-- disable direct field access
                ++methodCount;
                //-- caclulate name from Method name
                String fieldName = methodName.substring(IS.length());
                fieldName = JavaNaming.toJavaMemberName(fieldName);
               
                MethodSet methodSet = (MethodSet)methodSets.get(fieldName);
                if (methodSet == null) {
                    methodSet = new MethodSet(fieldName);
                    methodSets.put(fieldName, methodSet);
                }
                methodSet.get = method;
            }
            //-----------------------------------/
            //-- write methods (collection item)
            else if (methodName.startsWith(ADD)) {
                if (method.getParameterTypes().length != 1) continue;
                //-- disable direct field access
                ++methodCount;
                //-- make sure parameter type is "descriptable"
                if (!isDescriptable(method.getParameterTypes()[0])) continue;
                //-- caclulate name from Method name
                String fieldName = methodName.substring(3);
                fieldName = JavaNaming.toJavaMemberName(fieldName);
                MethodSet methodSet = (MethodSet) methodSets.get(fieldName);
                if (methodSet == null) {
                    methodSet = new MethodSet(fieldName);
                    methodSets.put(fieldName, methodSet);
                }
                methodSet.add = method;
            }
            //-- write method (singleton or collection)
            else if (methodName.startsWith(SET)) {
                if (method.getParameterTypes().length != 1) continue;
                //-- disable direct field access
                ++methodCount;
                //-- make sure parameter type is "descriptable"
                if (!isDescriptable(method.getParameterTypes()[0])) continue;
                //-- caclulate name from Method name
                String fieldName = methodName.substring(3);
                fieldName = JavaNaming.toJavaMemberName(fieldName);
                MethodSet methodSet = (MethodSet) methodSets.get(fieldName);
                if (methodSet == null) {
                    methodSet = new MethodSet(fieldName);
                    methodSets.put(fieldName, methodSet);
                }
                methodSet.set = method;
            }
            else if (methodName.startsWith(CREATE)) {
                if (method.getParameterTypes().length != 0) continue;
                Class type = method.getReturnType();
                //-- make sure return type is "descriptable"
                //-- and not null
                if (!isDescriptable(type)) continue;
                //-- caclulate name from Method name
                String fieldName = methodName.substring(CREATE.length());
                fieldName = JavaNaming.toJavaMemberName(fieldName);
                MethodSet methodSet = (MethodSet) methodSets.get(fieldName);
                if (methodSet == null) {
                    methodSet = new MethodSet(fieldName);
                    methodSets.put(fieldName, methodSet);
                }
                methodSet.create = method;
            }
        } //-- end create method sets
       
       
        //-- Loop Through MethodSets and create
        //-- descriptors
        Enumeration enumeration = methodSets.elements();
       
        while (enumeration.hasMoreElements()) {
           
            MethodSet methodSet = (MethodSet) enumeration.nextElement();
           
            //-- create XMLFieldDescriptor
            String xmlName = _naming.toXMLName(methodSet.fieldName);
               
            boolean isCollection = false;
           
            //-- calculate class type
            //-- 1st check for add-method, then set or get method
            Class type = null;
            if (methodSet.add != null) {
                type = methodSet.add.getParameterTypes()[0];
                isCollection = true;
            }
                       
            //-- if there was no add method, use get/set methods
            //-- to calculate type.
            if (type == null) {
                if (methodSet.get != null) {
                    type = methodSet.get.getReturnType();
                }
                else if (methodSet.set != null) {
                    type = methodSet.set.getParameterTypes()[0];
                }
                else {
                    //-- if we make it here, the only method found
                    //-- was a create method, which is useless by itself.
                    continue;
                }
            }
           
            //-- Handle Collections
            isCollection = (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 && (methodSet.add == null)) {
               
                try {
                    colHandler = CollectionHandlers.getHandler(type);
                }
                catch(MappingException mx) {
                    //-- No collection handler available,
                    //-- proceed anyway...
                }
               
                //-- 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();
                }
            }
           
            typeInfo = new TypeInfo(type, null, null, false, null, colHandler);
           
            //-- Create FieldHandler first, before the XMLFieldDescriptor
            //-- in case we need to use a custom handler
                                               
            FieldHandler handler = null;
            boolean customHandler = false;
            try {
                handler = new FieldHandlerImpl(methodSet.fieldName,
                                                null,
                                                null,
                                                methodSet.get,
                                                methodSet.set,
                                                typeInfo);
                //-- clean up
                if (methodSet.add != null)
                    ((FieldHandlerImpl)handler).setAddMethod(methodSet.add);
                                               
                if (methodSet.create != null)
                    ((FieldHandlerImpl)handler).setCreateMethod(methodSet.create);
                
                //-- 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, 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);
           
            //-- 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("*");
 
View Full Code Here

        else {
            xmlName = clsMap.getMapTo().getXml();

        }

        XMLClassDescriptorImpl xmlClassDesc
            = new XMLClassDescriptorAdapter( clsDesc, xmlName, _primitiveNodeType );

        if (clsMap.getAutoComplete()) {

            XMLClassDescriptor referenceDesc = null;
           
            Class type = xmlClassDesc.getJavaClass();
           
            //-- check compiled descriptors
            ClassDescriptorResolverImpl cdr
                = new ClassDescriptorResolverImpl();
            cdr.setIntrospection(false);
            referenceDesc = cdr.resolve(type);
            if (referenceDesc == null) {
                Introspector introspector = new Introspector();
                try {
                    referenceDesc = introspector.generateClassDescriptor(type);
                    if (clsMap.getExtends() != null) {
                        //-- clear parent from introspected descriptor since
                        //-- a mapping was provided in the mapping file
                        ((XMLClassDescriptorImpl)referenceDesc).setExtends(null);
                    }
                } catch (MarshalException mx) {
                    String error = "unable to introspect class '" +
                        type.getName() + "' for auto-complete: ";
                    throw new MappingException(error + mx.getMessage());
                }
            }

            //-- check for identity
            String identity = "";
            if (clsMap.getIdentityCount() > 0)
                identity = clsMap.getIdentity(0);

           
            FieldDescriptor[] fields = xmlClassDesc.getFields();

            // Attributes
            XMLFieldDescriptor[] introFields = referenceDesc.getAttributeDescriptors();
            for (int i = 0; i<introFields.length; ++i) {
                if (!isMatchFieldName(fields, introFields[i].getFieldName())) {
                    // If there is no field with this name, we can add it
                    if (introFields[i].getFieldName().equals(identity)) {
                        xmlClassDesc.setIdentity(introFields[i]);
                    }
                    else {
                        xmlClassDesc.addFieldDescriptor(introFields[i]);
                    }
                }
            }

            // Elements
            introFields = referenceDesc.getElementDescriptors();
            for (int i = 0; i<introFields.length; ++i) {
                if (!isMatchFieldName(fields, introFields[i].getFieldName())) {
                    // If there is no field with this name, we can add it
                    if (introFields[i].getFieldName().equals(identity)) {
                        xmlClassDesc.setIdentity(introFields[i]);
                    }
                    else {
                        xmlClassDesc.addFieldDescriptor(introFields[i]);
                    }
                }
            }

            // Content
            XMLFieldDescriptor field = referenceDesc.getContentDescriptor();
            if (field!= null)
                if (isMatchFieldName(fields, field.getFieldName()))
                    // If there is no field with this name, we can add
                    xmlClassDesc.addFieldDescriptor(field);


        } //-- End of auto-complete


        //-- copy ns-uri + ns-prefix
        if (mapTo != null) {
            xmlClassDesc.setNameSpacePrefix(mapTo.getNsPrefix());
            xmlClassDesc.setNameSpaceURI(mapTo.getNsUri());
        }
        return xmlClassDesc;
    } //-- createDescriptor
View Full Code Here

        //   xml : <p><e><c></e><p>
       
        //-- 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(),
                                         _primitiveNodeType);
        //-- 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
View Full Code Here

        // If there is a castor mapping, see if it is mapped there
        if (mapping != null)
        {
            try
            {
                XMLClassDescriptorImpl xd = (XMLClassDescriptorImpl) mapping
                        .getResolver(Mapping.XML).getDescriptor(clazz);
                if (xd != null)
                    isCastor = true;
            }
            catch (MappingException e)
View Full Code Here

    public void initType()
    {
        Class clazz = getTypeClass();
        Class xdClass = null;
        XMLClassDescriptorImpl xd = null;
        String nsUri;
        String nsPrefix;
        String localTypeName;

        // If mapping, get schema type info from there
        if (this.mapping != null)
        {
            try
            {
                xd = (XMLClassDescriptorImpl) mapping.getResolver(Mapping.XML).getDescriptor(clazz);
                nsUri = (xd.getNameSpaceURI() == null ? "" : xd.getNameSpaceURI());
                // Use xml name as schema type name, unless it has been introspected
                if (Introspector.introspected(xd)) {
                    localTypeName = clazz.getName();
                    localTypeName = localTypeName.substring(localTypeName.lastIndexOf('.') + 1);
                } else
                    localTypeName = xd.getXMLName();
                nsPrefix = xd.getNameSpacePrefix();
                if (nsPrefix == null || nsPrefix.length() == 0)
                    setSchemaType(new QName(nsUri, localTypeName));
                else
                    setSchemaType(new QName(nsUri, localTypeName, nsPrefix));
                setAbstract(!xd.isElementDefinition());
            }
            catch (MappingException e)
            {
                String error = "Error getting resolver from mapping.";
                log.error(error, e);
                throw new XFireRuntimeException(error, e);
            }
        }
        else
        // No mapping, check for accompanying xml descriptor class
        {
            try
            {
                xdClass = ClassLoaderUtils.loadClass(clazz.getName() + "Descriptor", this
                        .getClass());
                if (xdClass != null && (XMLClassDescriptorImpl.class.isAssignableFrom(xdClass)))
                {
                    xd = (XMLClassDescriptorImpl) xdClass.newInstance();
                    nsUri = (xd.getNameSpaceURI() == null ? "" : xd.getNameSpaceURI());
                    nsPrefix = (xd.getNameSpacePrefix() == null ? "" : xd.getNameSpacePrefix());
                    localTypeName = xd.getXMLName();
                    setSchemaType(new QName(nsUri, localTypeName, nsPrefix));
                    setAbstract(!xd.isElementDefinition());
                }
            }
            catch (ClassNotFoundException e)
            {
            }
View Full Code Here

        else {
            xmlName = clsMap.getMapTo().getXml();

        }

        XMLClassDescriptorImpl xmlClassDesc
            = new XMLClassDescriptorAdapter( clsDesc, xmlName, _primitiveNodeType );

        if (clsMap.getAutoComplete()) {

            XMLClassDescriptor referenceDesc = null;
            Class type = xmlClassDesc.getJavaClass();
           
            //-- check compiled descriptors
            ClassDescriptorResolverImpl cdr
                = new ClassDescriptorResolverImpl();
            cdr.setIntrospection(false);
            referenceDesc = cdr.resolve(type);
            if (referenceDesc == null) {
                Introspector introspector = new Introspector();
                try {
                    referenceDesc = introspector.generateClassDescriptor(type);
                } catch (MarshalException mx) {
                    String error = "unable to introspect class '" +
                        type.getName() + "' for auto-complete: ";
                    throw new MappingException(error + mx.getMessage());
                }
            }

            //-- check for identity
            String identity = "";
            if (clsMap.getIdentityCount() > 0)
                identity = clsMap.getIdentity(0);

            FieldDescriptor[] fields = xmlClassDesc.getFields();

            // Attributes
            XMLFieldDescriptor[] introFields = referenceDesc.getAttributeDescriptors();
            for (int i = 0; i<introFields.length; ++i) {
                if (!isMatchFieldName(fields, introFields[i].getFieldName())) {
                    // If there is no field with this name, we can add it
                    if (introFields[i].getFieldName().equals(identity)) {
                        xmlClassDesc.setIdentity(introFields[i]);
                    }
                    else {
                        xmlClassDesc.addFieldDescriptor(introFields[i]);
                    }
                }
            }

            // Elements
            introFields = referenceDesc.getElementDescriptors();
            for (int i = 0; i<introFields.length; ++i) {
                if (!isMatchFieldName(fields, introFields[i].getFieldName())) {
                    // If there is no field with this name, we can add it
                    if (introFields[i].getFieldName().equals(identity)) {
                        xmlClassDesc.setIdentity(introFields[i]);
                    }
                    else {
                        xmlClassDesc.addFieldDescriptor(introFields[i]);
                    }
                }
            }

            // Content
            XMLFieldDescriptor field = referenceDesc.getContentDescriptor();
            if (field!= null)
                if (isMatchFieldName(fields, field.getFieldName()))
                    // If there is no field with this name, we can add
                    xmlClassDesc.addFieldDescriptor(field);


        } //-- End of auto-complete


        //-- copy ns-uri + ns-prefix
        if (mapTo != null) {
            xmlClassDesc.setNameSpacePrefix(mapTo.getNsPrefix());
            xmlClassDesc.setNameSpaceURI(mapTo.getNsUri());
        }
        return xmlClassDesc;
    } //-- createDescriptor
View Full Code Here

        //   xml : <p><e><c></e><p>
       
        //-- 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(),
                                         _primitiveNodeType);
        //-- 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);
       
        //-- 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
View Full Code Here

TOP

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

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.