KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: Storage.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Made.TIR.Impl;
3 import java.io.BufferedReader JavaDoc;
4 import java.io.DataInputStream JavaDoc;
5 import java.io.DataOutputStream JavaDoc;
6 import java.io.File JavaDoc;
7 import java.io.FileReader JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.rmi.RemoteException JavaDoc;
10
11 import SOFA.SOFAnode.Made.TIR.CDLType;
12 import SOFA.SOFAnode.Made.TIR.Contained;
13 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
14 import SOFA.SOFAnode.Made.TIR.ExprOperDef;
15 import SOFA.SOFAnode.Made.TIR.ExprOperKind;
16 import SOFA.SOFAnode.Made.TIR.Identification;
17 import SOFA.SOFAnode.Made.TIR.PrimitiveDef;
18 import SOFA.SOFAnode.Made.TIR.ProtocolOperDef;
19 import SOFA.SOFAnode.Made.TIR.ProtocolOperKind;
20 import SOFA.SOFAnode.Made.TIR.StateKind;
21 import SOFA.SOFAnode.Made.TIR.StringDef;
22 import SOFA.SOFAnode.Made.TIR.WstringDef;
23
24 class Storage {
25   public static final String JavaDoc indexFile = "index.tir";
26   public static final int maxLenFileName = 8;
27   public File JavaDoc root; /* root directory of storage */
28   public File JavaDoc current; /* current directory of storage */
29   public File JavaDoc currentFile;
30   public DataInputStream JavaDoc curInFile;
31   public DataOutputStream JavaDoc curOutFile;
32   
33   public Storage(File JavaDoc r, File JavaDoc cur) {
34     root = r;
35     current = cur;
36     currentFile = null;
37     curInFile = null;
38     curOutFile = null;
39   }
40
41   /* read childs from disk (== read info from index.tir)
42      ret[][0] == file(dir)name
43      ret[][1] == defKind
44      ret[][2] == name
45      ret[][3] == version
46   */

47   public String JavaDoc[][] listOfChilds() throws TIRExceptStorage, RemoteException JavaDoc {
48     try {
49       File JavaDoc f = new File JavaDoc(current,indexFile);
50       BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(f));
51       boolean done = false;
52       String JavaDoc str;
53       int num = 0;
54       boolean b = false;
55       while (!done) {
56         str = in.readLine();
57     if (str==null)
58       done = true;
59     else {
60           if (b)
61         num++;
62       else
63         if (str.compareTo("CHILDS")==0)
64           b = true;
65     }
66       }
67       in.close();
68       String JavaDoc[][] ret = new String JavaDoc [num][4];
69       
70       if (num==0)
71         return ret;
72       in = new BufferedReader JavaDoc(new FileReader JavaDoc(f));
73       b = false;
74       done = false;
75       while (!done) {
76         str = in.readLine();
77     if (str.compareTo("CHILDS")==0)
78       done = true;
79       }
80       int j,k;
81       for (int i=0;i<num;i++) {
82         k = 0;
83         str = in.readLine();
84     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
85     for(j=0;j<str.length();j++) {
86           if (k<3 && str.charAt(j)=='?') {
87         ret[i][k] = sb.toString();
88             k++;
89         sb = new StringBuffer JavaDoc();
90       } else {
91             sb.append(str.charAt(j));
92       }
93     }
94     ret[i][k] = sb.toString();
95       }
96       in.close();
97       return ret;
98     } catch (IOException JavaDoc e2) {
99       throw new TIRExceptStorage("Can't access storage at \""+current.getAbsolutePath()+"\"");
100     }
101   }
102 /*
103   public static void main(String[] argv) throws TIRExceptStorage, RemoteException{
104     File f = new File("/home/petr/tmp");
105     Storage st = new Storage(f,f);
106     String[][] s = st.listOfChilds();
107     for(int i=0;i<s.length;i++) {
108       System.out.println(s[i][0]+"?"+s[i][1]+"?"+s[i][2]+"?"+s[i][3]);
109     }
110   } */

