Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 249   Methods: 8
NCLOC: 108   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ManifestTask.java 61.1% 78.7% 87.5% 75.3%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002-2003 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;
 56   
 
 57   
 import java.io.File;
 58   
 import java.io.FileInputStream;
 59   
 import java.io.InputStreamReader;
 60   
 import java.io.FileOutputStream;
 61   
 import java.io.OutputStreamWriter;
 62   
 import java.io.IOException;
 63   
 import java.io.PrintWriter;
 64   
 import org.apache.tools.ant.BuildException;
 65   
 import org.apache.tools.ant.Project;
 66   
 import org.apache.tools.ant.Task;
 67   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 68   
 
 69   
 /**
 70   
  * Creates a manifest file for inclusion in a JAR, Ant task wrapper
 71   
  * around {@link Manifest Manifest}.  This task can be used to write a
 72   
  * Manifest file, optionally replacing or updating an existing file.
 73   
  *
 74   
  * @author Conor MacNeill
 75   
  * @author Stefan Bodewig
 76   
  * @author <a href="mailto:j_a_fernandez@yahoo.com">Jose Alberto Fernandez</a>
 77   
  *
 78   
  * @since Ant 1.5
 79   
  *
 80   
  * @ant.task category="java"
 81   
  */
 82   
 public class ManifestTask extends Task {
 83   
 
 84   
     /**
 85   
      * Holds the real data.
 86   
      */
 87   
     private Manifest nestedManifest = new Manifest();
 88   
 
 89   
     /**
 90   
      * The file to which the manifest should be written when used as a task
 91   
      */
 92   
     private File manifestFile;
 93   
 
 94   
     /**
 95   
      * The mode with which the manifest file is written
 96   
      */
 97   
     private Mode mode;
 98   
 
 99   
     /** 
 100   
      * The encoding of the manifest file
 101   
      */
 102   
     private String encoding;
 103   
     
 104   
     /**
 105   
      * Helper class for Manifest's mode attribute.
 106   
      */
 107   
     public static class Mode extends EnumeratedAttribute {
 108   
         /**
 109   
          * Get Allowed values for the mode attribute.
 110   
          *
 111   
          * @return a String array of the allowed values.
 112   
          */
 113  9
         public String[] getValues() {
 114  9
             return new String[] {"update", "replace"};
 115   
         }
 116   
     }
 117   
 
 118  6
     public ManifestTask() {
 119  6
         mode = new Mode();
 120  6
         mode.setValue("replace");
 121   
     }
 122   
 
 123   
     /**
 124   
      * Add a section to the manifest
 125   
      *
 126   
      * @param section the manifest section to be added
 127   
      *
 128   
      * @exception ManifestException if the secti0on is not valid.
 129   
      */
 130  2
     public void addConfiguredSection(Manifest.Section section)
 131   
          throws ManifestException {
 132  2
         nestedManifest.addConfiguredSection(section);
 133   
     }
 134   
 
 135   
     /**
 136   
      * Add an attribute to the manifest - it is added to the main section.
 137   
      *
 138   
      * @param attribute the attribute to be added.
 139   
      *
 140   
      * @exception ManifestException if the attribute is not valid.
 141   
      */
 142  2
     public void addConfiguredAttribute(Manifest.Attribute attribute)
 143   
          throws ManifestException {
 144  2
         nestedManifest.addConfiguredAttribute(attribute);
 145   
     }
 146   
 
 147   
     /**
 148   
      * The name of the manifest file to create/update.
 149   
      * Required if used as a task.
 150   
      * @param f the Manifest file to be written
 151   
      */
 152  5
     public void setFile(File f) {
 153  5
         manifestFile = f;
 154   
     }
 155   
 
 156   
     /**
 157   
      * The encoding to use for reading in an existing manifest file
 158   
      * @param encoding the maniofets file encoding.
 159   
      */
 160  0
     public void setEncoding(String encoding) {
 161  0
         this.encoding = encoding;
 162   
     }
 163   
 
 164   
     /**
 165   
      * Update policy: either "update" or "replace"; default is "replace".
 166   
      * @param m the mode value - update or replace.
 167   
      */
 168  3
     public void setMode(Mode m) {
 169  3
         mode = m;
 170   
     }
 171   
 
 172   
     /**
 173   
      * Create or update the Manifest when used as a task.
 174   
      *
 175   
      * @throws BuildException if the manifest cannot be written.
 176   
      */
 177  6
     public void execute() throws BuildException {
 178  6
         if (manifestFile == null) {
 179  1
             throw new BuildException("the file attribute is required");
 180   
         }
 181   
 
 182  5
         Manifest toWrite = Manifest.getDefaultManifest();
 183  5
         Manifest current = null;
 184  5
         BuildException error = null;
 185   
 
 186  5
         if (manifestFile.exists()) {
 187  4
             FileInputStream fis = null;
 188  4
             InputStreamReader isr = null;
 189  4
             try {
 190  4
                 fis = new FileInputStream(manifestFile);
 191  4
                 if (encoding == null) {
 192  4
                     isr = new InputStreamReader(fis, "UTF-8");
 193   
                 } else {
 194  0
                     isr = new InputStreamReader(fis, encoding);
 195   
                 }
 196  4
                 current = new Manifest(isr);
 197   
             } catch (ManifestException m) {
 198  0
                 error = new BuildException("Existing manifest " + manifestFile
 199   
                                            + " is invalid", m, getLocation());
 200   
             } catch (IOException e) {
 201  0
                 error = new BuildException("Failed to read " + manifestFile,
 202   
                                            e, getLocation());
 203   
             } finally {
 204  4
                 if (isr != null) {
 205  4
                     try {
 206  4
                         isr.close();
 207   
                     } catch (IOException e) {}
 208   
                 }
 209   
             }
 210   
         }
 211   
 
 212  5
         try {
 213  5
             if (mode.getValue().equals("update") && manifestFile.exists()) {
 214  3
                 if (current != null) {
 215  3
                     toWrite.merge(current);
 216  0
                 } else if (error != null) {
 217  0
                     throw error;
 218   
                 }
 219   
             }
 220   
 
 221  5
             toWrite.merge(nestedManifest);
 222   
         } catch (ManifestException m) {
 223  0
             throw new BuildException("Manifest is invalid", m, getLocation());
 224   
         }
 225   
 
 226  5
         if (toWrite.equals(current)) {
 227  0
             log("Manifest has not changed, do not recreate",
 228   
                 Project.MSG_VERBOSE);
 229  0
             return;
 230   
         }
 231   
 
 232  5
         PrintWriter w = null;
 233  5
         try {
 234  5
             FileOutputStream fos = new FileOutputStream(manifestFile);
 235  5
             OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
 236  5
             w = new PrintWriter(osw);
 237  5
             toWrite.write(w);
 238   
         } catch (IOException e) {
 239  0
             throw new BuildException("Failed to write " + manifestFile,
 240   
                                      e, getLocation());
 241   
         } finally {
 242  5
             if (w != null) {
 243  5
                 w.close();
 244   
             }
 245   
         }
 246   
     }
 247   
 
 248   
 }
 249