001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.util.ojb;
018:
019: import java.util.Locale;
020: import java.util.StringTokenizer;
021:
022: import org.apache.jetspeed.util.JetspeedLocale;
023: import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
024: import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
025:
026: /**
027: * <p style="font-weight: bold">
028: * ObjectRelationalBridge field conversion.
029: * </p>
030: * Helps transparently map Locale objects into a database table
031: * that contains country, langauge and variant field and vice versa.
032: *
033: * field should be tokenized with commas
034: */
035: public class LocaleFieldConversion implements FieldConversion {
036: private static final String DELIM = ",";
037:
038: /**
039: * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
040: */
041: public Object javaToSql(Object arg0) throws ConversionException {
042: if (arg0 instanceof Locale) {
043:
044: Locale locale = (Locale) arg0;
045: String country = locale.getCountry();
046: String language = locale.getLanguage();
047: String variant = locale.getVariant();
048: StringBuffer buffer = new StringBuffer(40);
049: if (language != null) {
050: buffer.append(language);
051: }
052: buffer.append(DELIM);
053:
054: if (country != null) {
055: buffer.append(country);
056: }
057: buffer.append(DELIM);
058:
059: if (variant != null) {
060: buffer.append(variant);
061: }
062:
063: return buffer.toString().trim();
064: } else {
065: return arg0;
066: }
067: }
068:
069: /**
070: * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
071: */
072: public Object sqlToJava(Object arg0) throws ConversionException {
073: if (arg0 instanceof String) {
074: String localeString = (String) arg0;
075: StringTokenizer tokenizer = new StringTokenizer(
076: localeString, DELIM);
077: if (tokenizer.hasMoreTokens() == false) {
078: return JetspeedLocale.getDefaultLocale();
079: }
080: String language = tokenizer.nextToken().trim();
081: String country = null;
082: String variant = null;
083: if (tokenizer.hasMoreTokens()) {
084: country = tokenizer.nextToken().trim();
085: }
086: if (tokenizer.hasMoreTokens()) {
087: variant = tokenizer.nextToken().trim();
088: }
089: if (country != null && language != null && variant != null) {
090: return new Locale(language, country, variant);
091: } else if (country != null && language != null) {
092: return new Locale(language, country);
093: } else if (language != null) {
094: return new Locale(language, ""); // JDK 1.3 compatibility
095: } else {
096: return JetspeedLocale.getDefaultLocale();
097: }
098: } else {
099: return arg0;
100: }
101:
102: }
103:
104: }
|