Package org.exolab.castor.mapping

Examples of org.exolab.castor.mapping.MappingException


     */
    public void supportsSqlType(final int sqlType) throws MappingException {
        String factoryName = _factory.getFactoryName();
        if (factoryName.equals("hsql")) {
            if (sqlType != Types.INTEGER) {
                throw new MappingException(Messages.format(
                        "mapping.keyGenSQLType", getClass().getName(), new Integer(sqlType)));
            }
        } else if (factoryName.equals("derby")) {
            if (sqlType != Types.NUMERIC) {
                throw new MappingException(Messages.format(
                        "mapping.keyGenSQLType", getClass().getName(), new Integer(sqlType)));
            }
        } else {
            if ((sqlType != Types.INTEGER)
                    && (sqlType != Types.NUMERIC)
                    && (sqlType != Types.DECIMAL)
                    && (sqlType != Types.BIGINT)) {
                throw new MappingException(Messages.format(
                        "mapping.keyGenSQLType", getClass().getName(), new Integer(sqlType)));
            }
        }
    }
View Full Code Here


       
        boolean returning = "true".equals(params.getProperty("returning"));
        _triggerPresent = "true".equals(params.getProperty("trigger", "false"));

        if (!_factoryName.equals(OracleFactory.FACTORY_NAME) && returning) {
            throw new MappingException(Messages.format("mapping.keyGenParamNotCompat",
                    "returning=\"true\"", getClass().getName(), _factoryName));
        }
       
        _factory = factory;
       
        _seqName = params.getProperty("sequence", "{0}_seq");

        setStyle(_factoryName.equals(PostgreSQLFactory.FACTORY_NAME)
                || _factoryName.equals(InterbaseFactory.FACTORY_NAME)
                || _factoryName.equals(DB2Factory.FACTORY_NAME)
                ? BEFORE_INSERT : (returning ? DURING_INSERT : AFTER_INSERT));
        if (_triggerPresent && !returning) {
            setStyle(AFTER_INSERT);
        }
        if (_triggerPresent && (getStyle() == BEFORE_INSERT)) {
            throw new MappingException(Messages.format("mapping.keyGenParamNotCompat",
                    "trigger=\"true\"", getClass().getName(), _factoryName));
        }
       
        try {
            _increment = Integer.parseInt(params.getProperty("increment", "1"));
View Full Code Here

                && sqlType != Types.NUMERIC
                && sqlType != Types.DECIMAL
                && sqlType != Types.BIGINT
                && sqlType != Types.CHAR
                && sqlType != Types.VARCHAR) {
            throw new MappingException(Messages.format("mapping.keyGenSQLType",
                    getClass().getName(), new Integer(sqlType)));
        }
    }
View Full Code Here

        }

        // First find the table name
        st = new StringTokenizer(insert);
        if (!st.hasMoreTokens() || !st.nextToken().equalsIgnoreCase("INSERT")) {
            throw new MappingException(Messages.format("mapping.keyGenCannotParse", insert));
        }
        if (!st.hasMoreTokens() || !st.nextToken().equalsIgnoreCase("INTO")) {
            throw new MappingException(Messages.format("mapping.keyGenCannotParse", insert));
        }
        if (!st.hasMoreTokens()) {
            throw new MappingException(Messages.format("mapping.keyGenCannotParse", insert));
        }
        tableName = st.nextToken();

        // remove every double quote in the tablename
        int idxQuote = tableName.indexOf('"');
        if (idxQuote >= 0) {
            StringBuffer buffer2 = new StringBuffer();
            int pos = 0;

            do {
                buffer2.append(tableName.substring(pos, idxQuote));
                pos = idxQuote + 1;
                idxQuote = tableName.indexOf('"', pos);
            } while (idxQuote != -1);

            buffer2.append(tableName.substring(pos));

            tableName = buffer2.toString();
        }

        // due to varargs in 1.5, see CASTOR-1097
        seqName = MessageFormat.format(_seqName, new Object[] {tableName, primKeyName});
        nextval = _factory.quoteName(seqName + ".nextval");
        lp1 = insert.indexOf('(');
        lp2 = insert.indexOf('(', lp1 + 1);
        if (lp1 < 0) {
            throw new MappingException(Messages.format("mapping.keyGenCannotParse", insert));
        }
        sb = new StringBuffer(insert);
        // if no onInsert triggers in the DB, we have to supply the Key values manually
        if (!_triggerPresent) {
           if (lp2 < 0) {
View Full Code Here

    public void supportsSqlType(final int sqlType) throws MappingException {
        if ((sqlType != Types.INTEGER)
                && (sqlType != Types.BIGINT)
                && (sqlType != Types.NUMERIC)
                && (sqlType != Types.DECIMAL)) {
            throw new MappingException(Messages.format(
                    "mapping.keyGenSQLType", getClass().getName(), new Integer(sqlType)));
        }
    }
View Full Code Here

            while (e.hasMoreElements()) {
                Pair pair = (Pair) e.nextElement();
                initiateCm = (ClassMolder) pair._value;
                targetCm = (ClassMolder) _clsMolders.get(pair._key);
                if (targetCm == null) {
                    throw new MappingException(
                            "Extended element, \"" + pair._key + "\"  not found!");
                }
                initiateCm.setExtends(targetCm);
                ((SQLEngine) initiateCm.getPersistence()).setExtends(
                        (SQLEngine) targetCm.getPersistence());
            }
        }

        // resolve depends
        if (_needDependsClassMolder != null) {
            e = _needDependsClassMolder.elements();
            while (e.hasMoreElements()) {
                Pair pair = (Pair) e.nextElement();
                initiateCm = (ClassMolder) pair._value;
                targetCm = (ClassMolder) _clsMolders.get(pair._key);
                if (targetCm == null) {
                    throw new MappingException(
                            "Depended element, \"" + pair._key + "\"  not found!");
                }
                initiateCm.setDepends(targetCm);
            }
        }

        // resolve depends field
        if (_needFieldClass != null) {
            e = _needFieldClass.elements();
            while (e.hasMoreElements()) {
                Pair pair = (Pair) e.nextElement();
                initiateFm = (FieldMolder) pair._value;
                targetCm = (ClassMolder) _clsMolders.get(pair._key);
                if (targetCm == null) {
                    throw new MappingException("Field element, \"" + pair._key + "\"  not found!");
                }
                initiateFm.setFieldClassMolder(targetCm);
               
                // initiateFm.getEnclosingClassMolder().resetResolver (initiateFm);
            }
View Full Code Here

        ClassMolder clsMold = (ClassMolder) _clsMolders.get(extName);
        if (clsMold != null) {
            me.setExtends(clsMold);
            SQLEngine sql = ((SQLEngine) me.getPersistence());
            if (sql == null) {
                throw new MappingException(
                        "Class " + me + " extends on " + extName
                        + " which is not persistence capable!");
            }
            sql.setExtends((SQLEngine) clsMold.getPersistence());
            return true;
View Full Code Here

                _needFieldClass = new Vector();
            }
            _needFieldClass.add(new Pair(typeName, fieldMolder));
            return false;
        } catch (ClassNotFoundException e) {
            throw new MappingException("ClassNotFound :\n" + e);
        }
    }
