Package org.exolab.castor.mapping.xml

Examples of org.exolab.castor.mapping.xml.ClassMapping


            throw new MappingException("mapping.classNotConstructable", cls.getName());
        }

        XMLClassDescriptor xmlClass;
        FieldDescriptor[] fields;
        ClassMapping classMap;
        FieldMapping fieldMap;

        boolean introspected = false;
        try {
            if (_forceIntrospection) {
                xmlClass = _internalContext.getIntrospector().generateClassDescriptor(cls);
                introspected = true;
            } else {
                xmlClass = (XMLClassDescriptor) _internalContext.getXMLClassDescriptorResolver().resolve(cls);
                introspected = _internalContext.getIntrospector().introspected(xmlClass);
            }
        } catch (Exception except) {
            throw new MappingException(except);
        }
        classMap = new ClassMapping();
        classMap.setName(cls.getName());
        classMap.setDescription("Default mapping for class " + cls.getName());

        // -- prevent default access from showing up in the mapping
        classMap.setAccess(null);

        // -- map-to
        MapTo mapTo = new MapTo();
        mapTo.setXml(xmlClass.getXMLName());
        mapTo.setNsUri(xmlClass.getNameSpaceURI());
        mapTo.setNsPrefix(xmlClass.getNameSpacePrefix());
        classMap.setMapTo(mapTo);

        // -- add mapping to hashtable before processing
        // -- 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
                    // -- mapping file.
                    fieldMap.setContainer(false);
                }

                // -- try to guess collection type
                if (isArray) {
                    fieldMap.setCollection(FieldMappingCollectionType.ARRAY);
                } else {
                    // -- if the fieldType is the collection, then set
                    // appropriate
                    // -- collection type
                    String colName = CollectionHandlers.getCollectionName(fieldType);
                    if (colName != null) {
                        fieldMap.setCollection(FieldMappingCollectionType.valueOf(colName));
                        fieldType = Object.class;
                    } else if (_mappingLoader.returnsArray(cls, fieldName, fieldType)) {
                        // -- help maintain compatibility with generated
                        // descriptors
                        fieldMap.setCollection(FieldMappingCollectionType.ARRAY);
                    } else {
                        fieldMap.setCollection(FieldMappingCollectionType.ENUMERATE);
                    }
                }
            }

            // -- fieldType
            fieldMap.setType(fieldType.getName());

            // -- handle XML Specific information
            fieldMap.setBindXml(new BindXml());
            fieldMap.getBindXml().setName(((XMLFieldDescriptor) fdesc).getXMLName());
            fieldMap.getBindXml().setNode(
                    BindXmlNodeType.valueOf(((XMLFieldDescriptor) fields[i]).getNodeType()
                            .toString()));
            if (classMap.getClassChoice() == null) {
                classMap.setClassChoice(new ClassChoice());
            }
            classMap.getClassChoice().addFieldMapping(fieldMap);

            if (deep) {
                if (_mappings.get(fieldType) != null) {
                    continue;
                }
View Full Code Here


            String idCol = (String) _ids.get(idIndex);
            Integer idColNum = (Integer) _cols.get(idCol);

            DTXClassDescriptor desc = (DTXClassDescriptor) _classes.get(idCol);
            ClassMapping clsMapping = desc.getClassMapping();
            String elementName = null;

            if ((clsMapping.getMapTo() == null) || (clsMapping.getMapTo().getXml() == null)) {
                elementName = clsMapping.getName();
            } else {
                elementName = clsMapping.getMapTo().getXml();
            }

            String[] attrCols = desc.getAttrCols();
            String[] simpleElementCols = desc.getSimpleElementCols();
            String textCol = desc.getTextCol();
View Full Code Here

            String extendsTable = extendsTo.getTable();
            expr.addInnerJoin(table, identitySQL, extendsTable, identitySQL);
            /**
             * needs to be resolved by hand
             */
            initQuery(new ClassMapping(), expr);
            //(ClassMapping) clsMapping.getExtends(), expr);
        }

        for (int i = 0; i < fields.length; ++i) {
            FieldMapping field = fields[i];
            Sql fieldSql = field.getSql();
            ClassMapping relMapping = _eng.getClassMapping(field.getType());

            if (fieldSql == null) {
                if (relMapping != null) {

                    // We have a one-to-many relationship with a sub object.
                    // get those objects, too.

                    FieldMapping[] relFields = relMapping.getClassChoice().getFieldMapping();
                    MapTo relMapTo = relMapping.getMapTo();

                    if (relMapTo == null) {
                        throw new DTXException("dtx.NoRelatedMapTo");
                    }

                    String relTable = relMapTo.getTable();

                    String foreKey = null;

                    for (int k = 0; k < relFields.length; k++) {
                        Sql relSql = relFields[k].getSql();
                        if (relSql != null) {
                            String type = relFields[k].getType();
                            if (type != null && type.equals(clsMapping.getName())) {
                                foreKey = relSql.getName()[0];
                            }
                        }
                    }

                    if (foreKey != null) {
                        expr.addOuterJoin(table, identitySQL, relTable, foreKey);
                        DTXClassDescriptor relDesc = new DTXClassDescriptor(relMapping);

                        for (int n = 0; n < relFields.length; n++) {
                            FieldMapping relField = relFields[n];
                            Sql relSql = relFields[n].getSql();
                            if (relSql != null) {
                                String relFieldName = relSql.getName()[0];
                                if (relFieldName == null) {
                                    relFieldName = relFields[n].getName();
                                }
                                String relFullName = relTable + "." + relFieldName;

                                BindXml relFieldXml = relFields[n].getBindXml();
                                String node = "element";
                                if (relFieldXml != null) {
                                    node = relFieldXml.getNode().toString();
                                }

                                if (!relFieldName.equals(foreKey)) {
                                    expr.addColumn(relTable, relFieldName);
                                    _cols.put(relTable + "." + relFieldName,
                                            new Integer(++_lastCol));

                                    if (node.equalsIgnoreCase("attribute")) {
                                        relDesc.addAttr(relFullName, relField);
                                    } else if (node.equalsIgnoreCase("element")) {
                                        relDesc.addSimpleElement(relFullName, relField);
                                    } else if (node.equalsIgnoreCase("text")) {
                                        relDesc.setTextCol(relFullName, relField);
                                    }
                                }
                                if (relField.getName().equals(relMapping.getIdentity(0))) {
                                    _ids.add(relTable + "." + relFieldName);
                                    desc.addContained(relTable + "." + relFieldName, relMapping);
                                    _classes.put(relTable + "." + relFieldName, relDesc);
                                }
                            }
View Full Code Here

     * Default Constructor. Configures persistence of entity Book.
     */
    public AddressJDODescriptor() {
        super();
        addNature(ClassDescriptorJDONature.class.getName());
        ClassMapping mapping = new ClassMapping();
        ClassChoice choice = new ClassChoice();
        MapTo mapTo = new MapTo();

        LOG.debug("Constructor invoked");

        // Set DB table name
        new ClassDescriptorJDONature(this).setTableName("address");
        // Set corresponding Java class
        setJavaClass(Address.class);
        // Set access mode
        new ClassDescriptorJDONature(this).setAccessMode(AccessMode.Shared);
        // Set cache key
        new ClassDescriptorJDONature(this).addCacheParam("name", "org.castor.cpa.functional.onetoone.Address");

        // Configure class mapping
        mapping.setAccess(ClassMappingAccessType.SHARED);
        mapping.setAutoComplete(true);
        mapping.setName("org.castor.cpa.functional.onetoone.Address");
        // Set class choice
        mapping.setClassChoice(choice);
        // Set table mapping
        mapping.setMapTo(mapTo);
        // Set table
        mapTo.setTable("address");
        // Set mapping
        setMapping(mapping);

View Full Code Here

        FieldMapping[] extendFields;
        FieldMapping[] thisFields;
        FieldMapping[] fields = null;
        String[] identities;
        boolean idfield;
        ClassMapping extend = (ClassMapping) clsMap.getExtends();
        ClassMapping origin;
        ArrayList fieldList;

        if (extend != null) {
            origin = extend;
            while (origin.getExtends() != null) {
                origin = (ClassMapping) origin.getExtends();
            }
            identities = origin.getIdentity();
            identities = AbstractMappingLoader.getIdentityColumnNames (identities, origin);
            extendFields = getFullFields(extend);
            if (clsMap.getClassChoice() != null) {
                thisFields = clsMap.getClassChoice().getFieldMapping();
            } else {
View Full Code Here

     * If the class is an extended class, the id
     * fields of the extended class will be returned.
     */
    public static FieldMapping[] getIdFields(final ClassMapping clsMap)
            throws MappingException {
        ClassMapping base;
        FieldMapping[] fmDepended;
        FieldMapping[] fmResult;
        FieldMapping[] fmBase;
        FieldMapping[] fmIds;
        String[] identities;

        // start with the extended class
        base = clsMap;
        while (base.getExtends() != null) {
            base = (ClassMapping) base.getExtends();
        }
        fmDepended = null;

        identities = base.getIdentity();
       
        identities = AbstractMappingLoader.getIdentityColumnNames (identities, base);

        if (identities == null || identities.length == 0) {
            throw new MappingException("Identity is null!");
        }

        fmIds = new FieldMapping[identities.length];
        fmBase = base.getClassChoice().getFieldMapping();
        for (int i = 0; i < fmBase.length; i++) {
            for (int k = 0; k < identities.length; k++) {
                if (fmBase[i].getName().equals(identities[k])) {
                    fmIds[k] = fmBase[i];
                    break;
View Full Code Here

     */
    public ClassMapping createMapping(final ClassInfo classInfo) {
        JClass jClass    = classInfo.getJClass();
        String className = jClass.getName();

        ClassMapping classMapping = new ClassMapping();
        classMapping.setName(className);

        //-- Set namespace prefix
        MapTo mapTo = new MapTo();
        classMapping.setMapTo(mapTo);

        XMLInfoNature xmlNature = new XMLInfoNature(classInfo);

        String nsPrefix = xmlNature.getNamespacePrefix();
        if ((nsPrefix != null) && (nsPrefix.length() > 0)) {
            mapTo.setNsPrefix(nsPrefix);
        }

        //-- Set namespace URI
        String nsURI = xmlNature.getNamespaceURI();
        if ((nsURI != null) && (nsURI.length() > 0)) {
            mapTo.setNsUri(nsURI);
        }

        //-- set XML Name
        mapTo.setXml(xmlNature.getNodeName());

        //-- set Element Definition flag
        mapTo.setElementDefinition(xmlNature.isElementDefinition());

        //-- set grouping compositor
        if (xmlNature.isChoice()) {
            // TODO need a way to specify choice in Mapping file
        }

        boolean isAbstract = classInfo.isAbstract();
        if (!isAbstract) {
            isAbstract = jClass.getModifiers().isAbstract();
        }
        classInfo.setAbstract(isAbstract);

        //-- To prevent compiler warnings...make sure
        //-- we don't declare temp variables if field count is 0;
        if (classInfo.getFieldCount() == 0) {
            return classMapping;
        }

        //-- handle  content
        if (classInfo.allowContent()) {
            createFieldMapping(classMapping, classInfo.getTextField(), null);
        }

        ClassInfo base = classInfo.getBaseClass();
        if (base != null) {
            classMapping.setExtends(base.getJClass().getName());
        }

        FieldInfo[] atts = classInfo.getAttributeFields();

        //-----------------------------/
View Full Code Here

        // in the same order as it appeared in the mapping file.
        Enumeration enumeration = mapping.enumerateClassMapping();

        List retryList = new ArrayList();
        while (enumeration.hasMoreElements()) {
            ClassMapping clsMap = (ClassMapping) enumeration.nextElement();
            try {
                ClassDescriptor clsDesc = createClassDescriptor(clsMap);
                if (clsDesc != null) { addDescriptor(clsDesc); }
            } catch (MappingException mx) {
                // save for later for possible out-of-order mapping files...
                retryList.add(clsMap);
                continue;
            }
        }

        // handle possible retries, for now we only loop once on the retries, but we
        // should change this to keep looping until we have no more success rate.
        for (Iterator i = retryList.iterator(); i.hasNext();) {
            ClassMapping clsMap = (ClassMapping) i.next();
            ClassDescriptor clsDesc = createClassDescriptor(clsMap);
            if (clsDesc != null) { addDescriptor(clsDesc); }
        }

        // iterate over all class descriptors and resolve relations between them
View Full Code Here

     */
    protected final ClassDescriptor getExtended(final ClassMapping clsMap,
                                                final Class javaClass) throws MappingException {
        if (clsMap.getExtends() == null) { return null; }

        ClassMapping mapping = (ClassMapping) clsMap.getExtends();
        Class type = resolveType(mapping.getName());
        ClassDescriptor result = getDescriptor(type.getName());

        if (result == null) {
            throw new MappingException(
                    "mapping.extendsMissing", mapping, javaClass.getName());
View Full Code Here

     */
    protected final ClassDescriptor getDepended(final ClassMapping clsMap,
                                                final Class javaClass) throws MappingException {
        if (clsMap.getDepends() == null) { return null; }

        ClassMapping mapping = (ClassMapping) clsMap.getDepends();
        Class type = resolveType(mapping.getName());
        ClassDescriptor result = getDescriptor(type.getName());

        if (result == null) {
            throw new MappingException(
                    "Depends not found: " + mapping + " " + javaClass.getName());
View Full Code Here

TOP

Related Classes of org.exolab.castor.mapping.xml.ClassMapping

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.