111
112   /* Search identification in array returned by listOfChilds() */
113   public String JavaDoc[] existsChild(String JavaDoc[][] s, IdentificationImpl id) throws RemoteException JavaDoc{
114     if (s==null)
115       return null;
116     for(int i=0;i<s.length;i++) {
117       if (id.is_short_equal(s[i][2],s[i][3]))
118         return s[i];
119     }
120     return null;
121   }
122
123   /* read string from stream */
124   public static String JavaDoc readString(DataInputStream JavaDoc in) throws IOException JavaDoc {
125     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
126     int len;
127     len = in.readInt();
128     for(int i=0;i<len;i++) {
129       sb.append(in.readChar());
130     }
131     return sb.toString();
132   }
133
134   /* write string to stream */
135   public static void writeString(DataOutputStream JavaDoc out, String JavaDoc str) throws IOException JavaDoc {
136     out.writeInt(str.length());
137     out.writeChars(str);
138   }
139
140   public static void writeExprOperDef(Storage st, ExprOperDef ex) throws TIRExceptStorage, RemoteException JavaDoc {
141     ((TIRImplObject) ex).save(st);
142   }
143   
144   public static ExprOperDef readExprOperDef(Storage st) throws TIRExceptStorage {
145     ExprOperDef ret = null;
146     try {
147       int kind = st.curInFile.readInt();
148       switch (kind) {
149         case ExprOperKind.eok_short:
150       ret = new ExprShortImpl();
151       break;
152     case ExprOperKind.eok_long:
153       ret = new ExprLongImpl();
154       break;
155     case ExprOperKind.eok_longlong:
156       ret = new ExprLongLongImpl();
157       break;
158         case ExprOperKind.eok_unsigshort:
159       ret = new ExprUnsigShortImpl();
160       break;
161     case ExprOperKind.eok_unsiglong:
162       ret = new ExprUnsigLongImpl();
163       break;
164     case ExprOperKind.eok_unsiglonglong:
165       ret = new ExprUnsigLongLongImpl();
166       break;
167         case ExprOperKind.eok_float:
168       ret = new ExprFloatImpl();
169       break;
170         case ExprOperKind.eok_double:
171       ret = new ExprDoubleImpl();
172       break;
173         case ExprOperKind.eok_fixed:
174       ret = new ExprFixedImpl();
175       break;
176         case ExprOperKind.eok_octet:
177       ret = new ExprOctetImpl();
178       break;
179         case ExprOperKind.eok_default:
180           ret = new ExprDefaultImpl(StateKind.sk_normal);
181           break;
182         case ExprOperKind.eok_boolean:
183       ret = new ExprBooleanImpl();
184       break;
185         case ExprOperKind.eok_char:
186       ret = new ExprCharImpl();
187       break;
188         case ExprOperKind.eok_wchar:
189       ret = new ExprWcharImpl();
190       break;
191         case ExprOperKind.eok_string:
192       ret = new ExprStringImpl();
193       break;
194         case ExprOperKind.eok_wstring:
195       ret = new ExprWstringImpl();
196       break;
197         case ExprOperKind.eok_valuegen:
198       ret = new ValueGenImpl(StateKind.sk_normal);
199       break;
200         case ExprOperKind.eok_property:
201       ret = new ExprPropertyImpl();
202       break;
203         case ExprOperKind.eok_constant:
204       ret = new ExprConstantImpl();
205       break;
206         case ExprOperKind.eok_enum:
207       ret = new ExprEnumImpl();
208       break;
209         case ExprOperKind.eok_or:
210         case ExprOperKind.eok_xor:
211         case ExprOperKind.eok_and:
212         case ExprOperKind.eok_shr:
213         case ExprOperKind.eok_shl:
214         case ExprOperKind.eok_plus:
215         case ExprOperKind.eok_minus:
216         case ExprOperKind.eok_mul:
217         case ExprOperKind.eok_div:
218         case ExprOperKind.eok_mod:
219       ret = new ExprBinOperationDefImpl(kind);
220       break;
221     case ExprOperKind.eok_unplus:
222     case ExprOperKind.eok_unminus:
223     case ExprOperKind.eok_untilde:
224       ret = new ExprUnOperationDefImpl(kind);
225       break;
226     /* dopsat */
227     default:
228       throw new TIRExceptStorage("Unexpected kind ("+kind+")of object in \""
229                                   +st.currentFile+"\".");
230       }
231       ((TIRImplObject) ret).load(st);
232     } catch (IOException JavaDoc e) {
233       throw new TIRExceptStorage("Can't read in directory "+st.current+".");
234     }
235     return ret;
236   }
237
238   public static void writeCDLType(Storage st, CDLType type) throws TIRExceptStorage, IOException JavaDoc, RemoteException JavaDoc {
239     int dkind = type.get_def_kind().value();
240     st.curOutFile.writeInt(dkind); // saving kind
241
switch (dkind) {
242       case DefinitionKind.dk_Primitive:
243         st.curOutFile.writeInt(((PrimitiveDef)type).kind().value());
244         break;
245       case DefinitionKind.dk_Array:
246       case DefinitionKind.dk_Sequence:
247         ((TIRImplObject)type).save(st);
248         break;
249       case DefinitionKind.dk_String:
250         ((TIRImplObject)((StringDef)type).bound()).save(st);
251         break;
252       case DefinitionKind.dk_Wstring:
253         ((TIRImplObject)((WstringDef)type).bound()).save(st);
254         break;
255       case DefinitionKind.dk_Fixed:
256         ((TIRImplObject)type).save(st);
257     break;
258       case DefinitionKind.dk_Struct:
259       case DefinitionKind.dk_Union:
260       case DefinitionKind.dk_Enum:
261       case DefinitionKind.dk_Typedef:
262       case DefinitionKind.dk_Interface:
263       case DefinitionKind.dk_Exception:
264       case DefinitionKind.dk_Frame:
265     Identification idl = ((Contained)type).get_identification();
266         Storage.writeString(st.curOutFile, idl.lang_absolute_name().name());
267     Storage.writeString(st.curOutFile, idl.name());
268     Storage.writeString(st.curOutFile, idl.version());
269     break;
270     }
271   }
272
273   public static CDLType readCDLType(Storage st) throws TIRExceptStorage, IOException JavaDoc, RemoteException JavaDoc {
274     int dkind = st.curInFile.readInt();
275     int a;
276     CDLType ret = null;
277     ExprOperDef expr;
278     switch (dkind) {
279       case DefinitionKind.dk_Primitive:
280         a = st.curInFile.readInt();
281     ret = new NotYetInRep(a);
282     break;
283       case DefinitionKind.dk_Array:
284         ret = new ArrayDefImpl();
285     ((TIRImplObject) ret).load(st);
286     break;
287       case DefinitionKind.dk_Sequence:
288         ret = new SequenceDefImpl();
289     ((TIRImplObject) ret).load(st);
290     break;
291       case DefinitionKind.dk_String:
292         expr = Storage.readExprOperDef(st);
293         ret = new StringDefImpl(expr);
294     break;
295       case DefinitionKind.dk_Wstring:
296         expr = Storage.readExprOperDef(st);
297         ret = new WstringDefImpl(expr);
298     break;
299       case DefinitionKind.dk_Fixed:
300         ret = new FixedDefImpl();
301         ((TIRImplObject) ret).load(st);
302     break;
303       case DefinitionKind.dk_Struct:
304       case DefinitionKind.dk_Union:
305       case DefinitionKind.dk_Enum:
306       case DefinitionKind.dk_Typedef:
307       case DefinitionKind.dk_Interface:
308       case DefinitionKind.dk_Exception:
309       case DefinitionKind.dk_Frame:
310     String JavaDoc absn = readString(st.curInFile);
311     String JavaDoc name = readString(st.curInFile);
312     String JavaDoc ver = readString(st.curInFile);
313     ret = new NotYetInRep(absn,name,ver);
314     break;
315       default:
316         throw new TIRExceptStorage("Unexpected kind ("+dkind+") of type in \""
317                                   +st.currentFile+"\".");
318     }
319     
320     return ret;
321   }
322
323   public static ProtocolOperDef readProtocolOperDef(Storage st) throws TIRExceptStorage {
324     ProtocolOperDef ret = null;
325     try {
326       int kind = st.curInFile.readInt();
327       switch (kind) {
328         case ProtocolOperKind.pok_operand:
329       ret = new ProtocolOperandDefImpl();
330       break;
331         case ProtocolOperKind.pok_nulloperand:
332       ret = new ProtocolNullOperandDefImpl(StateKind.sk_normal);
333       break;
334         case ProtocolOperKind.pok_metaoperand:
335       ret = new ProtocolMetaOperandDefImpl();
336       break;
337         case ProtocolOperKind.pok_nestedoperand:
338       ret = new ProtocolNestedOperandDefImpl();
339       break;
340         case ProtocolOperKind.pok_repetition:
341       ret = new ProtocolUnOperationDefImpl(kind);
342       break;
343         case ProtocolOperKind.pok_combination:
344         case ProtocolOperKind.pok_andparallel:
345         case ProtocolOperKind.pok_orparallel:
346         case ProtocolOperKind.pok_sequence:
347         case ProtocolOperKind.pok_alternative:
348     case ProtocolOperKind.pok_restriction:
349       ret = new ProtocolBinOperationDefImpl(kind);
350       break;
351     default:
352       throw new TIRExceptStorage("Unexpected kind (ProtocolKind "+kind+")of object in \""
353                                   +st.currentFile+"\".");
354       }
355       ((TIRImplObject) ret).load(st);
356     } catch (IOException JavaDoc e) {
357       throw new TIRExceptStorage("Can't read in directory "+st.current+".");
358     }
359     return ret;
360   }
361
362   public static void writeProtocolOperDef(Storage st, ProtocolOperDef pr) throws TIRExceptStorage, RemoteException JavaDoc {
363     ((TIRImplObject) pr).save(st);
364   }
365   
366 }
367
368
369
Popular Tags