Coverage Report - org.codehaus.plexus.util.cli.EnhancedStringTokenizer
 
Classes in this File Line Coverage Branch Coverage Complexity
EnhancedStringTokenizer
0%
0/42
0%
0/22
3,286
 
 1  
 package org.codehaus.plexus.util.cli;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.util.StringTokenizer;
 23  
 
 24  
 /**
 25  
  * The java.util.StringTokenizer is horribly broken.
 26  
  * Given the string  1,,,3,,4      (, delim)
 27  
  * It will return 1,3,4
 28  
  * Which is clearly wrong - 1,EMPTY,EMPTY,3,EMPTY,4 is what it should return
 29  
  */
 30  
 public final class EnhancedStringTokenizer
 31  
 {
 32  0
     private StringTokenizer cst = null;
 33  
 
 34  
     String cdelim;
 35  
 
 36  
     final boolean cdelimSingleChar;
 37  
 
 38  
     final char cdelimChar;
 39  
 
 40  
     boolean creturnDelims;
 41  
 
 42  0
     String lastToken = null;
 43  
 
 44  0
     boolean delimLast = true;
 45  
 
 46  
     public EnhancedStringTokenizer( String str )
 47  
     {
 48  0
         this( str, " \t\n\r\f", false );
 49  0
     }
 50  
 
 51  
     public EnhancedStringTokenizer( String str, String delim )
 52  
     {
 53  0
         this( str, delim, false );
 54  0
     }
 55  
 
 56  
     public EnhancedStringTokenizer( String str, String delim, boolean returnDelims )
 57  0
     {
 58  0
         cst = new StringTokenizer( str, delim, true );
 59  0
         cdelim = delim;
 60  0
         creturnDelims = returnDelims;
 61  0
         cdelimSingleChar = ( delim.length() == 1 );
 62  0
         cdelimChar = delim.charAt( 0 );
 63  0
     }
 64  
 
 65  
     public boolean hasMoreTokens()
 66  
     {
 67  0
         return cst.hasMoreTokens();
 68  
     }
 69  
 
 70  
     private String internalNextToken()
 71  
     {
 72  0
         if ( lastToken != null )
 73  
         {
 74  0
             String last = lastToken;
 75  0
             lastToken = null;
 76  0
             return last;
 77  
         }
 78  
 
 79  0
         String token = cst.nextToken();
 80  0
         if ( isDelim( token ) )
 81  
         {
 82  0
             if ( delimLast )
 83  
             {
 84  0
                 lastToken = token;
 85  0
                 return "";
 86  
             }
 87  
             else
 88  
             {
 89  0
                 delimLast = true;
 90  0
                 return token;
 91  
             }
 92  
         }
 93  
         else
 94  
         {
 95  0
             delimLast = false;
 96  0
             return token;
 97  
         }
 98  
     }
 99  
 
 100  
     public String nextToken()
 101  
     {
 102  0
         String token = internalNextToken();
 103  0
         if ( creturnDelims )
 104  
         {
 105  0
             return token;
 106  
         }
 107  0
         if ( isDelim( token ) )
 108  
         {
 109  0
             return hasMoreTokens() ? internalNextToken() : "";
 110  
         }
 111  
         else
 112  
         {
 113  0
             return token;
 114  
         }
 115  
     }
 116  
 
 117  
     private boolean isDelim( String str )
 118  
     {
 119  0
         if ( str.length() == 1 )
 120  
         {
 121  0
             char ch = str.charAt( 0 );
 122  0
             if ( cdelimSingleChar )
 123  
             {
 124  0
                 if ( cdelimChar == ch )
 125  
                 {
 126  0
                     return true;
 127  
                 }
 128  
             }
 129  
             else
 130  
             {
 131  0
                 if ( cdelim.indexOf( ch ) >= 0 )
 132  
                 {
 133  0
                     return true;
 134  
                 }
 135  
             }
 136  
         }
 137  0
         return false;
 138  
 
 139  
     }
 140  
 }