View Javadoc

1   /*
2    * $Id: StringTokenizer2.java,v 1.2 2007/07/19 12:12:04 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.lang;
5   
6   import java.util.NoSuchElementException;
7   
8   /**
9    * 
10   * @author Java Team + Olivier Oeuillot (latest modification by $Author:
11   *         oeuillot $)
12   * @version $Revision: 1.2 $ $Date: 2007/07/19 12:12:04 $
13   */
14  public class StringTokenizer2 {
15  
16      private static final int MAX_STRING_SIZE = 16;
17  
18      private final char buffer[];
19  
20      private final String original;
21  
22      private final int maxPosition;
23  
24      private final boolean returnDelimiters;
25  
26      private String delimiters;
27  
28      private int currentPosition;
29  
30      private int newPosition;
31  
32      private boolean delimsChanged;
33  
34      private char maxDelimChar;
35  
36      public StringTokenizer2(String str, String delimiters, boolean returnDelims) {
37          this.buffer = str.toCharArray();
38          this.maxPosition = this.buffer.length;
39          this.original = (maxPosition <= MAX_STRING_SIZE) ? str : null;
40          this.returnDelimiters = returnDelims;
41  
42          setDelimiter(delimiters);
43  
44          currentPosition = 0;
45          newPosition = -1;
46          delimsChanged = false;
47  
48      }
49  
50      public StringTokenizer2(String str, String delim) {
51          this(str, delim, false);
52      }
53  
54      public StringTokenizer2(String str) {
55          this(str, " \t\n\r\f");
56      }
57  
58      private void setDelimiter(String delimiters) {
59          if (delimiters == null) {
60              throw new NullPointerException();
61          }
62          this.delimiters = delimiters;
63  
64          char m = 0;
65          for (int i = 0; i < delimiters.length(); i++) {
66              char c = delimiters.charAt(i);
67              if (m < c) {
68                  m = c;
69              }
70          }
71          this.maxDelimChar = m;
72      }
73  
74      private int skipDelimiters(int position) {
75          if (returnDelimiters) {
76              return position;
77          }
78  
79          for (; position < maxPosition; position++) {
80              char c = buffer[position];
81              if ((c > maxDelimChar) || (delimiters.indexOf(c) < 0)) {
82                  return position;
83              }
84          }
85          return position;
86      }
87  
88      /**
89       * Skips ahead from startPos and returns the index of the next delimiter
90       * character encountered, or maxPosition if no such delimiter is found.
91       */
92      private int scanToken(int startPos) {
93          int position = startPos;
94          for (; position < maxPosition; position++) {
95              char c = buffer[position];
96              if ((c <= maxDelimChar) && (delimiters.indexOf(c) >= 0)) {
97                  break;
98              }
99          }
100 
101         if (returnDelimiters && (startPos == position)) {
102             // On a rien trouvé, on cherche un séparateur
103             char c = buffer[position];
104             if ((c <= maxDelimChar) && (delimiters.indexOf(c) >= 0)) {
105                 position++;
106             }
107         }
108         return position;
109     }
110 
111     public boolean hasMoreTokens() {
112         newPosition = skipDelimiters(currentPosition);
113 
114         return (newPosition < maxPosition);
115     }
116 
117     public String nextToken() {
118 
119         currentPosition = (newPosition >= 0 && !delimsChanged) ? newPosition
120                 : skipDelimiters(currentPosition);
121 
122         /* Reset these anyway */
123         delimsChanged = false;
124         newPosition = -1;
125 
126         if (currentPosition >= maxPosition) {
127             throw new NoSuchElementException();
128         }
129 
130         int start = currentPosition;
131         currentPosition = scanToken(currentPosition);
132 
133         if (original != null) {
134             return original.substring(start, currentPosition);
135         }
136 
137         return new String(buffer, start, currentPosition - start);
138     }
139 
140     public String nextToken(String delim) {
141         setDelimiter(delim);
142 
143         /* delimiter string specified, so set the appropriate flag. */
144         delimsChanged = true;
145 
146         return nextToken();
147     }
148 
149     public int countTokens() {
150         int count = 0;
151 
152         for (int pos = currentPosition; pos < maxPosition; count++) {
153             pos = skipDelimiters(pos);
154             if (pos >= maxPosition) {
155                 // Fin de la string
156                 return maxPosition;
157             }
158 
159             pos = scanToken(pos);
160         }
161 
162         return count;
163     }
164 }