1 19 package org.netbeans.nbbuild; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.util.Hashtable ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.StringTokenizer ; 31 import org.apache.tools.ant.*; 32 import org.apache.tools.ant.types.*; 33 34 38 public class ShorterPaths extends Task { 39 40 41 43 public static class Replacement { 44 String name; 45 File dir; 46 47 public void setName(String name) { 48 this.name = name; 49 } 50 public void setDir(File dir) { 51 this.dir = dir; 52 } 53 } 54 private List <Replacement> replacements = new LinkedList <Replacement>(); public Replacement createReplacement() { 56 Replacement r = new Replacement(); 57 replacements.add(r); 58 return r; 59 } 60 public void addReplacement(Replacement r) { 62 replacements.add(r); 63 } 64 68 69 private Path in; 70 public void setIn(Path p) { 71 if (in == null) { 72 in = p.createPath(); 73 } 74 in.append(p); 75 } 76 public Path createIn () { 77 if (in == null) { 78 in = new Path(getProject()); 79 } 80 return in; 81 } 82 public void setinRef(Reference r) { 83 createIn().setRefid(r); 84 } 85 93 String out; 94 public void setOut(String out) { 95 this.out = out; 96 } 97 98 String extraLibs; 99 public void setExtraLibs(String extraLibs) { 100 this.extraLibs = extraLibs; 101 } 102 File extraLibsDir; 103 public void setExtraLibsDir(File extraLibsDir) { 104 this.extraLibsDir = extraLibsDir; 105 } 106 107 File testProperties; 108 public void setTestProperties(File testProperties) { 109 this.testProperties = testProperties; 110 } 111 112 113 114 public void execute() throws BuildException { 115 String paths[] = in.list(); 117 StringBuffer nbLibBuff = new StringBuffer () ; 118 StringBuffer externalLibBuf = new StringBuffer (); 120 try { 121 for (int i = 0 ; i < paths.length ; i++) { 122 String path = paths[i]; 123 File file = new File (path); 124 if (file.exists()) { 126 path = file.getCanonicalPath(); 128 simplyPath(path, externalLibBuf, nbLibBuff); 129 } else { 130 log("Path element "+ file + " doesn't exist.",Project.MSG_VERBOSE); 131 } 132 } 133 if (out != null) { 134 define(out, nbLibBuff.toString()); 135 } 136 if (this.extraLibs != null) { 137 define(extraLibs,externalLibBuf.toString()); 138 } 139 140 if (testProperties != null) { 141 PrintWriter pw = new PrintWriter (testProperties); 143 144 String extraProp = "test-unit-sys-prop"; 146 Hashtable properties = getProject().getProperties(); 147 StringBuffer outProp = new StringBuffer (); 148 for (Iterator it = properties.keySet().iterator(); it.hasNext();) { 149 String name = (String ) it.next(); 150 if (name.startsWith(extraProp)) { 151 if (name.equals("test-unit-sys-prop.xtest.data")) { 152 continue; 154 } 155 outProp.setLength(0); 157 StringTokenizer tokenizer = new StringTokenizer (properties.get(name).toString(), ":;"); 158 String nextToken = null; 159 while (nextToken != null || tokenizer.hasMoreTokens()) { 160 String token = nextToken ; 161 nextToken = null; 162 if (token == null) { 163 token = tokenizer.nextToken(); 164 } 165 if (tokenizer.hasMoreTokens()) { 166 nextToken = tokenizer.nextToken(); 167 } 168 String path = token + ":" + nextToken; 170 if (new File (path).exists()) { 171 nextToken = null; 172 } else { 173 path = token; 174 } 175 176 simplyPath(path,externalLibBuf,outProp); 177 } 178 pw.println(name + "=" + outProp); 179 } 180 } 181 pw.println("extra.test.libs.dir=" + externalLibBuf.toString()); 182 pw.println("test.unit.run.cp=" + nbLibBuff.toString()); 183 pw.close(); 184 } 185 } catch (IOException ex) { 186 throw new BuildException(ex); 187 } 188 } 189 190 private void simplyPath(String path, final StringBuffer externalLibBuf, final StringBuffer nbLibBuff) throws IOException { 191 boolean bAppend = false; 192 File file = new File (path); 193 if (file.exists()) { 194 path = file.getAbsolutePath(); 197 for (Replacement repl: replacements) { 198 String dirCan = repl.dir.getCanonicalPath(); 199 if (path.startsWith(dirCan)) { 200 if (nbLibBuff.length() > 0 ) { 201 nbLibBuff.append(":\\\n"); 202 } 203 204 nbLibBuff.append("${" + repl.name + "}"); 205 nbLibBuff.append(path.substring(dirCan.length()).replace(File.separatorChar,'/')); 207 bAppend = true; 208 break; 209 } 210 } 211 if (!bAppend) { 212 String fName = copyExtraLib(path); 213 if (fName != null) { 214 if (externalLibBuf.length() > 0 ) { 215 externalLibBuf.append(":\\\n"); 216 } 217 externalLibBuf.append("${extra.test.libs}/" + fName); 218 } 219 } 220 221 } else { 222 if (nbLibBuff.length() > 0 ) { 223 nbLibBuff.append(":\\\n"); 224 } 225 nbLibBuff.append(path); 226 } 227 228 } 229 230 private void define(String prop, String val) { 231 log("Setting " + prop + "=" + val, Project.MSG_VERBOSE); 232 String old = getProject().getProperty(prop); 233 if (old != null && !old.equals(val)) { 234 getProject().log("Warning: " + prop + " was already set to " + old, Project.MSG_WARN); 235 } 236 getProject().setNewProperty(prop, val); 237 } 238 239 private String copyExtraLib(String path) throws IOException { 240 String name = null; 241 File file = new File (path); 242 if (this.extraLibsDir != null && extraLibsDir.isDirectory() && file.isFile()) { 243 244 name = file.getName(); 245 byte buff[] = new byte[100000]; 246 FileInputStream fis = new FileInputStream (path); 247 FileOutputStream fos = new FileOutputStream (new File (extraLibsDir,name)); 248 int size = 0; 249 while ((size = fis.read(buff)) > 0 ) { 250 fos.write(buff,0,size); 251 } 252 fos.close(); 253 fis.close(); 254 } 255 return name; 256 } 257 258 259 } 260 261 262 | Popular Tags |