KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker.accesslayer.conversions;
2
3 /* Copyright 2004-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.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.commons.lang.StringUtils;
22
23 /**
24  * This implementation of the {@link FieldConversion} interface converts
25  * between a {@link java.util.List} of {@link java.lang.String} objects and a database
26  * <em>varchar</em> field.
27  * <br/>
28  * Strings may not contain "#" as this is used as separator.
29  *
30  * @author Guillaume Nodet
31  * @version $Id: StringList2VarcharFieldConversion.java,v 1.2.2.1 2005/12/21 22:23:38 tomdz Exp $
32  */

33 public class StringList2VarcharFieldConversion implements FieldConversion
34 {
35
36     private static final String JavaDoc NULLVALUE = "#NULL#";
37     private static final String JavaDoc EMPTYCOLLEC = "#EMTPY#";
38
39     public StringList2VarcharFieldConversion()
40     {
41     }
42
43     /* (non-Javadoc)
44      * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
45      */

46     public Object JavaDoc javaToSql(Object JavaDoc source) throws ConversionException
47     {
48
49         if (source == null)
50         {
51             return NULLVALUE;
52         }
53
54         try
55         {
56             List JavaDoc stringList = (List JavaDoc) source;
57             if (stringList.isEmpty())
58             {
59                 return NULLVALUE;
60             }
61
62             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
63             for (int i = 0; i < stringList.size(); i++)
64             {
65                 String JavaDoc newSt = (String JavaDoc) stringList.get(i);
66                 // introduced in JDK 1.4, replace with commons-lang
67
// newSt = newSt.replaceAll("#", "##");
68
newSt = StringUtils.replace(newSt, "#", "##");
69                 if (i > 0)
70                 {
71                     result.append("#");
72                 }
73                 result.append(newSt);
74             }
75             return result.toString();
76         }
77         catch (ClassCastException JavaDoc e)
78         {
79             throw new ConversionException("Object is not a List of String it is a"
80                     + source.getClass().getName());
81         }
82     }
83
84     /* (non-Javadoc)
85      * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
86      */

87     public Object JavaDoc sqlToJava(Object JavaDoc source) throws ConversionException
88     {
89         if (source == null)
90         {
91             return null;
92         }
93         if (source.toString().equals(NULLVALUE))
94         {
95             return null;
96         }
97         if (source.toString().equals(EMPTYCOLLEC))
98         {
99             return new ArrayList JavaDoc();
100         }
101         List JavaDoc v = new ArrayList JavaDoc();
102         StringBuffer JavaDoc input = new StringBuffer JavaDoc();
103         StringBuffer JavaDoc newString = new StringBuffer JavaDoc();
104         int pos = 0;
105         int length;
106
107         input.append(source.toString());
108         length = input.length();
109         while (pos < length)
110         {
111             if (input.charAt(pos) != '#')
112             {
113                 newString.append(input.charAt(pos));
114             }
115             else
116             {
117                 if (input.charAt(pos + 1) != '#')
118                 {
119                     v.add(newString.toString());
120                     newString = new StringBuffer JavaDoc();
121                 }
122                 else
123                 {
124                     newString.append('#');
125                     ++pos;
126                 }
127             }
128             ++pos;
129         }
130         v.add(newString.toString());
131         return v;
132     }
133 }
134
Popular Tags