1 2 package SOFA.SOFAnode.Made.TIR.Impl; 3 import java.io.DataInputStream ; 4 import java.io.DataOutputStream ; 5 import java.io.FileInputStream ; 6 import java.io.FileOutputStream ; 7 import java.io.IOException ; 8 import java.rmi.RemoteException ; 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 { 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 { 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 { 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 70 public DefinitionKind get_def_kind() throws RemoteException { 71 return (DefinitionKind) defKindImpl; 72 } 73 74 75 public StateKind get_state() throws RemoteException { 76 return (StateKind) stKindImpl; 77 } 78 79 public void tag(String t) throws RemoteException , 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 { 86 return typ; 87 } 88 89 public ExprOperDef value() throws RemoteException { 90 return val; 91 } 92 93 public void save(Storage st) throws RemoteException , TIRExceptStorage { 94 try { 95 boolean closeFile = false; 96 if (st.curOutFile==null) { 97 st.curOutFile = new DataOutputStream (new FileOutputStream (st.currentFile)); 98 closeFile = true; 99 } else { 100 ; 101 } 102 ((IdentificationImpl)id).save(st.curOutFile); Storage.writeCDLType(st, typ); 104 Storage.writeExprOperDef(st, val); 105 if (closeFile) { 106 st.curOutFile.close(); 107 st.curOutFile = null; 108 } 109 } catch (IOException e) { 110 throw new TIRExceptStorage("Can't write in file "+st.currentFile+"."); 111 } 112 } 113 114 public void load(Storage st) throws RemoteException , TIRExceptStorage { 115 try { 116 boolean closeFile = false; 117 if (st.curInFile==null) { st.curInFile = new DataInputStream (new FileInputStream (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 e) { 132 throw new TIRExceptStorage("Can't read in file "+st.currentFile+"."); 133 } 134 } 135 136 public void postLoad(RepositoryImpl r) throws RemoteException , TIRExceptStorage { 137 try { 138 139 if (typ.get_def_kind().value() == DefinitionKind.dk_none) { if (((NotYetInRep) typ).defKind == DefinitionKind.dk_Primitive) 141 typ = r.get_prim(((NotYetInRep) typ).primKind); 142 else { AbsoluteName nm = ((NotYetInRep) typ).nm; 144 Container con = r; Contained[] rt; 146 Contained ret; 147 IdentificationImpl id; 148 for (int i=0;i<nm.size()-1;i++) { 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 { boolean found = false; 156 id.version(((NotYetInRep) typ).id.version()); 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; } 163 } 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 , TIRExceptCommit {;} 194 195 public void doCommit(Container in, Repository rep) throws RemoteException { 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 {} 242 } 243 | Popular Tags |