Coverage Report - org.apache.tapestry.util.StringSplitter
 
Classes in this File Line Coverage Branch Coverage Complexity
StringSplitter
0%
0/33
0%
0/14
3.667
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.util;
 16  
 
 17  
 /**
 18  
  * Used to split a string into substrings based on a single character delimiter.
 19  
  * A fast, simple version of {@link java.util.StringTokenizer}.
 20  
  * 
 21  
  * @author Howard Lewis Ship
 22  
  */
 23  
 
 24  
 public class StringSplitter
 25  
 {
 26  
 
 27  
     private char _delimiter;
 28  
 
 29  
     public StringSplitter(char delimiter)
 30  0
     {
 31  0
         this._delimiter = delimiter;
 32  0
     }
 33  
 
 34  
     public char getDelimiter()
 35  
     {
 36  0
         return _delimiter;
 37  
     }
 38  
 
 39  
     /**
 40  
      * Splits a string on the delimter into an array of String tokens. The
 41  
      * delimiters are not included in the tokens. Null tokens (caused by two
 42  
      * consecutive delimiter) are reduced to an empty string. Leading delimiters
 43  
      * are ignored.
 44  
      */
 45  
 
 46  
     public String[] splitToArray(String value)
 47  
     {
 48  
         char[] buffer;
 49  
         int i;
 50  
         String[] result;
 51  0
         int resultCount = 0;
 52  
         int start;
 53  
         int length;
 54  
         String token;
 55  
         String[] newResult;
 56  0
         boolean first = true;
 57  
 
 58  0
         buffer = value.toCharArray();
 59  
 
 60  0
         result = new String[3];
 61  
 
 62  0
         start = 0;
 63  0
         length = 0;
 64  
 
 65  0
         for(i = 0; i < buffer.length; i++)
 66  
         {
 67  0
             if (buffer[i] != _delimiter)
 68  
             {
 69  0
                 length++;
 70  0
                 continue;
 71  
             }
 72  
 
 73  
             // This is used to ignore leading delimiter(s).
 74  
 
 75  0
             if (length > 0 || !first)
 76  
             {
 77  0
                 token = new String(buffer, start, length);
 78  
 
 79  0
                 if (resultCount == result.length)
 80  
                 {
 81  0
                     newResult = new String[result.length * 2];
 82  
 
 83  0
                     System.arraycopy(result, 0, newResult, 0, result.length);
 84  
 
 85  0
                     result = newResult;
 86  
                 }
 87  
 
 88  0
                 result[resultCount++] = token;
 89  
 
 90  0
                 first = false;
 91  
             }
 92  
 
 93  0
             start = i + 1;
 94  0
             length = 0;
 95  
         }
 96  
 
 97  
         // Special case: if the string contains no delimiters
 98  
         // then it isn't really split. Wrap the input string
 99  
         // in an array and return. This is a little optimization
 100  
         // to prevent a new String instance from being
 101  
         // created unnecessarily.
 102  
 
 103  0
         if (start == 0 && length == buffer.length)
 104  
         {
 105  0
             result = new String[1];
 106  0
             result[0] = value;
 107  0
             return result;
 108  
         }
 109  
 
 110  
         // If the string is all delimiters, then this
 111  
         // will result in a single empty token.
 112  
 
 113  0
         token = new String(buffer, start, length);
 114  
 
 115  0
         newResult = new String[resultCount + 1];
 116  0
         System.arraycopy(result, 0, newResult, 0, resultCount);
 117  0
         newResult[resultCount] = token;
 118  
 
 119  0
         return newResult;
 120  
     }
 121  
 }