KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: TypedefDefImpl.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Made.TIR.Impl;
3 import java.io.DataInputStream JavaDoc;
4 import java.io.DataOutputStream JavaDoc;
5 import java.io.FileInputStream JavaDoc;
6 import java.io.FileOutputStream JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.rmi.RemoteException JavaDoc;
9
10 import SOFA.SOFAnode.Made.TIR.AbsoluteName;
11 import SOFA.SOFAnode.Made.TIR.ArrayDef;
12 import SOFA.SOFAnode.Made.TIR.CDLType;
13 import SOFA.SOFAnode.Made.TIR.Contained;
14 import SOFA.SOFAnode.Made.TIR.Container;
15 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
16 import SOFA.SOFAnode.Made.TIR.FixedDef;
17 import SOFA.SOFAnode.Made.TIR.Identification;
18 import SOFA.SOFAnode.Made.TIR.PrimitiveDef;
19 import SOFA.SOFAnode.Made.TIR.Repository;
20 import SOFA.SOFAnode.Made.TIR.SequenceDef;
21 import SOFA.SOFAnode.Made.TIR.StateKind;
22 import SOFA.SOFAnode.Made.TIR.StringDef;
23 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit;
24 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
25 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
26 import SOFA.SOFAnode.Made.TIR.TypedefDef;
27 import SOFA.SOFAnode.Made.TIR.WstringDef;
28
29 public class TypedefDefImpl extends ContainedImpl implements TypedefDef {
30   DefinitionKindImpl defKindImpl;
31   StateKindImpl stKindImpl;
32   CDLType tp; // original type
33

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