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.runtime;
 
 import java.util.List;
 
 
 public class CorbaObjectWriter {
 
     private OutputStream stream;
 
     public CorbaObjectWriter(OutputStream outStream) {
          = outStream;
     }
 
     public void write(CorbaObjectHandler obj) {
         assert obj != null;
         switch (obj.getTypeCode().kind().value()) {
         case .:
             this.writeBoolean((Boolean)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeChar((Character)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeWChar((Character)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeOctet((Byte)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeShort((Short)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeUShort((Integer)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeLong((Integer)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeULong((Long)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeLongLong((Long)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeULongLong((BigInteger)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeFloat((Float)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeDouble((Double)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeString((String)((CorbaPrimitiveHandler)obj).getValue());
             break;
         case .:
             this.writeWString((String)((CorbaPrimitiveHandler)obj).getValue());
            break;
        case .:
            this.writeAny((CorbaAnyHandler)obj);
            break;
        // Now for the complex types
        case .:
            this.writeArray((CorbaArrayHandler)obj);
            break;
        case .:
            this.writeSequence(obj);
            break;
        case .:
            this.writeStruct((CorbaStructHandler)obj);
            break;
        case .:
            this.writeEnum((CorbaEnumHandler)obj);
            break;
        case .:
            this.writeException((CorbaExceptionHandler)obj);
            break;
        case .:
            this.writeFixed((CorbaFixedHandler)obj);
            break;
        case .:
            this.writeUnion(obj);
            break;
        case .:
            this.writeObjectReference((CorbaObjectReferenceHandler)obj);
            break;            
        default:
        // TODO: Provide Implementation. Do we throw an exception.
        }
    }
    // -- primitive types --
    public void writeBoolean(Boolean bthrows CorbaBindingException {
        if (b == null) {
            .write_boolean(false);
        } else {
            .write_boolean(b.booleanValue());
        }
    }
    public void writeChar(Character cthrows CorbaBindingException {
        if (c == null) {
            .write_char((char)0);
        } else {
            .write_char(c.charValue());
        }
    }
    public void writeWChar(Character cthrows CorbaBindingException {
        if (c == null) {
            .write_wchar((char)0);
        } else {
            .write_wchar(c.charValue());
        }
    }
    public void writeOctet(Byte bthrows CorbaBindingException {
        if (b == null) {
            .write_octet((byte)0);
        } else {
            .write_octet(b.byteValue());
        }
    }
    public void writeShort(Short sthrows CorbaBindingException {
        if (s == null) {
            .write_short((short)0);
        } else {
            .write_short(s.shortValue());
        }
    }
    public void writeUShort(Integer sthrows CorbaBindingException {
        if (s == null) {
            .write_ushort((short)0);
        } else {
            .write_ushort(s.shortValue());
        }
    }
    public void writeLong(Integer lthrows CorbaBindingException {
        if (l == null) {
            .write_long(0);
        } else {
            .write_long(l.intValue());
        }
    }
    public void writeULong(Long lthrows CorbaBindingException {
        if (l == null) {
            .write_ulong(0);
        } else {
            .write_ulong(l.intValue());
        }
    }
    public void writeLongLong(Long lthrows CorbaBindingException {
        if (l == null) {
            .write_longlong(0);
        } else {
            .write_longlong(l.longValue());
        }
    }
    public void writeULongLong(java.math.BigInteger lthrows CorbaBindingException {
        if (l == null) {
            .write_ulonglong(0);
        } else {
            .write_ulonglong(l.longValue());
        }
    }
    public void writeFloat(Float fthrows CorbaBindingException {
        if (f == null) {
            .write_float((float)0.0);
        } else {
            .write_float(f.floatValue());
        }
    }
    public void writeDouble(Double dthrows CorbaBindingException {
        if (d == null) {
            .write_double(0.0);
        } else {
            .write_double(d.doubleValue());
        }
    }
    public void writeString(String sthrows CorbaBindingException {
        if (s == null) {
            .write_string("");
        } else {
            .write_string(s);
        }
    }
    public void writeWString(String sthrows CorbaBindingException {
        if (s == null) {
            .write_wstring("");
        } else {
            .write_wstring(s);
        }
    }
    public void writeAny(CorbaAnyHandler anyHandlerthrows CorbaBindingException {
        CorbaObjectHandler containedType = anyHandler.getAnyContainedType();
        Any a = anyHandler.getValue();
        // This is true if we have an empty any
        if (containedType != null) {
            a.type(containedType.getTypeCode());
            OutputStream os = a.create_output_stream();
            CorbaObjectWriter writer = new CorbaObjectWriter(os);
            writer.write(containedType);
            a.read_value(os.create_input_stream(), containedType.getTypeCode());
        }
        .write_any(a);
    }
    // -- complex types --
    public void writeEnum(CorbaEnumHandler enumHandlerthrows CorbaBindingException {
        Enum enumType = (Enum)enumHandler.getType();
        String enumLabel = enumHandler.getValue();
        List<Enumeratorenumerators = enumType.getEnumerator();
        
        for (int i = 0; i < enumerators.size(); ++i) {
            if (enumerators.get(i).getValue().equals(enumLabel)) {
                .write_long(i);
                return;
            }
        }
        
        throw new CorbaBindingException("CorbaObjectWriter: unable to find enumeration label");
    }
    public void writeStruct(CorbaStructHandler structHandlerthrows CorbaBindingException {
        List<CorbaObjectHandlerstructElements = structHandler.getMembers();
        for (int i = 0; i < structElements.size(); ++i) {
            this.write(structElements.get(i));
        }
    }
    public void writeException(CorbaExceptionHandler exHandlerthrows CorbaBindingException {
        Exception exType = (Exception)exHandler.getType();
        List<CorbaObjectHandlerexMembers = exHandler.getMembers();
        .write_string(exType.getRepositoryID());
        for (int i = 0; i < exMembers.size(); ++i) {
            this.write(exMembers.get(i));
        }
    }
    public void writeFixed(CorbaFixedHandler fixedHandlerthrows CorbaBindingException {
        short scale = (short)fixedHandler.getScale();
        short fixed = (short)fixedHandler.getDigits();
        //the write_fixed method is a "late addition" and not all orbs implement it.
        //Some of them have a "write_fixed(BigDecimal, short, short)" method, we'll try that
        try {
            Method m = .getClass().getMethod("write_fixed"new Class[] {BigDecimal.class,
                                                                               .,
                                                                               .});
            m.invoke(fixedHandler.getValue(), fixedscale);
        } catch (Throwable e1) {
            .write_fixed(fixedHandler.getValue().movePointRight(scale));
        }
    }
    public void writeUnion(CorbaObjectHandler objthrows CorbaBindingException {
        Union unionType = (Unionobj.getType();        
        List<Unionbranchbranches = unionType.getUnionbranch();
        if (branches.size() > 0) {             
            CorbaObjectHandler discriminator = ((CorbaUnionHandler)obj).getDiscriminator();
            this.write(discriminator);
            CorbaObjectHandler unionValue = ((CorbaUnionHandler)obj).getValue();
            if (unionValue != null) {
                this.write(unionValue);
            }
        }
    }
    public void writeArray(CorbaArrayHandler arrayHandlerthrows CorbaBindingException {
        List<CorbaObjectHandlerarrayElements = arrayHandler.getElements();
        for (int i = 0; i < arrayElements.size(); ++i) {
            this.write(arrayElements.get(i));
        }
    }
    public void writeSequence(CorbaObjectHandler objthrows CorbaBindingException {
        if (obj instanceof CorbaOctetSequenceHandler) {
            byte[] value = ((CorbaOctetSequenceHandlerobj).getValue();
            .write_ulong(value.length);
            .write_octet_array(value, 0, value.length);
        } else {
            CorbaSequenceHandler seqHandler = (CorbaSequenceHandler)obj;
            List<CorbaObjectHandlerseqElements = seqHandler.getElements();
            int length = seqElements.size();
            .write_ulong(length);
            for (int i = 0; i < length; ++i) {
                this.write(seqElements.get(i));
            }
        }
    }
    
    public void writeObjectReference(CorbaObjectReferenceHandler objHandlerthrows CorbaBindingException {
        .write_Object(objHandler.getReference());
    }   
New to GrepCode? Check out our FAQ X