Search in sources :

Example 1 with UnionType

use of org.apache.axis2.corba.idl.types.UnionType in project axis-axis2-java-core by apache.

the class CorbaUtil method extractValue.

private static Object extractValue(DataType dataType, Object param, SchemaToIDLMapping mapping) throws CorbaInvocationException {
    if (param == null) {
        return null;
    }
    if (dataType instanceof Typedef) {
        Typedef typedef = (Typedef) dataType;
        AliasValue aliasValue = new AliasValue(typedef);
        OMElement paramElement;
        if (param instanceof OMElement)
            paramElement = (OMElement) param;
        else
            return null;
        DataType aliasType = typedef.getDataType();
        if (!(aliasType instanceof AbstractCollectionType || aliasType instanceof FixedType)) {
            paramElement = paramElement.getFirstElement();
            if (paramElement == null || !ARRAY_ITEM.equals(paramElement.getLocalName()))
                return null;
        }
        aliasValue.setValue(extractValue(aliasType, paramElement, mapping));
        return aliasValue;
    } else if (dataType instanceof PrimitiveDataType) {
        if (param != null)
            return parseValue(dataType, ((OMElement) param).getText());
    } else if (dataType instanceof AbstractCollectionType) {
        AbstractCollectionType collectionType = (AbstractCollectionType) dataType;
        OMElement paramElement;
        if (param instanceof OMElement)
            paramElement = (OMElement) param;
        else
            return null;
        Iterator paramsIter = paramElement.getChildElements();
        List children = new ArrayList();
        while (paramsIter.hasNext()) {
            children.add(extractValue(collectionType.getDataType(), paramsIter.next(), mapping));
        }
        AbstractCollectionValue collectionValue;
        if (collectionType.isArray()) {
            collectionValue = new ArrayValue((ArrayType) collectionType);
        } else if (collectionType.isSequence()) {
            collectionValue = new SequenceValue((SequenceType) collectionType);
        } else {
            return null;
        }
        collectionValue.setValues(children.toArray());
        return collectionValue;
    } else if (dataType instanceof EnumType) {
        EnumType enumType = (EnumType) dataType;
        String enumText = ((OMElement) param).getText();
        int index = enumType.getEnumMembers().indexOf(enumText);
        if (index >= 0) {
            EnumValue enumValue = new EnumValue(enumType);
            enumValue.setValue(index);
            return enumValue;
        }
    } else if (dataType instanceof UnionType) {
        UnionType unionType = (UnionType) dataType;
        OMElement unElement = ((OMElement) param).getFirstElement();
        String unionMemberName = unElement.getLocalName();
        UnionValue unionValue = new UnionValue(unionType);
        unionValue.setMemberName(unionMemberName);
        Member[] members = unionType.getMembers();
        UnionMember member = null;
        for (int i = 0; i < members.length; i++) {
            member = (UnionMember) members[i];
            if (member.getName().equals(unionMemberName)) {
                break;
            }
        }
        if (member != null) {
            unionValue.setMemberValue(extractValue(member.getDataType(), unElement, mapping));
        }
        return unionValue;
    } else if (dataType instanceof CompositeDataType) {
        CompositeDataType compositeType = (CompositeDataType) dataType;
        Member[] compositeMembers = compositeType.getMembers();
        Object[] compositeValues = extractParameters(((OMElement) param), compositeMembers, mapping);
        AbstractValue value;
        if (compositeType instanceof ValueType)
            value = new ObjectByValue((ValueType) compositeType);
        else if (compositeType instanceof Struct)
            value = new StructValue((Struct) compositeType);
        else
            throw new CorbaInvocationException("Parameter type not supported");
        value.setMemberValues(compositeValues);
        return value;
    } else if (dataType instanceof AnyType) {
        OMElement anyElement = (OMElement) param;
        DefaultNamespaceGenerator namespaceGenerator = new DefaultNamespaceGenerator();
        String defaultNamespace = namespaceGenerator.schemaNamespaceFromPackageName("").toString();
        OMElement typeElement = anyElement.getFirstChildWithName(new QName(defaultNamespace, "type"));
        if (typeElement != null) {
            OMElement definitionElement = typeElement.getFirstChildWithName(new QName(defaultNamespace, "definition"));
            OMElement typenameElement = typeElement.getFirstChildWithName(new QName(defaultNamespace, "typename"));
            OMElement anyValueElement = anyElement.getFirstChildWithName(new QName(defaultNamespace, "value"));
            if (typenameElement != null && anyValueElement != null) {
                String typeName = typenameElement.getText();
                String definition = definitionElement != null ? definitionElement.getText() : Constants.URI_DEFAULT_SCHEMA_XSD;
                Object anyContent;
                DataType anyValueType;
                if (definition.equals(Constants.URI_DEFAULT_SCHEMA_XSD)) {
                    String anyValueString = anyValueElement.getText();
                    if (typeName.equals("boolean")) {
                        anyValueType = PrimitiveDataType.getPrimitiveDataType("boolean");
                        anyContent = Boolean.parseBoolean(anyValueString);
                    } else if (typeName.equals("double")) {
                        anyValueType = PrimitiveDataType.getPrimitiveDataType("double");
                        anyContent = Double.parseDouble(anyValueString);
                    } else if (typeName.equals("float")) {
                        anyValueType = PrimitiveDataType.getPrimitiveDataType("float");
                        anyContent = Float.parseFloat(anyValueString);
                    } else if (typeName.equals("unsignedByte")) {
                        anyValueType = PrimitiveDataType.getPrimitiveDataType("octet");
                        anyContent = Byte.parseByte(anyValueString);
                    } else if (typeName.equals("int")) {
                        anyValueType = PrimitiveDataType.getPrimitiveDataType("long");
                        anyContent = Integer.parseInt(anyValueString);
                    } else if (typeName.equals("long")) {
                        anyValueType = PrimitiveDataType.getPrimitiveDataType("longlong");
                        anyContent = Long.parseLong(anyValueString);
                    } else if (typeName.equals("short")) {
                        anyValueType = PrimitiveDataType.getPrimitiveDataType("short");
                        anyContent = Short.parseShort(anyValueString);
                    } else if (typeName.equals("string")) {
                        anyValueType = PrimitiveDataType.getPrimitiveDataType("string");
                        anyContent = anyValueString;
                    } else if (typeName.equals("unsignedShort")) {
                        anyValueType = PrimitiveDataType.getPrimitiveDataType("ushort");
                        anyContent = Short.parseShort(anyValueString);
                    } else if (typeName.equals("unsignedInt")) {
                        anyValueType = PrimitiveDataType.getPrimitiveDataType("ulong");
                        anyContent = Integer.parseInt(anyValueString);
                    } else if (typeName.equals("unsignedLong")) {
                        anyValueType = PrimitiveDataType.getPrimitiveDataType("ulonglong");
                        anyContent = Long.parseLong(anyValueString);
                    } else {
                        throw new CorbaInvocationException("Unsupported data type: " + typeName);
                    }
                } else {
                    anyValueType = mapping.getDataType(new QName(definition, typeName));
                    if (anyValueType != null) {
                        anyContent = CorbaUtil.extractValue(anyValueType, anyValueElement.getFirstElement(), mapping);
                    } else {
                        throw new CorbaInvocationException("Unsupported data schema: " + definition + " type:" + typeName);
                    }
                }
                AnyValue anyValue = new AnyValue();
                anyValue.setContent(anyContent);
                anyValue.setContentType(anyValueType);
                return anyValue;
            }
        }
    } else if (dataType instanceof FixedType) {
        return new BigDecimal(((OMElement) param).getText());
    }
    return null;
}
Also used : DefaultNamespaceGenerator(org.apache.axis2.description.java2wsdl.DefaultNamespaceGenerator) OMElement(org.apache.axiom.om.OMElement) QName(javax.xml.namespace.QName) BigDecimal(java.math.BigDecimal) CorbaInvocationException(org.apache.axis2.corba.exceptions.CorbaInvocationException)

