Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ParameterTranslator |
|
| 2.6666666666666665;2.667 |
1 | /* | |
2 | * Copyright 2005-2008 The Kuali Foundation | |
3 | * | |
4 | * | |
5 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.opensource.org/licenses/ecl2.php | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.kuali.rice.kew.messaging; | |
18 | ||
19 | import java.util.ArrayList; | |
20 | import java.util.List; | |
21 | ||
22 | import org.kuali.rice.kew.util.Utilities; | |
23 | ||
24 | ||
25 | /** | |
26 | * A simple utility class which can handle translated a comma-seperated String | |
27 | * into an array of String paramters and vice-versa. | |
28 | * | |
29 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
30 | */ | |
31 | public class ParameterTranslator { | |
32 | ||
33 | private static final String SLASH_REGEXP = "\\\\"; | |
34 | private static final String SLASH_ESCAPE = "\\\\"; | |
35 | private static final String COMMA_REGEXP = ","; | |
36 | private static final String COMMA_ESCAPE = "\\,"; | |
37 | ||
38 | 0 | private String untranslatedString = ""; |
39 | ||
40 | 0 | public ParameterTranslator() {} |
41 | ||
42 | 0 | public ParameterTranslator(String untranslatedString) { |
43 | 0 | this.untranslatedString = untranslatedString; |
44 | 0 | } |
45 | ||
46 | public void addParameter(String value) { | |
47 | 0 | if (!Utilities.isEmpty(untranslatedString)) { |
48 | 0 | untranslatedString += ","; |
49 | } | |
50 | 0 | untranslatedString += escape(value); |
51 | 0 | } |
52 | ||
53 | private String escape(String value) { | |
54 | 0 | if (Utilities.isEmpty(value)) { |
55 | 0 | return ""; |
56 | } | |
57 | // escape '\' and ',' with "\\" and "\," | |
58 | 0 | value = value.replaceAll(SLASH_REGEXP, SLASH_ESCAPE); |
59 | 0 | value = value.replaceAll(COMMA_REGEXP, COMMA_ESCAPE); |
60 | 0 | return value; |
61 | } | |
62 | ||
63 | public String getUntranslatedString() { | |
64 | 0 | return untranslatedString; |
65 | } | |
66 | ||
67 | public String[] getParameters() { | |
68 | 0 | List strings = new ArrayList(); |
69 | 0 | boolean isEscaped = false; |
70 | 0 | StringBuffer buffer = null; |
71 | 0 | for (int index = 0; index < untranslatedString.length(); index++) { |
72 | 0 | char character = untranslatedString.charAt(index); |
73 | 0 | if (isEscaped) { |
74 | 0 | isEscaped = false; |
75 | 0 | if (buffer == null) { |
76 | 0 | buffer = new StringBuffer(); |
77 | } | |
78 | 0 | buffer.append(character); |
79 | } else { | |
80 | 0 | if (character == '\\') { |
81 | 0 | isEscaped = true; |
82 | 0 | } else if (character == ',') { |
83 | 0 | strings.add(buffer.toString()); |
84 | 0 | buffer = null; |
85 | } else { | |
86 | 0 | if (buffer == null) { |
87 | 0 | buffer = new StringBuffer(); |
88 | } | |
89 | 0 | buffer.append(character); |
90 | } | |
91 | } | |
92 | } | |
93 | // put whatever is left in the buffer (after the last ',') into the list of strings | |
94 | 0 | if (buffer != null) { |
95 | 0 | strings.add(buffer.toString()); |
96 | } | |
97 | 0 | return (String[])strings.toArray(new String[0]); |
98 | } | |
99 | ||
100 | } |