Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 269   Methods: 11
NCLOC: 127   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
Native2Ascii.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000,2002 The Apache Software Foundation.  All rights 
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.optional;
 56   
 
 57   
 import java.io.File;
 58   
 import org.apache.tools.ant.BuildException;
 59   
 import org.apache.tools.ant.DirectoryScanner;
 60   
 import org.apache.tools.ant.Project;
 61   
 import org.apache.tools.ant.taskdefs.MatchingTask;
 62   
 import org.apache.tools.ant.types.Commandline;
 63   
 import org.apache.tools.ant.types.Mapper;
 64   
 import org.apache.tools.ant.util.FileNameMapper;
 65   
 import org.apache.tools.ant.util.IdentityMapper;
 66   
 import org.apache.tools.ant.util.SourceFileScanner;
 67   
 
 68   
 /**
 69   
  * Converts files from native encodings to ASCII.
 70   
  *
 71   
  * @author <a href="asudell@acm.org">Drew Sudell</a>
 72   
  * @author Stefan Bodewig
 73   
  * @since Ant 1.2
 74   
  */
 75   
 public class Native2Ascii extends MatchingTask {
 76   
 
 77   
     private boolean reverse = false;  // convert from ascii back to native
 78   
     private String encoding = null;   // encoding to convert to/from
 79   
     private File srcDir = null;       // Where to find input files
 80   
     private File destDir = null;      // Where to put output files
 81   
     private String extension = null;  // Extension of output files if different
 82   
 
 83   
     private Mapper mapper;
 84   
 
 85   
     /**
 86   
      * Flag the conversion to run in the reverse sense,
 87   
      * that is Ascii to Native encoding.
 88   
      * 
 89   
      * @param reverse True if the conversion is to be reversed,
 90   
      *                otherwise false;
 91   
      */
 92  0
     public void setReverse(boolean reverse){
 93  0
         this.reverse = reverse;
 94   
     }
 95   
 
 96   
     /**
 97   
      * Set the encoding to translate to/from.
 98   
      * If unset, the default encoding for the JVM is used.
 99   
      *
 100   
      * @param encoding String containing the name of the Native 
 101   
      *                 encoding to convert from or to.
 102   
      */
 103  0
     public void setEncoding(String encoding){
 104  0
         this.encoding = encoding;
 105   
     }
 106   
 
 107   
     /**
 108   
      * Set the source directory in which to find files to convert.
 109   
      *
 110   
      * @param srcDir directory to find input file in.
 111   
      */
 112  0
     public void setSrc(File srcDir){
 113  0
         this.srcDir = srcDir;
 114   
     }
 115   
 
 116   
 
 117   
     /**
 118   
      * Set the destination directory to place converted files into.
 119   
      *
 120   
      * @param destDir directory to place output file into.
 121   
      */
 122  0
     public void setDest(File destDir){
 123  0
         this.destDir = destDir;
 124   
     }
 125   
 
 126   
     /**
 127   
      * Set the extension which converted files should have.
 128   
      * If unset, files will not be renamed.
 129   
      *
 130   
      * @param ext File extension to use for converted files.
 131   
      */
 132  0
     public void setExt(String ext){
 133  0
         this.extension = ext;
 134   
     }
 135   
 
 136   
     /**
 137   
      * Defines the FileNameMapper to use (nested mapper element).
 138   
      */
 139  0
     public Mapper createMapper() throws BuildException {
 140  0
         if (mapper != null) {
 141  0
             throw new BuildException("Cannot define more than one mapper",
 142   
                                      getLocation());
 143   
         }
 144  0
         mapper = new Mapper(getProject());
 145  0
         return mapper;
 146   
     }
 147   
 
 148  0
     public void execute() throws BuildException {
 149   
 
 150  0
         DirectoryScanner scanner = null; // Scanner to find our inputs
 151  0
         String[] files;                  // list of files to process
 152   
 
 153   
         // default srcDir to basedir
 154  0
         if (srcDir == null){
 155  0
             srcDir = getProject().resolveFile(".");
 156   
         }
 157   
 
 158   
         // Require destDir
 159  0
         if (destDir == null){
 160  0
             throw new BuildException("The dest attribute must be set.");
 161   
         }
 162   
 
 163   
         // if src and dest dirs are the same, require the extension
 164   
         // to be set, so we don't stomp every file.  One could still
 165   
         // include a file with the same extension, but ....
 166  0
         if (srcDir.equals(destDir) && extension == null && mapper == null){
 167  0
             throw new BuildException("The ext attribute or a mapper must be set if"
 168   
                                      + " src and dest dirs are the same.");
 169   
         }
 170   
 
 171  0
         FileNameMapper m = null;
 172  0
         if (mapper == null) {
 173  0
             if (extension == null) {
 174  0
                 m = new IdentityMapper();
 175   
             } else {
 176  0
                 m = new ExtMapper();
 177   
             }
 178   
         } else {
 179  0
             m = mapper.getImplementation();
 180   
         }
 181   
         
 182  0
         scanner = getDirectoryScanner(srcDir);
 183  0
         files = scanner.getIncludedFiles();
 184  0
         SourceFileScanner sfs = new SourceFileScanner(this);
 185  0
         files = sfs.restrict(files, srcDir, destDir, m);
 186  0
         int count = files.length;
 187  0
         if (count == 0) {
 188  0
             return;
 189   
         }
 190  0
         String message = "Converting " + count + " file"
 191   
             + (count != 1 ? "s" : "") + " from ";
 192  0
         log(message + srcDir + " to " + destDir);
 193  0
         for (int i = 0; i < files.length; i++){
 194  0
             convert(files[i], m.mapFileName(files[i])[0]);
 195   
         }
 196   
     }
 197   
 
 198   
     /**
 199   
      * Convert a single file.
 200   
      *
 201   
      * @param srcName name of the input file.
 202   
      * @param destName name of the input file.
 203   
      */
 204  0
     private void convert(String srcName, String destName) throws BuildException {
 205   
 
 206  0
         Commandline cmd = new Commandline();  // Command line to run
 207  0
         File srcFile;                         // File to convert
 208  0
         File destFile;                        // where to put the results
 209   
 
 210   
         // Set up the basic args (this could be done once, but
 211   
         // it's cleaner here)
 212  0
         if (reverse){
 213  0
             cmd.createArgument().setValue("-reverse");
 214   
         }
 215   
 
 216  0
         if (encoding != null){
 217  0
             cmd.createArgument().setValue("-encoding");
 218  0
             cmd.createArgument().setValue(encoding);
 219   
         }
 220   
 
 221   
         // Build the full file names
 222  0
         srcFile = new File(srcDir, srcName);
 223  0
         destFile = new File(destDir, destName);
 224   
 
 225  0
         cmd.createArgument().setFile(srcFile);
 226  0
         cmd.createArgument().setFile(destFile);
 227   
         // Make sure we're not about to clobber something
 228  0
         if (srcFile.equals(destFile)){
 229  0
             throw new BuildException("file " + srcFile 
 230   
                                      + " would overwrite its self");
 231   
         }
 232   
 
 233   
         // Make intermediate directories if needed
 234   
         // XXX JDK 1.1 dosen't have File.getParentFile,
 235  0
         String parentName = destFile.getParent();
 236  0
         if (parentName != null){
 237  0
             File parentFile = new File(parentName);
 238   
             
 239  0
             if ((!parentFile.exists()) && (!parentFile.mkdirs())){
 240  0
                 throw new BuildException("cannot create parent directory "
 241   
                                          + parentName);
 242   
             }
 243   
         }
 244   
                         
 245  0
         log("converting " + srcName, Project.MSG_VERBOSE);
 246  0
         sun.tools.native2ascii.Main n2a
 247   
             = new sun.tools.native2ascii.Main();
 248  0
         if (!n2a.convert(cmd.getArguments())){
 249  0
             throw new BuildException("conversion failed");
 250   
         }
 251   
     }
 252   
 
 253   
     private class ExtMapper implements FileNameMapper {
 254   
 
 255  0
         public void setFrom(String s) {}
 256  0
         public void setTo(String s) {}
 257   
 
 258  0
         public String[] mapFileName(String fileName) {
 259  0
             int lastDot = fileName.lastIndexOf('.');
 260  0
             if (lastDot >= 0) {
 261  0
                 return new String[] {fileName.substring(0, lastDot) 
 262   
                                          + extension};
 263   
             } else {
 264  0
                 return new String[] {fileName + extension};
 265   
             }
 266   
         }
 267   
     }
 268   
 }
 269