KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > TIR > Impl > UnionMemberImpl


1 /* $Id: UnionMemberImpl.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Made.TIR.Impl;
3 import java.io.IOException JavaDoc;
4 import java.rmi.RemoteException JavaDoc;
5
6 import SOFA.SOFAnode.Made.TIR.AbsoluteName;
7 import SOFA.SOFAnode.Made.TIR.ArrayDef;
8 import SOFA.SOFAnode.Made.TIR.CDLType;
9 import SOFA.SOFAnode.Made.TIR.Contained;
10 import SOFA.SOFAnode.Made.TIR.Container;
11 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
12 import SOFA.SOFAnode.Made.TIR.ExprOperDef;
13 import SOFA.SOFAnode.Made.TIR.FixedDef;
14 import SOFA.SOFAnode.Made.TIR.Identification;
15 import SOFA.SOFAnode.Made.TIR.PrimitiveDef;
16 import SOFA.SOFAnode.Made.TIR.Repository;
17 import SOFA.SOFAnode.Made.TIR.SequenceDef;
18 import SOFA.SOFAnode.Made.TIR.StateKind;
19 import SOFA.SOFAnode.Made.TIR.StringDef;
20 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit;
21 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
22 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
23 import SOFA.SOFAnode.Made.TIR.UnionMember;
24 import SOFA.SOFAnode.Made.TIR.WstringDef;
25
26 public class UnionMemberImpl extends ContainedImpl implements UnionMember, TIRImplObject {
27   CDLType tp;
28   ExprOperDef[] lab;
29   DefinitionKindImpl defKindImpl;
30   StateKindImpl stKindImpl;
31
32   UnionMemberImpl normal;
33
34   public UnionMemberImpl(Identification ident, Container in, Repository inrep, CDLType type, ExprOperDef[] label) throws RemoteException JavaDoc {
35     super(ident,in,inrep);
36     tp = type;
37     lab = label;
38     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_UnionMember);
39     stKindImpl = new StateKindImpl(StateKind.sk_work);
40     normal = null;
41   }
42
43   /* constuctor for load() */
44   public UnionMemberImpl(Container in, Repository inrep) throws RemoteException JavaDoc {
45     super(null,in,inrep);
46     tp = null;
47     lab = null;
48     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_UnionMember);
49     stKindImpl = new StateKindImpl(StateKind.sk_normal);
50     normal = null;
51   }
52
53   public UnionMemberImpl(UnionMemberImpl a, Container in, Repository inrep) throws RemoteException JavaDoc {
54     super(a.id,in,inrep);
55     tp = a.tp;
56     lab = a.lab;
57     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_UnionMember);
58     stKindImpl = new StateKindImpl(StateKind.sk_work);
59     normal = a;
60   }
61   
62   public String JavaDoc name() throws RemoteException JavaDoc {
63     return id.name();
64   }
65   
66   public CDLType type() throws RemoteException JavaDoc {
67     return tp;
68   }
69
70   public ExprOperDef[] label() throws RemoteException JavaDoc {
71     return lab;
72   }
73
74   /* from interface TIRObject */
75   public DefinitionKind get_def_kind() throws RemoteException JavaDoc {
76     return (DefinitionKind) defKindImpl;
77   }
78
79   /* from interface TIRObject */
80   public StateKind get_state() throws RemoteException JavaDoc {
81     return (StateKind) stKindImpl;
82   }
83
84   public void tag(String JavaDoc t) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
85     if (stKindImpl.value()!=StateKind.sk_work)
86       throw new TIRExceptCreate("you can call create method on work object");
87     super.tag(t);
88   }
89    
90   public void load(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
91     try {
92       id = new IdentificationImpl();
93       ((IdentificationImpl)id).load(st.curInFile);
94       tp = Storage.readCDLType(st);
95       int len = st.curInFile.readInt();
96       lab = new ExprOperDef [len];
97       for (int i=0;i<len;i++) {
98         lab[i] = Storage.readExprOperDef(st);
99       }
100     } catch (IOException JavaDoc e) {
101       throw new TIRExceptStorage("Can't read in file "+st.currentFile+".");
102     }
103   }
104
105   public void save(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
106     try {
107       ((IdentificationImpl)id).save(st.curOutFile);
108       Storage.writeCDLType(st,tp);
109       st.curOutFile.writeInt(lab.length);
110       for (int i=0;i<lab.length;i++) {
111         Storage.writeExprOperDef(st,lab[i]);
112       }
113     } catch (IOException JavaDoc e) {
114       throw new TIRExceptStorage("Can't write in file "+st.currentFile+".");
115     }
116   }
117
118   public void postLoad(RepositoryImpl r) throws RemoteException JavaDoc, TIRExceptStorage {
119     try {
120   
121     if (tp.get_def_kind().value() == DefinitionKind.dk_none) { // is tp NotYetInRep?
122
if (((NotYetInRep) tp).defKind == DefinitionKind.dk_Primitive)
123         tp = r.get_prim(((NotYetInRep) tp).primKind);
124       else { // in tp there is reference to some type
125
AbsoluteName nm = ((NotYetInRep) tp).nm;
126     Container con = r; // begin search from repository
127
Contained[] rt;
128     Contained ret;
129     IdentificationImpl id;
130         for (int i=0;i<nm.size()-1;i++) { // last element is name of object
131
id = new IdentificationImpl(nm.elementAt(0),"::"+nm.elementAt(i),"");
132           rt = ((SContainer) con).slookup_name(id);
133       if (rt.length==0) throw new TIRExceptStorage("Can't find type " + nm.name());
134           if (rt.length==1) {
135             con = (Container) rt[0];
136       } else { // type in struct (etc.) - more versions of this struct
137
boolean found = false;
138             id.version(((NotYetInRep) tp).id.version()); // substruct version is inherited from parent
139
int j = 0;
140         for (j=0;j<rt.length;j++) {
141           if (id.is_short_equal(rt[j].get_identification())) {
142             found = true;
143         break; // -- break for
144
}
145         } // -- end of for
146
if (!found) throw new TIRExceptStorage("Can't find type " + nm.name());
147         con = (Container) rt[j];
148       }
149     }
150     if ((ret = ((SContainer) con).slookup(((NotYetInRep) tp).id))==null)
151       throw new TIRExceptStorage("Can't find type " + nm.name());
152     tp = (CDLType) ret;
153     if (tp.get_def_kind().value()==DefinitionKind.dk_Array ||
154             tp.get_def_kind().value()==DefinitionKind.dk_Sequence ||
155             tp.get_def_kind().value()==DefinitionKind.dk_String ||
156             tp.get_def_kind().value()==DefinitionKind.dk_Wstring)
157       ((TIRImplObject) tp).postLoad(r);
158       }
159     } else {
160       if (tp.get_def_kind().value()==DefinitionKind.dk_Array ||
161           tp.get_def_kind().value()==DefinitionKind.dk_Sequence ||
162           tp.get_def_kind().value()==DefinitionKind.dk_String ||
163           tp.get_def_kind().value()==DefinitionKind.dk_Wstring)
164         ((TIRImplObject) tp).postLoad(r);
165     }
166     for (int i=0;i<lab.length;i++)
167       ((TIRImplObject) lab[i]).postLoad(r);
168
169     } catch (TIRExceptLock e) {;}
170   }
171
172   public boolean isNew() {
173     return ((stKindImpl.value()==StateKind.sk_work) && normal==null);
174   }
175
176   public void canCommit() throws RemoteException JavaDoc, TIRExceptCommit {;}
177
178   public void doCommit(Container in, Repository rep) throws RemoteException JavaDoc {
179     if (stKindImpl.value()==StateKind.sk_normal)
180       return;
181     if (isNew()) {
182       stKindImpl.toNormal();
183       switch (tp.get_def_kind().value()) {
184         case DefinitionKind.dk_Primitive:
185           tp = ((SRepository)rep).get_prim(((PrimitiveDef)tp).kind().value());
186           break;
187         case DefinitionKind.dk_Array:
188           ArrayDef nt = new ArrayDefImpl(ExprHelp.toNormal(((ArrayDef) tp).length()), ((ArrayDef) tp).element_type());
189           tp = nt;
190           ((TIRImplObject)tp).doCommit(in,rep);
191           break;
192         case DefinitionKind.dk_Sequence:
193           SequenceDef st = new SequenceDefImpl(ExprHelp.toNormal(((SequenceDef) tp).bound()), ((SequenceDef) tp).element_type());
194           tp = st;
195           ((TIRImplObject)tp).doCommit(in,rep);
196           break;
197         case DefinitionKind.dk_String:
198           StringDef str = new StringDefImpl(ExprHelp.toNormal(((StringDef) tp).bound()));
199           tp = str;
200           ((TIRImplObject)tp).doCommit(in,rep);
201           break;
202         case DefinitionKind.dk_Wstring:
203           WstringDef wstr = new WstringDefImpl(ExprHelp.toNormal(((WstringDef) tp).bound()));
204           tp = wstr;
205           ((TIRImplObject)tp).doCommit(in,rep);
206           break;
207         case DefinitionKind.dk_Fixed:
208           FixedDef ft = new FixedDefImpl(ExprHelp.toNormal(((FixedDef) tp).digits()), ExprHelp.toNormal(((FixedDef) tp).scale()));
209           tp = ft;
210           ((TIRImplObject)tp).doCommit(in,rep);
211           break;
212         default:
213           Identification idl = ((Contained)tp).get_identification();
214           tp = new NotYetInRep(idl.lang_absolute_name().name(), idl.name(), idl.version());
215           break;
216       }
217       for (int i=0;i<lab.length;i++) {
218         lab[i] = ExprHelp.toNormal(lab[i]);
219         ((TIRImplObject)lab[i]).doCommit(in,rep);
220       }
221     } else {
222       normal=null;
223     }
224   }
225
226   public void doAbort(long workId) throws RemoteException JavaDoc {}
227 }
228
Popular Tags