KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > ForEach


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.nbbuild;
20
21 import java.io.File JavaDoc;
22 import java.lang.String JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.taskdefs.Ant;
28 import org.apache.tools.ant.Task;
29
30 /**
31  * For each specified location call ant task with specified target name.
32  * If target name is not specified than name of current target is used.
33  *
34  * @author Libor Kramolis
35  */

36 public class ForEach extends Task {
37     private static final boolean DEBUG = false;
38     private static final boolean ECHO = true;
39
40     private List JavaDoc<String JavaDoc> locations;
41     private String JavaDoc target;
42     private String JavaDoc startdir;
43     
44     //
45
// init
46
//
47

48     public ForEach () {
49     }
50
51     //
52
// itself
53
//
54

55     /** Comma separated list of locations. */
56     public void setLocations (String JavaDoc s) {
57         if ( DEBUG ) log ("SET locations = " + s);
58
59         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc (s, ",");
60         locations = new ArrayList JavaDoc<String JavaDoc>();
61         while ( tok.hasMoreTokens() ) {
62             locations.add (tok.nextToken().trim());
63         }
64     }
65     
66     /** Name of target which will be used with ant task. If not specified,
67      * owning target name is used.
68      */

69     public void setTarget (String JavaDoc s) {
70         if ( DEBUG ) log ("SET target = " + s);
71
72         target = s;
73     }
74
75     /** Where cd first
76      */

77     public void setStartdir (String JavaDoc s) {
78         if ( DEBUG ) log ("SET startdir = " + s);
79
80         startdir = s;
81     }
82     
83     /** Execute this task. */
84     public void execute () throws BuildException {
85         if (locations == null) {
86             throw new BuildException("You must set at least one location!", getLocation());
87         }
88
89         if ( target == null ) {
90             target = this.getOwningTarget().getName();
91
92             if ( DEBUG ) log ("EXECUTE owningTarget = " + this.getOwningTarget());
93         }
94         File JavaDoc baseDir;
95     if ( startdir == null ) {
96             baseDir = getProject().getBaseDir();
97         } else {
98             baseDir = new File JavaDoc(getProject().getBaseDir(), startdir);
99         }
100
101         for (String JavaDoc dirName : locations) {
102             if ( ECHO ) log ("Process '" + dirName + "' location with '" + target + "' target ...");
103             
104             Ant ant = (Ant) getProject().createTask("ant");
105             ant.init();
106             ant.setLocation(getLocation());
107             
108             File JavaDoc dir = new File JavaDoc (baseDir, dirName);
109             ant.setDir (dir);
110             ant.setTarget (target);
111             
112             if ( DEBUG ) log ("--> next [ " + target + " ] " + dir.getAbsolutePath());
113             
114             ant.execute();
115         }
116     }
117     
118 }
119
Popular Tags