Package org.exolab.castor.mapping

Examples of org.exolab.castor.mapping.FieldDescriptor


        // -- fields so we can do recursive processing
        _mappings.put(cls, classMap);

        fields = xmlClass.getFields();
        for (int i = 0; i < fields.length; ++i) {
            FieldDescriptor fdesc = fields[i];

            String fieldName = fdesc.getFieldName();

            boolean isContainer = false;
            // -- check for collection wrapper
            if (introspected && fieldName.startsWith("##container")) {
                fdesc = fdesc.getClassDescriptor().getFields()[0];
                fieldName = fdesc.getFieldName();
                isContainer = true;
            }

            Class fieldType = fdesc.getFieldType();

            // -- check to make sure we can find the accessors...
            // -- if we used introspection we don't need to
            // -- enter this block...only when descriptors
            // -- were generated using the source code generator
            // -- or by hand.
            if ((!introspected) && fieldName.startsWith(UNDERSCORE)) {
                // -- check to see if we need to remove underscore
                if (!_mappingLoader.canFindAccessors(cls, fieldName, fieldType)) {
                    fieldName = fieldName.substring(1);
                }

                // -- check to see if we need to remove "List" prefix
                // -- used by generated source code
                if (!_mappingLoader.canFindAccessors(cls, fieldName, fieldType)) {
                    if (fieldName.endsWith("List")) {
                        int len = fieldName.length() - 4;
                        String tmpName = fieldName.substring(0, len);
                        if (_mappingLoader.canFindAccessors(cls, tmpName, fieldType)) {
                            fieldName = tmpName;
                        }
                    }
                }
            }

            fieldMap = new FieldMapping();
            fieldMap.setName(fieldName);

            // -- unwrap arrays of objects
            boolean isArray = fieldType.isArray();
            while (fieldType.isArray()) {
                fieldType = fieldType.getComponentType();
            }

            // -- To prevent outputing of optional fields...check
            // -- for value first before setting
            if (fdesc.isRequired()) {
                fieldMap.setRequired(true);
            }
            if (fdesc.isTransient()) {
                fieldMap.setTransient(true);
            }
            if (fdesc.isMultivalued()) {
                // -- special case for collections
                if (isContainer) {
                    // -- backwards than what you'd expect, but
                    // -- if the collection had a "container" wrapper
                    // -- then we specify container="false" in the
View Full Code Here


                if (idList.size() == 0) {
                    // Found no identities based on identity definition of field.
                    // Try to find identities based on identity definition on class.
                    String[] idNames = classMapping.getIdentity();
                   
                    FieldDescriptor identity;
                    for (int i = 0; i < idNames.length; i++) {
                        identity = findIdentityByName(fieldList, idNames[i], javaClass);
                        if (identity != null) {
                            idList.add(identity);
                        } else {
                            throw new MappingException("mapping.identityMissing",
                                    idNames[i], javaClass.getName());
                        }
                    }
                }
            } else {
                // Add all fields of extending class to field list.
                for (int i = 0; i < allFields.length; i++) { fieldList.add(allFields[i]); }
               
                // Add identity of extended class to identity list.
                if (extDesc.getIdentity() != null) { idList.add(extDesc.getIdentity()); }
               
                // Search redefined identities in extending class.
                FieldDescriptor identity;
                for (int i = 0; i < idList.size(); i++) {
                    String idname = ((FieldDescriptor) idList.get(i)).getFieldName();
                    identity = findIdentityByName(fieldList, idname, javaClass);
                    if (identity != null) { idList.set(i, identity); }
                }
            }
           
            FieldDescriptor xmlId = null;
            if (idList.size() != 0) { xmlId = (FieldDescriptor) idList.get(0); }
           
            if (xmlId != null) { xmlClassDesc.setIdentity((XMLFieldDescriptorImpl) xmlId); }
            for (int i = 0; i < fieldList.size(); i++) {
                FieldDescriptor fieldDesc = (FieldDescriptor) fieldList.get(i);
                if (fieldDesc != null) {
                    xmlClassDesc.addFieldDescriptor((XMLFieldDescriptorImpl) fieldDesc);
                }
            }
           
View Full Code Here

    }

    protected final FieldDescriptor findIdentityByName(
            final List fldList, final String idName, final Class javaClass) {
        for (int i = 0; i < fldList.size(); i++) {
            FieldDescriptor field = (FieldDescriptor) fldList.get(i);
            if (idName.equals(field.getFieldName())) {
                fldList.remove(i);
                return field;
            }
        }
        return null;
View Full Code Here

    protected FieldDescriptorImpl createFieldDesc( Class javaClass, FieldMapping fieldMap )
        throws MappingException
    {

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

        // Set table
        mapTo.setTable("address");
        // Set mapping
        setMapping(mapping);

        FieldDescriptor idFieldDescr = initId(choice);

        // Set fields
        setFields(new FieldDescriptor[] {});
        // Set identity
        setIdentities((new FieldDescriptor[] { idFieldDescr }));
View Full Code Here

        ClassDescriptor entityDescriptor = mappingLoader
                .getDescriptor("org.exolab.castor.jdo.engine.Entity");

        assertNotNull(entityDescriptor);

        FieldDescriptor id = entityDescriptor.getIdentity();
        assertEquals("id", id.getFieldName());
        FieldDescriptor[] fields = entityDescriptor.getFields();
        assertEquals("item", fields[0].getFieldName());

        ClassDescriptor itemDescriptor = mappingLoader
                .getDescriptor("org.exolab.castor.jdo.engine.Item");

        assertNotNull(itemDescriptor);

        id = itemDescriptor.getIdentity();
        assertEquals("id", id.getFieldName());
    }
View Full Code Here

        ClassDescriptor entityDescriptor = mappingLoader
                .getDescriptor("org.exolab.castor.jdo.engine.Employee");

        assertNotNull(entityDescriptor);

        FieldDescriptor id = entityDescriptor.getIdentity();
        assertEquals("id", id.getFieldName());
        FieldDescriptor[] fields = entityDescriptor.getFields();
        assertEquals("address", fields[0].getFieldName());
    }
View Full Code Here

        ClassDescriptor entityDescriptor = mappingLoader
                .getDescriptor("org.exolab.castor.jdo.engine.Address");

        assertNotNull(entityDescriptor);

        FieldDescriptor id = entityDescriptor.getIdentity();
        assertEquals("id", id.getFieldName());
    }