Example 2 with UnionType

use of org.apache.axis2.corba.idl.types.UnionType in project axis-axis2-java-core by apache.

the class IDLVisitor method findDataType.

private DataType findDataType(AST typeNode, String parentName, boolean root, boolean isInsideATypeDef) throws InvalidIDLException {
    // Check for sequences
    if (typeNode.getType() == IDLTokenTypes.LITERAL_sequence) {
        SequenceType sequenceType = visitAnonymousSequence(typeNode, parentName, root);
        if (isInsideATypeDef) {
            return sequenceType;
        }
        Typedef typedef = new Typedef();
        typedef.setDataType(sequenceType);
        typedef.setModule(module);
        typedef.setName(parentName + '_' + sequenceType.getName());
        idl.addType(typedef);
        return typedef;
    }
    DataType dataType;
    String typeName;
    if (typeNode.getType() == IDLTokenTypes.LITERAL_unsigned) {
        AST nextNode = typeNode.getNextSibling();
        if (nextNode == null) {
            throw new InvalidIDLException("'unsigned' without a data type");
        } else if (nextNode.getType() == IDLTokenTypes.LITERAL_short) {
            typeNode.setNextSibling(nextNode.getNextSibling());
            typeNode.setFirstChild(nextNode.getFirstChild());
            typeName = "ushort";
        } else if (nextNode.getType() == IDLTokenTypes.LITERAL_long) {
            AST nextToLong = nextNode.getNextSibling();
            if (nextToLong != null && nextToLong.getType() == IDLTokenTypes.LITERAL_long) {
                typeNode.setNextSibling(nextToLong.getNextSibling());
                typeNode.setFirstChild(nextToLong.getFirstChild());
                typeName = "ulonglong";
            } else {
                typeNode.setNextSibling(nextNode.getNextSibling());
                typeNode.setFirstChild(nextNode.getFirstChild());
                typeName = "ulong";
            }
        } else {
            throw new InvalidIDLException("either 'long' or 'short' is expected after the 'unsigned' keyword");
        }
    } else if (typeNode.getType() == IDLTokenTypes.LITERAL_long) {
        AST nextToLong = typeNode.getNextSibling();
        if (nextToLong != null && nextToLong.getType() == IDLTokenTypes.LITERAL_long) {
            typeNode.setNextSibling(nextToLong.getNextSibling());
            typeNode.setFirstChild(nextToLong.getFirstChild());
            typeName = "longlong";
        } else {
            typeName = "long";
        }
    } else if (typeNode.getType() == IDLTokenTypes.LITERAL_struct) {
        String innerModule = module + parentName + INNERTYPE_SUFFIX + CompositeDataType.MODULE_SEPERATOR;
        Struct innerElem = visitStruct(typeNode);
        innerElem.setModule(innerModule);
        idl.addType(innerElem);
        return innerElem;
    } else if (typeNode.getType() == IDLTokenTypes.LITERAL_valuetype) {
        String innerModule = module + parentName + INNERTYPE_SUFFIX + CompositeDataType.MODULE_SEPERATOR;
        ValueType innerElem = visitValueType(typeNode);
        innerElem.setModule(innerModule);
        idl.addType(innerElem);
        return innerElem;
    } else if (typeNode.getType() == IDLTokenTypes.LITERAL_exception) {
        String innerModule = module + parentName + INNERTYPE_SUFFIX + CompositeDataType.MODULE_SEPERATOR;
        Struct innerElem = visitException(typeNode);
        innerElem.setModule(innerModule);
        idl.addType(innerElem);
        return innerElem;
    } else if (typeNode.getType() == IDLTokenTypes.LITERAL_enum) {
        String innerModule = module + parentName + INNERTYPE_SUFFIX + CompositeDataType.MODULE_SEPERATOR;
        EnumType innerElem = visitEnum(typeNode);
        innerElem.setModule(innerModule);
        idl.addType(innerElem);
        return innerElem;
    } else if (typeNode.getType() == IDLTokenTypes.LITERAL_union) {
        String innerModule = module + parentName + INNERTYPE_SUFFIX + CompositeDataType.MODULE_SEPERATOR;
        UnionType innerElem = visitUnion(typeNode);
        innerElem.setModule(innerModule);
        idl.addType(innerElem);
        return innerElem;
    } else if (typeNode.getType() == IDLTokenTypes.LITERAL_fixed) {
        AST digitsNode = typeNode.getFirstChild();
        short digits = 0;
        short scale = 0;
        if (digitsNode != null) {
            AST scaleNode = digitsNode.getNextSibling();
            digits = Short.parseShort(digitsNode.getText());
            scale = Short.parseShort(scaleNode.getText());
        }
        FixedType fixedType = new FixedType(digits, scale);
        if (isInsideATypeDef) {
            return fixedType;
        }
        Typedef typedef = new Typedef();
        typedef.setDataType(fixedType);
        typedef.setModule(module);
        String name = typeNode.getNextSibling().getText();
        typedef.setName(parentName + '_' + name);
        idl.addType(typedef);
        return typedef;
    } else {
        typeName = getTypeName(typeNode);
    }
    /*   Map compositeDataTypes = idl.getCompositeDataTypes();
            if (compositeDataTypes!=null) {
            if (!module.equals("")) {
                if (!typeName.startsWith(module)) {
                    dataType = (DataType) idl.getCompositeDataTypes().get(module + typeName);
                }
            }

            if (dataType==null && moduleForInnerTypes!=null) {
                if (!typeName.startsWith(module)) {
                    dataType = (DataType) idl.getCompositeDataTypes().get(moduleForInnerTypes + typeName);
                }
            }

            if (dataType==null)
                dataType = (DataType) idl.getCompositeDataTypes().get(typeName);
        }


        if (dataType == null)
            dataType = PrimitiveDataType.getPrimitiveDataType(typeName);

        if (dataType == null)
            throw new InvalidIDLException("Invalid data type: " + typeName);
        }

        */
    dataType = getDataType(typeName);
    return dataType;
}
Also used : InvalidIDLException(org.apache.axis2.corba.exceptions.InvalidIDLException) AST(antlr.collections.AST)

