1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.DirectoryScanner; 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.Task; 28 import org.apache.tools.ant.taskdefs.Zip; 29 import org.apache.tools.ant.types.ZipFileSet; 30 import org.apache.tools.ant.types.selectors.SelectorUtils; 31 32 36 public final class Branding extends Task { 37 38 private File cluster; 39 private File overrides; 40 private String token; 41 42 public Branding() {} 43 44 public void setCluster(File cluster) { 45 this.cluster = cluster; 46 } 47 48 public void setOverrides(File overrides) { 49 this.overrides = overrides; 50 } 51 52 public void setToken(String token) { 53 this.token = token; 54 } 55 56 public void execute() throws BuildException { 57 if (cluster == null || !cluster.isDirectory()) { 58 throw new BuildException("Must specify a valid cluster directory", getLocation()); 59 } 60 if (overrides == null || !overrides.isDirectory()) { 61 throw new BuildException("Must specify a valid overrides directory", getLocation()); 62 } 63 if (token == null || !token.matches("[a-z][a-z0-9]*(_[a-z][a-z0-9]*)*")) { throw new BuildException("Must specify a valid branding token: " + token, getLocation()); 65 } 66 try { 67 lookForBrandingJars(overrides, cluster, overrides.getAbsolutePath() + File.separatorChar); 68 } catch (IOException e) { 69 throw new BuildException(e, getLocation()); 70 } 71 } 72 73 private boolean excluded(File f, String prefix) { String pathAbs = f.getAbsolutePath(); 75 if (!pathAbs.startsWith(prefix)) { 76 throw new BuildException("Examined file " + f + " should have a path starting with " + prefix, getLocation()); 77 } 78 String path = pathAbs.substring(prefix.length()); 80 for (String exclude : DirectoryScanner.getDefaultExcludes()) { 81 if (SelectorUtils.matchPath(exclude, path)) { 82 return true; 83 } 84 } 85 return false; 86 } 87 88 private boolean lookForBrandingJars(File srcDir, File destDir, String srcPrefix) throws IOException { 89 if (srcDir.getName().endsWith(".jar")) { 90 packBrandingJar(srcDir, destDir); 91 return true; 92 } else { 93 String [] kids = srcDir.list(); 94 if (kids == null) { 95 throw new IOException ("Could not list children of " + srcDir); 96 } 97 boolean used = false; 98 for (int i = 0; i < kids.length; i++) { 99 File kid = new File (srcDir, kids[i]); 100 if (excluded(kid, srcPrefix)) { 101 continue; 102 } 103 if (!kid.isDirectory()) { 104 log("Warning: stray file " + kid + " encountered; ignoring", Project.MSG_WARN); 105 continue; 106 } 107 used |= lookForBrandingJars(kid, new File (destDir, kids[i]), srcPrefix); 108 } 109 if (!used) { 110 log("Warning: stray directory " + srcDir + " with no brandables encountered; ignoring", Project.MSG_WARN); 111 } 112 return used; 113 } 114 } 115 116 private void packBrandingJar(File srcDir, File destJarBase) throws IOException { 117 DirectoryScanner scanner = new DirectoryScanner(); 118 scanner.setBasedir(srcDir); 119 scanner.addDefaultExcludes(); scanner.scan(); 121 String [] files = scanner.getIncludedFiles(); 122 Zip zip = (Zip) getProject().createTask("zip"); 123 String name = destJarBase.getName(); 124 String nameBase = name.substring(0, name.length() - ".jar".length()); 125 File destFolder = new File (destJarBase.getParentFile(), "locale"); 126 if (!destFolder.isDirectory() && !destFolder.mkdirs()) { 127 throw new IOException ("Could not create directory " + destFolder); 128 } 129 File destJar = new File (destFolder, nameBase + "_" + token + ".jar"); 130 zip.setDestFile(destJar); 131 zip.setCompress(true); 132 for (int i = 0; i < files.length; i++) { 133 ZipFileSet entry = new ZipFileSet(); 134 entry.setFile(new File (srcDir, files[i])); 135 String basePath = files[i].replace(File.separatorChar, '/'); 136 int slash = basePath.lastIndexOf('/'); 137 int dot = basePath.lastIndexOf('.'); 138 String infix = "_" + token; 139 String brandedPath; 140 if (dot == -1 || dot < slash) { 141 brandedPath = basePath + infix; 142 } else { 143 brandedPath = basePath.substring(0, dot) + infix + basePath.substring(dot); 144 } 145 entry.setFullpath(brandedPath); 146 zip.addZipfileset(entry); 147 } 148 zip.setLocation(getLocation()); 149 zip.init(); 150 zip.execute(); 151 } 152 153 } 154 | Popular Tags |