001: package org.apache.ojb.broker.accesslayer.conversions;
002:
003: /* Copyright 2004-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.apache.commons.lang.StringUtils;
022:
023: /**
024: * This implementation of the {@link FieldConversion} interface converts
025: * between a {@link java.util.List} of {@link java.lang.String} objects and a database
026: * <em>varchar</em> field.
027: * <br/>
028: * Strings may not contain "#" as this is used as separator.
029: *
030: * @author Guillaume Nodet
031: * @version $Id: StringList2VarcharFieldConversion.java,v 1.2.2.1 2005/12/21 22:23:38 tomdz Exp $
032: */
033: public class StringList2VarcharFieldConversion implements
034: FieldConversion {
035:
036: private static final String NULLVALUE = "#NULL#";
037: private static final String EMPTYCOLLEC = "#EMTPY#";
038:
039: public StringList2VarcharFieldConversion() {
040: }
041:
042: /* (non-Javadoc)
043: * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
044: */
045: public Object javaToSql(Object source) throws ConversionException {
046:
047: if (source == null) {
048: return NULLVALUE;
049: }
050:
051: try {
052: List stringList = (List) source;
053: if (stringList.isEmpty()) {
054: return NULLVALUE;
055: }
056:
057: StringBuffer result = new StringBuffer();
058: for (int i = 0; i < stringList.size(); i++) {
059: String newSt = (String) stringList.get(i);
060: // introduced in JDK 1.4, replace with commons-lang
061: // newSt = newSt.replaceAll("#", "##");
062: newSt = StringUtils.replace(newSt, "#", "##");
063: if (i > 0) {
064: result.append("#");
065: }
066: result.append(newSt);
067: }
068: return result.toString();
069: } catch (ClassCastException e) {
070: throw new ConversionException(
071: "Object is not a List of String it is a"
072: + source.getClass().getName());
073: }
074: }
075:
076: /* (non-Javadoc)
077: * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
078: */
079: public Object sqlToJava(Object source) throws ConversionException {
080: if (source == null) {
081: return null;
082: }
083: if (source.toString().equals(NULLVALUE)) {
084: return null;
085: }
086: if (source.toString().equals(EMPTYCOLLEC)) {
087: return new ArrayList();
088: }
089: List v = new ArrayList();
090: StringBuffer input = new StringBuffer();
091: StringBuffer newString = new StringBuffer();
092: int pos = 0;
093: int length;
094:
095: input.append(source.toString());
096: length = input.length();
097: while (pos < length) {
098: if (input.charAt(pos) != '#') {
099: newString.append(input.charAt(pos));
100: } else {
101: if (input.charAt(pos + 1) != '#') {
102: v.add(newString.toString());
103: newString = new StringBuffer();
104: } else {
105: newString.append('#');
106: ++pos;
107: }
108: }
109: ++pos;
110: }
111: v.add(newString.toString());
112: return v;
113: }
114: }
|