Example 3 with UnionType

use of org.apache.axis2.corba.idl.types.UnionType in project axis-axis2-java-core by apache.

the class IDLVisitor method visitUnion.

private UnionType visitUnion(AST node) throws InvalidIDLException {
    UnionType unionType = new UnionType();
    AST unNode = node.getFirstChild();
    String unName = unNode.toString();
    unionType.setModule(module);
    unionType.setName(unName);
    AST switchTypeNode = unNode.getNextSibling();
    DataType discrimType = findDataType(switchTypeNode, unName);
    unionType.setDiscriminatorType(discrimType);
    AST caseOrDefaultNode = switchTypeNode.getNextSibling();
    while (caseOrDefaultNode != null) {
        UnionMember unionMember = new UnionMember();
        AST typeNode;
        if (IDLTokenTypes.LITERAL_default == caseOrDefaultNode.getType()) {
            unionMember.setDefault(true);
            typeNode = caseOrDefaultNode.getFirstChild();
        } else {
            unionMember.setDefault(false);
            AST caseValueNode = caseOrDefaultNode.getFirstChild();
            String caseNodeText = caseValueNode.getText();
            if (!(discrimType instanceof EnumType) && IDLTokenTypes.IDENT == caseValueNode.getType()) {
                // Get const value
                DataType constType = getDataType(caseNodeText);
                if (constType instanceof ConstType) {
                    caseNodeText = ((ConstType) constType).getValue().toString();
                } else {
                    throw new InvalidIDLException(caseNodeText + "is not a constant name");
                }
            }
            unionMember.setDiscriminatorValue(caseNodeText);
            typeNode = caseValueNode.getNextSibling();
        }
        unionMember.setDataType(findDataType(typeNode, unName));
        AST memberNode = typeNode.getNextSibling();
        unionMember.setName(memberNode.toString());
        unionType.addMember(unionMember);
        caseOrDefaultNode = caseOrDefaultNode.getNextSibling();
    }
    return unionType;
}
Also used : InvalidIDLException(org.apache.axis2.corba.exceptions.InvalidIDLException) AST(antlr.collections.AST)