View Full Code Here

        _values = new Object[ _types.length ];
        _sql = sql;
        ClassDescriptorImpl cdi = (ClassDescriptorImpl) _engine.getDescriptor();
        _identSqlType = new int[cdi.getIdentities().length];
        for (int i = 0; i < _identSqlType.length; i++) {
            FieldDescriptor fldDesc = cdi.getIdentities()[i];
            _identSqlType[i] new FieldDescriptorJDONature(fldDesc).getSQLType()[0];
        }
       
        _isCallSql = isCallSql;
    }
View Full Code Here

    protected final FieldDescriptor[] divideFieldDescriptors(final FieldDescriptor[] fields,
            final String[] ids, final FieldDescriptor[] identities) {
        List fieldList = new ArrayList(fields.length);

        for (int i = 0; i < fields.length; i++) {
            FieldDescriptor field = fields[i];
            final int index = getIdColumnIndex(field, ids);
            if (index == -1) {
                // copy non identity field from list of fields.
                fieldList.add(field);
            } else {
                if (field instanceof FieldDescriptorImpl) {
                    ((FieldDescriptorImpl) field).setRequired(true);
                }
                if (field.getHandler() instanceof FieldHandlerImpl) {
                    ((FieldHandlerImpl) field.getHandler()).setRequired(true);
                }

                identities[index] = field;
            }
        }
View Full Code Here

TOP

Related Classes of org.exolab.castor.mapping.FieldDescriptor

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.