001: package org.apache.ojb.broker.accesslayer.conversions;
002:
003: /* Copyright 2002-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.Vector;
019:
020: /**
021: * Converts a Vector of string elements back and forth from a database varchar field
022: * Strings may not contain "#" as this is used as separator.
023: * This class maybe useful if it's important to have the string vector stored in a
024: * human readable form that allows editing.
025: * @see Object2ByteArrFieldConversion uses Java serialization and is not suited for
026: * this purpose.
027: *
028: * @author sschloesser mailto: stefan.schl@gmx.de
029: * @version $Id: StringVector2VarcharFieldConversion.java,v 1.6.2.2 2005/12/21 22:23:38 tomdz Exp $
030: */
031: public class StringVector2VarcharFieldConversion implements
032: FieldConversion {
033:
034: private static final String NULLVALUE = "#NULL#";
035: private static final String EMPTYCOLLEC = "#EMTPY#";
036: private static final String SEPARATOR = "#";
037:
038: /** Creates a new instance of StringVector2VarcharFieldConversion */
039: public StringVector2VarcharFieldConversion() {
040: }
041:
042: public Object javaToSql(Object obj)
043: throws org.apache.ojb.broker.accesslayer.conversions.ConversionException {
044:
045: if (obj == null) {
046: return NULLVALUE;
047: }
048:
049: if (!(obj instanceof Vector)) {
050: throw new ConversionException(
051: "Object is not a vector it is a"
052: + obj.getClass().getName());
053: }
054:
055: Vector v = (Vector) obj;
056: if (v.size() == 0) {
057: return EMPTYCOLLEC;
058: }
059:
060: StringBuffer result = new StringBuffer();
061: for (int i = 0; i < v.size(); i++) {
062: String newSt = v.get(i).toString();
063: if (newSt.indexOf(SEPARATOR) >= 0) {
064: throw new ConversionException(
065: "An entry in the Vector contains the forbidden "
066: + SEPARATOR
067: + " character used to separate the strings on the DB");
068: }
069: result.append(newSt);
070: result.append(SEPARATOR);
071: }
072: return result.toString();
073: }
074:
075: public Object sqlToJava(Object obj)
076: throws org.apache.ojb.broker.accesslayer.conversions.ConversionException {
077:
078: if (obj == null) {
079: return null;
080: }
081: if (obj.toString().equals(NULLVALUE)) {
082: return null;
083: }
084: if (obj.toString().equals(EMPTYCOLLEC)) {
085: return new Vector();
086: }
087:
088: Vector v = new Vector();
089: String input = obj.toString();
090: int pos = input.indexOf(SEPARATOR);
091:
092: while (pos >= 0) {
093: if (pos == 0) {
094: v.add("");
095: } else {
096: v.add(input.substring(0, pos));
097: }
098:
099: if (pos + 1 > input.length()) //# at end causes outof bounds
100: {
101: break;
102: }
103:
104: input = input.substring(pos + 1, input.length());
105: pos = input.indexOf(SEPARATOR);
106: }
107: return v;
108: }
109: }
|