Example 4 with UnionType

use of org.apache.axis2.corba.idl.types.UnionType in project axis-axis2-java-core by apache.

the class CorbaUtil method extractValue.

public static Object extractValue(DataType returnType, Any returned) {
    Object returnValue = null;
    TypeCode typeCode = returnType.getTypeCode();
    switch(typeCode.kind().value()) {
        case TCKind._tk_void:
            returnValue = null;
            break;
        case TCKind._tk_long:
            returnValue = new Integer(returned.extract_long());
            break;
        case TCKind._tk_ulong:
            returnValue = new Integer(returned.extract_ulong());
            break;
        case TCKind._tk_longlong:
            returnValue = new Long(returned.extract_longlong());
            break;
        case TCKind._tk_ulonglong:
            returnValue = new Long(returned.extract_ulonglong());
            break;
        case TCKind._tk_short:
            returnValue = new Short(returned.extract_short());
            break;
        case TCKind._tk_ushort:
            returnValue = new Short(returned.extract_ushort());
            break;
        case TCKind._tk_float:
            returnValue = new Float(returned.extract_float());
            break;
        case TCKind._tk_double:
            returnValue = new Double(returned.extract_double());
            break;
        case TCKind._tk_char:
            returnValue = new Character(returned.extract_char());
            break;
        case TCKind._tk_wchar:
            returnValue = new Character(returned.extract_wchar());
            break;
        case TCKind._tk_boolean:
            returnValue = Boolean.valueOf(returned.extract_boolean());
            break;
        case TCKind._tk_octet:
            returnValue = new Byte(returned.extract_octet());
            break;
        case TCKind._tk_string:
            returnValue = returned.extract_string();
            break;
        case TCKind._tk_wstring:
            returnValue = returned.extract_wstring();
            break;
        case TCKind._tk_any:
            returnValue = returned.extract_any();
            break;
        case TCKind._tk_value:
            returnValue = returned.extract_Value();
            break;
        case TCKind._tk_objref:
            returnValue = returned.extract_Object();
            break;
        // case TCKind._tk_longdouble :
        case TCKind._tk_struct:
            Struct struct = (Struct) returnType;
            StructValue structValue = new StructValue(struct);
            org.omg.CORBA_2_3.portable.InputStream inputStream = (org.omg.CORBA_2_3.portable.InputStream) returned.create_input_stream();
            structValue.read(inputStream);
            returnValue = structValue;
            break;
        case TCKind._tk_except:
            ExceptionType exceptionType = (ExceptionType) returnType;
            ExceptionValue exceptionValue = new ExceptionValue(exceptionType);
            inputStream = (org.omg.CORBA_2_3.portable.InputStream) returned.create_input_stream();
            exceptionValue.read(inputStream);
            returnValue = exceptionValue;
            break;
        case TCKind._tk_enum:
            EnumType enumType = (EnumType) returnType;
            EnumValue enumValue = new EnumValue(enumType);
            inputStream = (org.omg.CORBA_2_3.portable.InputStream) returned.create_input_stream();
            enumValue.read(inputStream);
            returnValue = enumValue;
            break;
        case TCKind._tk_union:
            UnionType unionType = (UnionType) returnType;
            inputStream = (org.omg.CORBA_2_3.portable.InputStream) returned.create_input_stream();
            UnionValue unionValue = new UnionValue(unionType);
            unionValue.read(inputStream);
            returnValue = unionValue;
            break;
        case TCKind._tk_alias:
            Typedef typedef = (Typedef) returnType;
            inputStream = (org.omg.CORBA_2_3.portable.InputStream) returned.create_input_stream();
            AliasValue aliasValue = new AliasValue(typedef);
            aliasValue.read(inputStream);
            returnValue = aliasValue;
            break;
        case TCKind._tk_sequence:
            SequenceType sequenceType = (SequenceType) returnType;
            inputStream = (org.omg.CORBA_2_3.portable.InputStream) returned.create_input_stream();
            SequenceValue sequenceValue = new SequenceValue(sequenceType);
            sequenceValue.read(inputStream);
            returnValue = sequenceValue;
            break;
        case TCKind._tk_array:
            ArrayType arrayType = (ArrayType) returnType;
            inputStream = (org.omg.CORBA_2_3.portable.InputStream) returned.create_input_stream();
            ArrayValue arrayValue = new ArrayValue(arrayType);
            arrayValue.read(inputStream);
            returnValue = arrayValue;
            break;
        default:
            log.error("ERROR! Invalid dataType");
            break;
    }
    return returnValue;
}
Also used : TypeCode(org.omg.CORBA.TypeCode) PreProcessorInputStream(org.apache.axis2.corba.idl.PreProcessorInputStream)

