Search in sources :

Example 1 with AbstractCollectionType

use of org.apache.axis2.corba.idl.types.AbstractCollectionType 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 AbstractCollectionType

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

the class SchemaGenerator method generateSchemaforFieldsandProperties.

// moved code common to Fields & properties out of above method
private XmlSchemaElement generateSchemaforFieldsandProperties(XmlSchema xmlSchema, DataType type, String name, boolean forceNotNillable) throws SchemaGeneratorException {
    boolean isArryType = false;
    long maxOccurs = 0;
    long minOccurs = 0;
    if (type instanceof AbstractCollectionType) {
        AbstractCollectionType collectionType = (AbstractCollectionType) type;
        type = collectionType.getDataType();
        isArryType = true;
        int elementCount = collectionType.getElementCount();
        if (collectionType.isArray()) {
            minOccurs = maxOccurs = elementCount;
        } else if (collectionType.isSequence()) {
            minOccurs = 0;
            maxOccurs = (elementCount == 0) ? Long.MAX_VALUE : elementCount;
        }
        if (type instanceof AbstractCollectionType) {
            AbstractCollectionType child = (AbstractCollectionType) type;
            Typedef typedef = new Typedef();
            typedef.setDataType(type);
            typedef.setModule(child.getElementModule());
            typedef.setName("_" + (child.getDepth() - 1) + "_" + child.getElementName());
            type = typedef;
        }
    }
    String propertyTypeName = CorbaUtil.getQualifiedName(type);
    if (isArryType && "byte".equals(propertyTypeName)) {
        propertyTypeName = "base64Binary";
    }
    XmlSchemaElement elt1 = new XmlSchemaElement(xmlSchema, false);
    elt1.setName(name);
    if (isArryType && (!propertyTypeName.equals("base64Binary"))) {
        elt1.setMaxOccurs(maxOccurs);
        elt1.setMinOccurs(minOccurs);
    }
    if (isNillable(type) && !forceNotNillable)
        elt1.setNillable(true);
    if (typeTable.isSimpleType(propertyTypeName)) {
        elt1.setSchemaTypeName(typeTable.getSimpleSchemaTypeName(propertyTypeName));
    } else if (type instanceof CompositeDataType) {
        generateSchema((CompositeDataType) type);
        elt1.setSchemaTypeName(typeTable.getComplexSchemaType(propertyTypeName));
        if (!((NamespaceMap) xmlSchema.getNamespaceContext()).values().contains(typeTable.getComplexSchemaType(propertyTypeName).getNamespaceURI())) {
            XmlSchemaImport importElement = new XmlSchemaImport(xmlSchema);
            importElement.setNamespace(typeTable.getComplexSchemaType(propertyTypeName).getNamespaceURI());
            xmlSchema.getItems().add(importElement);
            ((NamespaceMap) xmlSchema.getNamespaceContext()).put(generatePrefix(), typeTable.getComplexSchemaType(propertyTypeName).getNamespaceURI());
        }
    } else if (type instanceof AnyType) {
        elt1.setSchemaTypeName(generateSchemaForAnyType());
    } else {
        throw new SchemaGeneratorException("Unsupported type:" + type);
    }
    return elt1;
}
Also used : Typedef(org.apache.axis2.corba.idl.types.Typedef) AbstractCollectionType(org.apache.axis2.corba.idl.types.AbstractCollectionType) SchemaGeneratorException(org.apache.axis2.corba.exceptions.SchemaGeneratorException) CompositeDataType(org.apache.axis2.corba.idl.types.CompositeDataType) AnyType(org.apache.axis2.corba.idl.types.AnyType)

Example 3 with AbstractCollectionType

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

the class SequenceValue method write.

public void write(OutputStream outputStream) {
    AbstractCollectionType collectionType = (AbstractCollectionType) dataType;
    DataType memberType = collectionType.getDataType();
    int length = values.length;
    outputStream.write_long(length);
    for (int i = 0; i < length; i++) {
        write(values[i], memberType, outputStream);
    }
}
Also used : AbstractCollectionType(org.apache.axis2.corba.idl.types.AbstractCollectionType) DataType(org.apache.axis2.corba.idl.types.DataType)

Example 4 with AbstractCollectionType

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

the class SequenceValue method read.

