00001 package Sct.IS;
00002
00003 import java.lang.reflect.Array;
00004 import is.AnyInfo;
00005 import Sct.*;
00006
00007 public class AnyInfoAdapter implements Sct.Serializable {
00008
00009 public AnyInfoAdapter(AnyInfo a) {
00010 any = a;
00011 }
00012
00013 public AnyInfoAdapter() {
00014 any = new AnyInfo();
00015 }
00016
00017 public AnyInfo getInfo() {
00018 return any;
00019 }
00020
00021 public String getClassName() {
00022 return any.getType().getName();
00023 }
00024
00025 public String getUniqueID() {
00026 return new NameIS(any.getName()).getUniqueID();
00027 }
00028
00029 public void read(IStream s, ObjectManager o) throws UnsupportedOperationException {
00030 throw new UnsupportedOperationException("AnyInfoAdapter can only write AnyInfo objects - can't read into them!");
00031 }
00032
00033 public void write(OStream s, ObjectManager o) throws java.io.IOException {
00034 for (int i=0; i<any.getAttributeCount(); ++i) {
00035 write(s, any.getAttributeTypeName(i), any.getAttribute(i));
00036 }
00037 }
00038
00039
00040
00041
00042 private void write(OStream s, String name, Object o) throws java.io.IOException {
00043 if (o.getClass().isArray()) {
00044 writeArray(s, name, o);
00045 return;
00046 }
00047
00048 if (o instanceof Boolean) {
00049 s.writeBoolean(name, ((Boolean)o).booleanValue());
00050 return;
00051 }
00052
00053 if (o instanceof Byte) {
00054 s.writeByte(name, ((Byte)o).byteValue(), true);
00055 return;
00056 }
00057
00058 if (o instanceof Short) {
00059 s.writeShort(name, ((Short)o).shortValue(), true);
00060 return;
00061 }
00062
00063 if (o instanceof Integer) {
00064 s.writeInt(name, ((Integer)o).intValue(), true);
00065 return;
00066 }
00067
00068 if (o instanceof Float) {
00069 s.writeFloat(name, ((Float)o).floatValue());
00070 return;
00071 }
00072
00073 if (o instanceof Double) {
00074 s.writeDouble(name, ((Double)o).doubleValue());
00075 return;
00076 }
00077
00078 if (o instanceof String) {
00079 s.writeString(name, (String)o);
00080 return;
00081 }
00082
00083 s.writeString(name, o.toString());
00084 }
00085
00086 private void writeArray(OStream s, String name, Object o) throws java.io.IOException {
00087 Class c = o.getClass().getComponentType();
00088 int length = Array.getLength(o);
00089
00090 if (c==Boolean.class) {
00091 boolean[] array = new boolean[length];
00092 for (int i=0; i<length; ++i) array[i] = ((Boolean)Array.get(o, i)).booleanValue();
00093 s.writeBooleanArray(name, array);
00094 return;
00095 }
00096
00097 if (c==Byte.class) {
00098 byte[] array = new byte[length];
00099 for (int i=0; i<length; ++i) array[i] = ((Byte)Array.get(o, i)).byteValue();
00100 s.writeByteArray(name, array, true);
00101 return;
00102 }
00103
00104 if (c==Short.class) {
00105 short[] array = new short[length];
00106 for (int i=0; i<length; ++i) array[i] = ((Short)Array.get(o, i)).shortValue();
00107 s.writeShortArray(name, array, true);
00108 return;
00109 }
00110
00111 if (c==Integer.class) {
00112 int[] array = new int[length];
00113 for (int i=0; i<length; ++i) array[i] = ((Integer)Array.get(o, i)).intValue();
00114 s.writeIntArray(name, array, true);
00115 return;
00116 }
00117
00118 if (c==Float.class) {
00119 float[] array = new float[length];
00120 for (int i=0; i<length; ++i) array[i] = ((Float)Array.get(o, i)).floatValue();
00121 s.writeFloatArray(name, array);
00122 return;
00123 }
00124
00125 if (c==Double.class) {
00126 double[] array = new double[length];
00127 for (int i=0; i<length; ++i) array[i] = ((Double)Array.get(o, i)).doubleValue();
00128 s.writeDoubleArray(name, array);
00129 return;
00130 }
00131
00132 if (c==String.class) {
00133 String[] array = new String[length];
00134 for (int i=0; i<length; ++i) array[i] = (String)Array.get(o, i);
00135 s.writeStringArray(name, array);
00136 return;
00137 }
00138
00140 }
00141
00142 private AnyInfo any;
00143 }