Example 5 with UnionType

use of org.apache.axis2.corba.idl.types.UnionType in project axis-axis2-java-core by apache.

the class UnionValue method populateDiscriminator.

private void populateDiscriminator() {
    Member[] members = getMembers();
    UnionMember unionMember = null;
    for (int i = 0; i < members.length; i++) {
        unionMember = (UnionMember) members[i];
        if (unionMember.getName().equals(memberName))
            break;
    }
    if (unionMember != null) {
        setMemberType(unionMember.getDataType());
        if (!unionMember.isDefault()) {
            discriminator = CorbaUtil.parseValue(((UnionType) dataType).getDiscriminatorType(), unionMember.getDiscriminatorValue());
        } else if (unionMember.isDefault()) {
            DataType discriminatorType = ((UnionType) dataType).getDiscriminatorType();
            int kindVal = discriminatorType.getTypeCode().kind().value();
            switch(kindVal) {
                case TCKind._tk_long:
                    discriminator = Integer.valueOf(-2147483648);
                    break;
                case TCKind._tk_char:
                case TCKind._tk_wchar:
                    discriminator = Character.valueOf('\u0000');
                    break;
                case TCKind._tk_enum:
                    EnumType enumType = (EnumType) discriminatorType;
                    EnumValue enumValue = new EnumValue(enumType);
                    enumValue.setValue(0);
                    discriminator = enumValue;
                    break;
                default:
                    log.error("Unsupported union member type");
            }
        } else {
            discriminator = null;
        }
    }
}
Also used : UnionType(org.apache.axis2.corba.idl.types.UnionType) EnumType(org.apache.axis2.corba.idl.types.EnumType) DataType(org.apache.axis2.corba.idl.types.DataType) Member(org.apache.axis2.corba.idl.types.Member) UnionMember(org.apache.axis2.corba.idl.types.UnionMember) UnionMember(org.apache.axis2.corba.idl.types.UnionMember)

