1 /*
2 AntMake
3
4 Copyright (C) 2004 Jose San Leandro Armend?riz
5 jsanleandro@yahoo.es
6 chousz@yahoo.com
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Thanks to ACM S.L. for distributing this library under the GPL license.
23 Contact info: jsr000@terra.es
24 Postal Address: c/Playa de Lagoa, 1
25 Urb. Valdecaba?as
26 Boadilla del monte
27 28660 Madrid
28 Spain
29
30 ******************************************************************************
31 *
32 * Filename: $RCSfile: FolderStructureHelper.java,v $
33 *
34 * Author: Jose San Leandro Armend?riz
35 *
36 * Description: Provides some useful methods for managing the folder
37 * structure of a typical GNU Autotools-based Java project.
38 *
39 * Last modified by: $Author: chous $ at $Date: 2004/02/01 17:21:55 $
40 *
41 * File version: $Revision: 1.5 $
42 *
43 * Project version: $Name: $
44 *
45 * $Id: FolderStructureHelper.java,v 1.5 2004/02/01 17:21:55 chous Exp $
46 *
47 */
48 package org.acmsl.antmake;
49
50 /*
51 * Importing project classes.
52 */
53 import org.acmsl.antmake.AntMakeException;
54
55 /*
56 * Importing some ACM-SL Commons classes.
57 */
58 import org.acmsl.commons.utils.io.FileUtils;
59
60 /*
61 * Importing some Ant classes.
62 */
63 import org.apache.tools.ant.Project;
64 import org.apache.tools.ant.Task;
65
66 /*
67 * Importing some JDK1.3 classes.
68 */
69 import java.io.BufferedWriter;
70 import java.io.File;
71 import java.io.FileNotFoundException;
72 import java.io.FileWriter;
73 import java.io.InputStream;
74 import java.io.InputStreamReader;
75 import java.io.IOException;
76 import java.io.LineNumberReader;
77 import java.io.PrintWriter;
78 import java.lang.ref.WeakReference;
79
80 /***
81 * Provides some useful methods for managing the folder
82 * structure of a typical GNU Autotools-based Java project.
83 * @author <a href="mailto:jsanleandro@yahoo.es"
84 >Jose San Leandro Armend?riz</a>
85 * @version $Revision: 1.5 $
86 */
87 public abstract class FolderStructureHelper
88 {
89 /***
90 * Singleton implemented as a weak reference.
91 */
92 private static WeakReference singleton;
93
94 /***
95 * Specifies a new weak reference.
96 * @param helper the helper instance to use.
97 */
98 protected static void setReference(FolderStructureHelper helper)
99 {
100 singleton = new WeakReference(helper);
101 }
102
103 /***
104 * Retrieves the weak reference.
105 * @return such reference.
106 */
107 protected static WeakReference getReference()
108 {
109 return singleton;
110 }
111
112 /***
113 * Retrieves a FolderStructureHelper instance.
114 * @return such instance.
115 */
116 public static FolderStructureHelper getInstance()
117 {
118 FolderStructureHelper result = null;
119
120 WeakReference reference = getReference();
121
122 if (reference != null)
123 {
124 result = (FolderStructureHelper) reference.get();
125 }
126
127 if (result == null)
128 {
129 result = new FolderStructureHelper() {};
130
131 setReference(result);
132 }
133
134 return result;
135 }
136
137 /***
138 * Protected constructor to avoid accidental instantiation.
139 */
140 protected FolderStructureHelper() {};
141
142 /***
143 * Creates the basic folder structure starting from given
144 * folder.
145 * @param newBaseFolder the new base folder.
146 * @param oldBaseFolder the old base folder.
147 * @param files the files to include, which have to be relative to
148 * <i>oldBaseFolder</i>.
149 * @param task to be able to log messages.
150 * @throws AntMakeException if the structure cannot be created
151 * for some reason.
152 */
153 public void createFolderStructure(
154 File newBaseFolder, File oldBaseFolder, File[] files, Task task)
155 throws AntMakeException
156 {
157 if (newBaseFolder != null)
158 {
159 if (!newBaseFolder.isDirectory())
160 {
161 newBaseFolder = newBaseFolder.getParentFile();
162 }
163 }
164
165 FileUtils t_FileUtils = FileUtils.getInstance();
166
167 if ( (newBaseFolder != null)
168 && (oldBaseFolder != null)
169 && (files != null)
170 && (t_FileUtils != null))
171 {
172 for (int t_iFileIndex = 0;
173 t_iFileIndex < files.length;
174 t_iFileIndex++)
175 {
176 // basically, we need to copy the file from oldBaseFolder
177 // to the newBaseFolder.
178 try
179 {
180 File t_OldFile =
181 new File(
182 oldBaseFolder.getAbsolutePath()
183 + File.separator
184 + files[t_iFileIndex]);
185
186 File t_NewFile =
187 new File(
188 newBaseFolder.getAbsolutePath()
189 + File.separator
190 + files[t_iFileIndex]);
191
192 File t_Folder =
193 t_NewFile.getParentFile();
194
195 t_Folder.mkdirs();
196
197 t_NewFile.createNewFile();
198
199 if (task != null)
200 {
201 task.getProject().log(
202 task,
203 "Copying " + t_OldFile + " to " + t_NewFile,
204 Project.MSG_VERBOSE);
205 }
206
207 t_FileUtils.copy(
208 t_OldFile, t_NewFile);
209 }
210 catch (FileNotFoundException fileNotFoundException)
211 {
212 throw new AntMakeException(
213 "Could not copy "
214 + oldBaseFolder.getAbsolutePath()
215 + File.separator
216 + files[t_iFileIndex],
217 fileNotFoundException);
218 }
219 catch (IOException ioException)
220 {
221 throw new AntMakeException(
222 "Could not copy "
223 + oldBaseFolder.getAbsolutePath()
224 + File.separator
225 + files[t_iFileIndex],
226 ioException);
227 }
228 }
229 }
230 }
231
232 /***
233 * Creates the basic folder structure starting from given
234 * folder and packages.
235 * @param baseFolder the base folder.
236 * @param packageNames the package names.
237 */
238 protected void createFolderStructure(File baseFolder, String[] packageNames)
239 {
240 if (baseFolder != null)
241 {
242 if (!baseFolder.isDirectory())
243 {
244 baseFolder = baseFolder.getParentFile();
245 }
246 }
247
248 if ( (baseFolder != null)
249 && (packageNames != null))
250 {
251 // basically, we need to create the folders for the Java packages.
252 for (int packageIndex = 0;
253 packageIndex < packageNames.length;
254 packageIndex++)
255 {
256 new File(
257 baseFolder,
258 FileUtils.getInstance().packageToPath(
259 packageNames[packageIndex])).mkdirs();
260 }
261 }
262 }
263
264 /***
265 * Creates a temporary folder.
266 * @return such folder.
267 * @throws IOException if the folder cannot be created.
268 */
269 public File createTempFolder()
270 throws IOException
271 {
272 File result = File.createTempFile("antmake", "_temp");
273
274 String t_strPath = result.getAbsolutePath();
275
276 result.delete();
277
278 result = new File(t_strPath);
279
280 result.mkdirs();
281
282 result.mkdir();
283
284 return result;
285 }
286
287 /***
288 * Copies a file from the classpath to given
289 * location.
290 * @param fileName the file name.
291 * @param destination the destination folder.
292 * @param task the Ant task to print error messages.
293 * @param classLoader the optional class loader.
294 * @throws IOException if an abnormal situation takes place.
295 */
296 public void copy(
297 String fileName, File destination, Task task, ClassLoader classLoader)
298 throws IOException
299 {
300 if ( (fileName != null)
301 && (destination != null)
302 && (destination.isDirectory()))
303 {
304 IOException exception = null;
305
306 InputStream t_isInputFile = null;
307
308 InputStreamReader t_isrFileReader = null;
309
310 LineNumberReader t_LineNumberReader = null;
311
312 FileWriter t_FileWriter = null;
313
314 PrintWriter t_pwOutputWriter = null;
315
316 Project project = null;
317
318 if (task != null)
319 {
320 project = task.getProject();
321 }
322
323 try
324 {
325 t_isInputFile =
326 findResource(fileName, classLoader);
327
328 if (t_isInputFile == null)
329 {
330 if (project != null)
331 {
332 ClassLoader t_ClassLoader = project.getCoreLoader();
333
334 if (t_ClassLoader != null)
335 {
336 t_isInputFile =
337 findResource(fileName, t_ClassLoader);
338 }
339 }
340 }
341
342 if (t_isInputFile == null)
343 {
344 t_isInputFile = findResource(fileName, null);
345 }
346
347 if (t_isInputFile == null)
348 {
349 t_isInputFile =
350 getClass().getResourceAsStream(fileName);
351
352 if (t_isInputFile == null)
353 {
354 t_isInputFile =
355 getClass().getResourceAsStream(
356 File.separator + fileName);
357 }
358 }
359
360 if (t_isInputFile != null)
361 {
362 t_isrFileReader = new InputStreamReader(t_isInputFile);
363
364 t_LineNumberReader =
365 new LineNumberReader(t_isrFileReader);
366
367 t_FileWriter =
368 new FileWriter(
369 destination.getAbsolutePath()
370 + File.separator
371 + fileName);
372
373 t_pwOutputWriter =
374 new PrintWriter(
375 new BufferedWriter(t_FileWriter));
376
377 destination.mkdirs();
378 }
379 else
380 {
381 exception = new IOException(fileName + " not found");
382 }
383 }
384 catch (IOException ioException)
385 {
386 if (exception != null)
387 {
388 if (project != null)
389 {
390 project.log(
391 task,
392 "cannot copy file " + fileName
393 + " (" + ioException + ")",
394 Project.MSG_ERR);
395 }
396 }
397 else
398 {
399 exception = ioException;
400 }
401 }
402 finally
403 {
404 if ( (exception == null)
405 && (t_LineNumberReader != null)
406 && (t_pwOutputWriter != null))
407 {
408 try
409 {
410 String t_strCurrentLine =
411 t_LineNumberReader.readLine();
412
413 while (t_strCurrentLine != null)
414 {
415 t_pwOutputWriter.println(t_strCurrentLine);
416
417 t_strCurrentLine = t_LineNumberReader.readLine();
418 }
419 }
420 catch (IOException ioException)
421 {
422 if (exception != null)
423 {
424 if (project != null)
425 {
426 project.log(
427 task,
428 "cannot copy file " + fileName
429 + " (" + ioException + ")",
430 Project.MSG_ERR);
431 }
432 }
433 else
434 {
435 exception = ioException;
436
437 }
438 }
439 }
440
441 if (t_pwOutputWriter != null)
442 {
443 t_pwOutputWriter.flush();
444 t_pwOutputWriter.close();
445 }
446
447 try
448 {
449 if (t_FileWriter != null)
450 {
451 t_FileWriter.close();
452 }
453 }
454 catch (IOException ioException)
455 {
456 if (exception != null)
457 {
458 if (project != null)
459 {
460 project.log(
461 task,
462 "cannot close output file "
463 + destination.getAbsolutePath()
464 + File.separator
465 + fileName
466 + " (" + ioException + ")",
467 Project.MSG_ERR);
468 }
469 }
470 else
471 {
472 exception = ioException;
473 }
474 }
475
476 try
477 {
478 if (t_LineNumberReader != null)
479 {
480 t_LineNumberReader.close();
481 }
482 }
483 catch (IOException ioException)
484 {
485 if (exception != null)
486 {
487 if (project != null)
488 {
489 project.log(
490 task,
491 "cannot close input file "
492 + fileName
493 + " (" + ioException + ")",
494 Project.MSG_ERR);
495 }
496 }
497 else
498 {
499 exception = ioException;
500 }
501 }
502 finally
503 {
504 try
505 {
506 if (t_isrFileReader != null)
507 {
508 t_isrFileReader.close();
509 }
510 }
511 catch (IOException ioException)
512 {
513 if (exception != null)
514 {
515 if (project != null)
516 {
517 project.log(
518 task,
519 "cannot close input file "
520 + fileName
521 + " (" + ioException + ")",
522 Project.MSG_ERR);
523 }
524 }
525 else
526 {
527 exception = ioException;
528 }
529 }
530 }
531 }
532
533 if (exception != null)
534 {
535 throw exception;
536 }
537 }
538 }
539
540 /***
541 * Retrieves the input stream to read a resource from the
542 * classpath visible to given class loader.
543 * @param resource the resource to find.
544 * @param classLoader (<i>optional</i>) the class loader.
545 * @return such input stream, or <code>null</code> if the
546 * resource cannot be found.
547 */
548 public InputStream findResource(String resource, ClassLoader classLoader)
549 {
550 InputStream result = null;
551
552 if (resource != null)
553 {
554 if (classLoader == null)
555 {
556 classLoader = getClass().getClassLoader();
557 }
558
559 if (classLoader != null)
560 {
561 result = classLoader.getResourceAsStream(resource);
562 }
563
564 if (result == null)
565 {
566 result =
567 classLoader.getResourceAsStream(File.separator + resource);
568 }
569 }
570
571 return result;
572 }
573 }
This page was automatically generated by Maven