KickJava   Java API By Example, From Geeks To Geeks.

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


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