1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.messaging;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21
22
23
24
25
26
27
28 public class ParameterTranslator {
29
30 private static final String SLASH_REGEXP = "\\\\";
31 private static final String SLASH_ESCAPE = "\\\\";
32 private static final String COMMA_REGEXP = ",";
33 private static final String COMMA_ESCAPE = "\\,";
34
35 private String untranslatedString = "";
36
37 public ParameterTranslator() {}
38
39 public ParameterTranslator(String untranslatedString) {
40 this.untranslatedString = untranslatedString;
41 }
42
43 public void addParameter(String value) {
44 if (!org.apache.commons.lang.StringUtils.isEmpty(untranslatedString)) {
45 untranslatedString += ",";
46 }
47 untranslatedString += escape(value);
48 }
49
50 private String escape(String value) {
51 if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
52 return "";
53 }
54
55 value = value.replaceAll(SLASH_REGEXP, SLASH_ESCAPE);
56 value = value.replaceAll(COMMA_REGEXP, COMMA_ESCAPE);
57 return value;
58 }
59
60 public String getUntranslatedString() {
61 return untranslatedString;
62 }
63
64 public String[] getParameters() {
65 List strings = new ArrayList();
66 boolean isEscaped = false;
67 StringBuffer buffer = null;
68 for (int index = 0; index < untranslatedString.length(); index++) {
69 char character = untranslatedString.charAt(index);
70 if (isEscaped) {
71 isEscaped = false;
72 if (buffer == null) {
73 buffer = new StringBuffer();
74 }
75 buffer.append(character);
76 } else {
77 if (character == '\\') {
78 isEscaped = true;
79 } else if (character == ',') {
80 strings.add(buffer.toString());
81 buffer = null;
82 } else {
83 if (buffer == null) {
84 buffer = new StringBuffer();
85 }
86 buffer.append(character);
87 }
88 }
89 }
90
91 if (buffer != null) {
92 strings.add(buffer.toString());
93 }
94 return (String[])strings.toArray(new String[0]);
95 }
96
97 }