KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.nbbuild;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
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 /**
33  * Packs override resources into branding JARs with the correct paths.
34  * @author Jesse Glick
35  */

36 public final class Branding extends Task {
37
38     private File JavaDoc cluster;
39     private File JavaDoc overrides;
40     private String JavaDoc token;
41     
42     public Branding() {}
43     
44     public void setCluster(File JavaDoc cluster) {
45         this.cluster = cluster;
46     }
47     
48     public void setOverrides(File JavaDoc overrides) {
49         this.overrides = overrides;
50     }
51     
52     public void setToken(String JavaDoc 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]*)*")) { // cf. NbBundle.setBranding
64
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 JavaDoc e) {
69             throw new BuildException(e, getLocation());
70         }
71     }
72     
73     private boolean excluded(File JavaDoc f, String JavaDoc prefix) { // #68929
74
String JavaDoc 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         // Cannot just call matchPath on the pathAbs because a relative pattern will *never* match an absolute path!
79
String JavaDoc path = pathAbs.substring(prefix.length());
80         for (String JavaDoc exclude : DirectoryScanner.getDefaultExcludes()) {
81             if (SelectorUtils.matchPath(exclude, path)) {
82                 return true;
83             }
84         }
85         return false;
86     }
87     
88     private boolean lookForBrandingJars(File JavaDoc srcDir, File JavaDoc destDir, String JavaDoc srcPrefix) throws IOException JavaDoc {
89         if (srcDir.getName().endsWith(".jar")) {
90             packBrandingJar(srcDir, destDir);
91             return true;
92         } else {
93             String JavaDoc[] kids = srcDir.list();
94             if (kids == null) {
95                 throw new IOException JavaDoc("Could not list children of " + srcDir);
96             }
97             boolean used = false;
98             for (int i = 0; i < kids.length; i++) {
99                 File JavaDoc kid = new File JavaDoc(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 JavaDoc(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 JavaDoc srcDir, File JavaDoc destJarBase) throws IOException JavaDoc {
117         DirectoryScanner scanner = new DirectoryScanner();
118         scanner.setBasedir(srcDir);
119         scanner.addDefaultExcludes(); // #68929
120
scanner.scan();
121         String JavaDoc[] files = scanner.getIncludedFiles();
122         Zip zip = (Zip) getProject().createTask("zip");
123         String JavaDoc name = destJarBase.getName();
124         String JavaDoc nameBase = name.substring(0, name.length() - ".jar".length());
125         File JavaDoc destFolder = new File JavaDoc(destJarBase.getParentFile(), "locale");
126         if (!destFolder.isDirectory() && !destFolder.mkdirs()) {
127             throw new IOException JavaDoc("Could not create directory " + destFolder);
128         }
129         File JavaDoc destJar = new File JavaDoc(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 JavaDoc(srcDir, files[i]));
135             String JavaDoc basePath = files[i].replace(File.separatorChar, '/');
136             int slash = basePath.lastIndexOf('/');
137             int dot = basePath.lastIndexOf('.');
138             String JavaDoc infix = "_" + token;
139             String JavaDoc 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