KickJava   Java API By Example, From Geeks To Geeks.

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


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