KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > spds > util > StrParser


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19
20
21 package sync4j.syncclient.spds.util;
22
23
24
25 /**
26
27  * This class implements string parser by parserDelimiter.
28
29  *
30
31  * @author Fabio Maggi @ Funambol
32
33  * @version $Id: StrParser.java,v 1.2 2005/01/19 11:18:36 fabius Exp $
34
35  **/

36
37 public class StrParser {
38
39
40
41     // ------------------------------------------------------------ Private data
42

43
44
45     private int position;
46
47
48
49     private String JavaDoc str = null;
50
51     private String JavaDoc parserDelim = null;
52
53
54
55
56
57     //------------------------------------------------------------- Constructors
58

59
60
61     /**
62
63      * Constructs a string parser for the specified string by parserDelimiter.
64
65      *
66
67      * @param str string to be parsed
68
69      * @param parserDelim delimiter
70
71      **/

72
73     public StrParser(String JavaDoc str, String JavaDoc parserDelim) {
74
75
76
77         position = 0;
78
79
80
81         this.str = str;
82
83         this.parserDelim = parserDelim;
84
85
86
87     }
88
89
90
91     //------------------------------------------------------------- Public methods
92

93
94
95     /**
96
97      * Verify if available more elements about this StrParser.
98
99      *
100
101      * @return <code>true</code> if available more elements.
102
103      * <code>false</code> if not available more elements.
104
105      **/

106
107     public boolean hasMoreElements() {
108
109
110
111         /** low performance
112
113         if ((position <= str.length()-1) && ( str.substring(position).indexOf(parserDelim) != -1
114
115              || str.substring(position).length() > 0)) {
116
117             return true;
118
119         } else {
120
121             return false;
122
123         }
124
125         */

126
127
128
129         //high performance
130

131         if (str.substring(position).indexOf(parserDelim) != -1
132
133              || str.substring(position).length() > 0) {
134
135             return true;
136
137         } else {
138
139             return false;
140
141         }
142
143
144
145     }
146
147
148
149
150
151     /**
152
153      * @return next element about this StrParser.
154
155      */

156
157     public String JavaDoc nextElement() {
158
159
160
161         int positionOld;
162
163         int positionTmp;
164
165
166
167         positionOld = position;
168
169
170
171         positionTmp = str.substring(position).indexOf(parserDelim);
172
173
174
175
176
177         if (positionTmp != -1) {
178
179             position = position + positionTmp + parserDelim.length();
180
181             return str.substring(positionOld, position - parserDelim.length());
182
183         } else {
184
185             position = str.length();
186
187             return str.substring(positionOld, position);
188
189         }
190
191
192
193     }
194
195
196
197
198
199     /**
200
201      * @return number of elements about this StrParser.
202
203      */

204
205     public int countElements() {
206
207
208
209         int count = 0;
210
211         int index = 0;
212
213
214
215         String JavaDoc strTmp = null;
216
217
218
219         strTmp = str;
220
221
222
223         while (strTmp != null && strTmp.length() > 0) {
224
225             count++;
226
227
228
229             index = strTmp.indexOf(parserDelim);
230
231
232
233             if (index == -1)
234
235                 break;
236
237
238
239             strTmp = strTmp.substring(index + 1);
240
241         }
242
243
244
245         return count;
246
247     }
248
249 }
Popular Tags