1 package org.apache.ojb.broker.accesslayer.conversions;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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
18 import java.util.Vector;
19
20 /**
21 * Converts a Vector of string elements back and forth from a database varchar field
22 * Strings may not contain "#" as this is used as separator.
23 * This class maybe useful if it's important to have the string vector stored in a
24 * human readable form that allows editing.
25 * @see Object2ByteArrFieldConversion uses Java serialization and is not suited for
26 * this purpose.
27 *
28 * @author sschloesser mailto: stefan.schl@gmx.de
29 * @version $Id: StringVector2VarcharFieldConversion.java,v 1.1 2007-08-24 22:17:31 ewestfal Exp $
30 */
31 public class StringVector2VarcharFieldConversion implements FieldConversion
32 {
33
34 private static final String NULLVALUE = "#NULL#";
35 private static final String EMPTYCOLLEC = "#EMTPY#";
36 private static final String SEPARATOR = "#";
37
38 /** Creates a new instance of StringVector2VarcharFieldConversion */
39 public StringVector2VarcharFieldConversion()
40 {
41 }
42
43 public Object javaToSql(Object obj)
44 throws org.apache.ojb.broker.accesslayer.conversions.ConversionException
45 {
46
47 if (obj == null)
48 {
49 return NULLVALUE;
50 }
51
52 if (!(obj instanceof Vector))
53 {
54 throw new ConversionException(
55 "Object is not a vector it is a" + obj.getClass().getName());
56 }
57
58 Vector v = (Vector) obj;
59 if (v.size() == 0)
60 {
61 return EMPTYCOLLEC;
62 }
63
64 StringBuffer result = new StringBuffer();
65 for (int i = 0; i < v.size(); i++)
66 {
67 String newSt = v.get(i).toString();
68 if (newSt.indexOf(SEPARATOR) >= 0)
69 {
70 throw new ConversionException(
71 "An entry in the Vector contains the forbidden "
72 + SEPARATOR
73 + " character used to separate the strings on the DB");
74 }
75 result.append(newSt);
76 result.append(SEPARATOR);
77 }
78 return result.toString();
79 }
80
81 public Object sqlToJava(Object obj)
82 throws org.apache.ojb.broker.accesslayer.conversions.ConversionException
83 {
84
85 if (obj == null)
86 {
87 return null;
88 }
89 if (obj.toString().equals(NULLVALUE))
90 {
91 return null;
92 }
93 if (obj.toString().equals(EMPTYCOLLEC))
94 {
95 return new Vector();
96 }
97
98 Vector v = new Vector();
99 String input = obj.toString();
100 int pos = input.indexOf(SEPARATOR);
101
102 while (pos >= 0)
103 {
104 if (pos == 0)
105 {
106 v.add("");
107 }
108 else
109 {
110 v.add(input.substring(0, pos));
111 }
112
113 if (pos + 1 > input.length()) //# at end causes outof bounds
114 {
115 break;
116 }
117
118 input = input.substring(pos + 1, input.length());
119 pos = input.indexOf(SEPARATOR);
120 }
121 return v;
122 }
123 }