KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > sps > common > 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 package sync4j.syncclient.sps.common.util;
20
21
22 /**
23  * This class implements string parser by parserDelimiter.
24  *
25  * @author Fabio Maggi @ Funambol
26  * @version $Id: StrParser.java,v 1.3 2005/01/19 11:18:36 fabius Exp $
27  **/

28 public class StrParser {
29
30     // ------------------------------------------------------------ Private data
31

32     private int position;
33
34     private String JavaDoc str = null;
35     private String JavaDoc parserDelim = null;
36
37     //------------------------------------------------------------- Constructors
38

39     /**
40      * Constructs a string parser for the specified string by parserDelimiter.
41      *
42      * @param str string to be parsed
43      * @param parserDelim delimiter
44      **/

45     public StrParser(String JavaDoc str, String JavaDoc parserDelim) {
46
47         position = 0;
48
49         this.str = str;
50
51         this.parserDelim = parserDelim;
52
53     }
54
55     //------------------------------------------------------------- Public methods
56

57     /**
58      * Verify if available more elements about this StrParser.
59      *
60      * @return <code>true</code> if available more elements.
61      * <code>false</code> if not available more elements.
62      **/

63
64     public boolean hasMoreElements() {
65
66         //
67
// low performance
68
//
69
/**
70         if ((position <= str.length()-1) && ( str.substring(position).indexOf(parserDelim) != -1
71
72              || str.substring(position).length() > 0)) {
73
74             return true;
75
76         } else {
77
78             return false;
79
80         }
81
82         */

83
84         //
85
//high performance
86
//
87
if (str.substring(position).indexOf(parserDelim) != -1
88
89              || str.substring(position).length() > 0) {
90
91             return true;
92
93         } else {
94
95             return false;
96
97         }
98
99     }
100
101
102     /**
103      * @return next element about this StrParser.
104      */

105     public String JavaDoc nextElement() {
106
107         int positionOld;
108
109         int positionTmp;
110
111         positionOld = position;
112
113         positionTmp = str.substring(position).indexOf(parserDelim);
114
115         if (positionTmp != -1) {
116
117             position = position + positionTmp + parserDelim.length();
118
119             return str.substring(positionOld, position - parserDelim.length());
120
121         } else {
122
123             position = str.length();
124
125             return str.substring(positionOld, position);
126
127         }
128
129     }
130
131
132     /**
133      * @return number of elements about this StrParser.
134      */

135     public int countElements() {
136
137         int count = 0;
138         int index = 0;
139
140         String JavaDoc strTmp = null;
141
142         strTmp = str;
143
144         while (strTmp != null && strTmp.length() > 0) {
145
146             count++;
147
148             index = strTmp.indexOf(parserDelim);
149
150             if (index == -1) {
151                 break;
152             }
153
154             strTmp = strTmp.substring(index + 1);
155
156         }
157
158         return count;
159
160     }
161
162 }
Popular Tags