001    /*
002     * Copyright 2005-2008 The Kuali Foundation
003     * 
004     * 
005     * Licensed under the Educational Community License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     * 
009     * http://www.opensource.org/licenses/ecl2.php
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.kuali.rice.kew.messaging;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.kuali.rice.kew.util.Utilities;
023    
024    
025    /**
026     * A simple utility class which can handle translated a comma-seperated String
027     * into an array of String paramters and vice-versa.
028     *
029     * @author Kuali Rice Team (rice.collab@kuali.org)
030     */
031    public class ParameterTranslator {
032    
033            private static final String SLASH_REGEXP = "\\\\";
034        private static final String SLASH_ESCAPE = "\\\\";
035        private static final String COMMA_REGEXP = ",";
036        private static final String COMMA_ESCAPE = "\\,";
037            
038            private String untranslatedString = "";
039            
040            public ParameterTranslator() {}
041            
042            public ParameterTranslator(String untranslatedString) {
043                    this.untranslatedString = untranslatedString;
044            }
045            
046            public void addParameter(String value) {
047                    if (!Utilities.isEmpty(untranslatedString)) {
048                            untranslatedString += ",";
049                    }
050                    untranslatedString += escape(value);
051            }
052            
053            private String escape(String value) {
054                    if (Utilities.isEmpty(value)) {
055                            return "";
056                    }
057            // escape '\' and ',' with "\\" and "\,"
058            value = value.replaceAll(SLASH_REGEXP, SLASH_ESCAPE);
059            value = value.replaceAll(COMMA_REGEXP, COMMA_ESCAPE);
060            return value;
061        }
062            
063            public String getUntranslatedString() {
064                    return untranslatedString;
065            }
066            
067            public String[] getParameters() {
068                    List strings = new ArrayList();
069                    boolean isEscaped = false;
070                    StringBuffer buffer = null;
071                    for (int index = 0; index < untranslatedString.length(); index++) {
072                            char character = untranslatedString.charAt(index);
073                            if (isEscaped) {
074                                    isEscaped = false;
075                                    if (buffer == null) {
076                                            buffer = new StringBuffer();
077                                    }
078                                    buffer.append(character);
079                            } else {
080                                    if (character == '\\') {
081                                            isEscaped = true;
082                                    } else if (character == ',') {
083                                            strings.add(buffer.toString());
084                                            buffer = null;
085                                    } else {
086                                            if (buffer == null) {
087                                                    buffer = new StringBuffer();
088                                            }
089                                            buffer.append(character);
090                                    }
091                            }
092                    }
093                    // put whatever is left in the buffer (after the last ',') into the list of strings
094                    if (buffer != null) {
095                            strings.add(buffer.toString());
096                    }
097                    return (String[])strings.toArray(new String[0]);
098            }
099            
100    }