KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > LocaleType


1 //$Id: LocaleType.java,v 1.7 2005/02/19 12:58:24 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.Locale JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import org.hibernate.EntityMode;
11 import org.hibernate.Hibernate;
12 import org.hibernate.HibernateException;
13
14 /**
15  * <tt>locale</tt>: A type that maps an SQL VARCHAR to a Java Locale.
16  * @author Gavin King
17  */

18 public class LocaleType extends ImmutableType implements LiteralType {
19
20     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws HibernateException, SQLException JavaDoc {
21         return fromStringValue( (String JavaDoc) Hibernate.STRING.get(rs, name) );
22     }
23
24     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
25         Hibernate.STRING.set(st, value.toString(), index);
26     }
27
28     public Object JavaDoc fromStringValue(String JavaDoc string) {
29         if (string == null) {
30             return null;
31         }
32         else {
33             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(string, "_");
34             String JavaDoc language = tokens.hasMoreTokens() ? tokens.nextToken() : "";
35             String JavaDoc country = tokens.hasMoreTokens() ? tokens.nextToken() : "";
36             // Need to account for allowable '_' within the variant
37
String JavaDoc variant = "";
38             String JavaDoc sep = "";
39             while ( tokens.hasMoreTokens() ) {
40                 variant += sep + tokens.nextToken();
41                 sep = "_";
42             }
43             return new Locale JavaDoc(language, country, variant);
44         }
45     }
46     
47     public int compare(Object JavaDoc x, Object JavaDoc 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 JavaDoc toString(Object JavaDoc value) throws HibernateException {
56         return value.toString();
57     }
58
59     public Class JavaDoc getReturnedClass() {
60         return Locale JavaDoc.class;
61     }
62
63     public String JavaDoc getName() {
64         return "locale";
65     }
66
67     public String JavaDoc objectToSQLString(Object JavaDoc value) throws Exception JavaDoc {
68         return ( (LiteralType) Hibernate.STRING ).objectToSQLString( value.toString() );
69     }
70
71 }
72
73
74
75
76
77
78
Popular Tags