|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ParserUtility.java | 66.7% | 67.3% | 62.5% | 66.7% |
|
1 |
/*
|
|
2 |
|
|
3 |
VRMoo Common - Virtual Reality Object Oriented MUD Common Code
|
|
4 |
Copyright (C) 2003 VRMoo Development Team
|
|
5 |
|
|
6 |
|
|
7 |
This program is free software; you can redistribute it and/or modify
|
|
8 |
it under the terms of the GNU General Public License as published by
|
|
9 |
the Free Software Foundation; either version 2 of the License, or
|
|
10 |
(at your option) any later version.
|
|
11 |
|
|
12 |
This program is distributed in the hope that it will be useful,
|
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 |
GNU General Public License for more details.
|
|
16 |
|
|
17 |
You should have received a copy of the GNU General Public License
|
|
18 |
along with this program; if not, write to the Free Software
|
|
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 |
|
|
21 |
|
|
22 |
For information about VRMoo and its authors, please visit the website:
|
|
23 |
http://www.vrmoo.org/
|
|
24 |
|
|
25 |
*/
|
|
26 |
|
|
27 |
package org.vrmoo.common.util;
|
|
28 |
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.List;
|
|
31 |
|
|
32 |
import org.vrmoo.common.exception.VRMooParseException;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* This class contains a bunch of static methods to assist with parsing text
|
|
36 |
* into various useful data structures.
|
|
37 |
*
|
|
38 |
* @author Jeff Weston
|
|
39 |
*/
|
|
40 |
public class ParserUtility |
|
41 |
{ |
|
42 |
/**
|
|
43 |
* Specify that we are parsing ints.
|
|
44 |
*/
|
|
45 |
private static final int PARSE_MODE_INT = 1; |
|
46 |
|
|
47 |
/**
|
|
48 |
* Specify that we are parsing floats.
|
|
49 |
*/
|
|
50 |
private static final int PARSE_MODE_FLOAT = 2; |
|
51 |
|
|
52 |
/**
|
|
53 |
* Make the default constructor private since this class isn't meant to be
|
|
54 |
* instantiated.
|
|
55 |
*/
|
|
56 | 0 |
private ParserUtility( )
|
57 |
{ |
|
58 |
} |
|
59 |
|
|
60 |
/**
|
|
61 |
* Utility method for parsing a float value.
|
|
62 |
*
|
|
63 |
* @param floatString the string to parse into a float
|
|
64 |
*
|
|
65 |
* @return the parsed float
|
|
66 |
*
|
|
67 |
* @throws VRMooParseException for any parsing errors that occur
|
|
68 |
*/
|
|
69 | 50 |
public static float parseFloat( String floatString ) |
70 |
throws VRMooParseException
|
|
71 |
{ |
|
72 | 50 |
floatString = floatString.trim( ); |
73 |
|
|
74 | 50 |
float f = 0;
|
75 |
|
|
76 | 50 |
try
|
77 |
{ |
|
78 | 50 |
f = Float.parseFloat( floatString ); |
79 |
} |
|
80 |
catch ( NumberFormatException e )
|
|
81 |
{ |
|
82 | 0 |
throw new VRMooParseException( "Unable to parse float." ); |
83 |
} |
|
84 |
|
|
85 | 50 |
return f;
|
86 |
} |
|
87 |
|
|
88 |
/**
|
|
89 |
* Utility method for parsing a comma seperated array of floats with a check
|
|
90 |
* for the number of floats we should find.
|
|
91 |
*
|
|
92 |
* @param floatData the float data to parse
|
|
93 |
* @param floatCount the number of floats we should find
|
|
94 |
*
|
|
95 |
* @return an array of floats
|
|
96 |
*
|
|
97 |
* @throws VRMooParseException for any parsing errors that occur
|
|
98 |
*/
|
|
99 | 12 |
public static float[ ] parseFloatArray( String floatData, int floatCount ) |
100 |
throws VRMooParseException
|
|
101 |
{ |
|
102 | 12 |
float[ ] floats = parseFloatArray( floatData );
|
103 | 12 |
if ( floats.length != floatCount )
|
104 |
{ |
|
105 | 0 |
throw new VRMooParseException( "Unable to find " + |
106 |
floatCount + " floats." );
|
|
107 |
} |
|
108 | 12 |
return floats;
|
109 |
} |
|
110 |
|
|
111 |
/**
|
|
112 |
* Utility method for parsing a comma seperated array of floats.
|
|
113 |
*
|
|
114 |
* @param floatData the float data to parse
|
|
115 |
*
|
|
116 |
* @return an array of floats
|
|
117 |
*
|
|
118 |
* @throws VRMooParseException for any parsing errors that occur
|
|
119 |
*/
|
|
120 | 12 |
public static float[ ] parseFloatArray( String floatData ) |
121 |
throws VRMooParseException
|
|
122 |
{ |
|
123 | 12 |
List floatList = parseItems( floatData, PARSE_MODE_FLOAT ); |
124 |
|
|
125 | 12 |
float[ ] floats = new float[ floatList.size( ) ]; |
126 | 12 |
for ( int i = 0; i < floatList.size( ); i++ ) |
127 |
{ |
|
128 | 39 |
floats[ i ] = ( ( Float ) floatList.get( i ) ).floatValue( ); |
129 |
} |
|
130 |
|
|
131 | 12 |
return floats;
|
132 |
} |
|
133 |
|
|
134 |
/**
|
|
135 |
* Utility method for parsing an int value.
|
|
136 |
*
|
|
137 |
* @param intString the string to parse into a int
|
|
138 |
*
|
|
139 |
* @return the parsed int
|
|
140 |
*
|
|
141 |
* @throws VRMooParseException for any parsing errors that occur
|
|
142 |
*/
|
|
143 | 2 |
public static int parseInt( String intString ) |
144 |
throws VRMooParseException
|
|
145 |
{ |
|
146 | 2 |
intString = intString.trim( ); |
147 |
|
|
148 | 2 |
int i = 0;
|
149 |
|
|
150 | 2 |
try
|
151 |
{ |
|
152 | 2 |
i = Integer.parseInt( intString ); |
153 |
} |
|
154 |
catch ( NumberFormatException e )
|
|
155 |
{ |
|
156 | 0 |
throw new VRMooParseException( "Unable to parse int." ); |
157 |
} |
|
158 |
|
|
159 | 2 |
return i;
|
160 |
} |
|
161 |
|
|
162 |
/**
|
|
163 |
* Utility method for parsing a comma seperated array of ints.
|
|
164 |
*
|
|
165 |
* @param intData the float data to parse
|
|
166 |
*
|
|
167 |
* @return an array of ints
|
|
168 |
*
|
|
169 |
* @throws VRMooParseException for any parsing errors that occur
|
|
170 |
*/
|
|
171 | 0 |
public static int[ ] parseIntArray( String intData ) |
172 |
throws VRMooParseException
|
|
173 |
{ |
|
174 | 0 |
List intList = parseItems( intData, PARSE_MODE_INT ); |
175 |
|
|
176 | 0 |
int[ ] ints = new int[ intList.size( ) ]; |
177 | 0 |
for ( int i = 0; i < intList.size( ); i++ ) |
178 |
{ |
|
179 | 0 |
ints[ i ] = ( ( Integer ) intList.get( i ) ).intValue( ); |
180 |
} |
|
181 |
|
|
182 | 0 |
return ints;
|
183 |
} |
|
184 |
|
|
185 |
/**
|
|
186 |
* Utility method for parsing a long value.
|
|
187 |
*
|
|
188 |
* @param longString the string to parse into a long
|
|
189 |
*
|
|
190 |
* @return the parsed long
|
|
191 |
*
|
|
192 |
* @throws VRMooParseException for any parsing errors that occur
|
|
193 |
*/
|
|
194 | 0 |
public static long parseLong( String longString ) |
195 |
throws VRMooParseException
|
|
196 |
{ |
|
197 | 0 |
longString = longString.trim( ); |
198 |
|
|
199 | 0 |
long l = 0;
|
200 |
|
|
201 | 0 |
try
|
202 |
{ |
|
203 | 0 |
l = Long.parseLong( longString ); |
204 |
} |
|
205 |
catch ( NumberFormatException e )
|
|
206 |
{ |
|
207 | 0 |
throw new VRMooParseException( "Unable to parse long." ); |
208 |
} |
|
209 |
|
|
210 | 0 |
return l;
|
211 |
} |
|
212 |
|
|
213 |
/**
|
|
214 |
* Utility method for parsing a comma seperated array of items.
|
|
215 |
*
|
|
216 |
* @param data the data to parse
|
|
217 |
* @param parseMode which type of items we are parsing
|
|
218 |
*
|
|
219 |
* @return a <code>List</code> of the items parsed
|
|
220 |
*
|
|
221 |
* @throws VRMooParseException for any parsing errors that occur
|
|
222 |
*/
|
|
223 | 12 |
private static List parseItems( String data, int parseMode ) |
224 |
throws VRMooParseException
|
|
225 |
{ |
|
226 | 12 |
List list = new ArrayList( );
|
227 | 12 |
data = data.trim( ); |
228 |
|
|
229 | 12 |
if ( data.length( ) > 0 )
|
230 |
{ |
|
231 | 12 |
int nextIndex;
|
232 | 12 |
do
|
233 |
{ |
|
234 | 39 |
nextIndex = data.indexOf( "," );
|
235 | 39 |
String curItem; |
236 | 39 |
if ( nextIndex == -1 )
|
237 |
{ |
|
238 | 12 |
curItem = data; |
239 |
} |
|
240 |
else
|
|
241 |
{ |
|
242 | 27 |
curItem = data.substring( 0, nextIndex ).trim( ); |
243 | 27 |
data = data.substring( nextIndex + 1 ).trim( ); |
244 |
} |
|
245 | 39 |
switch ( parseMode )
|
246 |
{ |
|
247 |
case PARSE_MODE_INT :
|
|
248 | 0 |
list.add( new Integer( parseInt( curItem ) ) );
|
249 | 0 |
break;
|
250 |
case PARSE_MODE_FLOAT :
|
|
251 | 39 |
list.add( new Float( parseFloat( curItem ) ) );
|
252 | 39 |
break;
|
253 |
} |
|
254 |
} |
|
255 | 39 |
while ( nextIndex != -1 );
|
256 |
} |
|
257 |
|
|
258 | 12 |
return list;
|
259 |
} |
|
260 |
} |
|
261 |
|
|