KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > EEG > CodeWriter > JWriter


1 /*
2  * JWriter.java
3  *
4  * Created on 7. duben 2002, 10:23
5  */

6
7 package SOFA.Connector.EEG.CodeWriter;
8
9 import SOFA.SOFAnode.Made.TIR.Contained;
10 import SOFA.SOFAnode.Made.TIR.InterfaceDef;
11 import SOFA.SOFAnode.Made.TIR.OperationDef;
12 import SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods;
13
14 /**
15  *
16  * @author ghort
17  * @version
18  */

19 public class JWriter {
20
21     protected InterfaceDef inIface;
22     
23     protected String JavaDoc outFile;
24     protected String JavaDoc outClass;
25     protected String JavaDoc outPackage;
26
27     /** Creates new JWriter */
28     public JWriter(String JavaDoc iface) throws CodeWriterException {
29         try {
30             inIface=(InterfaceDef)TIRAccessMethods.lookupCDLContained(SOFA.Connector.TIRAccess.TIRAccess.repository,iface);
31         } catch (Exception JavaDoc e) {
32             throw new CodeWriterException("Can't access interface '"+iface+"'.",e);
33         }
34     }
35
36     public JWriter() {
37     };
38
39     public void setOut(String JavaDoc outFile, String JavaDoc outPackage, String JavaDoc outClass) {
40         this.outFile=outFile;
41         this.outClass=outClass;
42         this.outPackage=outPackage;
43     }
44
45     public void write(JWriterDirector director, String JavaDoc implIface) throws CodeWriterException {
46         java.io.PrintWriter JavaDoc out;
47         try {
48             out=new java.io.PrintWriter JavaDoc(new java.io.BufferedWriter JavaDoc(new java.io.FileWriter JavaDoc(outFile)));
49         } catch (Exception JavaDoc e) {
50             throw new CodeWriterException("Can't open output file.",e);
51         }
52
53         try {
54             int i;
55             Contained[] mts=TIRAccessMethods.getOperAttrOfInterface(inIface);
56
57             out.println("package "+outPackage+";");
58             out.println();
59             out.print("public class "+outClass);
60             
61             String JavaDoc extnds=director.getExtends();
62             if (extnds!=null) {
63                 out.print(" extends "+extnds);
64             }
65             
66             String JavaDoc[] impls=director.getImplements();
67             if (director.defaultImplements()) {
68                 out.print(" implements "+implIface);
69             } else {
70                 if (impls!=null) {
71                     out.print(" implements ");
72                 }
73             }
74             if (impls!=null) {
75                 for (i=0;i<impls.length;i++) {
76                     if (director.defaultImplements() || i>0) {
77                         out.print(", ");
78                     }
79                     out.print(impls[i]);
80                 }
81             }
82             
83             out.println(" {");
84             out.println();
85
86             String JavaDoc clsBody=director.getClassBody();
87             if (clsBody!=null) {
88                 out.println(clsBody);
89             }
90             
91             for (i=0;i<mts.length;i++) {
92                 OperationDef mt=(OperationDef)mts[i];
93                 String JavaDoc methodDef=director.getMethodDef(mt);
94                 
95                 if (methodDef!=null) {
96                     out.println("\tpublic "+methodDef+" {");
97                     out.println(director.getMethodBody(mt));
98                     out.println("\t}\n");
99                 }
100             }
101             
102             out.println("}");
103         } catch (JWriterDirectorException e) {
104             throw new CodeWriterException("Troubles with java writer director.",e);
105         } catch (java.rmi.RemoteException JavaDoc e) {
106             throw new CodeWriterException("Can't access TIR.",e);
107         } catch (SOFA.SOFAnode.Made.TIR.TIRExceptLock e) {
108             throw new CodeWriterException("TIR is locked.",e);
109         }
110
111         out.close();
112     }
113
114     public void write(JWriterDirector director) throws CodeWriterException {
115         java.io.PrintWriter JavaDoc out;
116         try {
117             out=new java.io.PrintWriter JavaDoc(new java.io.BufferedWriter JavaDoc(new java.io.FileWriter JavaDoc(outFile)));
118         } catch (Exception JavaDoc e) {
119             throw new CodeWriterException("Can't open output file.",e);
120         }
121
122         try {
123             int i;
124
125             out.println("package "+outPackage+";");
126             out.println();
127             out.print("public class "+outClass);
128             
129             String JavaDoc extnds=director.getExtends();
130             if (extnds!=null) {
131                 out.print(" extends "+extnds);
132             }
133             
134             String JavaDoc[] impls=director.getImplements();
135             if (impls!=null) {
136                 out.print(" implements ");
137
138                 for (i=0;i<impls.length;i++) {
139                     if (director.defaultImplements() || i>0) {
140                         out.print(", ");
141                     }
142                     out.print(impls[i]);
143                 }
144             }
145             
146             out.println(" {");
147             out.println();
148
149             String JavaDoc clsBody=director.getClassBody();
150             if (clsBody!=null) {
151                 out.println(clsBody);
152             }
153             
154             out.println("}");
155         } catch (JWriterDirectorException e) {
156             throw new CodeWriterException("Troubles with java writer director.",e);
157         }
158
159         out.close();
160     }
161     
162     public void writeInterface(JWriterInterfaceDirector director) throws CodeWriterException {
163         java.io.PrintWriter JavaDoc out;
164         try {
165             out=new java.io.PrintWriter JavaDoc(new java.io.BufferedWriter JavaDoc(new java.io.FileWriter JavaDoc(outFile)));
166         } catch (Exception JavaDoc e) {
167             throw new CodeWriterException("Can't open output file.",e);
168         }
169
170         try {
171             int i;
172             Contained[] mts=TIRAccessMethods.getOperAttrOfInterface(inIface);
173
174             out.println("package "+outPackage+";");
175             out.println();
176             out.print("public interface "+outClass);
177             
178             String JavaDoc[] exts=director.getExtends();
179             if (exts!=null) {
180                 out.print(" extends ");
181                 for (i=0;i<exts.length;i++) {
182                     if (i>0) {
183                         out.print(", ");
184                     }
185                     out.print(exts[i]);
186                 }
187             }
188             
189             out.println(" {");
190             out.println();
191
192             String JavaDoc ifaceBody=director.getInterfaceBody();
193             if (ifaceBody!=null) {
194                 out.println(ifaceBody);
195             }
196             
197             for (i=0;i<mts.length;i++) {
198                 String JavaDoc methodDef=director.getMethod((OperationDef)mts[i]);
199                 
200                 if (methodDef!=null) {
201                     out.println("\t"+methodDef+";");
202                 }
203             }
204             
205             out.println("}");
206         } catch (JWriterDirectorException e) {
207             throw new CodeWriterException("Troubles with java writer director.",e);
208         } catch (java.rmi.RemoteException JavaDoc e) {
209             throw new CodeWriterException("Can't access TIR.",e);
210         } catch (SOFA.SOFAnode.Made.TIR.TIRExceptLock e) {
211             throw new CodeWriterException("TIR is locked.",e);
212         }
213
214         out.close();
215     }
216 }
217
Popular Tags