KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tools.ant.*;
23 import org.apache.tools.ant.util.*;
24 import org.apache.tools.ant.taskdefs.*;
25 import java.io.*;
26 import java.util.*;
27
28 /** This task copies the localizable files to a directory.
29  * This task uses the L10nTask's getLocalizableFiles() function
30  * to get the list of localizable files for each module.
31  */

32 public class GetL9eFiles extends Task {
33     
34     /** The name of file that contains the localizable file
35      * regular expressions.
36      * <p>Default: <samp>l10n.list</samp>
37      */

38     protected String JavaDoc listFile = "l10n.list";
39     
40     /** The grandparent directory of the l10n.list files.
41      * <p>Default: <samp>..</samp>
42      */

43     protected String JavaDoc baseDir = "..";
44     protected File grandParent = null ;
45     
46     /** The target directory to copy all translatable files to.
47      * <p>Default: <samp>src-todo</samp>
48      */

49     protected File targetDir = null ;
50     
51     /** List of exclude patterns that override the listFiles.
52      * <p>Default: Ja localized files
53      */

54     protected String JavaDoc excludes = "**/ja/,**/*_ja.*" ;
55     
56     /** Used internally. */
57     protected FileUtils fileUtils = null ;
58     
59     public void setBaseDir(String JavaDoc s) {
60         File f ;
61         
62         baseDir = s;
63         f = new File( antBaseDir() + baseDir) ;
64         try {
65             grandParent = new File( f.getCanonicalPath()) ;
66         } catch( Exception JavaDoc e) {
67             e.printStackTrace();
68             throw new BuildException() ;
69         }
70     }
71     public void setListFile( String JavaDoc s) {
72         listFile = s ;
73     }
74     public void setTargetDir( File f) {
75         targetDir = f ;
76     }
77     public void setExcludes( String JavaDoc s) {
78         excludes = s ;
79     }
80     
81     /** A file filter that accepts only directories
82      */

83     class DirectoryFilter implements FileFilter {
84         public boolean accept(File f) {
85             return( f.isDirectory()) ;
86         }
87     }
88     
89     class TarFileFilter implements FileFilter {
90         public boolean accept(File f) {
91             return( f.getName().endsWith( ".tar")) ;
92         }
93     }
94     
95     public void execute() throws BuildException {
96         if( targetDir == null) {
97             targetDir = new File( antBaseDir() + "src-todo") ;
98         }
99         
100         // If needed, setup the grandParent variable. //
101
if( grandParent == null) {
102             setBaseDir( baseDir) ;
103         }
104         
105         // For each module with a list file. //
106
for (File module : getModulesWithListFiles()) {
107             // Copy the module's localizable files. //
108
copyL9eFiles( module) ;
109         }
110     }
111     
112     protected void copyL9eFiles( File module) {
113         String JavaDoc[] l9eFiles, changedFiles ;
114         int i ;
115         File fromFile, toFile ;
116         L10nTask l10nTask ;
117         
118         // Setup the file utils. //
119
if( fileUtils == null) {
120             fileUtils = FileUtils.getFileUtils() ;
121         }
122         
123         // Use the l10n task to read the list file and get a list of the //
124
// localizable files. //
125
getProject().addTaskDefinition("l10nTask", L10nTask.class);
126         l10nTask = (L10nTask) getProject().createTask("l10nTask");
127         l10nTask.init() ;
128         l10nTask.setLocalizableFile( listFile) ;
129         l10nTask.setExcludePattern( excludes) ;
130         l9eFiles = l10nTask.getLocalizableFiles( grandParent, module.getName()) ;
131         if( l9eFiles != null) {
132             
133             // Get a list of the files that have changed. //
134
changedFiles = getChangedFiles( l9eFiles) ;
135             
136             // Copy the localizable files that have changed. //
137
if( changedFiles != null && changedFiles.length > 0) {
138                 log( "Copying " + changedFiles.length + " files to " + targetDir.getPath()) ;
139                 for( i = 0; i < changedFiles.length; i++) {
140                     fromFile = new File( changedFiles[i]) ;
141                     toFile = new File( mapL9eFile( changedFiles[i],
142                             targetDir.getPath(),
143                             grandParent.getPath())) ;
144                     try {
145                         //log("Copying " + fromFile.getPath() + " to " + toFile.getPath()) ;
146
fileUtils.copyFile( fromFile, toFile) ;
147                     } catch (IOException ioe) {
148                         String JavaDoc msg = "Failed to copy " + fromFile.getPath() + " to " +
149                                 toFile.getPath() + " due to " + ioe.getMessage();
150                         throw new BuildException(msg, ioe, getLocation());
151                     }
152                 }
153             }
154         }
155     }
156     
157     protected String JavaDoc[] getChangedFiles( String JavaDoc[] files) {
158         L9eMapper mapper = new L9eMapper() ;
159         mapper.setFrom( grandParent.getPath()) ;
160         mapper.setTo( targetDir.getPath()) ;
161         
162         SourceFileScanner ds = new SourceFileScanner( this);
163         return( ds.restrict( files, null, null, mapper)) ;
164     }
165     
166     protected class L9eMapper implements FileNameMapper {
167         
168         protected String JavaDoc m_grandParent ;
169         protected String JavaDoc m_toDir ;
170         
171         public void setFrom(String JavaDoc from) {
172             m_grandParent = from ;
173         }
174         
175         public void setTo(String JavaDoc to) {
176             m_toDir = to ;
177         }
178         
179         // Returns an one-element array containing the destination file //
180
// name. //
181
public String JavaDoc[] mapFileName(String JavaDoc file) {
182             return new String JavaDoc[] { GetL9eFiles.mapL9eFile( file, m_toDir, m_grandParent) } ;
183         }
184     }
185     
186     protected static String JavaDoc mapL9eFile( String JavaDoc file, String JavaDoc toDir,
187             String JavaDoc grandParentName) {
188         return toDir + file.substring( grandParentName.length()) ;
189     }
190     
191     protected List<File> getModulesWithListFiles() {
192         List<File> modules = new LinkedList<File>();
193         for (File module : grandParent.listFiles(new DirectoryFilter())) {
194             File list = new File(module.getPath() + File.separator + listFile);
195             if (list.exists()) {
196                 modules.add( module) ;
197             }
198         }
199         return modules;
200     }
201     
202     protected String JavaDoc antBaseDir() {
203         return( getProject().getBaseDir().getAbsolutePath() + File.separator) ;
204     }
205     
206 }
207
Popular Tags