01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.cocoon.template.environment;
18:
19: import java.util.Locale;
20:
21: import org.apache.commons.lang.StringUtils;
22:
23: /**
24: * @version SVN $Id: ValueHelper.java 449189 2006-09-23 06:52:29Z crossley $
25: */
26: public class ValueHelper {
27:
28: public static Locale parseLocale(String locale, String variant) {
29: Locale ret = null;
30: String language = locale;
31: String country = null;
32: int index = StringUtils.indexOfAny(locale, "-_");
33:
34: if (index > -1) {
35: language = locale.substring(0, index);
36: country = locale.substring(index + 1);
37: }
38: if (StringUtils.isEmpty(language)) {
39: throw new IllegalArgumentException("No language in locale");
40: }
41: if (country == null) {
42: ret = variant != null ? new Locale(language, "", variant)
43: : new Locale(language, "");
44: } else if (country.length() > 0) {
45: ret = variant != null ? new Locale(language, country,
46: variant) : new Locale(language, country);
47: } else {
48: throw new IllegalArgumentException(
49: "Empty country in locale");
50: }
51: return ret;
52: }
53:
54: }
|