View Javadoc

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  	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      	// escape '\' and ',' with "\\" and "\,"
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  		// put whatever is left in the buffer (after the last ',') into the list of strings
94  		if (buffer != null) {
95  			strings.add(buffer.toString());
96  		}
97  		return (String[])strings.toArray(new String[0]);
98  	}
99  	
100 }