KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > conv > LocaleConverter


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

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 JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.util.Locale JavaDoc;
23
24 import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws-clause
25

26 import com.versant.core.common.BindingSupportImpl;
27
28 /**
29  * This converter converts Locale objects to and from SQL. It assumes that
30  * the Locale is stored in a column compatible with ResultSet.getString and
31  * PreparedStatement.setString. The Locale is stored as a 4 or 6 character
32  * String (lang + country [+ variant]).
33  * @keep-all
34  */

35 public class LocaleConverter extends JdbcConverterBase {
36
37     public static class Factory extends NoArgJdbcConverterFactory {
38
39         private LocaleConverter converter;
40
41         /**
42          * Create a converter for col using args as parameters. Return null if
43          * no converter is required.
44          */

45         public JdbcConverter createJdbcConverter(JdbcColumn col, Object JavaDoc args,
46                 JdbcTypeRegistry jdbcTypeRegistry) {
47             if (converter == null) converter = new LocaleConverter();
48             return converter;
49         }
50
51     }
52
53     /**
54      * Get the value of col from rs at position index.
55      * @exception SQLException on SQL errors
56      * @exception JDOFatalDataStoreException if the ResultSet value is invalid
57      */

58     public Object JavaDoc get(ResultSet JavaDoc rs, int index, JdbcColumn col)
59             throws SQLException JavaDoc, JDOFatalDataStoreException {
60         String JavaDoc 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 JavaDoc lang = s.substring(0, 2);
66             String JavaDoc country = null;
67             if (s.length() > 2) {
68                 country = s.substring(2, 4);
69             } else {
70                 country = "";
71             }
72             String JavaDoc variant = null;
73             if (s.length() > 4) {
74                 variant = s.substring(4, 6);
75             } else {
76                 variant = "";
77             }
78             return new Locale JavaDoc(lang, country, variant);
79         } catch (IndexOutOfBoundsException JavaDoc x) {
80             throw BindingSupportImpl.getInstance().fatalDatastore(
81                 "Invalid Locale value for " + col + ": '" + s + "'", x);
82         }
83     }
84
85     /**
86      * Set parameter index on ps to value (for col).
87      * @exception SQLException on SQL errors
88      * @exception JDOFatalDataStoreException if value is invalid
89      */

90     public void set(PreparedStatement JavaDoc ps, int index, JdbcColumn col, Object JavaDoc value)
91             throws SQLException JavaDoc, JDOFatalDataStoreException {
92         if (value == null) {
93             ps.setNull(index, col.jdbcType);
94         } else {
95             Locale JavaDoc l = (Locale JavaDoc)value;
96             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
97             s.append(l.getLanguage());
98             s.append(l.getCountry());
99             if (col.length >= 6) {
100                 String JavaDoc v = l.getVariant();
101                 if (v.length() == 0) v = " ";
102                 s.append(v);
103             }
104             ps.setString(index, s.toString());
105         }
106     }
107
108     /**
109      * Get the type of our expected value objects (e.g. java.util.Locale
110      * for a converter for Locale's).
111      */

112     public Class JavaDoc getValueType() {
113         return Locale JavaDoc.class;
114     }
115
116 }
117
118
Popular Tags