KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > corba > generator > dependencies > lib > CIDLFileDependenciesResolver


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Demarey.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.corba.generator.dependencies.lib;
28
29 // Package dependencies.
30
import org.objectweb.openccm.ast.api.DeclarationKind;
31 import org.objectweb.openccm.ast.api.CompositionDecl;
32 import org.objectweb.openccm.ast.api.HomeExecutorDecl;
33 import org.objectweb.openccm.ast.api.ExecutorDecl;
34 import org.objectweb.openccm.ast.api.Scope;
35 import org.objectweb.openccm.generator.common.lib.GenerationException;
36 import java.util.Set JavaDoc;
37 import java.util.TreeSet JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.io.File JavaDoc;
40 import java.io.FileWriter JavaDoc;
41
42 /**
43  * This class allows you to generate class file dependencies
44  * for a CIDL Composition.
45  *
46  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
47  *
48  * @version 0.1
49  */

50
51 public class CIDLFileDependenciesResolver
52      extends org.objectweb.openccm.generator.common.lib.GeneratorBase
53   implements org.objectweb.corba.generator.dependencies.api.CIDLFileDependenciesResolver
54 {
55     // ==================================================================
56
//
57
// Internal state.
58
//
59
// ==================================================================
60

61     /** Delimiter constant */
62     private static final String JavaDoc delimiter =
63     "==================================================================\n";
64
65     /** Class to resolve CIDL declarations dependencies. */
66     org.objectweb.corba.generator.dependencies.api.CIDLDeclarationDependencies _resolver;
67
68     /** Filters to apply to dependencies.
69         A dependencie that starts with <filter> */

70     String JavaDoc[] _filters;
71
72     // ==================================================================
73
//
74
// Constructor.
75
//
76
// ==================================================================
77

78     /**
79      * The default constructor.
80      *
81      * @param ast - The Abstract Syntax Tree.
82      */

83     public
84     CIDLFileDependenciesResolver(org.objectweb.openccm.ast.api.AST ast)
85     {
86         super(ast);
87
88         // Init internal states
89
_resolver = new org.objectweb.corba.generator.dependencies.lib.CIDLDeclarationDependencies();
90         _filters = null;
91     }
92
93     // ==================================================================
94
//
95
// Internal methods.
96
//
97
// ==================================================================
98

99     /**
100      * Apply filters to input.
101      * If input starts with one of filters, it won't be added.
102      *
103      * @param input - The string to check.
104      *
105      * @return input if filters don't match, else an empty string.
106      */

107     private String JavaDoc
108     applyFilers(String JavaDoc input)
109     {
110         if (_filters == null)
111             return input;
112
113         for (int i=0; i<_filters.length; i++)
114         {
115             if ( input.startsWith(_filters[i]) )
116                 return "";
117         }
118
119         return input;
120     }
121
122     /**
123      * Add Component Executor File dependencies to output.
124      *
125      * @param output - The output stream to write in.
126      * @param exec - The Component executor declaration.
127      *
128      * @return The list of dependencies as a set of Files.
129      */

130     private void
131     addExecutorDependencies( FileWriter JavaDoc output,
132                              ExecutorDecl exec )
133     throws java.io.IOException JavaDoc
134     {
135         Set JavaDoc files = null;
136         Iterator JavaDoc it = null;
137
138         files = _resolver.getExecutorDependencies(exec);
139         it = (new TreeSet JavaDoc(files)).iterator();
140
141         output.write("# " + delimiter);
142         output.write("# Component " + exec.getAbsoluteName() + "\n");
143         output.write("# " + delimiter);
144
145         while ( it.hasNext() )
146         {
147             output.write( applyFilers(it.next() + "\n") );
148         }
149         output.write("\n");
150     }
151
152     /**
153      * Add Home ExecutorFile dependencies to output.
154      *
155      * @param output - The output stream to write in.
156      * @param home_exec - The home declaration.
157      *
158      * @throws java.io.IOException if a I/O error occurs.
159      */

160     private void
161     addHomeExecutorDependencies( FileWriter JavaDoc output,
162                                  HomeExecutorDecl home_exec )
163     throws java.io.IOException JavaDoc
164     {
165         Set JavaDoc files = null;
166         Iterator JavaDoc it = null;
167
168         files = _resolver.getHomeExecutorDependencies(home_exec);
169         it = (new TreeSet JavaDoc(files)).iterator();
170
171         output.write("# " + delimiter);
172         output.write("# Home " + home_exec.getAbsoluteName() + "\n");
173         output.write("# " + delimiter);
174
175         while ( it.hasNext() )
176         {
177             output.write( applyFilers(it.next() + "\n") );
178         }
179         output.write("\n");
180     }
181
182     /**
183      * Add Composition File dependencies to output.
184      *
185      * @param output - The output stream to write in.
186      * @param composition - The composition declaration.
187      *
188      * @throws java.io.IOException if a I/O error occurs.
189      */

190     private void
191     addCompositionDependencies(FileWriter JavaDoc output,
192                                CompositionDecl composition)
193     throws java.io.IOException JavaDoc
194     {
195         org.objectweb.openccm.ast.utils.api.CompositionInfo comp = null;
196
197         comp = new org.objectweb.openccm.ast.utils.lib.CompositionInfo(composition);
198
199         output.write("# " + delimiter);
200         output.write("# Package for Composition :\n");
201         output.write("# " + composition.getAbsoluteName() + "\n");
202         output.write("# " + delimiter);
203         output.write("\n");
204
205         // Add component executor dependencies
206
addExecutorDependencies(output, comp.getComponentExecutor());
207
208         // Add home executor dependencies
209
addHomeExecutorDependencies(output, comp.getHomeExecutor());
210     }
211
212     // ==================================================================
213
//
214
// Public methods for org.objectweb.openccm.generator.cidl.api.CIDLFileDependenciesResolver
215
//
216
// ==================================================================
217

218     /**
219      * Set filters for class dependencies.
220      *
221      * @param filters - The list of filters to add.
222      */

223     public void
224     set_filters(String JavaDoc[] filters)
225     {
226         _filters = filters;
227     }
228
229     /**
230      * Generate class files dependencies for a composition.
231      *
232      * @param composition - The composition declaration.
233      * @param file - The name of the file to write dependencies in.
234      *
235      * @throws GenerationException if an exception occurs.
236      */

237     public void
238     generate_composition_dependencies(CompositionDecl composition,
239                                       String JavaDoc file)
240     {
241         java.util.Iterator JavaDoc it = null;
242         File JavaDoc output_file = null;
243         FileWriter JavaDoc output = null;
244         int i = 0;
245
246         try
247         {
248             // Check output file name
249
output_file = new File JavaDoc(file);
250             while ( output_file.exists() )
251             {
252                 output_file = new File JavaDoc( file + String.valueOf(i) );
253                 i++;
254             }
255             // Create the file and its directory tree
256
int index = output_file.getPath().lastIndexOf( System.getProperty("file.separator") );
257             if (index != -1)
258             {
259                 File JavaDoc dir = new File JavaDoc( output_file.getPath().substring(0, index+1) );
260                 dir.mkdirs();
261             }
262             output_file.createNewFile();
263
264             // Add dependencies to this file
265
output = new FileWriter JavaDoc(output_file);
266             addCompositionDependencies(output, composition);
267             output.close();
268             System.out.println("File '" + output_file.getAbsolutePath() + "' has been generated");
269         }catch(java.io.IOException JavaDoc ex){
270             ex.printStackTrace();
271         }
272     }
273
274     /**
275      * Generate class files dependencies for a composition.
276      *
277      * @param name - The name of the composition.
278      * @param file - The name of the file to write dependencies in.
279      *
280      * @throws GenerationException if an exception occurs.
281      */

282     public void
283     generate_composition_dependencies(String JavaDoc name, String JavaDoc file)
284     throws GenerationException
285     {
286         CompositionDecl composition = null;
287
288         // Get the composition declaration
289
composition = (CompositionDecl) getDeclaration(name);
290         generate_composition_dependencies(composition, file);
291     }
292
293     /**
294      * Generate class files dependencies for all composition
295      * contained in this scope.
296      *
297      * @param filescope - The source AST FileScope returned by the compile() method.
298      * @param base_dir - The base directory for dependencies output.
299      *
300      * @throws GenerationException if an exception occurs.
301      */

302     public void
303     generate_dependencies(Scope scope,
304                           String JavaDoc base_dir)
305     throws GenerationException
306     {
307         java.util.List JavaDoc vect = null;
308         CompositionDecl[] compositions = null;
309         String JavaDoc filename = null;
310
311         vect = getAllDeclarations(scope, DeclarationKind.dk_composition);
312         compositions = (CompositionDecl[])vect.toArray(new CompositionDecl[0]);
313         for (int i=0; i<compositions.length; i++)
314         {
315             filename = base_dir + "/" + compositions[i].getName() + ".dep";
316             generate_composition_dependencies(compositions[i], filename);
317         }
318     }
319
320     // ==================================================================
321
//
322
// Public methods.
323
//
324
// ==================================================================
325

326 }
327
Popular Tags