1 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 32 public class GetL9eFiles extends Task { 33 34 38 protected String listFile = "l10n.list"; 39 40 43 protected String baseDir = ".."; 44 protected File grandParent = null ; 45 46 49 protected File targetDir = null ; 50 51 54 protected String excludes = "**/ja/,**/*_ja.*" ; 55 56 57 protected FileUtils fileUtils = null ; 58 59 public void setBaseDir(String 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 e) { 67 e.printStackTrace(); 68 throw new BuildException() ; 69 } 70 } 71 public void setListFile( String s) { 72 listFile = s ; 73 } 74 public void setTargetDir( File f) { 75 targetDir = f ; 76 } 77 public void setExcludes( String s) { 78 excludes = s ; 79 } 80 81 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( grandParent == null) { 102 setBaseDir( baseDir) ; 103 } 104 105 for (File module : getModulesWithListFiles()) { 107 copyL9eFiles( module) ; 109 } 110 } 111 112 protected void copyL9eFiles( File module) { 113 String [] l9eFiles, changedFiles ; 114 int i ; 115 File fromFile, toFile ; 116 L10nTask l10nTask ; 117 118 if( fileUtils == null) { 120 fileUtils = FileUtils.getFileUtils() ; 121 } 122 123 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 changedFiles = getChangedFiles( l9eFiles) ; 135 136 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 fileUtils.copyFile( fromFile, toFile) ; 147 } catch (IOException ioe) { 148 String 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 [] getChangedFiles( String [] 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 m_grandParent ; 169 protected String m_toDir ; 170 171 public void setFrom(String from) { 172 m_grandParent = from ; 173 } 174 175 public void setTo(String to) { 176 m_toDir = to ; 177 } 178 179 public String [] mapFileName(String file) { 182 return new String [] { GetL9eFiles.mapL9eFile( file, m_toDir, m_grandParent) } ; 183 } 184 } 185 186 protected static String mapL9eFile( String file, String toDir, 187 String 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 antBaseDir() { 203 return( getProject().getBaseDir().getAbsolutePath() + File.separator) ; 204 } 205 206 } 207 | Popular Tags |