01: /**
02: *
03: */package com.bostechcorp.cbesb.runtime.parser;
04:
05: import java.util.Iterator;
06: import java.util.Vector;
07:
08: import org.apache.commons.logging.Log;
09: import org.apache.commons.logging.LogFactory;
10:
11: public class DataEscapingUtil {
12:
13: // private transient Log log = LogFactory.getLog(DataEscapingUtil.class);
14:
15: private char escapeChar;
16: private char quoteChar;
17: private Vector<String> delimiters;
18:
19: public DataEscapingUtil() {
20: delimiters = new Vector<String>();
21: }
22:
23: public void addDelimiter(String delimiter) {
24: // for (Iterator iter=delimiters.iterator(); iter.hasNext();)
25: // {
26: // String value = (String)iter.next();
27: // if (value.equals(delimiter))
28: // {
29: // return;
30: // }
31: // }
32:
33: for (String value : delimiters) {
34: if (value.equals(delimiter)) {
35: return;
36: }
37: }
38: delimiters.add(delimiter);
39: }
40:
41: public void setEscapeChar(char escapeChar) {
42: this .escapeChar = escapeChar;
43: this .quoteChar = 0;
44: }
45:
46: public void setQuoteChar(char quoteChar) {
47: this .escapeChar = 0;
48: this .quoteChar = quoteChar;
49: }
50:
51: public String escapeData(String data) {
52: String newData;
53: if (quoteChar != 0) {
54: newData = quoteChar + data + quoteChar;
55: } else if (escapeChar != 0) {
56: String escapeCharStr = String.valueOf(escapeChar);
57: newData = data.replace(escapeCharStr, escapeCharStr
58: + escapeCharStr);
59: for (Iterator iter = delimiters.iterator(); iter.hasNext();) {
60: String delimiter = (String) iter.next();
61: newData = newData.replace(delimiter, escapeCharStr
62: + delimiter);
63: }
64:
65: } else {
66: newData = data;
67: }
68: return newData;
69: }
70: }
|