1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
27
28
29
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 private String untranslatedString = "";
39
40 public ParameterTranslator() {}
41
42 public ParameterTranslator(String untranslatedString) {
43 this.untranslatedString = untranslatedString;
44 }
45
46 public void addParameter(String value) {
47 if (!Utilities.isEmpty(untranslatedString)) {
48 untranslatedString += ",";
49 }
50 untranslatedString += escape(value);
51 }
52
53 private String escape(String value) {
54 if (Utilities.isEmpty(value)) {
55 return "";
56 }
57
58 value = value.replaceAll(SLASH_REGEXP, SLASH_ESCAPE);
59 value = value.replaceAll(COMMA_REGEXP, COMMA_ESCAPE);
60 return value;
61 }
62
63 public String getUntranslatedString() {
64 return untranslatedString;
65 }
66
67 public String[] getParameters() {
68 List strings = new ArrayList();
69 boolean isEscaped = false;
70 StringBuffer buffer = null;
71 for (int index = 0; index < untranslatedString.length(); index++) {
72 char character = untranslatedString.charAt(index);
73 if (isEscaped) {
74 isEscaped = false;
75 if (buffer == null) {
76 buffer = new StringBuffer();
77 }
78 buffer.append(character);
79 } else {
80 if (character == '\\') {
81 isEscaped = true;
82 } else if (character == ',') {
83 strings.add(buffer.toString());
84 buffer = null;
85 } else {
86 if (buffer == null) {
87 buffer = new StringBuffer();
88 }
89 buffer.append(character);
90 }
91 }
92 }
93
94 if (buffer != null) {
95 strings.add(buffer.toString());
96 }
97 return (String[])strings.toArray(new String[0]);
98 }
99
100 }