/**
* 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.yoko.bindings.corba.types;
import javax.xml.namespace.QName;
import org.apache.yoko.bindings.corba.CorbaBindingException;
import org.omg.CORBA.Any;
import org.omg.CORBA.ORB;
import org.omg.CORBA.TCKind;
import org.omg.CORBA.TypeCode;
public class CorbaAnyHandler extends CorbaObjectHandler {
private Any value;
private String schemaType;
public CorbaAnyHandler(QName anyName, QName anyIdlType, TypeCode anyTC, Object anyType) {
super(anyName, anyIdlType, anyTC, anyType);
schemaType = null;
value = null;
}
public String getSchemaType() {
return schemaType;
}
public void setSchemaType(String type) {
schemaType = type;
}
public Any getValue() {
return value;
}
public void setValue(Any v) throws CorbaBindingException {
value = v;
// Need to find the appropriate xsi:type for the any
// TODO: See the comment in setValueFromData regarding the xs: prefix. We'll also need
// to make a similar change here.
switch (value.type().kind().value()) {
case TCKind._tk_boolean:
schemaType = "xs:boolean";
break;
case TCKind._tk_char:
schemaType = "xs:byte";
break;
case TCKind._tk_wchar:
schemaType = "xs:string";
break;
case TCKind._tk_octet:
schemaType = "xs:unsignedByte";
break;
case TCKind._tk_short:
schemaType = "xs:short";
break;
case TCKind._tk_ushort:
schemaType = "xs:unsignedShort";
break;
case TCKind._tk_long:
schemaType = "xs:int";
break;
case TCKind._tk_ulong:
schemaType = "xs:unsignedInt";
break;
case TCKind._tk_longlong:
schemaType = "xs:long";
break;
case TCKind._tk_ulonglong:
schemaType = "xs:unsignedLong";
break;
case TCKind._tk_double:
schemaType = "xs:double";
break;
case TCKind._tk_float:
schemaType = "xs:float";
break;
case TCKind._tk_string:
schemaType = "xs:string";
break;
case TCKind._tk_wstring:
schemaType = "xs:string";
break;
default:
throw new CorbaBindingException("Unsupported type in CORBA Any");
}
}
public String getValueData() throws CorbaBindingException {
String result = null;
switch (value.type().kind().value()) {
case TCKind._tk_boolean:
result = Boolean.toString(value.extract_boolean());
break;
case TCKind._tk_char:
result = Byte.toString((byte)value.extract_char());
break;
case TCKind._tk_wchar:
result = Character.toString(value.extract_wchar());
break;
case TCKind._tk_octet:
result = Byte.toString(value.extract_octet());
break;
case TCKind._tk_short:
result = Short.toString(value.extract_short());
break;
case TCKind._tk_ushort:
result = Short.toString(value.extract_ushort());
break;
case TCKind._tk_long:
result = Integer.toString(value.extract_long());
break;
case TCKind._tk_ulong:
result = Integer.toString(value.extract_ulong());
break;
case TCKind._tk_longlong:
result = Long.toString(value.extract_longlong());
break;
case TCKind._tk_ulonglong:
result = Long.toString(value.extract_ulonglong());
break;
case TCKind._tk_double:
result = Double.toString(value.extract_double());
break;
case TCKind._tk_float:
result = Float.toString(value.extract_float());
break;
case TCKind._tk_string:
result = value.extract_string();
break;
case TCKind._tk_wstring:
result = value.extract_wstring();
break;
default:
throw new CorbaBindingException("Unsupported type in CORBA Any");
}
return result;
}
public void setValueFromData(ORB orb, String data, String type)
throws CorbaBindingException {
value = orb.create_any();
// TODO: For now, the xs: is hard coded because the namespace for xs: is not included in the
// STAX element where we get the type information from. This will need to be changed so that
// it does not depend on the XML Schema namespace using the xs: prefix.
if (type.equals("xs:boolean")) {
value.insert_boolean(Boolean.parseBoolean(data));
} else if (type.equals("xs:byte")) {
value.insert_char((char)Byte.valueOf(data).byteValue());
} else if (type.equals("xs:double")) {
value.insert_double(Double.parseDouble(data));
} else if (type.equals("xs:float")) {
value.insert_float(Float.parseFloat(data));
} else if (type.equals("xs:int")) {
value.insert_long(Integer.parseInt(data));
} else if (type.equals("xs:long")) {
value.insert_longlong(Long.parseLong(data));
} else if (type.equals("xs:short")) {
value.insert_short(Short.parseShort(data));
} else if (type.equals("xs:string")) {
// NOTE: CORBA wchar, string, and wstrings will all be inserted as strings since they all
// map to the same XML Schema type. There is no way in Celtix of differentiating
// between the three when used in an any.
value.insert_string(data);
} else if (type.equals("xs:unsignedByte")) {
value.insert_octet(Byte.parseByte(data));
} else if (type.equals("xs:unsignedInt")) {
value.insert_ulong(Integer.parseInt(data));
} else if (type.equals("xs:unsignedLong")) {
value.insert_ulonglong(Long.parseLong(data));
} else if (type.equals("xs:unsignedShort")) {
value.insert_ushort(Short.parseShort(data));
} else {
throw new CorbaBindingException("Unsupported XML Schema type");
}
schemaType = type;
}
}