1 2 12 package com.versant.core.jdbc.sql.conv; 13 14 import com.versant.core.jdbc.JdbcConverter; 15 import com.versant.core.jdbc.JdbcConverterFactory; 16 import com.versant.core.jdbc.JdbcTypeRegistry; 17 import com.versant.core.jdbc.metadata.JdbcColumn; 18 19 import java.sql.PreparedStatement ; 20 import java.sql.SQLException ; 21 import java.sql.ResultSet ; 22 import java.util.Locale ; 23 24 import javax.jdo.JDOFatalDataStoreException; 26 import com.versant.core.common.BindingSupportImpl; 27 28 35 public class LocaleConverter extends JdbcConverterBase { 36 37 public static class Factory extends NoArgJdbcConverterFactory { 38 39 private LocaleConverter converter; 40 41 45 public JdbcConverter createJdbcConverter(JdbcColumn col, Object args, 46 JdbcTypeRegistry jdbcTypeRegistry) { 47 if (converter == null) converter = new LocaleConverter(); 48 return converter; 49 } 50 51 } 52 53 58 public Object get(ResultSet rs, int index, JdbcColumn col) 59 throws SQLException , JDOFatalDataStoreException { 60 String s = rs.getString(index); 61 if (s == null) return null; 62 s = s.trim(); 63 if (s.length() == 0) return null; 64 try { 65 String lang = s.substring(0, 2); 66 String country = null; 67 if (s.length() > 2) { 68 country = s.substring(2, 4); 69 } else { 70 country = ""; 71 } 72 String variant = null; 73 if (s.length() > 4) { 74 variant = s.substring(4, 6); 75 } else { 76 variant = ""; 77 } 78 return new Locale (lang, country, variant); 79 } catch (IndexOutOfBoundsException x) { 80 throw BindingSupportImpl.getInstance().fatalDatastore( 81 "Invalid Locale value for " + col + ": '" + s + "'", x); 82 } 83 } 84 85 90 public void set(PreparedStatement ps, int index, JdbcColumn col, Object value) 91 throws SQLException , JDOFatalDataStoreException { 92 if (value == null) { 93 ps.setNull(index, col.jdbcType); 94 } else { 95 Locale l = (Locale )value; 96 StringBuffer s = new StringBuffer (); 97 s.append(l.getLanguage()); 98 s.append(l.getCountry()); 99 if (col.length >= 6) { 100 String v = l.getVariant(); 101 if (v.length() == 0) v = " "; 102 s.append(v); 103 } 104 ps.setString(index, s.toString()); 105 } 106 } 107 108 112 public Class getValueType() { 113 return Locale .class; 114 } 115 116 } 117 118 | Popular Tags |