Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StringArrayParser |
|
| 11.0;11 |
1 | /* | |
2 | * Copyright 2003-2004 The Apache Software Foundation | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.apache.commons.convert1.array; | |
17 | ||
18 | ||
19 | import java.util.ArrayList; | |
20 | import java.util.List; | |
21 | import java.io.IOException; | |
22 | import java.io.StreamTokenizer; | |
23 | import java.io.StringReader; | |
24 | ||
25 | import org.apache.commons.convert1.ConversionException; | |
26 | ||
27 | /** | |
28 | * Temporary file containing the String to String[] | |
29 | * parsing. | |
30 | */ | |
31 | 0 | public class StringArrayParser { |
32 | ||
33 | // TODO: This needs to be extracted out so that the | |
34 | // parsing part is not tied to an incoming String. | |
35 | /** | |
36 | * <p>Parse an incoming String of the form similar to an array initializer | |
37 | * in the Java language into a <code>List</code> individual Strings | |
38 | * for each element, according to the following rules.</p> | |
39 | * <ul> | |
40 | * <li>The string must have matching '{' and '}' delimiters around | |
41 | * a comma-delimited list of values.</li> | |
42 | * <li>Whitespace before and after each element is stripped. | |
43 | * <li>If an element is itself delimited by matching single or double | |
44 | * quotes, the usual rules for interpreting a quoted String apply.</li> | |
45 | * </ul> | |
46 | * | |
47 | * @param svalue String value to be parsed | |
48 | * | |
49 | * @exception ConversionException if the syntax of <code>svalue</code> | |
50 | * is not syntactically valid | |
51 | * @exception NullPointerException if <code>svalue</code> | |
52 | * is <code>null</code> | |
53 | */ | |
54 | static List parseElements(String svalue) { | |
55 | ||
56 | // Validate the passed argument | |
57 | 0 | if (svalue == null) { |
58 | 0 | throw new NullPointerException(); |
59 | } | |
60 | ||
61 | // Trim any matching '{' and '}' delimiters | |
62 | 0 | svalue = svalue.trim(); |
63 | 0 | if (svalue.startsWith("{") && svalue.endsWith("}")) { |
64 | 0 | svalue = svalue.substring(1, svalue.length() - 1); |
65 | } | |
66 | ||
67 | try { | |
68 | ||
69 | // Set up a StreamTokenizer on the characters in this String | |
70 | 0 | StreamTokenizer st = |
71 | new StreamTokenizer(new StringReader(svalue)); | |
72 | 0 | st.whitespaceChars(',',','); // Commas are delimiters |
73 | 0 | st.ordinaryChars('0', '9'); // Needed to turn off numeric flag |
74 | 0 | st.ordinaryChars('.', '.'); |
75 | 0 | st.ordinaryChars('-', '-'); |
76 | 0 | st.wordChars('0', '9'); // Needed to make part of tokens |
77 | 0 | st.wordChars('.', '.'); |
78 | 0 | st.wordChars('-', '-'); |
79 | ||
80 | // Split comma-delimited tokens into a List | |
81 | 0 | ArrayList list = new ArrayList(); |
82 | while (true) { | |
83 | 0 | int ttype = st.nextToken(); |
84 | 0 | if ((ttype == StreamTokenizer.TT_WORD) || |
85 | (ttype > 0)) { | |
86 | 0 | list.add(st.sval); |
87 | 0 | } else if (ttype == StreamTokenizer.TT_EOF) { |
88 | 0 | break; |
89 | } else { | |
90 | 0 | throw new ConversionException |
91 | ("Encountered token of type " + ttype); | |
92 | } | |
93 | 0 | } |
94 | ||
95 | // Return the completed list | |
96 | 0 | return (list); |
97 | ||
98 | 0 | } catch (IOException e) { |
99 | ||
100 | 0 | throw new ConversionException(e); |
101 | ||
102 | } | |
103 | ||
104 | ||
105 | ||
106 | } | |
107 | ||
108 | } |