KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > impl > JMI1_0ImplementationGenerator


1 /**
2  * copyright 2002 2003 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.impl;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import javax.jmi.model.Association;
26 import javax.jmi.model.DataType;
27 import javax.jmi.model.ModelElement;
28 import javax.jmi.model.MofClass;
29 import javax.jmi.model.MofPackage;
30 import javax.jmi.model.StructureType;
31 import javax.jmi.model.VisibilityKindEnum;
32
33 import org.objectweb.modfact.jmi.generator.ZipGenerator;
34 import org.objectweb.modfact.jmi.helper.ImplHelper;
35 import org.objectweb.modfact.jmi.helper.JMIProvider;
36 import org.objectweb.modfact.jmi.helper.MofHelper;
37 import org.objectweb.modfact.jmi.logging.Level;
38 import org.objectweb.modfact.jmi.logging.ModFactLogger;
39
40
41 /**
42  * @author Xavier
43  *
44  */

45 public class JMI1_0ImplementationGenerator extends ZipGenerator {
46     
47     MofPackage[] input;
48     
49     ModFactLogger logger;
50
51     /**
52      * Set Input
53      */

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

64     public void setLogger(ModFactLogger log) {
65         logger = log;
66     }
67
68     /**
69      * @see org.objectweb.modfact.generation.Generator#generate()
70      */

71     public void generate()
72             throws IOException JavaDoc {
73
74
75         logger.log(Level.FINE, "Begin generation of JMI Implementation Interfaces");
76
77         for (int p = 0; p < input.length; p++) {
78             if (input[p]
79                 .getVisibility()
80                 .equals(VisibilityKindEnum.forName("public_vis"))) {
81
82                 //Traitemement du Package
83
PackageImplementationGenerator packageGenerator = new PackageImplementationGenerator();
84                 MofPackage[] param = new MofPackage[1];
85                 param[0] = input[p];
86                 packageGenerator.setInput(param);
87                 ByteArrayOutputStream JavaDoc entryOutput1 =
88                     new ByteArrayOutputStream JavaDoc();
89                 packageGenerator.setOutput(entryOutput1);
90                 packageGenerator.generate();
91                 
92                 String JavaDoc path = ImplHelper.implPrefix + JMIProvider.shortQualifierOf(input[p]);
93                 path = path.replace('.', '/') +"/";
94                 
95                 String JavaDoc entryName = path
96                     +JMIProvider.jmiPackageExtentName(input[p]) + "PackageImpl" + ".java";
97                     
98                 writeEntry(entryName, entryOutput1);
99                 entryOutput1.close();
100                 
101                 logger.log(Level.FINE, entryName+" is generated");
102
103                 //Traitement des Classes
104
MofClass[] classes = MofHelper.classesOfPackage(input[p]);
105                 
106                 for (int i = 0; i < classes.length; i++) {
107                     if (classes[i].getVisibility()
108                         == VisibilityKindEnum.forName("public_vis")) {
109
110                         // Cette Partie génère toutes les ClasseProxy
111
ClassProxyImplementationGenerator classProxyGenerator = new ClassProxyImplementationGenerator();
112                         MofClass[] paramCP = new MofClass[1];
113                         paramCP[0] = classes[i];
114                         classProxyGenerator.setInput(paramCP);
115                         ByteArrayOutputStream JavaDoc entryOutputClassProxy =
116                             new ByteArrayOutputStream JavaDoc();
117                         classProxyGenerator.setOutput(entryOutputClassProxy);
118                         classProxyGenerator.generate();
119
120                         String JavaDoc entryNameClassProxy = path
121                             +JMIProvider.jmiClassName(classes[i])
122                                 + "ClassImpl"
123                                 + ".java";
124                         writeEntry(entryNameClassProxy, entryOutputClassProxy);
125                         entryOutputClassProxy.close();
126                         
127                         logger.log(Level.FINE, entryNameClassProxy+" is generated");
128
129                         // Cette partie génère toutes les Classes
130
InstanceImplementationGenerator classImpl = new InstanceImplementationGenerator();
131                         MofClass[] paramCI = new MofClass[1];
132                         paramCI[0] = classes[i];
133                         classImpl.setInput(paramCI);
134                         ByteArrayOutputStream JavaDoc entryOutputInstance = new ByteArrayOutputStream JavaDoc();
135                         classImpl.setOutput(entryOutputInstance);
136                         classImpl.generate();
137
138                         String JavaDoc entryNameInstance = path
139                             +JMIProvider.jmiClassName(classes[i]) + "Impl.java";
140                         writeEntry(entryNameInstance, entryOutputInstance);
141                         entryOutputInstance.close();
142                         
143                         logger.log(Level.FINE, entryNameInstance+" is generated");
144
145                         //Cette partie traite les DataType (Enum et Struct) des classes
146
DataType[] datatypes =
147                             MofHelper.datatypesOf(classes[i], false);
148                         for (int j = 0; j < datatypes.length; j++) {
149                             
150                             if (datatypes[j] instanceof StructureType) {
151                                 StructureTypeImplementationGenerator structGenerator =
152                                     new StructureTypeImplementationGenerator();
153                                 DataType[] inImpl = new DataType[1];
154                                 inImpl[0] = datatypes[j];
155                                 structGenerator.setInput(inImpl);
156                                 structGenerator.setLogger(logger);
157
158                                 ByteArrayOutputStream JavaDoc structEntryOutputImpl =
159                                     new ByteArrayOutputStream JavaDoc();
160                                 structGenerator.setOutput(
161                                     structEntryOutputImpl);
162                                 structGenerator.generate();
163
164                                 String JavaDoc entryNameStruct = path
165                                     +datatypes[j].getName() + "Impl.java";
166                                 writeEntry(
167                                     entryNameStruct,
168                                     structEntryOutputImpl);
169                                 structEntryOutputImpl.close();
170                                 
171                                 logger.log(Level.FINE, entryNameStruct+" is generated");
172                             }
173                         }
174
175                     }
176                 }
177
178                 //Traitement des Associations
179
Association[] associations =
180                     MofHelper.associationsOfPackage(input[p]);
181                 for (int i = 0; i < associations.length; i++) {
182                     if (associations[i]
183                         .getVisibility()
184                         .equals(VisibilityKindEnum.forName("public_vis"))) {
185                         AssociationImplementationGenerator associationGenerator =
186                             new AssociationImplementationGenerator();
187                         Association[] paramA = new Association[1];
188                         paramA[0] = associations[i];
189                         associationGenerator.setInput(paramA);
190                         ByteArrayOutputStream JavaDoc entryOutputAssociation =
191                             new ByteArrayOutputStream JavaDoc();
192                         associationGenerator.setOutput(entryOutputAssociation);
193
194                         associationGenerator.generate();
195
196                         String JavaDoc entryNameAssociation = path+
197                             JMIProvider.jmiAssociationName(associations[i])
198                                 + "Impl.java";
199                         writeEntry(
200                             entryNameAssociation,
201                             entryOutputAssociation);
202                         entryOutputAssociation.close();
203                         
204                         logger.log(Level.FINE, entryNameAssociation+" is generated");
205                     }
206                 }
207
208                 //Traitement des DataTypes Enum et Struct
209
DataType[] datatypes = MofHelper.datatypesOf(input[p], false);
210                 for (int i = 0; i < datatypes.length; i++) {
211                     if (datatypes[i] instanceof StructureType) {
212                         StructureTypeImplementationGenerator structureTypeInterfaceGenerator =
213                             new StructureTypeImplementationGenerator();
214                         DataType[] paramT = new DataType[1];
215                         paramT[0] = datatypes[i];
216                         structureTypeInterfaceGenerator.setInput(paramT);
217                         ByteArrayOutputStream JavaDoc entryOutputDataType =
218                             new ByteArrayOutputStream JavaDoc();
219                         structureTypeInterfaceGenerator.setOutput(
220                             entryOutputDataType);
221
222                         structureTypeInterfaceGenerator.generate();
223
224                         String JavaDoc entryNameStructure = path+
225                             datatypes[i].getName() + "Impl.java";
226                         writeEntry(entryNameStructure, entryOutputDataType);
227                         entryOutputDataType.close();
228                         
229                         logger.log(Level.FINE, entryNameStructure+" is generated");
230                     }
231                 }
232
233             }
234         }
235         out.finish();
236         logger.log(Level.FINE, " Generation is completed without error");
237     }
238
239 }
240
Popular Tags