View Javadoc
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-307 USA 21 22 23 Thanks to ACM S.L. for distributing this library under the LGPL license. 24 Contact info: jsr000@terra.es 25 Postal Address: c/Playa de Lagoa, 1 26 Urb. Valdecaba?as 27 Boadilla del monte 28 29 ****************************************************************************** 30 * 31 * Filename: $RCSfile: AntMakeUtils.java,v $ 32 * 33 * Author: Jose San Leandro Armend?riz 34 * 35 * Description: Provides some methods useful for managing AntMake-related 36 * logic. 37 * 38 * File version: $Revision: 1.9 $ 39 * 40 * Project version: $Name: $ 41 * ("Name" means no concrete version has been checked out) 42 * 43 * $Id: AntMakeUtils.java,v 1.9 2004/01/28 07:20:30 chous Exp $ 44 * 45 */ 46 package org.acmsl.antmake; 47 48 /* 49 * Importing project classes. 50 */ 51 import org.acmsl.antmake.AntMake; 52 import org.acmsl.antmake.AntMakeException; 53 import org.acmsl.antmake.FolderStructureHelper; 54 55 /* 56 * Importing some ACM-SL Commons classes. 57 */ 58 import org.acmsl.commons.utils.io.FileUtils; 59 import org.acmsl.commons.regexpplugin.Helper; 60 import org.acmsl.commons.regexpplugin.MalformedPatternException; 61 import org.acmsl.commons.regexpplugin.RegexpEngineNotFoundException; 62 import org.acmsl.commons.regexpplugin.RegexpManager; 63 64 /* 65 * Importing some Ant classes. 66 */ 67 import org.apache.tools.ant.DirectoryScanner; 68 import org.apache.tools.ant.Project; 69 import org.apache.tools.ant.Task; 70 import org.apache.tools.ant.types.FileSet; 71 import org.apache.tools.ant.types.Path; 72 73 /* 74 * Importing some JDK1.3 classes. 75 */ 76 import java.io.File; 77 import java.io.IOException; 78 import java.lang.ref.WeakReference; 79 import java.util.ArrayList; 80 import java.util.Collection; 81 import java.util.StringTokenizer; 82 83 /* 84 * Importing some Commons-Logging classes. 85 */ 86 import org.apache.commons.logging.LogFactory; 87 88 /*** 89 * Provides some methods useful for managing AntMake-related 90 * logic. 91 * @author <a href="mailto:jsanleandro@yahoo.es" 92 >Jose San Leandro Armend?riz</a> 93 * @version $Revision: 1.9 $ 94 */ 95 public abstract class AntMakeUtils 96 { 97 /*** 98 * Empty fileset array. 99 */ 100 public static final FileSet[] EMPTY_FILESET_ARRAY = 101 new FileSet[0]; 102 103 /*** 104 * Empty file array. 105 */ 106 public static final File[] EMPTY_FILE_ARRAY = new File[0]; 107 108 /*** 109 * An empty String array. 110 */ 111 protected static final String[] EMPTY_STRING_ARRAY = 112 AntMake.EMPTY_STRING_ARRAY; 113 114 /*** 115 * Singleton implemented as a weak reference. 116 */ 117 private static WeakReference m__Singleton; 118 119 /*** 120 * Specifies a new weak reference. 121 * @param utils the utils instance to use. 122 */ 123 protected static void setReference(AntMakeUtils utils) 124 { 125 m__Singleton = new WeakReference(utils); 126 } 127 128 /*** 129 * Retrieves the weak reference. 130 * @return such reference. 131 */ 132 protected static WeakReference getReference() 133 { 134 return m__Singleton; 135 } 136 137 /*** 138 * Retrieves a AntMakeUtils instance. 139 * @return such instance. 140 */ 141 public static AntMakeUtils getInstance() 142 { 143 AntMakeUtils result = null; 144 145 WeakReference reference = getReference(); 146 147 if (reference != null) 148 { 149 result = (AntMakeUtils) reference.get(); 150 } 151 152 if (result == null) 153 { 154 result = new AntMakeUtils() {}; 155 156 setReference(result); 157 } 158 159 return result; 160 } 161 162 /*** 163 * Protected constructor to avoid accidental instantiation. 164 */ 165 protected AntMakeUtils() {}; 166 167 /*** 168 * Translates given path to a collection of filesets. 169 * @param path the path to translate. 170 * @param inFilters the included file filters, for retrieving files 171 * through descendent folders recursively. 172 * @param outFilters the excluded file filters. 173 * @return the filesets. 174 */ 175 public FileSet[] toFileSets( 176 Path path, String[] inFilters, String[] outFilters) 177 { 178 Collection t_cResult = new ArrayList(); 179 180 if (path != null) 181 { 182 String[] t_astrLocations = path.list(); 183 184 if (t_astrLocations != null) 185 { 186 for (int t_iLocationIndex = 0; 187 t_iLocationIndex < t_astrLocations.length; 188 t_iLocationIndex++) 189 { 190 t_cResult.add( 191 toFileSet( 192 t_astrLocations[t_iLocationIndex], 193 path.getProject(), 194 inFilters, 195 outFilters)); 196 } 197 } 198 } 199 200 return (FileSet[]) t_cResult.toArray(EMPTY_FILESET_ARRAY); 201 } 202 203 /*** 204 * Builds a FileSet for given location, taking all specified 205 * file types. 206 * @param location the location. 207 * @param project the project. 208 * @param inFilters the included file filters, for retrieving files 209 * through descendent folders recursively. 210 * @param outFilters the excluded file filters. 211 * @return the fileset. 212 */ 213 public FileSet toFileSet( 214 String location, 215 Project project, 216 String[] inFilters, 217 String[] outFilters) 218 { 219 FileSet result = null; 220 221 if (location != null) 222 { 223 File t_Location = new File(location); 224 225 result = new FileSet(); 226 result.setProject(project); 227 228 if (t_Location.isDirectory()) 229 { 230 result.setDir(t_Location); 231 232 if ( (inFilters == null) 233 || (inFilters.length == 0)) 234 { 235 result.setIncludes("**/*"); 236 } 237 else 238 { 239 for (int t_iFilterIndex = 0; 240 t_iFilterIndex < inFilters.length; 241 t_iFilterIndex++) 242 { 243 result.setIncludes( 244 inFilters[t_iFilterIndex]); 245 } 246 } 247 248 if (outFilters != null) 249 { 250 for (int t_iFilterIndex = 0; 251 t_iFilterIndex < outFilters.length; 252 t_iFilterIndex++) 253 { 254 result.setExcludes( 255 outFilters[t_iFilterIndex]); 256 } 257 } 258 } 259 else if (t_Location.isFile()) 260 { 261 result.setIncludesfile(t_Location); 262 } 263 } 264 265 return result; 266 } 267 268 /*** 269 * Retrieves the paths of the files included by given fileset. 270 * @param fileSet the fileset. 271 * @param project the project. 272 * @return the included files. 273 */ 274 public String[] findIncludedFiles(FileSet fileSet, Project project) 275 { 276 String[] result = EMPTY_STRING_ARRAY; 277 278 if (fileSet != null) 279 { 280 DirectoryScanner t_DirectoryScanner = 281 fileSet.getDirectoryScanner(project); 282 283 if (t_DirectoryScanner != null) 284 { 285 result = t_DirectoryScanner.getIncludedFiles(); 286 } 287 } 288 289 return result; 290 } 291 292 /*** 293 * Retrieves the files contained in given filesets, and copies 294 * them to given folder. 295 * @param fileSets such filesets. 296 * @param folder the destination folder. 297 * @param task the Ant task. 298 * @return the files. 299 * @throws AntMakeException if there's some problem merging 300 * the files 301 */ 302 public File[] mergeFiles(FileSet[] fileSets, File folder, Task task) 303 throws AntMakeException 304 { 305 Collection t_cResult = new ArrayList(); 306 307 FolderStructureHelper t_Helper = 308 FolderStructureHelper.getInstance(); 309 310 FileUtils t_FileUtils = FileUtils.getInstance(); 311 312 if ( (folder == null) 313 || (!folder.isDirectory())) 314 { 315 throw new AntMakeException( 316 "Invalid destination folder: " + folder); 317 } 318 319 if ( (fileSets != null) 320 && (t_Helper != null) 321 && (t_FileUtils != null)) 322 { 323 for (int t_iFileSetIndex = 0; 324 t_iFileSetIndex < fileSets.length; 325 t_iFileSetIndex++) 326 { 327 DirectoryScanner t_DirectoryScanner = 328 fileSets[t_iFileSetIndex].getDirectoryScanner(task.getProject()); 329 330 File t_BaseDir = t_DirectoryScanner.getBasedir(); 331 332 String[] t_astrIncludedFiles = 333 t_DirectoryScanner.getIncludedFiles(); 334 335 File t_DestinationFolder = null; 336 File t_DestinationFile = null; 337 338 // Copy the files to the temporary base folder. 339 for (int t_iFileIndex = 0; 340 t_iFileIndex < t_astrIncludedFiles.length; 341 t_iFileIndex++) 342 { 343 t_DestinationFile = 344 new File( 345 folder.getAbsolutePath() 346 + File.separator 347 + t_astrIncludedFiles[t_iFileIndex]); 348 349 t_DestinationFolder = 350 t_DestinationFile.getParentFile(); 351 352 if (t_DestinationFolder != null) 353 { 354 t_DestinationFolder.mkdirs(); 355 356 t_DestinationFolder.mkdir(); 357 358 try 359 { 360 t_DestinationFile.createNewFile(); 361 362 t_FileUtils.copy( 363 new File( 364 t_BaseDir.getAbsolutePath() 365 + File.separator 366 + t_astrIncludedFiles[t_iFileIndex]), 367 t_DestinationFile); 368 369 t_cResult.add( 370 new File(t_astrIncludedFiles[t_iFileIndex])); 371 } 372 catch (IOException ioException) 373 { 374 task.getProject().log( 375 task, "" + ioException, Project.MSG_ERR); 376 377 t_DestinationFolder.delete(); 378 379 throw new AntMakeException( 380 "cannot copy file " 381 + t_BaseDir.getAbsolutePath() 382 + File.separator 383 + t_astrIncludedFiles[t_iFileIndex] 384 + " to " 385 + t_DestinationFolder.getAbsolutePath(), 386 ioException); 387 } 388 } 389 } 390 } 391 } 392 393 if ( (task != null) 394 && (t_cResult.size() > 0)) 395 { 396 task.getProject().log( 397 task, 398 "temporary folder's build process done", 399 Project.MSG_VERBOSE); 400 } 401 402 return (File[]) t_cResult.toArray(EMPTY_FILE_ARRAY); 403 } 404 405 /*** 406 * Retrieves the package name associated to given relative folder. 407 * @param folder the folder. 408 * @return the associated package name. 409 */ 410 public String toPackage(String folder) 411 { 412 String result = ""; 413 414 if (folder != null) 415 { 416 result = replaceAll(folder, File.separator, "."); 417 } 418 419 return result; 420 } 421 422 /*** 423 * Retrieves the leaf folder of to given path. 424 * @param path the path. 425 * @return the leaf folder. 426 */ 427 public String getLeafFolder(String path) 428 { 429 String result = ""; 430 431 if (path != null) 432 { 433 result = path; 434 435 String[] t_astrSubfolders = getSubfolders(path); 436 437 if ( (t_astrSubfolders != null) 438 && (t_astrSubfolders.length > 0)) 439 { 440 result = t_astrSubfolders[t_astrSubfolders.length - 1]; 441 } 442 } 443 444 return result; 445 } 446 447 /*** 448 * Retrieves the specific package associated to given relative 449 * folder. 450 * @param folder the folder. 451 * @return such package name. 452 */ 453 public String toSpecificPackage(String folder) 454 { 455 String result = ""; 456 457 if (folder != null) 458 { 459 result = folder; 460 461 String[] t_astrSubfolders = split(folder, File.separator); 462 463 if ( (t_astrSubfolders != null) 464 && (t_astrSubfolders.length > 0)) 465 { 466 result = t_astrSubfolders[t_astrSubfolders.length - 1]; 467 } 468 } 469 470 return result; 471 } 472 473 /*** 474 * Retrieves the relative folder associated to given package name. 475 * @param packageName the package name. 476 * @return the associated folder. 477 */ 478 public String toRelativeFolder(String packageName) 479 { 480 String result = ""; 481 482 <b>if (packageName != null) 483 { 484 result = replaceAll(packageName, "//.", File.separator); 485 } 486 487 return result; 488 } 489 490 /*** 491 * Replaces all ocurrences of a pattern with an alternative 492 * text inside given input. 493 * @param input the original text. 494 * @param pattern to identify what to replace. 495 * @param replacement the replacement. 496 * @return the text with replacements. 497 */ 498 public String replaceAll(String input, String pattern, String replacement) 499 { 500 String result = input; 501 502 if ( (input != null) 503 && (pattern != null) 504 && (replacement != null)) 505 { 506 Helper helper = createHelper(); 507 508 if (helper != null) 509 { 510 try 511 { 512 result = 513 helper.replaceAll(input, pattern, replacement); 514 } 515 catch (MalformedPatternException malformedPatternException) 516 { 517 LogFactory.getLog(AntMakeUtils.class).error( 518 "Invalid pattern", 519 malformedPatternException); 520 } 521 } 522 } 523 524 return result; 525 } 526 527 /*** 528 * Splits given path into its subfolders. 529 * @param path the path to split. 530 * @return the subfolders. 531 */ 532 public String[] getSubfolders(String path) 533 { 534 Collection t_cResult = new ArrayList(); 535 536 if (path != null) 537 { 538 String[] t_astrSubfolders = split(path, File.separator); 539 540 StringBuffer t_sbPreviousFolder = new StringBuffer(); 541 542 for (int t_iSubfolderIndex = 0; 543 t_iSubfolderIndex < t_astrSubfolders.length; 544 t_iSubfolderIndex++) 545 { 546 if (t_iSubfolderIndex > 0) 547 { 548 t_sbPreviousFolder.append(File.separator); 549 } 550 551 t_sbPreviousFolder.append( 552 t_astrSubfolders[t_iSubfolderIndex]); 553 554 t_cResult.add(t_sbPreviousFolder.toString()); 555 } 556 } 557 558 return (String[]) t_cResult.toArray(EMPTY_STRING_ARRAY); 559 } 560 561 /*** 562 * Divides given input into tokens delimited by given 563 * separator. 564 * @param input the input. 565 * @return its tokens. 566 */ 567 public String[] split(String input, String separator) 568 { 569 Collection t_cResult = new ArrayList(); 570 571 if ( (input != null) 572 && (separator != null)) 573 { 574 StringTokenizer t_InputTokenizer = 575 new StringTokenizer(input, separator, false); 576 577 while (t_InputTokenizer.hasMoreTokens()) 578 { 579 t_cResult.add(t_InputTokenizer.nextToken()); 580 } 581 } 582 583 return (String[]) t_cResult.toArray(EMPTY_STRING_ARRAY); 584 } 585 586 /*** 587 * Creates a helper instance. 588 * @return such instance. 589 */ 590 protected Helper createHelper() 591 { 592 Helper result = null; 593 594 try 595 { 596 result = RegexpManager.createHelper(); 597 } 598 catch (RegexpEngineNotFoundException regexpEngineNotFoundException) 599 { 600 LogFactory.getLog(AntMakeUtils.class).fatal( 601 "Cannot create regexp helper", 602 regexpEngineNotFoundException); 603 } 604 605 return result; 606 } 607 608 /*** 609 * Logs a concrete message using given task. 610 * @param task the task to use to log the message. 611 * @param message the message to log. 612 * @param level the log level the message is associated to. 613 */ 614 public void log(Task task, String message, int level) 615 { 616 if (task != null) 617 { 618 Project t_Project = task.getProject(); 619 620 if (t_Project != null) 621 { 622 t_Project.log(task, message, level); 623 } 624 } 625 } 626 }

This page was automatically generated by Maven