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-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  
 
 23  
 /**
 24  
  * A simple utility class which can handle translated a comma-seperated String
 25  
  * into an array of String paramters and vice-versa.
 26  
  *
 27  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 28  
  */
 29  
 public class ParameterTranslator {
 30  
 
 31  
         private static final String SLASH_REGEXP = "\\\\";
 32  
     private static final String SLASH_ESCAPE = "\\\\";
 33  
     private static final String COMMA_REGEXP = ",";
 34  
     private static final String COMMA_ESCAPE = "\\,";
 35  
         
 36  0
         private String untranslatedString = "";
 37  
         
 38  0
         public ParameterTranslator() {}
 39  
         
 40  0
         public ParameterTranslator(String untranslatedString) {
 41  0
                 this.untranslatedString = untranslatedString;
 42  0
         }
 43  
         
 44  
         public void addParameter(String value) {
 45  0
                 if (!org.apache.commons.lang.StringUtils.isEmpty(untranslatedString)) {
 46  0
                         untranslatedString += ",";
 47  
                 }
 48  0
                 untranslatedString += escape(value);
 49  0
         }
 50  
         
 51  
         private String escape(String value) {
 52  0
                 if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
 53  0
                         return "";
 54  
                 }
 55  
             // escape '\' and ',' with "\\" and "\,"
 56  0
             value = value.replaceAll(SLASH_REGEXP, SLASH_ESCAPE);
 57  0
             value = value.replaceAll(COMMA_REGEXP, COMMA_ESCAPE);
 58  0
             return value;
 59  
     }
 60  
         
 61  
         public String getUntranslatedString() {
 62  0
                 return untranslatedString;
 63  
         }
 64  
         
 65  
         public String[] getParameters() {
 66  0
                 List strings = new ArrayList();
 67  0
                 boolean isEscaped = false;
 68  0
                 StringBuffer buffer = null;
 69  0
                 for (int index = 0; index < untranslatedString.length(); index++) {
 70  0
                         char character = untranslatedString.charAt(index);
 71  0
                         if (isEscaped) {
 72  0
                                 isEscaped = false;
 73  0
                                 if (buffer == null) {
 74  0
                                         buffer = new StringBuffer();
 75  
                                 }
 76  0
                                 buffer.append(character);
 77  
                         } else {
 78  0
                                 if (character == '\\') {
 79  0
                                         isEscaped = true;
 80  0
                                 } else if (character == ',') {
 81  0
                                         strings.add(buffer.toString());
 82  0
                                         buffer = null;
 83  
                                 } else {
 84  0
                                         if (buffer == null) {
 85  0
                                                 buffer = new StringBuffer();
 86  
                                         }
 87  0
                                         buffer.append(character);
 88  
                                 }
 89  
                         }
 90  
                 }
 91  
                 // put whatever is left in the buffer (after the last ',') into the list of strings
 92  0
                 if (buffer != null) {
 93  0
                         strings.add(buffer.toString());
 94  
                 }
 95  0
                 return (String[])strings.toArray(new String[0]);
 96  
         }
 97  
         
 98  
 }