1 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 19 public class JWriter { 20 21 protected InterfaceDef inIface; 22 23 protected String outFile; 24 protected String outClass; 25 protected String outPackage; 26 27 28 public JWriter(String iface) throws CodeWriterException { 29 try { 30 inIface=(InterfaceDef)TIRAccessMethods.lookupCDLContained(SOFA.Connector.TIRAccess.TIRAccess.repository,iface); 31 } catch (Exception e) { 32 throw new CodeWriterException("Can't access interface '"+iface+"'.",e); 33 } 34 } 35 36 public JWriter() { 37 }; 38 39 public void setOut(String outFile, String outPackage, String outClass) { 40 this.outFile=outFile; 41 this.outClass=outClass; 42 this.outPackage=outPackage; 43 } 44 45 public void write(JWriterDirector director, String implIface) throws CodeWriterException { 46 java.io.PrintWriter out; 47 try { 48 out=new java.io.PrintWriter (new java.io.BufferedWriter (new java.io.FileWriter (outFile))); 49 } catch (Exception 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 extnds=director.getExtends(); 62 if (extnds!=null) { 63 out.print(" extends "+extnds); 64 } 65 66 String [] 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 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 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 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 out; 116 try { 117 out=new java.io.PrintWriter (new java.io.BufferedWriter (new java.io.FileWriter (outFile))); 118 } catch (Exception 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 extnds=director.getExtends(); 130 if (extnds!=null) { 131 out.print(" extends "+extnds); 132 } 133 134 String [] 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 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 out; 164 try { 165 out=new java.io.PrintWriter (new java.io.BufferedWriter (new java.io.FileWriter (outFile))); 166 } catch (Exception 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 [] 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 ifaceBody=director.getInterfaceBody(); 193 if (ifaceBody!=null) { 194 out.println(ifaceBody); 195 } 196 197 for (i=0;i<mts.length;i++) { 198 String 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 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 |