001 package org.apache.myfaces.tobago.util;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 import javax.faces.convert.ConverterException;
021 import javax.faces.convert.DateTimeConverter;
022 import java.text.DateFormat;
023 import java.text.SimpleDateFormat;
024 import java.util.Locale;
025
026 /**
027 * This code is taken from myfaces core.
028 * TODO: Should be sharable (e.g. myfaces-commons).
029 * <p/>
030 * User: lofwyr
031 * Date: 06.11.2006 17:20:29
032 */
033 public class DateFormatUtils {
034
035 private static final String TYPE_DATE = "date";
036 private static final String TYPE_TIME = "time";
037 private static final String TYPE_BOTH = "both";
038 private static final String STYLE_DEFAULT = "default";
039 private static final String STYLE_MEDIUM = "medium";
040 private static final String STYLE_SHORT = "short";
041 private static final String STYLE_LONG = "long";
042 private static final String STYLE_FULL = "full";
043
044 private DateFormatUtils() {
045 }
046
047 /**
048 * Find a pattern for the converter.
049 * Returns the pattern inside the converter, if any.
050 * Otherwise compute the pattern.
051 *
052 * @return the patter or null, if DateFormat.getDateInstance() returns no SimpleDateFormat.
053 */
054 public static String findPattern(DateTimeConverter converter) {
055 String pattern = converter.getPattern();
056
057 if (pattern == null) {
058 DateFormat dateFormat = getDateFormat(
059 converter.getType(), converter.getDateStyle(),
060 converter.getTimeStyle(), converter.getLocale());
061 if (dateFormat instanceof SimpleDateFormat) {
062 SimpleDateFormat format = (SimpleDateFormat) dateFormat;
063 pattern = format.toPattern();
064 }
065 }
066
067 return pattern;
068 }
069
070 public static DateFormat getDateFormat(String type, String dateStyle, String timeStyle, Locale locale) {
071 DateFormat format;
072 if (type.equals(TYPE_DATE)) {
073 format = DateFormat.getDateInstance(calcStyle(dateStyle), locale);
074 } else if (type.equals(TYPE_TIME)) {
075 format = DateFormat.getTimeInstance(calcStyle(timeStyle), locale);
076 } else if (type.equals(TYPE_BOTH)) {
077 format = DateFormat.getDateTimeInstance(calcStyle(dateStyle),
078 calcStyle(timeStyle),
079 locale);
080 } else {
081 throw new ConverterException("invalid type '" + type + "'");
082 }
083
084 // format cannot be lenient (JSR-127)
085 format.setLenient(false);
086 return format;
087 }
088
089 private static int calcStyle(String name) {
090 if (name.equals(STYLE_DEFAULT)) {
091 return DateFormat.DEFAULT;
092 }
093 if (name.equals(STYLE_MEDIUM)) {
094 return DateFormat.MEDIUM;
095 }
096 if (name.equals(STYLE_SHORT)) {
097 return DateFormat.SHORT;
098 }
099 if (name.equals(STYLE_LONG)) {
100 return DateFormat.LONG;
101 }
102 if (name.equals(STYLE_FULL)) {
103 return DateFormat.FULL;
104 }
105
106 throw new ConverterException("invalid style '" + name + "'");
107 }
108
109 }