Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.cxf.binding.corba.types;
 
 import java.util.List;
 import java.util.Map;
 
 
 
 public final class CorbaHandlerUtils {
     private CorbaHandlerUtils() {
         //utility class
     }
 
     public static CorbaObjectHandler createTypeHandler(ORB orb,
                                                        QName name,
                                                        QName idlType,
                                                        CorbaTypeMap typeMap) {
         CorbaObjectHandler handler = null;
         TypeCode tc = CorbaUtils.getTypeCode(orbidlTypetypeMap);
         try {
             while (tc.kind().value() == .) {
                 Alias alias = (Alias) CorbaUtils.getCorbaType(idlTypetypeMap);
                 if (alias == null) {
                     throw new CorbaBindingException("Couldn't find corba alias type: " + idlType);
                 }
                 tc = tc.content_type();
                 idlType = alias.getBasetype();
             }
         } catch (Throwable ex) {
             throw new CorbaBindingException(ex);
         }
         if (CorbaUtils.isPrimitiveIdlType(idlType)) {
             handler = new CorbaPrimitiveHandler(nameidlTypetcnull);
         } else if (tc.kind().value() == .) {
             // Any is a special kind of primitive so it gets its own handler
             handler = new CorbaAnyHandler(nameidlTypetcnull);
             ((CorbaAnyHandler)handler).setTypeMap(typeMap);
         } else {
             CorbaType type = CorbaUtils.getCorbaType(idlTypetypeMap);
             switch (tc.kind().value()) {
             case .:
                 handler = new CorbaArrayHandler(nameidlTypetctype);
                 break;
             case .:
                 handler = new CorbaEnumHandler(nameidlTypetctype);
                 break;
             case .:
                 handler = new CorbaExceptionHandler(nameidlTypetctype);
                 break;
             case .:
                 handler = new CorbaFixedHandler(nameidlTypetctype);
                 break;
             case .:
                 if (isOctets(type)) {
                     handler = new CorbaOctetSequenceHandler(nameidlTypetctype);
                 } else {
                    handler = new CorbaSequenceHandler(nameidlTypetctype);
                }
                break;
            case .:
                handler = new CorbaStructHandler(nameidlTypetctype);
                break;
            case .:
                handler = new CorbaUnionHandler(nameidlTypetctype);
                break;
            case .:
            case .:
                // These need to be here to catch the anonymous string types.
                handler = new CorbaPrimitiveHandler(nameidlTypetctype);
                break;
            case .:
                handler = new CorbaObjectReferenceHandler(nameidlTypetctype);
                break;
            default:
                handler = new CorbaObjectHandler(nameidlTypetctype);
            }
        }
        return handler;
    }
    public static CorbaObjectHandler initializeObjectHandler(ORB orb,
                                                             QName name,
                                                             QName idlType,
                                                             CorbaTypeMap typeMap,
                                                             ServiceInfo serviceInfo) {
        Map<QNameCorbaObjectHandlerseenTypes = new HashMap<QNameCorbaObjectHandler>();
        return initializeObjectHandler(orbnameidlTypetypeMapserviceInfoseenTypes);
    }
    public static CorbaObjectHandler initializeObjectHandler(ORB orb,
                                                             QName name,
                                                             QName idlType,
                                                             CorbaTypeMap typeMap,
                                                             ServiceInfo serviceInfo,
                                                             Map<QNameCorbaObjectHandlerseenTypes) {
        CorbaObjectHandler obj = createTypeHandler(orbnameidlTypetypeMap);
        if (!CorbaUtils.isPrimitiveIdlType(idlType)) {
            switch (obj.getTypeCode().kind().value()) {
            case .:
                ((CorbaAnyHandler)obj).setValue(orb.create_any());
                break;
            case .:
                initializeArrayHandler(orbobjtypeMapserviceInfoseenTypes);
                break;
            case .:
                initializeExceptionHandler(orbobjtypeMapserviceInfoseenTypes);
                break;
            case .:
                if (!isOctets(obj.getType())) {
                    initializeSequenceHandler(orbobjtypeMapserviceInfoseenTypes);
                }
                break;
            case .:
                initializeStructHandler(orbobjtypeMapserviceInfoseenTypes);
                break;
            case .:
                initializeUnionHandler(orbobjtypeMapserviceInfoseenTypes);
                break;
            default:
               // TODO: Should we raise an exception or log?
            }
        }
        return obj;
    }
    public static void initializeArrayHandler(ORB orb,
                                              CorbaObjectHandler obj,
                                              CorbaTypeMap typeMap,
                                              ServiceInfo serviceInfo,
                                              Map<QNameCorbaObjectHandlerseenTypes) {
        QName arrayElementType = null;
        long arrayBound = 0;
        CorbaType baseType = obj.getType();
        QName elementName;
        if (baseType instanceof Array) {
            Array arrayType = (Array)baseType;
            arrayElementType = arrayType.getElemtype();
            arrayBound = arrayType.getBound();
            elementName = arrayType.getElemname();
        } else {
            Anonarray anonArrayType = (Anonarray)baseType;
            arrayElementType = anonArrayType.getElemtype();
            arrayBound = anonArrayType.getBound();
            elementName = anonArrayType.getElemname();
        }
        for (int i = 0; i < arrayBound; ++i) {
            CorbaObjectHandler elementObj =
                initializeObjectHandler(orbelementNamearrayElementTypetypeMapserviceInfo,
                                        seenTypes);
            ((CorbaArrayHandler)obj).addElement(elementObj);
        }
    }
    public static void initializeExceptionHandler(ORB orb,
                                                  CorbaObjectHandler obj,
                                                  CorbaTypeMap typeMap,
                                                  ServiceInfo serviceInfo,
                                                  Map<QNameCorbaObjectHandlerseenTypes) {
        Exception exceptType = (Exception)obj.getType();
        List<MemberTypeexceptMembers = exceptType.getMember();
        QName typeName = exceptType.getType();
        for (int i = 0; i < exceptMembers.size(); ++i) {
            MemberType member = exceptMembers.get(i);
            QName memberName;
            if (member.isSetQualified() && member.isQualified() && (typeName != null)) {
                memberName = new QName(typeName.getNamespaceURI(), member.getName());
            } else {
                memberName = new QName(""member.getName());
            }
            QName memberType = member.getIdltype();
            CorbaObjectHandler memberObj = initializeObjectHandler(orb,
                                                                   memberName,
                                                                   memberType,
                                                                   typeMap,
                                                                   serviceInfo,
                                                                   seenTypes);
            ((CorbaExceptionHandler)obj).addMember(memberObj);
        }
    }
    public static void initializeSequenceHandler(ORB orb,
                                                 CorbaObjectHandler obj,
                                                 CorbaTypeMap typeMap,
                                                 ServiceInfo serviceInfo,
                                                 Map<QNameCorbaObjectHandlerseenTypes) {
        QName seqElementType = null;
        long seqBound = 0;
        CorbaType baseType = obj.getType();
        QName elementName;
        if (baseType instanceof Sequence) {
            Sequence seqType = (Sequence)baseType;
            seqElementType = seqType.getElemtype();
            seqBound = seqType.getBound();
            elementName = seqType.getElemname();
        } else {
            Anonsequence seqType = (Anonsequence)baseType;
            seqElementType = seqType.getElemtype();
            seqBound = seqType.getBound();
            elementName = seqType.getElemname();
        }
        if (seqBound == 0) {
            // This is an unbounded sequence.  Store a 'template' object that we can use to create
            // new objects as needed
            CorbaObjectHandler elementObj = null;
            // Check for a recursive type
            if (seenTypes.get(seqElementType) != null) {
                elementObj = seenTypes.get(seqElementType);
                elementObj.setRecursive(true);
                ((CorbaSequenceHandler)obj).hasRecursiveTypeElement(true);
            } else {
                elementObj =
                    initializeObjectHandler(orbelementNameseqElementTypetypeMap,
                                            serviceInfoseenTypes);
            }
            ((CorbaSequenceHandler)obj).setTemplateElement(elementObj);
        }
        for (int i = 0; i < seqBound; ++i) {
            CorbaObjectHandler elementObj = null;
            // Check for a recursive type
            if (seenTypes.get(seqElementType) != null) {
                // Even though this is bounded, if we have a recursive type, we'll still use the
                // template object so that we don't overwrite the value of the element.
                elementObj = seenTypes.get(seqElementType);
                elementObj.setRecursive(true);
                ((CorbaSequenceHandler)obj).hasRecursiveTypeElement(true);
                ((CorbaSequenceHandler)obj).setTemplateElement(elementObj);
            } else {
                elementObj =
                    initializeObjectHandler(orbelementNameseqElementTypetypeMapserviceInfo,
                                            seenTypes);
                ((CorbaSequenceHandler)obj).addElement(elementObj);
            }
        }
    }
    public static void initializeStructHandler(ORB orb,
                                               CorbaObjectHandler obj,
                                               CorbaTypeMap typeMap,
                                               ServiceInfo serviceInfo,
                                               Map<QNameCorbaObjectHandlerseenTypes) {
        Struct structType = (Struct)obj.getType();
        List<MemberTypestructMembers = structType.getMember();
        QName typeName = structType.getType();
        seenTypes.put(obj.getIdlType(), obj);
        for (int i = 0; i < structMembers.size(); ++i) {
            MemberType member = structMembers.get(i);
            QName memberName;
            if (member.isSetQualified() && member.isQualified() && (typeName != null)) {
                memberName = new QName(typeName.getNamespaceURI(), member.getName());
            } else {
                memberName = new QName(""member.getName());
            }
            QName memberType = member.getIdltype();
            CorbaObjectHandler memberObj = initializeObjectHandler(orb,
                                                                   memberName,
                                                                   memberType,
                                                                   typeMap,
                                                                   serviceInfo,
                                                                   seenTypes);
            if (member.isSetAnonschematype() && member.isAnonschematype()) {
                memberObj.setAnonymousType(true);
            }
            ((CorbaStructHandler)obj).addMember(memberObj);
        }
        seenTypes.remove(obj.getIdlType());
    }
    public static void initializeUnionHandler(ORB orb,
                                              CorbaObjectHandler obj,
                                              CorbaTypeMap typeMap,
                                              ServiceInfo serviceInfo,
                                              Map<QNameCorbaObjectHandlerseenTypes) {
        Union unionType = (Union)obj.getType();
        // First handle the discriminator
        CorbaObjectHandler discObj = initializeObjectHandler(orb,
                                                             new QName("discriminator"),
                                                             unionType.getDiscriminator(),
                                                             typeMap,
                                                             serviceInfo,
                                                             seenTypes);
        ((CorbaUnionHandler)obj).setDiscriminator(discObj);
        QName typeName = unionType.getType();
        seenTypes.put(obj.getIdlType(), obj);
        // Now handle all of the branches
        List<UnionbranchunionBranches = unionType.getUnionbranch();
        for (int i = 0; i < unionBranches.size(); i++) {
            Unionbranch branch = unionBranches.get(i);
            QName branchName;
            if (branch.isSetQualified() && branch.isQualified() && (typeName != null)) {
                branchName = new QName(typeName.getNamespaceURI(), branch.getName());
            } else {
                branchName = new QName(""branch.getName());
            }
            QName branchIdlType = branch.getIdltype();
            CorbaObjectHandler branchObj =
                initializeObjectHandler(orbbranchNamebranchIdlTypetypeMapserviceInfo,
                                        seenTypes);
            ((CorbaUnionHandler)obj).addCase(branchObj);
        }
        seenTypes.remove(obj.getIdlType());
    }
    //Change this method to access the XmlSchemaCollection.
    public static String getNamespaceURI(ServiceInfo serviceInfoQName typeName) {
        String nsUri = "";
        if ((typeName != null)
            && (CorbaUtils.isElementFormQualified(serviceInfotypeName.getNamespaceURI()))) {
            nsUri = typeName.getNamespaceURI();
        }
        return nsUri;
    }
                                                               ServiceInfo serviceInfo) {
        XmlSchemaObject stype = schemaType;
        XmlSchemaElement el = null;
        if (schemaType instanceof XmlSchemaElement) {
            stype = ((XmlSchemaElementschemaType).getSchemaType();
            if (stype == null) {
                stype = CorbaUtils.getXmlSchemaType(serviceInfo,
                                                    ((XmlSchemaElementschemaType)
                                                        .getRef().getTargetQName());
            }
        }
        if (stype instanceof XmlSchemaComplexType) {
            //only one element inside the XmlSchemaComplexType
            XmlSchemaComplexType ctype = (XmlSchemaComplexTypestype;
            XmlSchemaParticle group = ctype.getParticle();
            /* This code seems to think that we're guaranteed a sequence here */
            XmlSchemaSequence seq = (XmlSchemaSequencegroup;
            /* and an element in it, too */
            el = (XmlSchemaElementseq.getItems().get(0);
        } else {
            el = (XmlSchemaElementschemaType;
        }
        return el;
    }
    public static CorbaTypeListener getTypeListener(QName name,
                                                    QName idlType,
                                                    CorbaTypeMap typeMap,
                                                    ORB orbServiceInfo serviceInfo)
        throws CorbaBindingException {
        CorbaObjectHandler handler = null;
        TypeCode tc = CorbaUtils.getTypeCode(orbidlTypetypeMap);
        try {
            while (tc.kind().value() == .) {
                Alias alias = (Alias) CorbaUtils.getCorbaType(idlTypetypeMap);
                if (alias == null) {
                    throw new CorbaBindingException("Couldn't find corba alias type: " + idlType);
                }
                tc = tc.content_type();
                idlType = alias.getBasetype();
            }
        } catch (Throwable ex) {
            throw new CorbaBindingException(ex);
        }
        CorbaTypeListener result = null;
        if (CorbaUtils.isPrimitiveIdlType(idlType)) {
            handler = new CorbaPrimitiveHandler(nameidlTypetcnull);
            result = new CorbaPrimitiveListener(handler);
        } else {
            CorbaType type = CorbaUtils.getCorbaType(idlTypetypeMap);
            switch (tc.kind().value()) {
            case .:
                handler = new CorbaAnyHandler(nameidlTypetctype);
                ((CorbaAnyHandler)handler).setTypeMap(typeMap);
                result = new CorbaAnyListener(handlertypeMaporbserviceInfo);
                break;
            case .:
                handler = new CorbaArrayHandler(nameidlTypetctype);
                result = new CorbaArrayListener(handlertypeMaporbserviceInfo);
                break;
            case .:
                handler = new CorbaEnumHandler(nameidlTypetctype);
                result = new CorbaEnumListener(handler);
                break;
            case .:
                handler = new CorbaExceptionHandler(nameidlTypetctype);
                result = new CorbaExceptionListener(handlertypeMaporbserviceInfo);
                break;
            case .:
                handler = new CorbaFixedHandler(nameidlTypetctype);
                result = new CorbaFixedListener(handler);
                break;
            case .:
                if (isOctets(type)) {
                    handler = new CorbaOctetSequenceHandler(nameidlTypetctype);
                    result = new CorbaOctetSequenceListener(handler);
                } else {
                    handler = new CorbaSequenceHandler(nameidlTypetctype);
                    result = new CorbaSequenceListener(handlertypeMaporbserviceInfo);
                }
                break;
            case .:
            case .:
                // These can be handled just like regular strings
                handler = new CorbaPrimitiveHandler(nameidlTypetctype);
                result = new CorbaPrimitiveListener(handler);
                break;
            case .:
                handler = new CorbaStructHandler(nameidlTypetctype);
                result = new CorbaStructListener(handlertypeMaporbserviceInfo);
                break;
            case .:
                handler = new CorbaUnionHandler(nameidlTypetctype);
                result = new CorbaUnionListener(handlertypeMaporbserviceInfo);
                break;
            case .:
                handler =
                    new CorbaObjectReferenceHandler(nameidlTypetctype);
                result = new CorbaObjectReferenceListener(handlerorb);
                break;
            default:
                throw new CorbaBindingException("Unsupported complex type " + idlType);
            }
        }
        return result;
    }
                                                              ServiceInfo serviceInfo,
                                                              ORB orb)
        throws CorbaBindingException {
        QName idlType = handler.getIdlType();
        TypeCode tc = handler.getTypeCode();
        CorbaTypeEventProducer result = null;
        if (CorbaUtils.isPrimitiveIdlType(idlType)) {
            result = new CorbaPrimitiveTypeEventProducer(handler);
        } else {
            switch (tc.kind().value()) {
            case .:
                result = new CorbaAnyEventProducer(handlerserviceInfoorb);
                break;
            case .:
                result = new CorbaArrayEventProducer(handlerserviceInfoorb);
                break;
            case .:
                result = new CorbaEnumEventProducer(handler);
                break;
            case .:
                result = new CorbaExceptionEventProducer(handlerserviceInfoorb);
                break;
            case .:
                result = new CorbaFixedEventProducer(handler);
                break;
            case .:
                if (isOctets(handler.getType())) {
                    result = new CorbaOctetSequenceEventProducer(handler);
                } else {
                    result = new CorbaSequenceEventProducer(handlerserviceInfoorb);
                }
                break;
            case .:
            case .:
                // These can be handled just like regular strings
                result = new CorbaPrimitiveTypeEventProducer(handler);
                break;
            case .:
                if (handler.isAnonymousType()) {
                    result = new CorbaAnonStructEventProducer(handlerserviceInfoorb);
                } else {
                    result = new CorbaStructEventProducer(handlerserviceInfoorb);
                }
                break;
            case .:
                result = new CorbaUnionEventProducer(handlerserviceInfoorb);
                break;
            case .:
                result = new CorbaObjectReferenceEventProducer(handlerserviceInfoorb);
                break;
            default:
                throw new CorbaBindingException("Unsupported complex type "
                                                + idlType);
            }
        }
        return result;
    }
    public static boolean isPrimitiveIDLTypeSequence(CorbaObjectHandler handler) {
        CorbaType seqType = handler.getType();
        QName seqElementType;
        if (seqType instanceof Anonsequence) {
            Anonsequence anonSeqType = (AnonsequenceseqType;
            seqElementType = anonSeqType.getElemtype();
        } else {
            Sequence type = (SequenceseqType;
            seqElementType = type.getElemtype();
        }
        return CorbaUtils.isPrimitiveIdlType(seqElementType);
    }
    public static boolean isAnonType(XmlSchemaObject schemaObj) {
        boolean result = false;
        if ((schemaObj != null) && !(schemaObj instanceof XmlSchemaElement)
             && !(schemaObj instanceof XmlSchemaComplexType)) {
            result = true;
        }
        return result;
    }
    public static boolean isOctets(CorbaType baseType) {
        return baseType.getType().equals(.)
            || baseType.getType().equals(.);
    }
New to GrepCode? Check out our FAQ X