001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.nbbuild;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import org.apache.tools.ant.BuildException;
047: import org.apache.tools.ant.DirectoryScanner;
048: import org.apache.tools.ant.Project;
049: import org.apache.tools.ant.Task;
050: import org.apache.tools.ant.taskdefs.Zip;
051: import org.apache.tools.ant.types.ZipFileSet;
052: import org.apache.tools.ant.types.selectors.SelectorUtils;
053:
054: /**
055: * Packs override resources into branding JARs with the correct paths.
056: * @author Jesse Glick
057: */
058: public final class Branding extends Task {
059:
060: private File cluster;
061: private File overrides;
062: private String token;
063:
064: public Branding() {
065: }
066:
067: public void setCluster(File cluster) {
068: this .cluster = cluster;
069: }
070:
071: public void setOverrides(File overrides) {
072: this .overrides = overrides;
073: }
074:
075: public void setToken(String token) {
076: this .token = token;
077: }
078:
079: public void execute() throws BuildException {
080: if (cluster == null || !cluster.isDirectory()) {
081: throw new BuildException(
082: "Must specify a valid cluster directory",
083: getLocation());
084: }
085: if (overrides == null || !overrides.isDirectory()) {
086: throw new BuildException(
087: "Must specify a valid overrides directory",
088: getLocation());
089: }
090: if (token == null
091: || !token.matches("[a-z][a-z0-9]*(_[a-z][a-z0-9]*)*")) { // cf. NbBundle.setBranding
092: throw new BuildException(
093: "Must specify a valid branding token: " + token,
094: getLocation());
095: }
096: try {
097: lookForBrandingJars(overrides, cluster, overrides
098: .getAbsolutePath()
099: + File.separatorChar);
100: } catch (IOException e) {
101: throw new BuildException(e, getLocation());
102: }
103: }
104:
105: private boolean excluded(File f, String prefix) { // #68929
106: String pathAbs = f.getAbsolutePath();
107: if (!pathAbs.startsWith(prefix)) {
108: throw new BuildException("Examined file " + f
109: + " should have a path starting with " + prefix,
110: getLocation());
111: }
112: // Cannot just call matchPath on the pathAbs because a relative pattern will *never* match an absolute path!
113: String path = pathAbs.substring(prefix.length());
114: for (String exclude : DirectoryScanner.getDefaultExcludes()) {
115: if (SelectorUtils.matchPath(exclude, path)) {
116: return true;
117: }
118: }
119: return false;
120: }
121:
122: private boolean lookForBrandingJars(File srcDir, File destDir,
123: String srcPrefix) throws IOException {
124: if (srcDir.getName().endsWith(".jar")) {
125: packBrandingJar(srcDir, destDir);
126: return true;
127: } else {
128: String[] kids = srcDir.list();
129: if (kids == null) {
130: throw new IOException("Could not list children of "
131: + srcDir);
132: }
133: boolean used = false;
134: for (int i = 0; i < kids.length; i++) {
135: File kid = new File(srcDir, kids[i]);
136: if (excluded(kid, srcPrefix)) {
137: continue;
138: }
139: if (!kid.isDirectory()) {
140: log("Warning: stray file " + kid
141: + " encountered; ignoring",
142: Project.MSG_WARN);
143: continue;
144: }
145: used |= lookForBrandingJars(kid, new File(destDir,
146: kids[i]), srcPrefix);
147: }
148: if (!used) {
149: log("Warning: stray directory " + srcDir
150: + " with no brandables encountered; ignoring",
151: Project.MSG_WARN);
152: }
153: return used;
154: }
155: }
156:
157: private void packBrandingJar(File srcDir, File destJarBase)
158: throws IOException {
159: DirectoryScanner scanner = new DirectoryScanner();
160: scanner.setBasedir(srcDir);
161: scanner.addDefaultExcludes(); // #68929
162: scanner.scan();
163: String[] files = scanner.getIncludedFiles();
164: Zip zip = (Zip) getProject().createTask("zip");
165: String name = destJarBase.getName();
166: String nameBase = name.substring(0, name.length()
167: - ".jar".length());
168: File destFolder = new File(destJarBase.getParentFile(),
169: "locale");
170: if (!destFolder.isDirectory() && !destFolder.mkdirs()) {
171: throw new IOException("Could not create directory "
172: + destFolder);
173: }
174: File destJar = new File(destFolder, nameBase + "_" + token
175: + ".jar");
176: zip.setDestFile(destJar);
177: zip.setCompress(true);
178: for (int i = 0; i < files.length; i++) {
179: ZipFileSet entry = new ZipFileSet();
180: entry.setFile(new File(srcDir, files[i]));
181: String basePath = files[i].replace(File.separatorChar, '/');
182: int slash = basePath.lastIndexOf('/');
183: int dot = basePath.lastIndexOf('.');
184: String infix = "_" + token;
185: String brandedPath;
186: if (dot == -1 || dot < slash) {
187: brandedPath = basePath + infix;
188: } else {
189: brandedPath = basePath.substring(0, dot) + infix
190: + basePath.substring(dot);
191: }
192: entry.setFullpath(brandedPath);
193: zip.addZipfileset(entry);
194: }
195: zip.setLocation(getLocation());
196: zip.init();
197: zip.execute();
198: }
199:
200: }
|