KickJava   Java API By Example, From Geeks To Geeks.

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


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