KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.taskdefs.Ant;
27 import org.apache.tools.ant.taskdefs.Property;
28
29 /**
30  * For each specified location call ant task with specified target name.
31  * If target name is not specified than name of current target is used.
32  *
33  * @author Libor Kramolis
34  */

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

47     public EPForEach () {
48         locations = new ArrayList JavaDoc<String JavaDoc>();
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     //override setDir() to noop
67
public void setDir() {}
68     
69     private String JavaDoc target;
70     public String JavaDoc getTarget() {
71         return target;
72     }
73     public void setTarget(String JavaDoc v) {
74         target = v;
75     }
76     
77     private boolean inheritAll;
78     public boolean getInheritAll() {
79         return inheritAll;
80     }
81     public void setInheritAll(boolean v) {
82         inheritAll = v;
83     }
84     
85     /** Where cd first
86      */

87     public void setStartdir (String JavaDoc s) {
88         if ( DEBUG ) log ("SET startdir = " + s);
89
90         startdir = s;
91     }
92     
93     public void setSkipNonExistentDir(boolean v) {
94         skipNonExistentDir = v;
95     }
96     public boolean getSkipNonExistentDir() {
97         return skipNonExistentDir;
98     }
99     
100     /** the properties to pass to the new project */
101     private List JavaDoc<Property> properties = new ArrayList JavaDoc<Property>();
102
103     /**
104      * Property to pass to the new project.
105      * The property is passed as a 'user property'.
106      * @return the created <code>Property</code> object.
107      */

108     public Property createProperty() {
109         Property p = new Property();
110         p.setProject(getProject());
111         p.setTaskName("property");
112         properties.add(p);
113         return p;
114     }
115
116     /** Execute this task. */
117     public void execute () throws BuildException {
118         if (locations.isEmpty()) {
119             if (skipNonExistentDir) {
120                 if ( DEBUG ) {
121                     log("for-each: No locations no loops!");
122                 }
123                 return;
124             } else {
125                 throw new BuildException("You must set at least one location!", getLocation());
126             }
127         }
128
129         if ( getTarget() == null ) {
130             setTarget(this.getOwningTarget().getName());
131
132             if ( DEBUG ) log ("EXECUTE owningTarget = " + this.getOwningTarget());
133         }
134         File JavaDoc baseDir;
135         if ( startdir == null ) {
136             baseDir = getProject().getBaseDir();
137         } else {
138             baseDir = new File JavaDoc(getProject().getBaseDir(), startdir);
139         }
140
141         for (String JavaDoc dirName : locations) {
142             if ( ECHO ) log ("Process '" + dirName + "' location with '" + target + "' target ...");
143             
144             File JavaDoc dir = new File JavaDoc (baseDir, dirName);
145             File JavaDoc buildXml = new File JavaDoc(dir, "build.xml");
146             if (! buildXml.exists() && skipNonExistentDir) {
147                 if ( DEBUG ) log ("Skipped non-existent " + dir.getAbsolutePath());
148                 return;
149             }
150
151             String JavaDoc location = (new File JavaDoc(dir, "build.xml")).getAbsolutePath();
152             Ant ant = (Ant) getProject().createTask("ant");
153             ant.init();
154             ant.setLocation(getLocation());
155             ant.setAntfile(location);
156             ant.setTarget (getTarget());
157             ant.setInheritAll(getInheritAll());
158             for (Property t : properties) {
159                 Property p = ant.createProperty();
160                 
161                 p.setName(t.getName());
162                 p.setValue(t.getValue());
163                 p.execute();
164             }
165             if (getPropertyName() != null && getPropertyValue() != null) {
166                 org.apache.tools.ant.taskdefs.Property p = ant.createProperty();
167                 p.setName(getPropertyName());
168                 p.setValue(getPropertyValue());
169             }
170             if ( DEBUG ) log ("--> next [ " + target + " ] " + dir.getAbsolutePath());
171             
172             ant.execute();
173         }
174         
175     }
176     public void handleErrorOutput(String JavaDoc output) {
177         log(output);
178     }
179     
180     private String JavaDoc propertyName;
181     private String JavaDoc propertyValue;
182         
183     public String JavaDoc getPropertyName() {
184         return propertyName;
185     }
186     public void setPropertyName(String JavaDoc name) {
187         propertyName = name;
188     }
189     public String JavaDoc getPropertyValue() {
190         return propertyValue;
191     }
192     public void setPropertyValue(String JavaDoc v) {
193         propertyValue = v;
194     }
195
196 }
197
Popular Tags