Aggregations

QName (javax.xml.namespace.QName)3 UnionType (org.apache.axis2.corba.idl.types.UnionType)3 AST (antlr.collections.AST)2 BigDecimal (java.math.BigDecimal)2 OMElement (org.apache.axiom.om.OMElement)2 InvalidIDLException (org.apache.axis2.corba.exceptions.InvalidIDLException)2 DataType (org.apache.axis2.corba.idl.types.DataType)2 EnumType (org.apache.axis2.corba.idl.types.EnumType)2 Member (org.apache.axis2.corba.idl.types.Member)2 TypeCode (org.omg.CORBA.TypeCode)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 OMNamespace (org.apache.axiom.om.OMNamespace)1 AxisFault (org.apache.axis2.AxisFault)1 CorbaInvocationException (org.apache.axis2.corba.exceptions.CorbaInvocationException)1 PreProcessorInputStream (org.apache.axis2.corba.idl.PreProcessorInputStream)1 CompositeDataType (org.apache.axis2.corba.idl.types.CompositeDataType)1 FixedType (org.apache.axis2.corba.idl.types.FixedType)1 PrimitiveDataType (org.apache.axis2.corba.idl.types.PrimitiveDataType)1 Typedef (org.apache.axis2.corba.idl.types.Typedef)1