1 package org.hibernate.type; 3 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.util.Locale ; 8 import java.util.StringTokenizer ; 9 10 import org.hibernate.EntityMode; 11 import org.hibernate.Hibernate; 12 import org.hibernate.HibernateException; 13 14 18 public class LocaleType extends ImmutableType implements LiteralType { 19 20 public Object get(ResultSet rs, String name) throws HibernateException, SQLException { 21 return fromStringValue( (String ) Hibernate.STRING.get(rs, name) ); 22 } 23 24 public void set(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { 25 Hibernate.STRING.set(st, value.toString(), index); 26 } 27 28 public Object fromStringValue(String string) { 29 if (string == null) { 30 return null; 31 } 32 else { 33 StringTokenizer tokens = new StringTokenizer (string, "_"); 34 String language = tokens.hasMoreTokens() ? tokens.nextToken() : ""; 35 String country = tokens.hasMoreTokens() ? tokens.nextToken() : ""; 36 String variant = ""; 38 String sep = ""; 39 while ( tokens.hasMoreTokens() ) { 40 variant += sep + tokens.nextToken(); 41 sep = "_"; 42 } 43 return new Locale (language, country, variant); 44 } 45 } 46 47 public int compare(Object x, Object y, EntityMode entityMode) { 48 return x.toString().compareTo( y.toString() ); 49 } 50 51 public int sqlType() { 52 return Hibernate.STRING.sqlType(); 53 } 54 55 public String toString(Object value) throws HibernateException { 56 return value.toString(); 57 } 58 59 public Class getReturnedClass() { 60 return Locale .class; 61 } 62 63 public String getName() { 64 return "locale"; 65 } 66 67 public String objectToSQLString(Object value) throws Exception { 68 return ( (LiteralType) Hibernate.STRING ).objectToSQLString( value.toString() ); 69 } 70 71 } 72 73 74 75 76 77 78 | Popular Tags |