%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.jetspeed.util.ojb.LocaleFieldConversion |
|
|
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 | 0 | 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 | 0 | if (arg0 instanceof Locale) |
45 | { |
|
46 | ||
47 | 0 | Locale locale = (Locale) arg0; |
48 | 0 | String country = locale.getCountry(); |
49 | 0 | String language = locale.getLanguage(); |
50 | 0 | String variant = locale.getVariant(); |
51 | 0 | StringBuffer buffer = new StringBuffer(40); |
52 | 0 | if (language != null) |
53 | { |
|
54 | 0 | buffer.append(language); |
55 | } |
|
56 | 0 | buffer.append(DELIM); |
57 | ||
58 | 0 | if (country != null) |
59 | { |
|
60 | 0 | buffer.append(country); |
61 | } |
|
62 | 0 | buffer.append(DELIM); |
63 | ||
64 | 0 | if (variant != null) |
65 | { |
|
66 | 0 | buffer.append(variant); |
67 | } |
|
68 | ||
69 | 0 | return buffer.toString().trim(); |
70 | } |
|
71 | else |
|
72 | { |
|
73 | 0 | 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 | 0 | if (arg0 instanceof String) |
83 | { |
|
84 | 0 | String localeString = (String) arg0; |
85 | 0 | StringTokenizer tokenizer = new StringTokenizer(localeString, DELIM); |
86 | 0 | if(tokenizer.hasMoreTokens() == false) |
87 | { |
|
88 | 0 | return JetspeedLocale.getDefaultLocale(); |
89 | } |
|
90 | 0 | String language = tokenizer.nextToken().trim(); |
91 | 0 | String country = null; |
92 | 0 | String variant = null; |
93 | 0 | if(tokenizer.hasMoreTokens()) |
94 | { |
|
95 | 0 | country = tokenizer.nextToken().trim(); |
96 | } |
|
97 | 0 | if(tokenizer.hasMoreTokens()) |
98 | { |
|
99 | 0 | variant = tokenizer.nextToken().trim(); |
100 | } |
|
101 | 0 | if (country != null && language != class="keyword">null && variant != class="keyword">null) |
102 | { |
|
103 | 0 | return new Locale (language, country, variant); |
104 | } |
|
105 | 0 | else if (country != null && language != class="keyword">null) |
106 | { |
|
107 | 0 | return new Locale(language, country); |
108 | } |
|
109 | 0 | else if (language != null) |
110 | { |
|
111 | 0 | return new Locale(language, ""); // JDK 1.3 compatibility |
112 | } |
|
113 | else |
|
114 | { |
|
115 | 0 | return JetspeedLocale.getDefaultLocale(); |
116 | } |
|
117 | } |
|
118 | else |
|
119 | { |
|
120 | 0 | return arg0; |
121 | } |
|
122 | ||
123 | } |
|
124 | ||
125 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |