1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
package org.postgresql.util;
import java.sql.*;
import java.util.*;
/*
* This class is used to tokenize the text output of org.postgres.
*
* <p>It's mainly used by the geometric classes, but is useful in parsing any
* output from custom data types output from org.postgresql.
*
* @see org.postgresql.geometric.PGbox
* @see org.postgresql.geometric.PGcircle
* @see org.postgresql.geometric.PGlseg
* @see org.postgresql.geometric.PGpath
* @see org.postgresql.geometric.PGpoint
* @see org.postgresql.geometric.PGpolygon
*/
public class PGtokenizer
{
// Our tokens
protected Vector tokens;
/*
* Create a tokeniser.
*
* <p>We could have used StringTokenizer to do this, however, we needed to
* handle nesting of '(' ')' '[' ']' '<' and '>' as these are used
* by the geometric data types.
*
* @param string containing tokens
* @param delim single character to split the tokens
*/
public PGtokenizer(String string, char delim)
{
tokenize(string, delim);
}
/*
* This resets this tokenizer with a new string and/or delimiter.
*
* @param string containing tokens
* @param delim single character to split the tokens
*/
public int tokenize(String string, char delim)
{
tokens = new Vector();
// nest holds how many levels we are in the current token.
// if this is > 0 then we don't split a token when delim is matched.
//
// The Geometric datatypes use this, because often a type may have others
// (usualls PGpoint) imbedded within a token.
//
// Peter 1998 Jan 6 - Added < and > to the nesting rules
int nest = 0, p, s;
for (p = 0, s = 0;p < string.length();p++)
{
char c = string.charAt(p);
// increase nesting if an open character is found
if (c == '(' || c == '[' || c == '<')
nest++;
// decrease nesting if a close character is found
if (c == ')' || c == ']' || c == '>')
nest--;
if (nest == 0 && c == delim)
{
tokens.addElement(string.substring(s, p));
s = p + 1; // +1 to skip the delimiter
}
}
// Don't forget the last token ;-)
if (s < string.length())
tokens.addElement(string.substring(s));
return tokens.size();
}
/*
* @return the number of tokens available
*/
public int getSize()
{
return tokens.size();
}
/*
* @param n Token number ( 0 ... getSize()-1 )
* @return The token value
*/
public String getToken(int n)
{
return (String)tokens.elementAt(n);
}
/*
* This returns a new tokenizer based on one of our tokens.
*
* The geometric datatypes use this to process nested tokens (usually
* PGpoint).
*
* @param n Token number ( 0 ... getSize()-1 )
* @param delim The delimiter to use
* @return A new instance of PGtokenizer based on the token
*/
public PGtokenizer tokenizeToken(int n, char delim)
{
return new PGtokenizer(getToken(n), delim);
}
/*
* This removes the lead/trailing strings from a string
* @param s Source string
* @param l Leading string to remove
* @param t Trailing string to remove
* @return String without the lead/trailing strings
*/
public static String remove(String s, String l, String t)
{
if (s.startsWith(l))
s = s.substring(l.length());
if (s.endsWith(t))
s = s.substring(0, s.length() - t.length());
return s;
}
/*
* This removes the lead/trailing strings from all tokens
* @param l Leading string to remove
* @param t Trailing string to remove
*/
public void remove(String l, String t)
{
for (int i = 0;i < tokens.size();i++)
{
tokens.setElementAt(remove((String)tokens.elementAt(i), l, t), i);
}
}
/*
* Removes ( and ) from the beginning and end of a string
* @param s String to remove from
* @return String without the ( or )
*/
public static String removePara(String s)
{
return remove(s, "(", ")");
}
/*
* Removes ( and ) from the beginning and end of all tokens
* @return String without the ( or )
*/
public void removePara()
{
remove("(", ")");
}
/*
* Removes [ and ] from the beginning and end of a string
* @param s String to remove from
* @return String without the [ or ]
*/
public static String removeBox(String s)
{
return remove(s, "[", "]");
}
/*
* Removes [ and ] from the beginning and end of all tokens
* @return String without the [ or ]
*/
public void removeBox()
{
remove("[", "]");
}
/*
* Removes < and > from the beginning and end of a string
* @param s String to remove from
* @return String without the < or >
*/
public static String removeAngle(String s)
{
return remove(s, "<", ">");
}
/*
* Removes < and > from the beginning and end of all tokens
* @return String without the < or >
*/
public void removeAngle()
{
remove("<", ">");
}
}
|