Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 202   Methods: 3
NCLOC: 69   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PathTokenizer.java 20.8% 34.4% 100% 32.2%
 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   
 package org.apache.tools.ant;
 55   
 
 56   
 import java.io.File;
 57   
 import java.util.NoSuchElementException;
 58   
 import java.util.StringTokenizer;
 59   
 import org.apache.tools.ant.taskdefs.condition.Os;
 60   
 
 61   
 /**
 62   
  * A Path tokenizer takes a path and returns the components that make up
 63   
  * that path.
 64   
  *
 65   
  * The path can use path separators of either ':' or ';' and file separators
 66   
  * of either '/' or '\'.
 67   
  *
 68   
  * @author Conor MacNeill
 69   
  * @author <a href="mailto:jtulley@novell.com">Jeff Tulley</a> 
 70   
  */ 
 71   
 public class PathTokenizer {
 72   
     /**
 73   
      * A tokenizer to break the string up based on the ':' or ';' separators.
 74   
      */
 75   
     private StringTokenizer tokenizer;
 76   
     
 77   
     /**
 78   
      * A String which stores any path components which have been read ahead
 79   
      * due to DOS filesystem compensation.
 80   
      */
 81   
     private String lookahead = null;
 82   
 
 83   
     /**
 84   
      * A boolean that determines if we are running on Novell NetWare, which
 85   
      * exhibits slightly different path name characteristics (multi-character
 86   
      * volume / drive names)
 87   
      */
 88   
     private boolean onNetWare = Os.isFamily("netware");
 89   
 
 90   
     /**
 91   
      * Flag to indicate whether or not we are running on a platform with a
 92   
      * DOS style filesystem
 93   
      */
 94   
     private boolean dosStyleFilesystem;
 95   
 
 96   
     /**
 97   
      * Constructs a path tokenizer for the specified path.
 98   
      * 
 99   
      * @param path The path to tokenize. Must not be <code>null</code>.
 100   
      */
 101  211
     public PathTokenizer(String path) {
 102  211
         if (onNetWare) {
 103   
             // For NetWare, use the boolean=true mode, so we can use delimiter 
 104   
             // information to make a better decision later.
 105  0
             tokenizer = new StringTokenizer(path, ":;", true);
 106   
         } else {
 107   
             // on Windows and Unix, we can ignore delimiters and still have
 108   
             // enough information to tokenize correctly.
 109  211
             tokenizer = new StringTokenizer(path, ":;", false);
 110   
         }
 111  211
         dosStyleFilesystem = File.pathSeparatorChar == ';'; 
 112   
     }
 113   
 
 114   
     /**
 115   
      * Tests if there are more path elements available from this tokenizer's
 116   
      * path. If this method returns <code>true</code>, then a subsequent call 
 117   
      * to nextToken will successfully return a token.
 118   
      * 
 119   
      * @return <code>true</code> if and only if there is at least one token 
 120   
      * in the string after the current position; <code>false</code> otherwise.
 121   
      */
 122  4038
     public boolean hasMoreTokens() {
 123  4038
         if (lookahead != null) {
 124  0
             return true;
 125   
         }
 126   
         
 127  4038
         return tokenizer.hasMoreTokens();
 128   
     }
 129   
     
 130   
     /**
 131   
      * Returns the next path element from this tokenizer.
 132   
      * 
 133   
      * @return the next path element from this tokenizer.
 134   
      * 
 135   
      * @exception NoSuchElementException if there are no more elements in this 
 136   
      *            tokenizer's path.
 137   
      */
 138  3827
     public String nextToken() throws NoSuchElementException {
 139  3827
         String token = null;
 140  3827
         if (lookahead != null) {
 141  0
             token = lookahead;
 142  0
             lookahead = null;
 143   
         } else {
 144  3827
             token = tokenizer.nextToken().trim();
 145   
         }            
 146   
             
 147  3827
         if (!onNetWare) {
 148  3827
             if (token.length() == 1 && Character.isLetter(token.charAt(0))
 149   
                                     && dosStyleFilesystem
 150   
                                     && tokenizer.hasMoreTokens()) {
 151   
                 // we are on a dos style system so this path could be a drive
 152   
                 // spec. We look at the next token
 153  0
                 String nextToken = tokenizer.nextToken().trim();
 154  0
                 if (nextToken.startsWith("\\") || nextToken.startsWith("/")) {
 155   
                     // we know we are on a DOS style platform and the next path
 156   
                     // starts with a slash or backslash, so we know this is a 
 157   
                     // drive spec
 158  0
                     token += ":" + nextToken;
 159   
                 } else {
 160   
                     // store the token just read for next time
 161  0
                     lookahead = nextToken;
 162   
                 }
 163   
             }
 164   
         } else {
 165   
             // we are on NetWare, tokenizing is handled a little differently,
 166   
             // due to the fact that NetWare has multiple-character volume names.
 167  0
             if (token.equals(File.pathSeparator) || token.equals(":")) {
 168   
                 // ignore ";" and get the next token
 169  0
                 token = tokenizer.nextToken().trim();
 170   
             }
 171   
             
 172  0
             if (tokenizer.hasMoreTokens()) {
 173   
                 // this path could be a drive spec, so look at the next token
 174  0
                 String nextToken = tokenizer.nextToken().trim();
 175   
                 
 176   
                 // make sure we aren't going to get the path separator next
 177  0
                 if (!nextToken.equals(File.pathSeparator)) {
 178  0
                     if (nextToken.equals(":")) {
 179  0
                         if (!token.startsWith("/") && !token.startsWith("\\")){ 
 180   
                             // it indeed is a drive spec, get the next bit
 181  0
                             String oneMore = tokenizer.nextToken().trim();
 182  0
                             if (!oneMore.equals(File.pathSeparator)) {
 183  0
                                 token += ":" + oneMore;
 184   
                             } else {
 185  0
                                 token += ":";
 186  0
                                 lookahead = oneMore;
 187   
                             }
 188   
                         }
 189   
                         // implicit else: ignore the ':' since we have either a
 190   
                         // UNIX or a relative path
 191   
                     } else {
 192   
                         // store the token just read for next time
 193  0
                         lookahead = nextToken;
 194   
                     }
 195   
                 }
 196   
             }
 197   
         }
 198  3827
         return token;
 199   
     }
 200   
 }
 201   
           
 202