KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > accesslayer > conversions > StringVector2VarcharFieldConversion


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 JavaDoc;
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.6.2.2 2005/12/21 22:23:38 tomdz Exp $
30  */

31 public class StringVector2VarcharFieldConversion implements FieldConversion
32 {
33
34     private static final String JavaDoc NULLVALUE = "#NULL#";
35     private static final String JavaDoc EMPTYCOLLEC = "#EMTPY#";
36     private static final String JavaDoc SEPARATOR = "#";
37
38     /** Creates a new instance of StringVector2VarcharFieldConversion */
39     public StringVector2VarcharFieldConversion()
40     {
41     }
42
43     public Object JavaDoc javaToSql(Object JavaDoc 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 JavaDoc))
53         {
54             throw new ConversionException(
55                 "Object is not a vector it is a" + obj.getClass().getName());
56         }
57
58         Vector JavaDoc v = (Vector JavaDoc) obj;
59         if (v.size() == 0)
60         {
61             return EMPTYCOLLEC;
62         }
63
64         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
65         for (int i = 0; i < v.size(); i++)
66         {
67             String JavaDoc 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 JavaDoc sqlToJava(Object JavaDoc 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 JavaDoc();
96         }
97
98         Vector JavaDoc v = new Vector JavaDoc();
99         String JavaDoc 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 }
124
Popular Tags