Package org.apache.yoko.bindings.corba.types

Examples of org.apache.yoko.bindings.corba.types.CorbaUnionHandler


        QName objIdlType = new QName(staxTestNamespaceURI, "TestUnion", staxTestPrefix);
        CorbaTypeImpl type = CorbaUtils.getCorbaType(objIdlType, staxObject.getTypeMaps());
        assertTrue(type instanceof Union);
        Union unionType = (Union)type;
        TypeCode objTypeCode = CorbaUtils.getTypeCode(orb, objIdlType, staxObject.getTypeMaps());
        CorbaUnionHandler obj = new CorbaUnionHandler(objName, objIdlType, objTypeCode, unionType);
        assertNotNull(obj);
       
        Character unionValue = new Character('u');
        TypeCode unionValueTC = CorbaUtils.getTypeCode(orb, CorbaConstants.NT_CORBA_WCHAR,
                                                       staxObject.getTypeMaps());
        CorbaPrimitiveHandler unionValueObj = new CorbaPrimitiveHandler(new QName("case12"),
                                                                        CorbaConstants.NT_CORBA_WCHAR,
                                                                        unionValueTC,
                                                                        null);
        unionValueObj.setValue(unionValue);
        Short discriminatorValue = new Short((short)1);
        TypeCode discriminatorTC = CorbaUtils.getTypeCode(orb, unionType.getDiscriminator(),
                                                          staxObject.getTypeMaps());
        CorbaPrimitiveHandler discriminator = new CorbaPrimitiveHandler(new QName("discriminator"),
                                                                       unionType.getDiscriminator(),
                                                                       discriminatorTC,
                                                                       null);
        discriminator.setValue(discriminatorValue);

        obj.setDiscriminator(discriminator);
        obj.setValue("1", unionValueObj);
       
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
     
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        XMLEventWriter evtWriter = null;
        try {
            evtWriter = outputFactory.createXMLEventWriter(outStream);
        } catch (XMLStreamException ex) {
        }
        assertNotNull(evtWriter);
       
        try {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            staxObject.writeObjectToStax(obj, evtWriter, factory);
            evtWriter.flush();
        } catch (XMLStreamException ex) {
           
        }

        ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());       
        XMLEventReader evtReader = null;
        try {           
            evtReader = inputFactory.createXMLEventReader(inStream);
            // read the first tag which should be a start document
            XMLEvent evt = evtReader.nextEvent();
            assertTrue(evt.isStartDocument());
        } catch (XMLStreamException ex) {
        }
        assertNotNull(evtReader);
       
        CorbaObjectHandler resultObj = staxObject.readObjectFromStax(evtReader,
                                                                     obj.getIdlType());
        assertNotNull(resultObj);
       
        assertTrue(resultObj instanceof CorbaUnionHandler);
        CorbaUnionHandler result = (CorbaUnionHandler)resultObj;
       
        CorbaObjectHandler resultDiscriminatorObj = result.getDiscriminator();
        assertTrue(resultDiscriminatorObj instanceof CorbaPrimitiveHandler);
        CorbaPrimitiveHandler resultDiscriminator = (CorbaPrimitiveHandler)resultDiscriminatorObj;
        Object resultDiscValueObj = resultDiscriminator.getValue();
        assertTrue(resultDiscValueObj instanceof Short);
        Short resultDiscValue = (Short)resultDiscValueObj;
        assertTrue(resultDiscValue.shortValue() == discriminatorValue.shortValue());
       
        CorbaObjectHandler resultValueObj = result.getValue();
        assertTrue(resultValueObj instanceof CorbaPrimitiveHandler);
        CorbaPrimitiveHandler resultValue = (CorbaPrimitiveHandler)resultValueObj;
        Object unionResultValueObj = resultValue.getValue();
        assertTrue(unionResultValueObj instanceof Character);
        Character unionResultValue = (Character)unionResultValueObj;
