Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 274   Methods: 14
NCLOC: 147   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
Mapper.java 50% 64.3% 71.4% 60.8%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-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.types;
 56   
 
 57   
 import java.util.Properties;
 58   
 import java.util.Stack;
 59   
 import org.apache.tools.ant.AntClassLoader;
 60   
 import org.apache.tools.ant.BuildException;
 61   
 import org.apache.tools.ant.Project;
 62   
 import org.apache.tools.ant.util.FileNameMapper;
 63   
 
 64   
 /**
 65   
  * Element to define a FileNameMapper.
 66   
  *
 67   
  * @author Stefan Bodewig
 68   
  */
 69   
 public class Mapper extends DataType implements Cloneable {
 70   
 
 71   
     protected MapperType type = null;
 72   
 
 73  23
     public Mapper(Project p) {
 74  23
         setProject(p);
 75   
     }
 76   
 
 77   
     /**
 78   
      * Set the type of FileNameMapper to use.
 79   
      */
 80  13
     public void setType(MapperType type) {
 81  13
         if (isReference()) {
 82  1
             throw tooManyAttributes();
 83   
         }
 84  12
         this.type = type;
 85   
     }
 86   
 
 87   
     protected String classname = null;
 88   
 
 89   
     /**
 90   
      * Set the class name of the FileNameMapper to use.
 91   
      */
 92  0
     public void setClassname(String classname) {
 93  0
         if (isReference()) {
 94  0
             throw tooManyAttributes();
 95   
         }
 96  0
         this.classname = classname;
 97   
     }
 98   
 
 99   
     protected Path classpath = null;
 100   
 
 101   
     /**
 102   
      * Set the classpath to load the FileNameMapper through (attribute).
 103   
      */
 104  0
     public void setClasspath(Path classpath) {
 105  0
         if (isReference()) {
 106  0
             throw tooManyAttributes();
 107   
         }
 108  0
         if (this.classpath == null) {
 109  0
             this.classpath = classpath;
 110   
         } else {
 111  0
             this.classpath.append(classpath);
 112   
         }
 113   
     }
 114   
 
 115   
     /**
 116   
      * Set the classpath to load the FileNameMapper through (nested element).
 117   
      */
 118  0
     public Path createClasspath() {
 119  0
         if (isReference()) {
 120  0
             throw noChildrenAllowed();
 121   
         }
 122  0
         if (this.classpath == null) {
 123  0
             this.classpath = new Path(getProject());
 124   
         }
 125  0
         return this.classpath.createPath();
 126   
     }
 127   
 
 128   
     /**
 129   
      * Set the classpath to load the FileNameMapper through via
 130   
      * reference (attribute).
 131   
      */
 132  0
     public void setClasspathRef(Reference r) {
 133  0
         if (isReference()) {
 134  0
             throw tooManyAttributes();
 135   
         }
 136  0
         createClasspath().setRefid(r);
 137   
     }
 138   
 
 139   
     protected String from = null;
 140   
 
 141   
     /**
 142   
      * Set the argument to FileNameMapper.setFrom
 143   
      */
 144  7
     public void setFrom(String from) {
 145  7
         if (isReference()) {
 146  1
             throw tooManyAttributes();
 147   
         }
 148  6
         this.from = from;
 149   
     }
 150   
 
 151   
     protected String to = null;
 152   
 
 153   
     /**
 154   
      * Set the argument to FileNameMapper.setTo
 155   
      */
 156  9
     public void setTo(String to) {
 157  9
         if (isReference()) {
 158  1
             throw tooManyAttributes();
 159   
         }
 160  8
         this.to = to;
 161   
     }
 162   
 
 163   
     /**
 164   
      * Make this Mapper instance a reference to another Mapper.
 165   
      *
 166   
      * <p>You must not set any other attribute if you make it a
 167   
      * reference.</p>
 168   
      */
 169  9
     public void setRefid(Reference r) throws BuildException {
 170  9
         if (type != null || from != null || to != null) {
 171  1
             throw tooManyAttributes();
 172   
         }
 173  8
         super.setRefid(r);
 174   
     }
 175   
 
 176   
     /**
 177   
      * Returns a fully configured FileNameMapper implementation.
 178   
      */
 179  139
     public FileNameMapper getImplementation() throws BuildException {
 180  139
         if (isReference()) {
 181  4
             return getRef().getImplementation();
 182   
         }
 183   
 
 184  135
         if (type == null && classname == null) {
 185  0
             throw new BuildException("one of the attributes type or classname is required");
 186   
         }
 187   
 
 188  135
         if (type != null && classname != null) {
 189  0
             throw new BuildException("must not specify both type and classname attribute");
 190   
         }
 191   
 
 192  135
         try {
 193  135
             if (type != null) {
 194  135
                 classname = type.getImplementation();
 195   
             }
 196   
 
 197  135
             Class c = null;
 198  135
             if (classpath == null) {
 199  135
                 c = Class.forName(classname);
 200   
             } else {
 201  0
                 AntClassLoader al = getProject().createClassLoader(classpath);
 202  0
                 c = al.loadClass(classname);
 203  0
                 AntClassLoader.initializeClass(c);
 204   
             }
 205   
 
 206  135
             FileNameMapper m = (FileNameMapper) c.newInstance();
 207  135
             m.setFrom(from);
 208  135
             m.setTo(to);
 209  135
             return m;
 210   
         } catch (BuildException be) {
 211  0
             throw be;
 212   
         } catch (Throwable t) {
 213  0
             throw new BuildException(t);
 214   
         } finally {
 215  135
             if (type != null) {
 216  135
                 classname = null;
 217   
             }
 218   
         }
 219   
     }
 220   
 
 221   
     /**
 222   
      * Performs the check for circular references and returns the
 223   
      * referenced Mapper.
 224   
      */
 225  4
     protected Mapper getRef() {
 226  4
         if (!isChecked()) {
 227  3
             Stack stk = new Stack();
 228  3
             stk.push(this);
 229  3
             dieOnCircularReference(stk, getProject());
 230   
         }
 231   
 
 232  2
         Object o = getRefid().getReferencedObject(getProject());
 233  2
         if (!(o instanceof Mapper)) {
 234  0
             String msg = getRefid().getRefId() + " doesn\'t denote a mapper";
 235  0
             throw new BuildException(msg);
 236   
         } else {
 237  2
             return (Mapper) o;
 238   
         }
 239   
     }
 240   
 
 241   
     /**
 242   
      * Class as Argument to FileNameMapper.setType.
 243   
      */
 244   
     public static class MapperType extends EnumeratedAttribute {
 245   
         private Properties implementations;
 246   
 
 247  10
         public MapperType() {
 248  10
             implementations = new Properties();
 249  10
             implementations.put("identity",
 250   
                                 "org.apache.tools.ant.util.IdentityMapper");
 251  10
             implementations.put("flatten",
 252   
                                 "org.apache.tools.ant.util.FlatFileNameMapper");
 253  10
             implementations.put("glob",
 254   
                                 "org.apache.tools.ant.util.GlobPatternMapper");
 255  10
             implementations.put("merge",
 256   
                                 "org.apache.tools.ant.util.MergingMapper");
 257  10
             implementations.put("regexp",
 258   
                                 "org.apache.tools.ant.util.RegexpPatternMapper");
 259  10
             implementations.put("package",
 260   
                                 "org.apache.tools.ant.util.PackageNameMapper");
 261   
         }
 262   
 
 263  10
         public String[] getValues() {
 264  10
             return new String[] {"identity", "flatten", "glob",
 265   
                                  "merge", "regexp", "package"};
 266   
         }
 267   
 
 268  135
         public String getImplementation() {
 269  135
             return implementations.getProperty(getValue());
 270   
         }
 271   
     }
 272   
 
 273   
 }
 274