Handle hostnames with upper-case letters
[webmin.git] / proc / StringSplitter.java
1 import java.util.Vector;
2
3 // StringSplitter
4 // A stringsplitter object splits a string into a number of substrings,
5 // each separated by one separator character. Separator characters can be
6 // included in the string by escaping them with a \
7 public class StringSplitter
8 {
9         Vector parts = new Vector();
10         int pos = 0;
11
12         StringSplitter(String str, char sep)
13         {
14         this(str, sep, true);
15         }
16
17         StringSplitter(String str, char sep, boolean escape)
18         {
19         StringBuffer current;
20
21         parts.addElement(current = new StringBuffer());
22         for(int i=0; i<str.length(); i++) {
23                 char c = str.charAt(i);
24                 if (c == '\\' && i != str.length()-1 && escape)
25                         current.append(str.charAt(++i));
26                 else if (c == sep)
27                         parts.addElement(current = new StringBuffer());
28                 else
29                         current.append(c);
30                 }
31         }
32
33         // countTokens
34         // The number of tokens left in the string
35         int countTokens()
36         {
37         return parts.size() - pos;
38         }
39
40         // hasMoreTokens
41         // Can we call nextToken?
42         boolean hasMoreTokens()
43         {
44         return pos < parts.size();
45         }
46
47         // nextToken
48         // Returns the string value of the next token
49         String nextToken()
50         {
51         if (pos < parts.size())
52                 return ((StringBuffer)parts.elementAt(pos++)).toString();
53         else
54                 return null;
55         }
56
57         // gettokens
58         // Returns a vector of strings split from the given input string
59         Vector gettokens()
60         {
61         return parts;
62         }
63 }
64
65
66 // StringJoiner
67 // The complement of StringSplitter. Takes a number of substrings and adds
68 // them to a string, separated by some character. If the separator character
69 // appears in one of the substrings, escape it with a \
70 class StringJoiner
71 {
72         char sep;
73         StringBuffer str = new StringBuffer();
74         int count = 0;
75
76         // Create a new StringJoiner using the given separator
77         StringJoiner(char s)
78         {
79         sep = s;
80         }
81
82         // add
83         // Add one string, and a separator
84         void add(String s)
85         {
86         if (count != 0)
87                 str.append(sep);
88         for(int i=0; i<s.length(); i++) {
89                 char c = s.charAt(i);
90                 if (c == sep || c == '\\') str.append('\\');
91                 str.append(c);
92                 }
93         count++;
94         }
95
96         // toString
97         // Get the resulting string
98         public String toString()
99         {
100         return str.toString();
101         }
102 }
103