View Full Code Here


                                                                      QName idlType,
                                                                      TypeCode tc,
                                                                      XmlSchemaObject schemaType)
        throws CorbaBindingException {
        Union unionType = (Union) CorbaUtils.getCorbaType(idlType, typeMaps);
        CorbaUnionHandler obj = new CorbaUnionHandler(name, idlType, tc, unionType);
        CorbaPrimitiveHandler discObj =
            new CorbaPrimitiveHandler(new QName("discriminator"),
                                      unionType.getDiscriminator(),
                                      orb.get_primitive_tc(TCKind.from_int(TCKind._tk_boolean)),
                                      null);

        //only one union branch for a attribute / element nillable types.
        List<Unionbranch> branches = unionType.getUnionbranch();
        try {
            XMLEvent evt = reader.peek();
            if (schemaType instanceof XmlSchemaAttribute) {
                CorbaPrimitiveHandler value = getAttributeValue(evt, name, branches.get(0).getIdltype());
                if (value != null) {
                    discObj.setValue(Boolean.TRUE);
                    obj.setValue("value", value);
                    obj.addCase(value);
                } else {
                    discObj.setValue(Boolean.FALSE);
                }
            } else if (!isElementNil(evt, name)) {
                discObj.setValue(Boolean.TRUE);
                CorbaObjectHandler branchObj = readObjectFromStax(reader,
                                                                  branches.get(0).getIdltype(),
                                                                  schemaType,
                                                                  true);
                obj.setValue("value", branchObj);
            } else {
                discObj.setValue(Boolean.FALSE);
                reader.nextEvent().asStartElement();
                reader.nextEvent().asEndElement();
            }
            obj.setDiscriminator(discObj);
        } catch (java.lang.Exception ex) {
            LOG.log(Level.SEVERE, "Received exception while reading object of type " + idlType);
            throw new CorbaBindingException("Error while reading union corba type", ex);
        }
        return obj;
View Full Code Here

    protected CorbaObjectHandler readChoiceFromStax(XMLEventReader reader,
                                                    QName idlType,
                                                    TypeCode tc,
                                                    XmlSchemaObject schemaType)
        throws CorbaBindingException {
        CorbaUnionHandler obj = null;
        try {
            XmlSchemaChoice choiceType = (XmlSchemaChoice) schemaType;
            Union unionType = (Union) CorbaUtils.getCorbaType(idlType, typeMaps);
            QName elName = reader.nextEvent().asStartElement().getName();
            obj = new CorbaUnionHandler(elName, idlType, tc, unionType);
            // Build the entire union with all branches, etc.  Then read info from the XML Event Reader
            String branchName = null;
            XMLEvent evt = reader.peek();
            if (evt.isStartElement()) {
                StartElement branchElement = evt.asStartElement();
                branchName = branchElement.getName().getLocalPart();
            }
            List<Unionbranch> branches = unionType.getUnionbranch();
            XmlSchemaObjectCollection items = choiceType.getItems();               
            for (int i = 0; i < branches.size(); i++) {
                Unionbranch branch = branches.get(i);
                CorbaObjectHandler branchObj = null;
                if (branch.getName().equals(branchName)) {
                    branchObj = readObjectFromStax(reader, branch.getIdltype(), items.getItem(i), true);
                    // We also need to set the discriminator since this is the branch with the actual
                    // union value
                    CorbaObjectHandler discObj =
                        CorbaHandlerUtils.createTypeHandler(orb,
                                                            new QName("discriminator"),
                                                            unionType.getDiscriminator(),
                                                            typeMaps,
                                                            serviceInfo);
                    obj.setDiscriminator(discObj);
                   
                    // Determine the value of the discriminator. 
                    List<CaseType> branchCases = branch.getCase();
                    String discValue = null;
                    if (branchCases.size() != 0) {
                        // This represents a union case.  Set the discriminator based on the first
                        // label value associated with the branch (since we don't have this information)
                        // from the Stax representation of the Celtix object).
                        CaseType caseLabel = branchCases.get(0);
                        discValue = caseLabel.getLabel();
                    } else {
                        // This represents the default case.
                        discValue = obj.createDefaultDiscriminatorLabel();
                    }
                    obj.setDiscriminatorValueFromData(discValue);
                    obj.setValue(branchName, branchObj);
                } else {
                    XmlSchemaElement el = (XmlSchemaElement) items.getItem(i);
                    QName qname = new QName("", branch.getName());
                    if (CorbaUtils.isElementFormQualified(serviceInfo, el.getQName().getNamespaceURI())) {
                        qname = el.getQName();
                    }
                    // Create an object holder with no value
                    branchObj = CorbaHandlerUtils.createTypeHandler(orb,
                                                                    qname,
                                                                    branch.getIdltype(),
                                                                    typeMaps,
                                                                    serviceInfo);
                }
                obj.addCase(branchObj);
            }
            reader.nextEvent().asEndElement();
        } catch (java.lang.Exception ex) {
            LOG.log(Level.SEVERE, "Received exception while reading object of type " + idlType);
            throw new CorbaBindingException("Error while reading union corba type", ex);
View Full Code Here

            stype = el.getSchemaType();
            if (stype == null) {
                stype = CorbaUtils.getXmlSchemaType(serviceInfo, el.getRefName());
            }
        }
        CorbaUnionHandler unionHandler = (CorbaUnionHandler)obj;
        if (!writeElement) {
            writeAttributeOrNillableElementToStax(unionHandler, stype, objName, writer, factory);
        } else {
            StartElement startEl = factory.createStartElement(objName, null, null);
            writer.add(startEl);
            CorbaObjectHandler unionValue = unionHandler.getValue();
            if (unionValue != null) {
                writeObjectToStax(unionValue, stype, writer, factory, true);
            }
            EndElement endEl = factory.createEndElement(objName, null);
            writer.add(endEl);
View Full Code Here

        java.math.BigDecimal fixedValue = stream.read_fixed().movePointLeft((int)scale);
        fixedHandler.setValue(fixedValue);
    }

    public void readUnion(CorbaObjectHandler obj) throws CorbaBindingException {
        CorbaUnionHandler unionHandler = (CorbaUnionHandler)obj;
        Union unionType = (Union)unionHandler.getType();
        List<Unionbranch> branches = unionType.getUnionbranch();
        CorbaObjectHandler discriminator = unionHandler.getDiscriminator();
        if (branches.size() > 0) {
            this.read(discriminator);
            String discLabel = null;
            if (discriminator.getTypeCodeKind().value() == TCKind._tk_enum) {
                discLabel = ((CorbaEnumHandler)discriminator).getValue();
            } else {
                discLabel = ((CorbaPrimitiveHandler)discriminator).getDataFromValue();
            }
       
            // Now find the label in the union to get the right case
            Unionbranch defaultBranch = null;
            boolean caseFound = false;
            for (Iterator<Unionbranch> branchIter = branches.iterator(); branchIter.hasNext();) {
                Unionbranch branch = branchIter.next();
                if (branch.isSetDefault() && branch.isDefault()) {
                    defaultBranch = branch;
                }
                List<CaseType> cases = branch.getCase();
                for (Iterator<CaseType> caseIter = cases.iterator(); caseIter.hasNext();) {
                    CaseType c = caseIter.next();
                    if (c.getLabel().equalsIgnoreCase(discLabel)) {
                        CorbaObjectHandler branchObj = unionHandler.getBranchByName(branch.getName());
                        this.read(branchObj);
                        unionHandler.setValue(branch.getName(), branchObj);
                        caseFound = true;
                        break;
                    }
                }
                if (caseFound) {
                    break;
                }
            }
           
            // If we never find a case that matches the value of the discriminiator, then we must have
            // found the default case.
            if (!caseFound && defaultBranch != null) {
                CorbaObjectHandler branchObj = unionHandler.getBranchByName(defaultBranch.getName());
                this.read(branchObj);
                unionHandler.setValue(defaultBranch.getName(), branchObj);
            }
        }
    }
View Full Code Here

        QName objIdlType = new QName(staxTestNamespaceURI, "TestUnion", staxTestPrefix);
        CorbaTypeImpl type = CorbaUtils.getCorbaType(objIdlType, staxBindingImpl.getCorbaTypeMaps());
        assertTrue(type instanceof Union);
        Union unionType = (Union)type;
        TypeCode objTypeCode = CorbaUtils.getTypeCode(orb, objIdlType, staxBindingImpl.getCorbaTypeMaps());
        CorbaUnionHandler obj = new CorbaUnionHandler(objName, objIdlType, objTypeCode, unionType);
        assertNotNull(obj);
       
        Character unionValue = new Character('u');
        TypeCode unionValueTC = CorbaUtils.getTypeCode(orb, CorbaConstants.NT_CORBA_WCHAR,
                                                       staxBindingImpl.getCorbaTypeMaps());
        CorbaPrimitiveHandler unionValueObj = new CorbaPrimitiveHandler(new QName("case12"),
                                                                        CorbaConstants.NT_CORBA_WCHAR,
                                                                        unionValueTC,
                                                                        null);
        unionValueObj.setValue(unionValue);
        Short discriminatorValue = new Short((short)1);
        TypeCode discriminatorTC = CorbaUtils.getTypeCode(orb, unionType.getDiscriminator(),
                                                          staxBindingImpl.getCorbaTypeMaps());
        CorbaPrimitiveHandler discriminator = new CorbaPrimitiveHandler(new QName("discriminator"),
                                                                       unionType.getDiscriminator(),
                                                                       discriminatorTC,
                                                                       null);
        discriminator.setValue(discriminatorValue);

        obj.setDiscriminator(discriminator);
        obj.setValue("1", unionValueObj);
       
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
     
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        XMLEventWriter evtWriter = null;
        try {
            evtWriter = outputFactory.createXMLEventWriter(outStream);
        } catch (XMLStreamException ex) {
        }
        assertNotNull(evtWriter);
       
        try {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            StartElement startEl = factory.createStartElement(obj.getName(), null, null);
            evtWriter.add(startEl);
            staxBindingImpl.writeUnionToStax(obj, evtWriter, factory);
            EndElement endEl = factory.createEndElement(obj.getName(), null);
            evtWriter.add(endEl);
            evtWriter.flush();
        } catch (XMLStreamException ex) {
           
        }

        ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());       
        XMLEventReader evtReader = null;
        try {           
            evtReader = inputFactory.createXMLEventReader(inStream);
            // read the first tag which should be a start document
            XMLEvent evt = evtReader.nextEvent();
            assertTrue(evt.isStartDocument());
        } catch (XMLStreamException ex) {
        }
        assertNotNull(evtReader);
       
        CorbaObjectHandler resultObj = staxBindingImpl.readUnionFromStax(evtReader,
                                                                     obj.getIdlType(),
                                                                     obj.getTypeCode());
        assertNotNull(resultObj);
       
        assertTrue(resultObj instanceof CorbaUnionHandler);
        CorbaUnionHandler result = (CorbaUnionHandler)resultObj;
       
        CorbaObjectHandler resultDiscriminatorObj = result.getDiscriminator();
        assertTrue(resultDiscriminatorObj instanceof CorbaPrimitiveHandler);
        CorbaPrimitiveHandler resultDiscriminator = (CorbaPrimitiveHandler)resultDiscriminatorObj;
        Object resultDiscValueObj = resultDiscriminator.getValue();
        assertTrue(resultDiscValueObj instanceof Short);
        Short resultDiscValue = (Short)resultDiscValueObj;
        assertTrue(resultDiscValue.shortValue() == discriminatorValue.shortValue());
       
        CorbaObjectHandler resultValueObj = result.getValue();
        assertTrue(resultValueObj instanceof CorbaPrimitiveHandler);
        CorbaPrimitiveHandler resultValue = (CorbaPrimitiveHandler)resultValueObj;
        Object unionResultValueObj = resultValue.getValue();
        assertTrue(unionResultValueObj instanceof Character);
        Character unionResultValue = (Character)unionResultValueObj;
View Full Code Here

    }


    public CorbaObjectHandler readUnionFromStax(XMLEventReader reader, QName idlType, TypeCode tc)
        throws CorbaBindingException {
        CorbaUnionHandler obj = null;
        CorbaTypeImpl typeDefinition = CorbaUtils.getCorbaType(idlType, typeMaps);
       
        try {
            Union unionType = (Union)typeDefinition;
            StartElement unionStartEl = reader.nextEvent().asStartElement();
            obj = new CorbaUnionHandler(unionStartEl.getName(), idlType, tc, unionType);

            // Read the discriminator value from the reader.  We can't rely on the value from
            // Stax since it may be a default value if the caller has forgotten to explicitly
            // set the discriminator in their union class.  This could cause all sorts of
            // problems when we try create the CORBA union.  For now, we'll just read and ignore.
            // TODO: Revisit this if we want to make this assumption.
            reader.nextEvent().asStartElement().getName();
            reader.nextEvent().asCharacters();
            reader.nextEvent().asEndElement();
           
            // Build the entire union with all branches, etc.  Then read info from the XML Event Reader
            StartElement branchElement = reader.peek().asStartElement();
            String branchName = branchElement.getName().getLocalPart();
            List<Unionbranch> branches = unionType.getUnionbranch();
            for (Iterator<Unionbranch> iter = branches.iterator(); iter.hasNext();) {
                Unionbranch branch = iter.next();
                CorbaObjectHandler branchObj = null;
                if (branch.getName().equals(branchName)) {
                    branchObj = readObjectFromStax(reader, branch.getIdltype(), true);
                    // We also need to set the discriminator since this is the branch with the actual
                    // union value
                    CorbaObjectHandler discObj =
                        CorbaHandlerUtils.createTypeHandler(orb, new QName("discriminator"),
                                                            unionType.getDiscriminator(), typeMaps);
                    obj.setDiscriminator(discObj);

                    // Determine the value of the discriminator. 
                    List<CaseType> branchCases = branch.getCase();
                    String discValue = null;
                    if (branchCases.size() != 0) {
                        // This represents a union case.  Set the discriminator based on the first
                        // label value associated with the branch (since we don't have this information)
                        // from the Stax representation of the Celtix object).
                        CaseType caseLabel = branchCases.get(0);
                        discValue = caseLabel.getLabel();
                    } else {
                        // This represents the default case.  Since Celtix does not provide a
                        // discriminator value, we'll create one.
                        discValue = obj.createDefaultDiscriminatorLabel();
                    }
                    obj.setDiscriminatorValueFromData(discValue);
                    obj.setValue(branchName, branchObj);
                } else {
                    // Create an object holder with no value
                    branchObj = CorbaHandlerUtils.createTypeHandler(orb, new QName(branch.getName()),
                                                                    branch.getIdltype(), typeMaps);
                }
                obj.addCase(branchObj);
            }
            reader.nextEvent().asEndElement();
        } catch (java.lang.Exception ex) {
            LOG.log(Level.SEVERE, "Received exception while reading object of type " + idlType);
            throw new CorbaBindingException("Error while reading union corba type", ex);
View Full Code Here

        return obj;
    }
   
    public void writeUnionToStax(CorbaObjectHandler obj, XMLEventWriter writer, XMLEventFactory factory)
        throws XMLStreamException {
        CorbaUnionHandler unionHandler = (CorbaUnionHandler)obj;
        CorbaObjectHandler discValue = unionHandler.getDiscriminator();
        writeObjectToStax(discValue, writer, factory, true);
        CorbaObjectHandler unionValue = unionHandler.getValue();
        writeObjectToStax(unionValue, writer, factory, true);
    }
View Full Code Here

        java.math.BigDecimal fixedValue = stream.read_fixed().movePointLeft((int)scale);
        fixedHandler.setValue(fixedValue);
    }

    public void readUnion(CorbaObjectHandler obj) throws CorbaBindingException {
        CorbaUnionHandler unionHandler = (CorbaUnionHandler)obj;       
        CorbaObjectHandler discriminator = unionHandler.getDiscriminator();
       
        this.read(discriminator);
       
        String discLabel = null;
        if (discriminator.getTypeCodeKind().value() == TCKind._tk_enum) {
            discLabel = ((CorbaEnumHandler)discriminator).getValue();
        } else {
            discLabel = ((CorbaPrimitiveHandler)discriminator).getValueData();
        }
       
        // Now find the label in the union to get the right case
        Union unionType = (Union)unionHandler.getType();
        List<Unionbranch> branches = unionType.getUnionbranch();
        Unionbranch defaultBranch = null;
        boolean caseFound = false;
        for (Iterator<Unionbranch> iter = branches.iterator(); iter.hasNext();) {
            Unionbranch branch = iter.next();
            if (branch.isSetDefault() && branch.isDefault()) {
                defaultBranch = branch;
            }
            List<CaseType> cases = branch.getCase();
            for (Iterator<CaseType> caseIter = cases.iterator(); caseIter.hasNext();) {
                CaseType c = caseIter.next();
                if (c.getLabel().equals(discLabel)) {
                    CorbaObjectHandler branchObj = unionHandler.getBranchByName(branch.getName());
                    this.read(branchObj);
                    unionHandler.setValue(branch.getName(), branchObj);
                    caseFound = true;
                    break;
                }
            }
            if (caseFound) {
                break;
            }
        }
       
        // If we never find a case that matches the value of the discriminiator, then we must have
        // found the default case.
        if (!caseFound && defaultBranch != null) {
            CorbaObjectHandler branchObj = unionHandler.getBranchByName(defaultBranch.getName());
            this.read(branchObj);
            unionHandler.setValue(defaultBranch.getName(), branchObj);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.yoko.bindings.corba.types.CorbaUnionHandler

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.