View Full Code Here

           
            // Obtain the Java class.
            Class javaClass = resolveType(classMapping.getName());
            if (classMapping.getVerifyConstructable()) {
                if (!Types.isConstructable(javaClass, true)) {
                    throw new MappingException(
                            "mapping.classNotConstructable", javaClass.getName());
                }
            }
            xmlClassDesc.setJavaClass(javaClass);

            // Obtain XML name.
            String xmlName;
            MapTo mapTo = classMapping.getMapTo();
            if ((mapTo != null) && (mapTo.getXml() != null)) {
                xmlName = mapTo.getXml();
            } else {
                String clsName = getInternalContext().getJavaNaming().getClassName(javaClass);
                xmlName = getInternalContext().getXMLNaming().toXMLName(clsName);
            }
            xmlClassDesc.setXMLName(xmlName);

            // If this class extends another class, we need to obtain the extended
            // class and make sure this class indeed extends it.
            ClassDescriptor extDesc = getExtended(classMapping, javaClass);
            xmlClassDesc.setExtends((XMLClassDescriptor) extDesc);
           
            // Create all field descriptors.
            FieldDescriptorImpl[] allFields = createFieldDescriptors(classMapping, javaClass);

            // Make sure there are no two fields with the same name.
            checkFieldNameDuplicates(allFields, javaClass);
           
            // Identify identity and normal fields. Note that order must be preserved.
            List fieldList = new ArrayList(allFields.length);
            List idList = new ArrayList();
            if (extDesc == null) {
                // Sort fields into 2 lists based on identity definition of field.
                for (int i = 0; i < allFields.length; i++) {
                    if (!allFields[i].isIdentity()) {
                        fieldList.add(allFields[i]);
                    } else {
                        idList.add(allFields[i]);
                    }
                }
               
                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);
                }
            }
           
            if (classMapping.getAutoComplete()) {

                XMLClassDescriptor referenceDesc = null;
               
                Class type = xmlClassDesc.getJavaClass();
               
                //-- check compiled descriptors
                if ((getInternalContext() == null)
                        || (getInternalContext().getXMLClassDescriptorResolver() == null)) {
                    String message = "Internal context or class descriptor resolver within are not valid";
                    LOG.warn(message);
                    throw new IllegalStateException(message);
                }
                try {
                    referenceDesc = (XMLClassDescriptor) getInternalContext().getXMLClassDescriptorResolver().resolve(type);
                } catch (ResolverException rx) {
                    throw new MappingException(rx);
                }

                if (referenceDesc == null) {
                    Introspector introspector = getInternalContext().getIntrospector();
                    try {
                        referenceDesc = introspector.generateClassDescriptor(type);
                        if (classMapping.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 = "";
View Full Code Here

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

TOP

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

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.