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 org.apache.tools.ant.*;
045: import org.apache.tools.ant.util.*;
046: import org.apache.tools.ant.taskdefs.*;
047: import java.io.*;
048: import java.util.*;
049:
050: /** This task copies the localizable files to a directory.
051: * This task uses the L10nTask's getLocalizableFiles() function
052: * to get the list of localizable files for each module.
053: */
054: public class GetL9eFiles extends Task {
055:
056: /** The name of file that contains the localizable file
057: * regular expressions.
058: * <p>Default: <samp>l10n.list</samp>
059: */
060: protected String listFile = "l10n.list";
061:
062: /** The grandparent directory of the l10n.list files.
063: * <p>Default: <samp>..</samp>
064: */
065: protected String baseDir = "..";
066: protected File grandParent = null;
067:
068: /** The target directory to copy all translatable files to.
069: * <p>Default: <samp>src-todo</samp>
070: */
071: protected File targetDir = null;
072:
073: /** List of exclude patterns that override the listFiles.
074: * <p>Default: Ja localized files
075: */
076: protected String excludes = "**/ja/,**/*_ja.*";
077:
078: /** Used internally. */
079: protected FileUtils fileUtils = null;
080:
081: public void setBaseDir(String s) {
082: File f;
083:
084: baseDir = s;
085: f = new File(antBaseDir() + baseDir);
086: try {
087: grandParent = new File(f.getCanonicalPath());
088: } catch (Exception e) {
089: e.printStackTrace();
090: throw new BuildException();
091: }
092: }
093:
094: public void setListFile(String s) {
095: listFile = s;
096: }
097:
098: public void setTargetDir(File f) {
099: targetDir = f;
100: }
101:
102: public void setExcludes(String s) {
103: excludes = s;
104: }
105:
106: /** A file filter that accepts only directories
107: */
108: class DirectoryFilter implements FileFilter {
109: public boolean accept(File f) {
110: return (f.isDirectory());
111: }
112: }
113:
114: class TarFileFilter implements FileFilter {
115: public boolean accept(File f) {
116: return (f.getName().endsWith(".tar"));
117: }
118: }
119:
120: public void execute() throws BuildException {
121: if (targetDir == null) {
122: targetDir = new File(antBaseDir() + "src-todo");
123: }
124:
125: // If needed, setup the grandParent variable. //
126: if (grandParent == null) {
127: setBaseDir(baseDir);
128: }
129:
130: // For each module with a list file. //
131: for (File module : getModulesWithListFiles()) {
132: // Copy the module's localizable files. //
133: copyL9eFiles(module);
134: }
135: }
136:
137: protected void copyL9eFiles(File module) {
138: String[] l9eFiles, changedFiles;
139: int i;
140: File fromFile, toFile;
141: L10nTask l10nTask;
142:
143: // Setup the file utils. //
144: if (fileUtils == null) {
145: fileUtils = FileUtils.getFileUtils();
146: }
147:
148: // Use the l10n task to read the list file and get a list of the //
149: // localizable files. //
150: getProject().addTaskDefinition("l10nTask", L10nTask.class);
151: l10nTask = (L10nTask) getProject().createTask("l10nTask");
152: l10nTask.init();
153: l10nTask.setLocalizableFile(listFile);
154: l10nTask.setExcludePattern(excludes);
155: l9eFiles = l10nTask.getLocalizableFiles(grandParent, module
156: .getName());
157: if (l9eFiles != null) {
158:
159: // Get a list of the files that have changed. //
160: changedFiles = getChangedFiles(l9eFiles);
161:
162: // Copy the localizable files that have changed. //
163: if (changedFiles != null && changedFiles.length > 0) {
164: log("Copying " + changedFiles.length + " files to "
165: + targetDir.getPath());
166: for (i = 0; i < changedFiles.length; i++) {
167: fromFile = new File(changedFiles[i]);
168: toFile = new File(mapL9eFile(changedFiles[i],
169: targetDir.getPath(), grandParent.getPath()));
170: try {
171: //log("Copying " + fromFile.getPath() + " to " + toFile.getPath()) ;
172: fileUtils.copyFile(fromFile, toFile);
173: } catch (IOException ioe) {
174: String msg = "Failed to copy "
175: + fromFile.getPath() + " to "
176: + toFile.getPath() + " due to "
177: + ioe.getMessage();
178: throw new BuildException(msg, ioe,
179: getLocation());
180: }
181: }
182: }
183: }
184: }
185:
186: protected String[] getChangedFiles(String[] files) {
187: L9eMapper mapper = new L9eMapper();
188: mapper.setFrom(grandParent.getPath());
189: mapper.setTo(targetDir.getPath());
190:
191: SourceFileScanner ds = new SourceFileScanner(this );
192: return (ds.restrict(files, null, null, mapper));
193: }
194:
195: protected class L9eMapper implements FileNameMapper {
196:
197: protected String m_grandParent;
198: protected String m_toDir;
199:
200: public void setFrom(String from) {
201: m_grandParent = from;
202: }
203:
204: public void setTo(String to) {
205: m_toDir = to;
206: }
207:
208: // Returns an one-element array containing the destination file //
209: // name. //
210: public String[] mapFileName(String file) {
211: return new String[] { GetL9eFiles.mapL9eFile(file, m_toDir,
212: m_grandParent) };
213: }
214: }
215:
216: protected static String mapL9eFile(String file, String toDir,
217: String grandParentName) {
218: return toDir + file.substring(grandParentName.length());
219: }
220:
221: protected List<File> getModulesWithListFiles() {
222: List<File> modules = new LinkedList<File>();
223: for (File module : grandParent.listFiles(new DirectoryFilter())) {
224: File list = new File(module.getPath() + File.separator
225: + listFile);
226: if (list.exists()) {
227: modules.add(module);
228: }
229: }
230: return modules;
231: }
232:
233: protected String antBaseDir() {
234: return (getProject().getBaseDir().getAbsolutePath() + File.separator);
235: }
236:
237: }
|