001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.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/ecl1.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 edu.iu.uis.eden.messaging;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import edu.iu.uis.eden.util.Utilities;
023:
024: /**
025: * A simple utility class which can handle translated a comma-seperated String
026: * into an array of String paramters and vice-versa.
027: *
028: * @author ewestfal
029: */
030: public class ParameterTranslator {
031:
032: private static final String SLASH_REGEXP = "\\\\";
033: private static final String SLASH_ESCAPE = "\\\\";
034: private static final String COMMA_REGEXP = ",";
035: private static final String COMMA_ESCAPE = "\\,";
036:
037: private String untranslatedString = "";
038:
039: public ParameterTranslator() {
040: }
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: }
|