Package org.exolab.castor.mapping

Examples of org.exolab.castor.mapping.FieldDescriptor


     */
    private FieldDescriptor getFieldDesc(final String fieldName,
                                            final ClassDescriptor clsDesc) {

        ClassDescriptor classDescriptor = clsDesc;
        FieldDescriptor fieldDescriptor;
        while (classDescriptor != null) {
            fieldDescriptor = new ClassDescriptorJDONature(classDescriptor).getField(fieldName);
            if (fieldDescriptor != null) {
                return fieldDescriptor;
            }
View Full Code Here


    private Object[] getFieldAndClassDesc(final String fieldName,
                                          final ClassDescriptor clsDesc,
                                          final QueryExpression expr,
                                          final Vector path, final int tableIndex) {
       
        FieldDescriptor field = null;
        ClassDescriptor cd = clsDesc;
        FieldDescriptor tempField = null;
        ClassDescriptor tempCd = clsDesc;
        Object[] retVal;

        while (tempCd != null) {
            tempField = new ClassDescriptorJDONature(tempCd).getField(fieldName);
View Full Code Here

     */
    private FieldDescriptor checkProjection(final ParseTreeNode projection,
                                               final boolean topLevel,
                                               final boolean onlySimple)
    throws QueryException {
        FieldDescriptor field = null;

        if (projection.getChildCount() == 0) {
            if (topLevel) {
                _projectionType = PARENT_OBJECT;
                _projectionName = projection.getToken().getTokenValue();
                if (!_projectionName.equals(_fromClassAlias)) {
                    throw new QueryException("Object name not the same in SELECT and "
                            + "FROM - select: " + _projectionName + ", from: "
                            + _fromClassAlias);
                }
            } else {
                if (onlySimple) {
                    throw new QueryException("Only primitive values are allowed to be "
                            + "passed as parameters to Aggregate and SQL functions.");
                }
                return null;
            }
        } else {
            int tokenType = projection.getToken().getTokenType();
            switch (tokenType) {
            case TokenType.IDENTIFIER:
                //a SQL function call -- check the arguments
                _projectionType = FUNCTION;
                for (Iterator iter = projection.getChild(0).children(); iter.hasNext(); ) {
                    checkProjection((ParseTreeNode) iter.next(), false, true);
                }
                break;
            case TokenType.DOT:
                //a path expression -- check if it is valid, and create a paramInfo
                Iterator iter = projection.children();
                ParseTreeNode curNode = null;
                String curName = null;
                StringBuffer projectionName = new StringBuffer();
                Vector projectionInfo = new Vector();

                //check that the first word before the dot is our class
                if (iter.hasNext()) {
                    curNode = (ParseTreeNode) iter.next();
                    curName = curNode.getToken().getTokenValue();
                    if ((!curName.equals(_projectionName))
                            && (!curName.equals(_projectionAlias))
                            && (!curName.equals(_fromClassName))
                            && (!curName.equals(_fromClassAlias))) {
                       
                        // reset the enumeration
                        iter = projection.children();
                        curName = _fromClassAlias;
                    }
                    projectionName.append(curName);
                    projectionInfo.addElement(curName);
                }

                //use the ClassDescriptor to check that the rest of the path is valid.
                ClassDescriptor curClassDesc = _clsDesc;
                FieldDescriptor curField = null;
                int count = 0;
                String curToken;
                while (iter.hasNext()) {
                    // there may be nested attribute name
                    curField = null;
                    curName = null;
                    while ((curField == null) && iter.hasNext()) {
                        curNode = (ParseTreeNode) iter.next();
                        curToken = curNode.getToken().getTokenValue();
                        if (curName == null) {
                            curName = curToken;
                        } else {
                            curName = curName + "." + curToken;
                        }
                        curField = getFieldDesc(curName, curClassDesc);
                    }
                    if (curField == null) {
                        throw new QueryException("An unknown field was requested: "
                                + curName + " (" + curClassDesc + ")");
                    }
                    projectionName.append(".").append(curName);
                    projectionInfo.addElement(curName);
                    curClassDesc = curField.getClassDescriptor();
                    if ((curClassDesc == null) && iter.hasNext()) {
                        throw new QueryException("An non-reference field was requested: "
                                + curName + " (" + curClassDesc + ")");
                    }
                    count++;
                }
                field = curField;

                getPathInfo().put(projection, projectionInfo);
                getFieldInfo().put(projection, curField);

                Class theClass = curField.getFieldType();
                // is it actually a Java primitive, or String, or a subclass of Number
                boolean isSimple = Types.isSimpleType(theClass);

                if (topLevel) {
                    _projectionName = projectionName.toString();
View Full Code Here

     * @throws QueryException if the field does not exist.
     */
    private FieldDescriptor checkField(final ParseTreeNode fieldTree)
    throws QueryException {
        //see if we've checked this field before.
        FieldDescriptor field = (FieldDescriptor) getFieldInfo().get(fieldTree);
        if (field != null) { return field; }

        if (fieldTree.getToken().getTokenType() == TokenType.DOT) {
            field = checkProjection(fieldTree, false, false);
        } else {
View Full Code Here

        }

        //Get the system defined type
        String systemType = "";
        // Get the SQL type if known
        FieldDescriptor desc = null;
        int operation = paramTree.getParent().getToken().getTokenType();
        switch (operation) {
        case TokenType.PLUS: case TokenType.MINUS: case TokenType.TIMES:
        case TokenType.DIVIDE: case TokenType.KEYWORD_MOD: case TokenType.KEYWORD_ABS:
        case TokenType.KEYWORD_LIMIT: //Alex
View Full Code Here

            case TokenType.TIME_LITERAL:
            case TokenType.TIMESTAMP_LITERAL: return "java.util.Time";
           
            case TokenType.DOT:
            case TokenType.IDENTIFIER:
                FieldDescriptor field = checkField(curChild);
                return field.getFieldType().getName();
            default:
                break;
            }
        }
View Full Code Here

        ClassDescriptor tempDesc = _clsDesc.getExtends();
        while (tempDesc != null) {
            String tableName = new ClassDescriptorJDONature(tempDesc).getTableName();
            _queryExpr.addTable(tableName);
           
            FieldDescriptor leftField = oldDesc.getIdentity();
            FieldDescriptor rightField = tempDesc.getIdentity();
           
            _queryExpr.addInnerJoin(
                    new ClassDescriptorJDONature(oldDesc).getTableName(),
                    new FieldDescriptorJDONature(leftField).getSQLName(),
                    new ClassDescriptorJDONature(tempDesc).getTableName(),
View Full Code Here

        // the class for the join is even this class or one of the base classes
        ClassDescriptor sourceClass = _clsDesc;

        for (int i = 1; i < path.size() - 1; i++) {
            FieldDescriptor fieldDesc = null;

            // Find the sourceclass and the fielsddescriptor in the class hierachie
            Object[] fieldAndClass = getFieldAndClassDesc(
                    (String) path.elementAt(i), sourceClass, _queryExpr, path, i - 1);
            if (fieldAndClass == null) {
                throw new IllegalStateException("Field not found:" + path.elementAt(i));
            }
            fieldDesc = (FieldDescriptor) fieldAndClass[0];
            sourceClass = (ClassDescriptor) fieldAndClass[1];

            ClassDescriptor clsDesc = fieldDesc.getClassDescriptor();
            FieldDescriptorJDONature fieldJDONature = new FieldDescriptorJDONature(fieldDesc);
            if (clsDesc != null) {
                //we must add this table as a join
                ClassDescriptorJDONature sourceClassJDONature =
                    new ClassDescriptorJDONature(sourceClass);
                if (fieldJDONature.getManyKey() == null) {
                    //a many -> one relationship
                    FieldDescriptor foreignKey = clsDesc.getIdentity();
                    String sourceTableAlias = sourceClassJDONature.getTableName();
                    if (i > 1) {
                        sourceTableAlias = buildTableAlias(sourceTableAlias, path, i - 1);
                    }

                    ClassDescriptorJDONature clsDescNature;
                    clsDescNature = new ClassDescriptorJDONature(clsDesc);
                    _queryExpr.addInnerJoin(
                            sourceClassJDONature.getTableName(),
                            new FieldDescriptorJDONature(fieldDesc).getSQLName(),
                            sourceTableAlias,
                            clsDescNature.getTableName(),
                            new FieldDescriptorJDONature(foreignKey).getSQLName(),
                            buildTableAlias(clsDescNature.getTableName(), path, i));
                } else if (fieldJDONature.getManyTable() == null) {
                    //a one -> many relationship
                    FieldDescriptor identity = sourceClass.getIdentity();
                    String sourceTableAlias = sourceClassJDONature.getTableName();
                    if (i > 1) {
                        sourceTableAlias = buildTableAlias(sourceTableAlias, path, i - 1);
                    }

                    ClassDescriptorJDONature clsDescNature;
                    clsDescNature = new ClassDescriptorJDONature(clsDesc);
                    _queryExpr.addInnerJoin(
                            sourceClassJDONature.getTableName(),
                            new FieldDescriptorJDONature(identity).getSQLName(),
                            sourceTableAlias,
                            clsDescNature.getTableName(),
                            fieldJDONature.getManyKey(),
                            buildTableAlias(clsDescNature.getTableName(), path, i));
                } else {
                    //a many -> many relationship
                    FieldDescriptor identity = sourceClass.getIdentity();
                    FieldDescriptor foreignKey = clsDesc.getIdentity();
                    String manyTableAlias = fieldJDONature.getManyTable();
                    String sourceTableAlias = sourceClassJDONature.getTableName();
                    if (i > 1) {
                        manyTableAlias = buildTableAlias(manyTableAlias, path, i - 1);
                        sourceTableAlias = buildTableAlias(sourceTableAlias, path, i - 1);
View Full Code Here

                // Exception follows in addJoinsForPathExpression()
              }
              addJoinsForPathExpression(path);
            }
           
            FieldDescriptor field = (FieldDescriptor) getFieldInfo().get(exprTree);
            if (field == null) {
                throw new IllegalStateException(
                        "fieldInfo for " + exprTree.toStringEx() + " not found");
            }
           
            ClassDescriptor clsDesc = field.getContainingClassDescriptor();
            if (clsDesc == null) {
                throw new IllegalStateException(
                        "ContainingClass of " + field.toString() + " is null !");
            }
           
            String clsTableAlias;
            ClassDescriptorJDONature classDescriptorJDONature =
                new ClassDescriptorJDONature(clsDesc);
                if (tokenType == TokenType.DOT && path != null && path.size() > 2) {
              clsTableAlias = buildTableAlias(
                      classDescriptorJDONature.getTableName(), path, path.size() - 2);
              ClassDescriptor srcDesc = _clsDesc;
              for (int i = 1; i < path.size(); i++) {
                Object[] fieldAndClass = getFieldAndClassDesc(
                                                              (String) path.elementAt(i),
                                                              srcDesc, _queryExpr, path, i - 1);
                if (fieldAndClass == null) {
                  throw new IllegalStateException("Field not found: "
                                                  + path.elementAt(i) + " class "
                                                  + srcDesc.getJavaClass());
                }
                FieldDescriptor fieldDesc = (FieldDescriptor) fieldAndClass[0];
                srcDesc = fieldDesc.getClassDescriptor();
              }
            } else {
              clsTableAlias = buildTableAlias(classDescriptorJDONature.getTableName(), path, 9999);
            }
           
View Full Code Here

    protected FieldDescriptor createFieldDesc( Class javaClass, FieldMapping fieldMap )
        throws MappingException
    {
       
        FieldDescriptor        fieldDesc;
        CollectionType         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 == CollectionType.HASHTABLE) ||
                (colType == CollectionType.MAP))
            {
                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 = _primitiveNodeType;
            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 = _naming.toXMLName( fieldDesc.getFieldName() );
            match = xmlName + ' ' + fieldDesc.getFieldName();
        }

        xmlDesc = new XMLFieldDescriptorImpl( fieldDesc, xmlName, nodeType, _primitiveNodeType );

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

        xmlDesc.setContainer(fieldMap.getContainer());

        if (xml != null) {
           
            //-- has class descriptor for type specified
            if (xml.getClassMapping() != null) {
                ClassDescriptor cd = createDescriptor(xml.getClassMapping());
                xmlDesc.setClassDescriptor((XMLClassDescriptor)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(NameValidator.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.setProperty(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 (CollectionHandlers.hasHandler(type)) {
                String typeName = CollectionHandlers.getCollectionName(type);
                colType = CollectionType.valueOf(typeName);
            }
        }
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.