Coverage Report - org.kuali.rice.kew.messaging.ParameterTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
ParameterTranslator
0%
0/36
0%
0/18
2.667
 
 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  0
         private String untranslatedString = "";
 36  
         
 37  0
         public ParameterTranslator() {}
 38  
         
 39  0
         public ParameterTranslator(String untranslatedString) {
 40  0
                 this.untranslatedString = untranslatedString;
 41  0
         }
 42  
         
 43  
         public void addParameter(String value) {
 44  0
                 if (!org.apache.commons.lang.StringUtils.isEmpty(untranslatedString)) {
 45  0
                         untranslatedString += ",";
 46  
                 }
 47  0
                 untranslatedString += escape(value);
 48  0
         }
 49  
         
 50  
         private String escape(String value) {
 51  0
                 if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
 52  0
                         return "";
 53  
                 }
 54  
             // escape '\' and ',' with "\\" and "\,"
 55  0
             value = value.replaceAll(SLASH_REGEXP, SLASH_ESCAPE);
 56  0
             value = value.replaceAll(COMMA_REGEXP, COMMA_ESCAPE);
 57  0
             return value;
 58  
     }
 59  
         
 60  
         public String getUntranslatedString() {
 61  0
                 return untranslatedString;
 62  
         }
 63  
         
 64  
         public String[] getParameters() {
 65  0
                 List strings = new ArrayList();
 66  0
                 boolean isEscaped = false;
 67  0
                 StringBuffer buffer = null;
 68  0
                 for (int index = 0; index < untranslatedString.length(); index++) {
 69  0
                         char character = untranslatedString.charAt(index);
 70  0
                         if (isEscaped) {
 71  0
                                 isEscaped = false;
 72  0
                                 if (buffer == null) {
 73  0
                                         buffer = new StringBuffer();
 74  
                                 }
 75  0
                                 buffer.append(character);
 76  
                         } else {
 77  0
                                 if (character == '\\') {
 78  0
                                         isEscaped = true;
 79  0
                                 } else if (character == ',') {
 80  0
                                         strings.add(buffer.toString());
 81  0
                                         buffer = null;
 82  
                                 } else {
 83  0
                                         if (buffer == null) {
 84  0
                                                 buffer = new StringBuffer();
 85  
                                         }
 86  0
                                         buffer.append(character);
 87  
                                 }
 88  
                         }
 89  
                 }
 90  
                 // put whatever is left in the buffer (after the last ',') into the list of strings
 91  0
                 if (buffer != null) {
 92  0
                         strings.add(buffer.toString());
 93  
                 }
 94  0
                 return (String[])strings.toArray(new String[0]);
 95  
         }
 96  
         
 97  
 }