1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package org.coach.idltree; 26 27 import org.w3c.dom.Node ; 28 import java.util.*; 29 import java.io.*; 30 import java.lang.reflect.*; 31 32 import org.omg.CORBA.ORB ; 33 import org.omg.CORBA.TypeCode ; 34 import org.omg.CORBA.Any ; 35 import org.omg.CORBA.TCKind ; 36 37 45 public class IdlEnum extends IdlNode implements IdlWritable { 46 protected String [] members; 47 protected int index; 48 49 protected IdlEnum() { 50 setUserObject(this); 51 } 52 53 protected IdlEnum(Any any) { 54 this(); 55 setNode(any); 56 } 57 58 protected IdlEnum(TypeCode tc) { 59 this(); 60 setNode(tc); 61 } 62 63 68 public void setValue(String v) { 69 boolean found = false; 70 for (int i = 0; i < members.length; i++) { 71 if (members[i].equals(v)) { 72 found = true; 73 index = i; 74 break; 75 } 76 } 77 if (!found) { 78 throw new RuntimeException (v + " is not a valid enumeration in " + id); 79 } 80 value = v; 81 } 82 83 protected void setNode(TypeCode tc) { 84 try { 85 this.tc = tc; 86 if (tc.kind().value() == TCKind._tk_enum) { 87 id = tc.id(); 88 type = "enum " + id; 89 value = tc.member_name(0); 90 members = getMembers(tc); 91 index = 0; 92 } else if (tc.kind().value() == TCKind._tk_alias) { 93 id = tc.id(); 94 type = id; 95 TypeCode stc = tc.content_type(); 96 while (stc.kind().value() == TCKind._tk_alias) { 97 stc = stc.content_type(); 98 } 99 if (stc.kind().value() != TCKind._tk_enum) { 100 throw new RuntimeException ("Enum type expected"); 101 } 102 value = stc.member_name(0); 103 members = getMembers(stc); 104 index = 0; 105 } else { 106 throw new RuntimeException ("Enum type expected"); 107 } 108 } catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } 112 113 protected void setNode(Any any) { 114 try { 115 setNode(any.type()); 116 org.omg.CORBA.portable.InputStream in = any.create_input_stream(); 117 index = in.read_long(); 118 value = tc.member_name(index); 119 } catch (Exception e) { 120 e.printStackTrace(); 121 } 122 } 123 124 129 public void write(org.omg.CORBA.portable.OutputStream os) { 130 try { 131 os.write_long(index); 132 } catch (Exception e) { 133 e.printStackTrace(); 134 } 135 } 136 137 142 public void read(org.omg.CORBA.portable.InputStream is) { 143 try { 144 index = is.read_long(); 145 value = tc.member_name(index); 146 } catch (Exception e) { 147 e.printStackTrace(); 148 } 149 } 150 151 156 public String [] getEnums() { 157 return members; 158 } 159 160 165 public int getIndex() { 166 return index; 167 } 168 169 173 public void setIndex(int i) { 174 if (i < 0 || i >= members.length) { 175 throw new RuntimeException ("Enum index out of range"); 176 } 177 value = members[i]; 178 index = i; 179 } 180 181 186 public Any toAny() { 187 try { 188 Any any = orb.create_any(); 189 any.type(tc); 190 org.omg.CORBA.portable.OutputStream out = any.create_output_stream(); 191 out.write_long(index); 192 any.read_value(out.create_input_stream(), tc); 193 return any; 194 } catch (Exception e) { 195 e.printStackTrace(); 196 } 197 return null; 198 } 199 200 private String [] getMembers(TypeCode tc) { 201 try { 202 String [] members = new String [tc.member_count()]; 203 for (int i = 0; i < members.length; i++) { 204 members[i] = tc.member_name(i); 205 } 206 return members; 207 } catch (Exception e) { 208 e.printStackTrace(); 209 } 210 return null; 211 } 212 213 215 225 public IdlEnum(String xml) { 226 this(XmlNode.getNode(xml)); 227 } 228 229 IdlEnum(Node n) { 230 this(); 231 id = XmlNode.getId(n); 232 setNode(XmlNode.type(id)); 233 setValue(XmlNode.getText(n)); 234 } 235 236 241 public void write(IdlWriter w) { 242 w.write_enum(id, value); 243 } 244 } | Popular Tags |