View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.kew.messaging;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  
22  /**
23   * A simple utility class which can handle translated a comma-seperated String
24   * into an array of String paramters and vice-versa.
25   *
26   * @author Kuali Rice Team (rice.collab@kuali.org)
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      	// escape '\' and ',' with "\\" and "\,"
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  		// put whatever is left in the buffer (after the last ',') into the list of strings
91  		if (buffer != null) {
92  			strings.add(buffer.toString());
93  		}
94  		return (String[])strings.toArray(new String[0]);
95  	}
96  	
97  }