KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > api > JMI1_0Generator


1 /**
2  * copyright 2002 2004 Laboratoire d'Informatique Paris 6 (LIP6)
3  *
4  * This file is part of ModFact.
5  *
6  * ModFact is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * at your option) any later version.
10  *
11  * ModFact is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with ModFact; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package org.objectweb.modfact.jmi.api;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.jmi.model.Association;
28 import javax.jmi.model.DataType;
29 import javax.jmi.model.EnumerationType;
30 import javax.jmi.model.ModelElement;
31 import javax.jmi.model.MofClass;
32 import javax.jmi.model.MofException;
33 import javax.jmi.model.MofPackage;
34 import javax.jmi.model.StructureType;
35 import javax.jmi.model.VisibilityKindEnum;
36
37 import org.objectweb.modfact.jmi.generator.ZipGenerator;
38 import org.objectweb.modfact.jmi.helper.JMIProvider;
39 import org.objectweb.modfact.jmi.helper.MofHelper;
40 import org.objectweb.modfact.jmi.logging.Level;
41 import org.objectweb.modfact.jmi.logging.ModFactLogger;
42
43 /**
44  * @author Xavier
45  *
46  */

47 public class JMI1_0Generator extends ZipGenerator {
48     
49     MofPackage[] input;
50     
51     ModFactLogger logger;
52
53     /**
54      * Set Input
55      */

56     public void setInput(ModelElement[] elt) {
57         input = new MofPackage[elt.length];
58         for (int i = 0; i < input.length; i++) {
59             input[i] = (MofPackage) elt[i];
60         }
61     }
62
63     /**
64      * Set Trace
65      */

66     public void setLogger(ModFactLogger log) {
67         logger = log;
68     }
69
70     /**
71      * @see fr.lip6.modfact.generation.Generator#generate()
72      */

73     public void generate()
74             throws IOException JavaDoc {
75
76
77         logger.log(Level.FINE, "Begin generation of JMI Interfaces");
78
79         for (int p = 0; p < input.length; p++) {
80             if (input[p]
81                 .getVisibility()
82                 .equals(VisibilityKindEnum.forName("public_vis"))) {
83
84                 //Traitemement du Package
85
PackageInterfaceGenerator packageGenerator = new PackageInterfaceGenerator();
86                 MofPackage[] param = new MofPackage[1];
87                 param[0] = input[p];
88                 packageGenerator.setInput(param);
89                 ByteArrayOutputStream JavaDoc entryOutput1 =
90                     new ByteArrayOutputStream JavaDoc();
91                 packageGenerator.setOutput(entryOutput1);
92                 packageGenerator.generate();
93                 
94                 String JavaDoc path = JMIProvider.qualifierOf(input[p]);
95                 path = path.replace('.', '/') +"/";
96                 
97                 String JavaDoc entryName = path
98                     +JMIProvider.jmiPackageExtentName(input[p]) + "Package" + ".java";
99                     
100                 writeEntry(entryName, entryOutput1);
101                 entryOutput1.close();
102                 
103                 logger.log(Level.FINE, entryName+" is generated");
104
105                 //Traitement des Classes
106
MofClass[] classes = MofHelper.classesOfPackage(input[p]);
107                 
108                 for (int i = 0; i < classes.length; i++) {
109                     if (classes[i].getVisibility()
110                         == VisibilityKindEnum.forName("public_vis")) {
111
112                         // Cette Partie génère toutes les ClasseProxy
113
ClassProxyInterfaceGenerator classProxyGenerator = new ClassProxyInterfaceGenerator();
114                         MofClass[] paramCP = new MofClass[1];
115                         paramCP[0] = classes[i];
116                         classProxyGenerator.setInput(paramCP);
117                         ByteArrayOutputStream JavaDoc entryOutputClassProxy =
118                             new ByteArrayOutputStream JavaDoc();
119                         classProxyGenerator.setOutput(entryOutputClassProxy);
120                         classProxyGenerator.generate();
121
122                         String JavaDoc entryNameClassProxy = path
123                             +JMIProvider.jmiClassName(classes[i])
124                                 + "Class"
125                                 + ".java";
126                         writeEntry(entryNameClassProxy, entryOutputClassProxy);
127                         entryOutputClassProxy.close();
128                         
129                         logger.log(Level.FINE, entryNameClassProxy+" is generated");
130
131                         // Cette partie génère toutes les Classes
132
InstanceInterfaceGenerator classImpl = new InstanceInterfaceGenerator();
133                         MofClass[] paramCI = new MofClass[1];
134                         paramCI[0] = classes[i];
135                         classImpl.setInput(paramCI);
136                         ByteArrayOutputStream JavaDoc entryOutputInstance = new ByteArrayOutputStream JavaDoc();
137                         classImpl.setOutput(entryOutputInstance);
138                         classImpl.generate();
139
140                         String JavaDoc entryNameInstance = path
141                             +JMIProvider.jmiClassName(classes[i]) + ".java";
142                         writeEntry(entryNameInstance, entryOutputInstance);
143                         entryOutputInstance.close();
144                         
145                         logger.log(Level.FINE, entryNameInstance+" is generated");
146
147                         //Cette partie traite les DataType (Enum et Struct) des classes
148
DataType[] datatypes =
149                             MofHelper.datatypesOf(classes[i], false);
150                         for (int j = 0; j < datatypes.length; j++) {
151                             
152                             ///// ENUM
153
if (datatypes[j] instanceof EnumerationType) {
154                                 EnumerationTypeInterfaceGenerator enumInterfaceGenerator =
155                                     new EnumerationTypeInterfaceGenerator();
156                                 DataType[] in = new DataType[1];
157                                 in[0] = datatypes[j];
158                                 enumInterfaceGenerator.setInput(in);
159                                 enumInterfaceGenerator.setLogger(logger);
160                                 ByteArrayOutputStream JavaDoc enumEntryOutputInt =
161                                     new ByteArrayOutputStream JavaDoc();
162                                 enumInterfaceGenerator.setOutput(
163                                     enumEntryOutputInt);
164                                 enumInterfaceGenerator.generate();
165
166                                 String JavaDoc entryNameEnumInt = path
167                                     +datatypes[j].getName() + ".java";
168                                 writeEntry(
169                                     entryNameEnumInt,
170                                     enumEntryOutputInt);
171                                 enumEntryOutputInt.close();
172                                 
173                                 logger.log(Level.FINE, entryNameEnumInt+" is generated");
174
175                                 EnumerationTypeImplementationGenerator enumGenerator =
176                                     new EnumerationTypeImplementationGenerator();
177                                 DataType[] inImpl = new DataType[1];
178                                 inImpl[0] = datatypes[j];
179                                 enumGenerator.setInput(inImpl);
180                                 enumGenerator.setLogger(logger);
181                                 
182                                 ByteArrayOutputStream JavaDoc enumEntryOutputImpl =
183                                     new ByteArrayOutputStream JavaDoc();
184                                 enumGenerator.setOutput(enumEntryOutputImpl);
185                                 enumGenerator.generate();
186
187                                 String JavaDoc entryNameEnumImpl = path+
188                                     datatypes[j].getName() + "Enum.java";
189                                 writeEntry(
190                                     entryNameEnumImpl,
191                                     enumEntryOutputImpl);
192                                 enumEntryOutputImpl.close();
193                                 
194                                 logger.log(Level.FINE, entryNameEnumImpl+" is generated");
195                             
196                             
197                             ////// STRUCT
198
} else if (datatypes[j] instanceof StructureType) {
199                                 StructureTypeInterfaceGenerator structGenerator =
200                                     new StructureTypeInterfaceGenerator();
201                                 DataType[] inImpl = new DataType[1];
202                                 inImpl[0] = datatypes[j];
203                                 structGenerator.setInput(inImpl);
204                                 structGenerator.setLogger(logger);
205
206                                 ByteArrayOutputStream JavaDoc structEntryOutputImpl =
207                                     new ByteArrayOutputStream JavaDoc();
208                                 structGenerator.setOutput(
209                                     structEntryOutputImpl);
210                                 structGenerator.generate();
211
212                                 String JavaDoc entryNameStruct = path
213                                     +datatypes[j].getName() + ".java";
214                                 writeEntry(
215                                     entryNameStruct,
216                                     structEntryOutputImpl);
217                                 structEntryOutputImpl.close();
218                                 
219                                 logger.log(Level.FINE, entryNameStruct+" is generated");
220                             }
221                         }
222
223                     }
224                 }
225
226                 //Traitement des Associations
227
Association[] associations =
228                     MofHelper.associationsOfPackage(input[p]);
229                 for (int i = 0; i < associations.length; i++) {
230                     if (associations[i]
231                         .getVisibility()
232                         .equals(VisibilityKindEnum.forName("public_vis"))) {
233                         AssociationGenerator associationGenerator =
234                             new AssociationGenerator();
235                         Association[] paramA = new Association[1];
236                         paramA[0] = associations[i];
237                         associationGenerator.setInput(paramA);
238                         ByteArrayOutputStream JavaDoc entryOutputAssociation =
239                             new ByteArrayOutputStream JavaDoc();
240                         associationGenerator.setOutput(entryOutputAssociation);
241
242                         associationGenerator.generate();
243
244                         String JavaDoc entryNameAssociation = path+
245                             JMIProvider.jmiAssociationName(associations[i])
246                                 + ".java";
247                         writeEntry(
248                             entryNameAssociation,
249                             entryOutputAssociation);
250                         entryOutputAssociation.close();
251                         
252                         logger.log(Level.FINE, entryNameAssociation+" is generated");
253                     }
254                 }
255
256                 //Traitement des DataTypes Enum et Struct
257
DataType[] datatypes = MofHelper.datatypesOf(input[p], false);
258                 for (int i = 0; i < datatypes.length; i++) {
259                     
260                     
261                     // STRUCT
262
if (datatypes[i] instanceof StructureType) {
263                         StructureTypeInterfaceGenerator structureTypeInterfaceGenerator =
264                             new StructureTypeInterfaceGenerator();
265                         DataType[] paramT = new DataType[1];
266                         paramT[0] = datatypes[i];
267                         structureTypeInterfaceGenerator.setInput(paramT);
268                         ByteArrayOutputStream JavaDoc entryOutputDataType =
269                             new ByteArrayOutputStream JavaDoc();
270                         structureTypeInterfaceGenerator.setOutput(
271                             entryOutputDataType);
272
273                         structureTypeInterfaceGenerator.generate();
274
275                         String JavaDoc entryNameStructure = path+
276                             datatypes[i].getName() + ".java";
277                         writeEntry(entryNameStructure, entryOutputDataType);
278                         entryOutputDataType.close();
279                         
280                         logger.log(Level.FINE, entryNameStructure+" is generated");
281                         
282                         
283                     // ENUM
284
} else if (datatypes[i] instanceof EnumerationType) {
285                         EnumerationTypeImplementationGenerator enumTypeTemplateImpl =
286                             new EnumerationTypeImplementationGenerator();
287                         DataType[] paramT = new DataType[1];
288                         paramT[0] = datatypes[i];
289                         enumTypeTemplateImpl.setInput(paramT);
290                         ByteArrayOutputStream JavaDoc entryOutputDataType =
291                             new ByteArrayOutputStream JavaDoc();
292                         enumTypeTemplateImpl.setOutput(entryOutputDataType);
293
294                         enumTypeTemplateImpl.generate();
295
296                         String JavaDoc entryNameEnumImpl = path+
297                             datatypes[i].getName() + "Enum.java";
298                         writeEntry(entryNameEnumImpl, entryOutputDataType);
299                         entryOutputDataType.close();
300                         
301                         logger.log(Level.FINE, entryNameEnumImpl+" is generated");
302
303                         EnumerationTypeInterfaceGenerator enumTypeTemplate =
304                             new EnumerationTypeInterfaceGenerator();
305                         enumTypeTemplate.setInput(paramT);
306                         ByteArrayOutputStream JavaDoc entryOutputDataTypeInt =
307                             new ByteArrayOutputStream JavaDoc();
308                         enumTypeTemplate.setOutput(entryOutputDataTypeInt);
309                         enumTypeTemplate.generate();
310
311                         String JavaDoc entryNameEnumInt = path + datatypes[i].getName() + ".java";
312                         writeEntry(entryNameEnumInt, entryOutputDataTypeInt);
313                         entryOutputDataTypeInt.close();
314                         
315                         logger.log(Level.FINE, entryNameEnumInt+" is generated");
316                     }
317                 }
318                 
319                 exceptionTemplate(input[p], path);
320
321             } // end is visible
322
}
323         out.finish();
324         logger.log(Level.FINE, " Generation is completed without error");
325     }
326     
327     
328     void exceptionTemplate(MofPackage p, String JavaDoc path) throws IOException JavaDoc {
329         Iterator JavaDoc it = MofHelper.filterContentsByClass(p, MofException.class, false).iterator();
330         while(it.hasNext()) {
331           MofException e = (MofException) it.next();
332           String JavaDoc entryName = path +e.getName() + "Exception.java";
333           ByteArrayOutputStream JavaDoc expOut = new ByteArrayOutputStream JavaDoc();
334           PrintWriter JavaDoc w = new PrintWriter JavaDoc(expOut);
335           w.println("public class " +entryName.replace('/','.') +" extends javax.jmi.reflect.RefException {");
336           w.println("}");
337           writeEntry(entryName, expOut);
338           w.close();
339         }
340     }
341
342 }
343
Popular Tags