1 19 package org.netbeans.nbbuild; 20 21 import java.io.File ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.StringTokenizer ; 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 35 public class EPForEach extends Ant { 36 private static final boolean DEBUG = false; 37 private static final boolean ECHO = true; 38 39 private List <String > locations; 40 private String startdir; 41 private boolean skipNonExistentDir = false; 42 43 47 public EPForEach () { 48 locations = new ArrayList <String >(); 49 } 50 51 55 56 public void setLocations (String s) { 57 if ( DEBUG ) log ("SET locations = " + s); 58 59 StringTokenizer tok = new StringTokenizer (s, ",:"); 60 locations = new ArrayList <String >(); 61 while ( tok.hasMoreTokens() ) { 62 locations.add (tok.nextToken().trim()); 63 } 64 } 65 66 public void setDir() {} 68 69 private String target; 70 public String getTarget() { 71 return target; 72 } 73 public void setTarget(String 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 87 public void setStartdir (String 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 101 private List <Property> properties = new ArrayList <Property>(); 102 103 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 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 baseDir; 135 if ( startdir == null ) { 136 baseDir = getProject().getBaseDir(); 137 } else { 138 baseDir = new File (getProject().getBaseDir(), startdir); 139 } 140 141 for (String dirName : locations) { 142 if ( ECHO ) log ("Process '" + dirName + "' location with '" + target + "' target ..."); 143 144 File dir = new File (baseDir, dirName); 145 File buildXml = new File (dir, "build.xml"); 146 if (! buildXml.exists() && skipNonExistentDir) { 147 if ( DEBUG ) log ("Skipped non-existent " + dir.getAbsolutePath()); 148 return; 149 } 150 151 String location = (new File (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 output) { 177 log(output); 178 } 179 180 private String propertyName; 181 private String propertyValue; 182 183 public String getPropertyName() { 184 return propertyName; 185 } 186 public void setPropertyName(String name) { 187 propertyName = name; 188 } 189 public String getPropertyValue() { 190 return propertyValue; 191 } 192 public void setPropertyValue(String v) { 193 propertyValue = v; 194 } 195 196 } 197 | Popular Tags |