1 18 19 package org.apache.tools.ant.taskdefs.optional; 20 21 import java.io.File ; 22 import org.apache.tools.ant.BuildException; 23 import org.apache.tools.ant.DirectoryScanner; 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.taskdefs.MatchingTask; 26 import org.apache.tools.ant.taskdefs.optional.native2ascii.Native2AsciiAdapter; 27 import org.apache.tools.ant.taskdefs.optional.native2ascii.Native2AsciiAdapterFactory; 28 import org.apache.tools.ant.types.Mapper; 29 import org.apache.tools.ant.util.FileNameMapper; 30 import org.apache.tools.ant.util.IdentityMapper; 31 import org.apache.tools.ant.util.SourceFileScanner; 32 import org.apache.tools.ant.util.facade.FacadeTaskHelper; 33 import org.apache.tools.ant.util.facade.ImplementationSpecificArgument; 34 35 40 public class Native2Ascii extends MatchingTask { 41 42 private boolean reverse = false; private String encoding = null; private File srcDir = null; private File destDir = null; private String extension = null; 48 private Mapper mapper; 49 private FacadeTaskHelper facade = null; 50 51 52 public Native2Ascii() { 53 facade = new FacadeTaskHelper(Native2AsciiAdapterFactory.getDefault()); 54 } 55 56 63 public void setReverse(boolean reverse) { 64 this.reverse = reverse; 65 } 66 67 72 public boolean getReverse() { 73 return reverse; 74 } 75 76 83 public void setEncoding(String encoding) { 84 this.encoding = encoding; 85 } 86 87 92 public String getEncoding() { 93 return encoding; 94 } 95 96 101 public void setSrc(File srcDir) { 102 this.srcDir = srcDir; 103 } 104 105 106 111 public void setDest(File destDir) { 112 this.destDir = destDir; 113 } 114 115 121 public void setExt(String ext) { 122 this.extension = ext; 123 } 124 125 130 public void setImplementation(String impl) { 131 if ("default".equals(impl)) { 132 facade.setImplementation(Native2AsciiAdapterFactory.getDefault()); 133 } else { 134 facade.setImplementation(impl); 135 } 136 } 137 138 145 public Mapper createMapper() throws BuildException { 146 if (mapper != null) { 147 throw new BuildException("Cannot define more than one mapper", 148 getLocation()); 149 } 150 mapper = new Mapper(getProject()); 151 return mapper; 152 } 153 154 159 public void add(FileNameMapper fileNameMapper) { 160 createMapper().add(fileNameMapper); 161 } 162 163 169 public ImplementationSpecificArgument createArg() { 170 ImplementationSpecificArgument arg = 171 new ImplementationSpecificArgument(); 172 facade.addImplementationArgument(arg); 173 return arg; 174 } 175 176 181 public void execute() throws BuildException { 182 183 DirectoryScanner scanner = null; String [] files; 186 if (srcDir == null) { 188 srcDir = getProject().resolveFile("."); 189 } 190 191 if (destDir == null) { 193 throw new BuildException("The dest attribute must be set."); 194 } 195 196 if (srcDir.equals(destDir) && extension == null && mapper == null) { 200 throw new BuildException("The ext attribute or a mapper must be set if" 201 + " src and dest dirs are the same."); 202 } 203 204 FileNameMapper m = null; 205 if (mapper == null) { 206 if (extension == null) { 207 m = new IdentityMapper(); 208 } else { 209 m = new ExtMapper(); 210 } 211 } else { 212 m = mapper.getImplementation(); 213 } 214 215 scanner = getDirectoryScanner(srcDir); 216 files = scanner.getIncludedFiles(); 217 SourceFileScanner sfs = new SourceFileScanner(this); 218 files = sfs.restrict(files, srcDir, destDir, m); 219 int count = files.length; 220 if (count == 0) { 221 return; 222 } 223 String message = "Converting " + count + " file" 224 + (count != 1 ? "s" : "") + " from "; 225 log(message + srcDir + " to " + destDir); 226 for (int i = 0; i < files.length; i++) { 227 convert(files[i], m.mapFileName(files[i])[0]); 228 } 229 } 230 231 237 private void convert(String srcName, String destName) 238 throws BuildException { 239 File srcFile; File destFile; 242 srcFile = new File (srcDir, srcName); 244 destFile = new File (destDir, destName); 245 246 if (srcFile.equals(destFile)) { 248 throw new BuildException("file " + srcFile 249 + " would overwrite its self"); 250 } 251 252 String parentName = destFile.getParent(); 255 if (parentName != null) { 256 File parentFile = new File (parentName); 257 258 if ((!parentFile.exists()) && (!parentFile.mkdirs())) { 259 throw new BuildException("cannot create parent directory " 260 + parentName); 261 } 262 } 263 264 log("converting " + srcName, Project.MSG_VERBOSE); 265 Native2AsciiAdapter ad = 266 Native2AsciiAdapterFactory.getAdapter(facade.getImplementation(), 267 this); 268 if (!ad.convert(this, srcFile, destFile)) { 269 throw new BuildException("conversion failed"); 270 } 271 } 272 273 279 public String [] getCurrentArgs() { 280 return facade.getArgs(); 281 } 282 283 private class ExtMapper implements FileNameMapper { 284 285 public void setFrom(String s) { 286 } 287 public void setTo(String s) { 288 } 289 290 public String [] mapFileName(String fileName) { 291 int lastDot = fileName.lastIndexOf('.'); 292 if (lastDot >= 0) { 293 return new String [] {fileName.substring(0, lastDot) 294 + extension}; 295 } else { 296 return new String [] {fileName + extension}; 297 } 298 } 299 } 300 } 301 | Popular Tags |