public void read(InputStream inputStream) {
    AbstractCollectionType collectionType = (AbstractCollectionType) dataType;
    DataType memberType = collectionType.getDataType();
    int length = inputStream.read_long();
    values = new Object[length];
    for (int i = 0; i < length; i++) {
        values[i] = read(memberType, inputStream);
    }
}
Also used : AbstractCollectionType(org.apache.axis2.corba.idl.types.AbstractCollectionType) DataType(org.apache.axis2.corba.idl.types.DataType)

Example 5 with AbstractCollectionType

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

the class CorbaUtil method processResponse.

private static void processResponse(OMElement child, OMElement bodyContent, Object resObject, DataType dataType, SOAPFactory fac, OMNamespace defaultNS, boolean qualified, AxisService service) throws AxisFault {
    if (dataType instanceof PrimitiveDataType) {
        child.addChild(fac.createOMText(child, resObject.toString()));
    } else if (dataType instanceof Typedef) {
        Typedef typedef = (Typedef) dataType;
        AliasValue aliasValue = (AliasValue) resObject;
        OMNamespace ns = getNameSpaceForType(fac, service, typedef);
        OMElement item = fac.createOMElement(ARRAY_ITEM, ns, child);
        processResponse(item, child, aliasValue.getValue(), typedef.getDataType(), fac, ns, qualified, service);
    } else if (dataType instanceof FixedType) {
        child.addChild(fac.createOMText(child, resObject.toString()));
    } else if (dataType instanceof AbstractCollectionType) {
        AbstractCollectionType collectionType = (AbstractCollectionType) dataType;
        AbstractCollectionValue collectionValue = (AbstractCollectionValue) resObject;
        Object[] values = collectionValue.getValues();
        int length = values.length;
        for (int i = 0; i < length; i++) {
            OMElement outer = bodyContent;
            if (collectionType.getDataType() instanceof AbstractCollectionType) {
                outer = child;
                if (qualified) {
                    child = fac.createOMElement(ARRAY_ITEM, defaultNS);
                } else {
                    child = fac.createOMElement(ARRAY_ITEM, null);
                }
                outer.addChild(child);
            }
            processResponse(child, outer, values[i], collectionType.getDataType(), fac, defaultNS, qualified, service);
            if (i < (length - 1)) {
                if (qualified) {
                    child = fac.createOMElement(ARRAY_ITEM, defaultNS);
                } else {
                    child = fac.createOMElement(ARRAY_ITEM, null);
                }
                bodyContent.addChild(child);
            }
        }
    } else if (dataType instanceof ValueType || dataType instanceof Struct) {
        AbstractValue resValue = (AbstractValue) resObject;
        Member[] members = resValue.getMembers();
        Object[] memberValues = resValue.getMemberValues();
        OMNamespace ns = getNameSpaceForType(fac, service, (CompositeDataType) dataType);
        for (int i = 0; i < memberValues.length; i++) {
            OMElement memberElement = fac.createOMElement(members[i].getName(), ns);
            processResponse(memberElement, bodyContent, memberValues[i], members[i].getDataType(), fac, ns, qualified, service);
            child.addChild(memberElement);
        }
    } else if (dataType instanceof UnionType) {
        UnionValue unionValue = (UnionValue) resObject;
        OMElement unMember;
        OMNamespace ns = getNameSpaceForType(fac, service, (CompositeDataType) dataType);
        if (qualified) {
            unMember = fac.createOMElement(unionValue.getMemberName(), ns);
        } else {
            unMember = fac.createOMElement(unionValue.getMemberName(), null);
        }
        processResponse(unMember, child, unionValue.getMemberValue(), unionValue.getMemberType(), fac, ns, qualified, service);
        child.addChild(unMember);
    } else if (dataType instanceof EnumType) {
        EnumValue enumValue = (EnumValue) resObject;
        child.addChild(fac.createOMText(child, enumValue.getValueAsString()));
    } else if (dataType instanceof AnyType) {
        Any any = (Any) resObject;
        TypeCode typeCode = any.type();
        DataType contentDataType;
        String dataTypeNameSpaceURI;
        if (PrimitiveDataType.isPrimitive(typeCode)) {
            contentDataType = new PrimitiveDataType(typeCode);
            dataTypeNameSpaceURI = Constants.URI_DEFAULT_SCHEMA_XSD;
        } else if (TCKind._tk_any == typeCode.kind().value()) {
            dataTypeNameSpaceURI = Constants.URI_DEFAULT_SCHEMA_XSD;
            contentDataType = new AnyType();
        } else {
            try {
                String id = typeCode.id();
                IDL idl = (IDL) service.getParameterValue(IDL_LITERAL);
                Map complexTypes = idl.getCompositeDataTypes();
                String typeKey = id.substring(id.indexOf(":") + 1, id.lastIndexOf(":")).replaceAll("/", "::");
                contentDataType = (DataType) complexTypes.get(typeKey);
                OMNamespace namespace = getNameSpaceForType(fac, service, (CompositeDataType) contentDataType);
                dataTypeNameSpaceURI = namespace.getNamespaceURI();
            } catch (BadKind badKind) {
                throw AxisFault.makeFault(badKind);
            }
        }
        if (contentDataType == null) {
            throw new AxisFault("can't find the data type of the returned value.");
        }
        Object value = CorbaUtil.extractValue(contentDataType, any);
        TypeTable typeTable = service.getTypeTable();
        QName anySchema = typeTable.getComplexSchemaType(CorbaConstants.ANY_TYPE_NAME);
        if (anySchema == null) {
            throw new AxisFault("CORBA.Any schema type is not defined.");
        }
        String defaultNSURI = anySchema.getNamespaceURI();
        String defaultNSPrefix = anySchema.getPrefix();
        OMElement valueElement = fac.createOMElement(new QName(defaultNSURI, "value", defaultNSPrefix));
        processResponse(valueElement, child, value, contentDataType, fac, defaultNS, qualified, service);
        child.addChild(valueElement);
        OMElement definitionElement = fac.createOMElement(new QName(defaultNSURI, "definition", defaultNSPrefix));
        definitionElement.addChild(fac.createOMText(dataTypeNameSpaceURI));
        OMElement typeNameElement = fac.createOMElement(new QName(defaultNSURI, "typename", defaultNSPrefix));
        String typeName;
        if (contentDataType instanceof PrimitiveDataType) {
            typeName = ((PrimitiveDataType) contentDataType).getTypeName();
            if (String.class.getName().equals(typeName)) {
                typeName = "string";
            }
        } else if (contentDataType instanceof CompositeDataType) {
            typeName = ((CompositeDataType) contentDataType).getName();
        } else if (contentDataType instanceof AnyType) {
            typeName = CorbaConstants.ANY_TYPE_NAME;
        } else {
            throw new AxisFault("Invalid return type");
        }
        typeNameElement.addChild(fac.createOMText(typeName));
        OMElement typeElement = fac.createOMElement(new QName(defaultNSURI, "type", defaultNSPrefix));
        typeElement.addChild(definitionElement);
        typeElement.addChild(typeNameElement);
        child.addChild(typeElement);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) OMElement(org.apache.axiom.om.OMElement) Any(org.omg.CORBA.Any) TypeTable(org.apache.axis2.description.java2wsdl.TypeTable) BadKind(org.omg.CORBA.TypeCodePackage.BadKind) OMNamespace(org.apache.axiom.om.OMNamespace) TypeCode(org.omg.CORBA.TypeCode) QName(javax.xml.namespace.QName)

Aggregations

AbstractCollectionType (org.apache.axis2.corba.idl.types.AbstractCollectionType)6 DataType (org.apache.axis2.corba.idl.types.DataType)4 QName (javax.xml.namespace.QName)2 OMElement (org.apache.axiom.om.OMElement)2 BigDecimal (java.math.BigDecimal)1 OMNamespace (org.apache.axiom.om.OMNamespace)1 AxisFault (org.apache.axis2.AxisFault)1 CorbaInvocationException (org.apache.axis2.corba.exceptions.CorbaInvocationException)1 SchemaGeneratorException (org.apache.axis2.corba.exceptions.SchemaGeneratorException)1 AnyType (org.apache.axis2.corba.idl.types.AnyType)1 CompositeDataType (org.apache.axis2.corba.idl.types.CompositeDataType)1 Typedef (org.apache.axis2.corba.idl.types.Typedef)1 DefaultNamespaceGenerator (org.apache.axis2.description.java2wsdl.DefaultNamespaceGenerator)1 TypeTable (org.apache.axis2.description.java2wsdl.TypeTable)1 Any (org.omg.CORBA.Any)1 TypeCode (org.omg.CORBA.TypeCode)1 BadKind (org.omg.CORBA.TypeCodePackage.BadKind)1