Debian Codegraph @

ssl-proxies/src/main/java/org/globus/util/QuotedStringTokenizer.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. 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 distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.util;
16
17 import java.util.Enumeration;
18
19 /*
20 * This class is not thread safe.
21 */
22 public class QuotedStringTokenizer implements Enumeration {
23
24 private int limit;
25 private int start;
26 private String str;
27
28 public QuotedStringTokenizer(String str) {
29 this.str = str;
30 start = 0;
31 limit = str.length();
32 }
33
34 public Object nextElement() {
35 return nextToken();
36 }
37
38 public String nextToken() {
39 while ((start < limit) && Character.isWhitespace(str.charAt(start))) {
40 start++; // eliminate leading whitespace
41 }
42
43 if (start == limit) return null;
44
45 StringBuffer buf = new StringBuffer(limit-start);
46 char ch;
47 char quote = str.charAt(start);
48 if (quote == '"' || quote == '\'') {
49 start++;
50 for (int i=start;i<limit;i++) {
51 ch = str.charAt(i);
52 start++;
53 if (ch == quote) {
54 break;
55 } else if (ch == '\\') {
56 buf.append( str.charAt(++i) );
57 start++;
58 } else {
59 buf.append(ch);
60 }
61 }
62 return buf.toString();
63 } else {
64 for (int i=start;i<limit;i++) {
65 ch = str.charAt(i);
66 start++;
67 if (Character.isWhitespace(ch)) {
68 break;
69 } else {
70 buf.append(ch);
71 }
72 }
73 }
74
75 return buf.toString();
76 }
77
78 public boolean hasMoreElements() {
79 return hasMoreTokens();
80 }
81
82 public boolean hasMoreTokens() {
83 while ((start < limit) && (str.charAt(start) <= ' ')) {
84 start++; // eliminate leading whitespace
85 }
86
87 return (start != limit);
88 }
89
90 public int countTokens() {
91 int localStart = start;
92 int i = 0;
93 while( nextToken() != null ) {
94 i++;
95 }
96 start = localStart;
97 return i;
98 }
99
100 }