View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.util.ojb;
18  
19  import java.util.Locale;
20  import java.util.StringTokenizer;
21  
22  import org.apache.jetspeed.util.JetspeedLocale;
23  import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
24  import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
25  
26  /***
27   * <p style="font-weight: bold">
28   * ObjectRelationalBridge field conversion.
29   * </p>
30   * Helps transparently map Locale objects into a database table
31   * that contains country, langauge and variant field and vice versa.
32   * 
33   * field should be tokenized with commas
34   */
35  public class LocaleFieldConversion implements FieldConversion
36  {
37      private static final String DELIM = ",";
38  
39      /***
40       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
41       */
42      public Object javaToSql(Object arg0) throws ConversionException
43      {
44          if (arg0 instanceof Locale)
45          {
46  
47              Locale locale = (Locale) arg0;
48              String country = locale.getCountry();
49              String language = locale.getLanguage();
50              String variant = locale.getVariant();
51              StringBuffer buffer = new StringBuffer(40);
52              if (language != null)
53              {
54                  buffer.append(language);
55              }
56              buffer.append(DELIM);
57  
58              if (country != null)
59              {
60                  buffer.append(country);
61              }
62              buffer.append(DELIM);
63  
64              if (variant != null)
65              {
66                  buffer.append(variant);
67              }
68  
69              return buffer.toString().trim();
70          }
71          else
72          {
73              return arg0;
74          }
75      }
76  
77      /***
78       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
79       */
80      public Object sqlToJava(Object arg0) throws ConversionException
81      {
82          if (arg0 instanceof String)
83          {
84              String localeString = (String) arg0;
85              StringTokenizer tokenizer = new StringTokenizer(localeString, DELIM);
86              if(tokenizer.hasMoreTokens() == false)            
87              {
88                  return JetspeedLocale.getDefaultLocale();    
89              }
90              String language = tokenizer.nextToken().trim();
91              String country = null;
92              String variant = null;
93              if(tokenizer.hasMoreTokens())
94              {
95                  country = tokenizer.nextToken().trim();
96              }           
97              if(tokenizer.hasMoreTokens())
98              {
99                  variant = tokenizer.nextToken().trim();
100             }
101             if (country != null && language != null && variant != null)
102             {
103                 return new Locale (language, country, variant);
104             }
105             else if (country != null && language != null)
106             {
107                 return new Locale(language, country);
108             }
109             else if (language != null)
110             {
111                 return new Locale(language, ""); // JDK 1.3 compatibility
112             }
113             else
114             {
115                 return JetspeedLocale.getDefaultLocale();
116             }
117         }
118         else
119         {
120             return arg0;
121         }
122 
123     }
124 
125 }