Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 240   Methods: 13
NCLOC: 97   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SelectSelector.java 69.2% 79.2% 84.6% 77%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 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.types.selectors;
 56   
 
 57   
 import java.util.Enumeration;
 58   
 import java.io.File;
 59   
 
 60   
 import org.apache.tools.ant.Project;
 61   
 
 62   
 /**
 63   
  * This selector just holds one other selector and forwards all
 64   
  * requests to it. It exists so that there is a single selector
 65   
  * type that can exist outside of any targets, as an element of
 66   
  * project. It overrides all of the reference stuff so that it
 67   
  * works as expected. Note that this is the only selector you
 68   
  * can reference.
 69   
  *
 70   
  * @author <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
 71   
  * @since 1.5
 72   
  */
 73   
 public class SelectSelector extends BaseSelectorContainer {
 74   
 
 75   
     private String ifProperty;
 76   
     private String unlessProperty;
 77   
 
 78   
     /**
 79   
      * Default constructor.
 80   
      */
 81  73
     public SelectSelector() {
 82   
     }
 83   
 
 84  71
     public String toString() {
 85  71
         StringBuffer buf = new StringBuffer();
 86  71
         if (hasSelectors()) {
 87  71
             buf.append("{select");
 88  71
             if (ifProperty != null) {
 89  0
                 buf.append(" if: ");
 90  0
                 buf.append(ifProperty);
 91   
             }
 92  71
             if (unlessProperty != null) {
 93  35
                 buf.append(" unless: ");
 94  35
                 buf.append(unlessProperty);
 95   
             }
 96  71
             buf.append(" ");
 97  71
             buf.append(super.toString());
 98  71
             buf.append("}");
 99   
         }
 100  71
         return buf.toString();
 101   
     }
 102   
 
 103   
     /**
 104   
      * Performs the check for circular references and returns the
 105   
      * referenced Selector.
 106   
      */
 107  344618
     private SelectSelector getRef() {
 108  344618
         Object o = getCheckedRef(this.getClass(), "SelectSelector");
 109  344618
         return (SelectSelector) o;
 110   
     }
 111   
 
 112   
     /**
 113   
      * Indicates whether there are any selectors here.
 114   
      */
 115  106
     public boolean hasSelectors() {
 116  106
         if (isReference()) {
 117  35
             return getRef().hasSelectors();
 118   
         }
 119  71
         return super.hasSelectors();
 120   
     }
 121   
 
 122   
     /**
 123   
      * Gives the count of the number of selectors in this container
 124   
      */
 125  337600
     public int selectorCount() {
 126  337600
         if (isReference()) {
 127  168452
             return getRef().selectorCount();
 128   
         }
 129  169148
         return super.selectorCount();
 130   
     }
 131   
 
 132   
     /**
 133   
      * Returns the set of selectors as an array.
 134   
      */
 135  0
     public FileSelector[] getSelectors(Project p) {
 136  0
         if (isReference()) {
 137  0
             return getRef().getSelectors(p);
 138   
         }
 139  0
         return super.getSelectors(p);
 140   
     }
 141   
 
 142   
     /**
 143   
      * Returns an enumerator for accessing the set of selectors.
 144   
      */
 145  353690
     public Enumeration selectorElements() {
 146  353690
         if (isReference()) {
 147  176131
             return getRef().selectorElements();
 148   
         }
 149  177559
         return super.selectorElements();
 150   
     }
 151   
 
 152   
     /**
 153   
      * Add a new selector into this container.
 154   
      *
 155   
      * @param selector the new selector to add
 156   
      * @return the selector that was added
 157   
      */
 158  36
     public void appendSelector(FileSelector selector) {
 159  36
         if (isReference()) {
 160  0
             throw noChildrenAllowed();
 161   
         }
 162  36
         super.appendSelector(selector);
 163   
     }
 164   
 
 165   
 
 166   
 
 167   
     /**
 168   
      * Makes sure that there is only one entry, sets an error message if
 169   
      * not.
 170   
      */
 171  169148
     public void verifySettings() {
 172  169148
         int cnt = selectorCount();
 173  169148
         if (cnt < 0 || cnt > 1) {
 174  0
             setError("Only one selector is allowed within the " +
 175   
                      "<selector> tag");
 176   
         }
 177   
     }
 178   
 
 179   
     /**
 180   
      * Ensures that the selector passes the conditions placed
 181   
      * on it with <code>if</code> and <code>unless</code>.
 182   
      */
 183  42938
     public boolean passesConditions() {
 184  42938
         if (ifProperty != null &&
 185   
             getProject().getProperty(ifProperty) == null) {
 186  0
             return false;
 187  42938
         } else if (unlessProperty != null &&
 188   
                    getProject().getProperty(unlessProperty) != null) {
 189  34598
             return false;
 190   
         }
 191  8340
         return true;
 192   
     }
 193   
 
 194   
     /**
 195   
      * Sets the if attribute to a property which must exist for the
 196   
      * selector to select any files.
 197   
      */
 198  0
     public void setIf(String ifProperty) {
 199  0
         this.ifProperty = ifProperty;
 200   
     }
 201   
 
 202   
     /**
 203   
      * Sets the unless attribute to a property which cannot exist for the
 204   
      * selector to select any files.
 205   
      */
 206  35
     public void setUnless(String unlessProperty) {
 207  35
         this.unlessProperty = unlessProperty;
 208   
     }
 209   
 
 210   
     /**
 211   
      * Returns true (the file is selected) only if the if property (if any)
 212   
      * exists, the unless property (if any) doesn't exist, and the
 213   
      * contained selector (if any) selects the file. If there is no contained
 214   
      * selector, return true (because we assume that the point was to test
 215   
      * the if and unless conditions).
 216   
      *
 217   
      * @param basedir the base directory the scan is being done from
 218   
      * @param filename the name of the file to check
 219   
      * @param file a java.io.File object for the filename that the selector
 220   
      * can use
 221   
      * @return whether the file should be selected or not
 222   
      */
 223  42938
     public boolean isSelected(File basedir, String filename, File file) {
 224  42938
         validate();
 225   
 
 226   
         // Deal with if and unless properties first
 227  42938
         if (!(passesConditions())) {
 228  34598
             return false;
 229   
         }
 230   
 
 231  8340
         Enumeration e = selectorElements();
 232  8340
         if (!(e.hasMoreElements())) {
 233  0
             return true;
 234   
         }
 235  8340
         FileSelector f = (FileSelector)e.nextElement();
 236  8340
         return f.isSelected(basedir,filename,file);
 237   
     }
 238   
 }
 239   
 
 240