1 package org.apache.ojb.broker.accesslayer.conversions; 2 3 17 18 import java.util.Vector ; 19 20 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 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()) { 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 |