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.xbean.propertyeditor;
18  
19  import java.text.DateFormat;
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import java.util.Locale;
24  import java.util.List;
25  import java.util.ArrayList;
26  
27  /**
28   * A property editor for Date typed properties.
29   * 
30   * @version $Rev$ $Date$
31   */
32  public class DateEditor extends AbstractConverter {
33  
34      private List<DateFormat> formats = new ArrayList<DateFormat>();
35  
36      public DateEditor() {
37          super(Date.class);
38  
39          formats.add(DateFormat.getInstance());
40          formats.add(DateFormat.getDateInstance());
41          formats.add(new SimpleDateFormat("yyyy-MM-dd")); // Atom (ISO 8601))) -- short version;
42          formats.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz")); // Atom (ISO 8601)));
43      }
44  
45      /**
46       * Convert the text value of the property into a Date object instance.
47       * 
48       * @return a Date object constructed from the property text value.
49       * @throws PropertyEditorException
50       *                 Unable to parse the string value into a Date.
51       */
52      protected Object toObjectImpl(String text) {
53          for (DateFormat format : formats) {
54              try {
55                  return format.parse(text);
56              } catch (ParseException e) {
57              }
58          }
59  
60          try {
61              return complexParse(text);
62          } catch (ParseException e) {
63              // any format errors show up as a ParseException, which we turn into
64              // a PropertyEditorException.
65              throw new PropertyEditorException(e);
66          }
67      }
68  
69      private Object complexParse(String text) throws ParseException {
70          // find out whether the first token is a locale id and style in that
71          // order
72          // if there's locale, style is mandatory
73          Locale locale = Locale.getDefault();
74          int style = DateFormat.MEDIUM;
75          int firstSpaceIndex = text.indexOf(' ');
76          if (firstSpaceIndex != -1) {
77              String token = text.substring(0, firstSpaceIndex).intern();
78              if (token.startsWith("locale")) {
79                  String localeStr = token.substring(token.indexOf('=') + 1);
80                  int underscoreIndex = localeStr.indexOf('_');
81                  if (underscoreIndex != -1) {
82                      String language = localeStr.substring(0, underscoreIndex);
83                      String country = localeStr.substring(underscoreIndex + 1);
84                      locale = new Locale(language, country);
85                  } else {
86                      locale = new Locale(localeStr);
87                  }
88                  // locale is followed by mandatory style
89                  int nextSpaceIndex = text.indexOf(' ', firstSpaceIndex + 1);
90                  token = text.substring(firstSpaceIndex + 1, nextSpaceIndex);
91                  String styleStr = token.substring(token.indexOf('=') + 1);
92                  if (styleStr.equalsIgnoreCase("SHORT")) {
93                      style = DateFormat.SHORT;
94                  } else if (styleStr.equalsIgnoreCase("MEDIUM")) {
95                      style = DateFormat.MEDIUM;
96                  } else if (styleStr.equalsIgnoreCase("LONG")) {
97                      style = DateFormat.LONG;
98                  } else if (styleStr.equalsIgnoreCase("FULL")) {
99                      style = DateFormat.FULL;
100                 } else {
101                     // unknown style name
102                     // throw exception or assume default?
103                     style = DateFormat.MEDIUM;
104                 }
105                 text = text.substring(nextSpaceIndex + 1);
106             }
107         }
108         DateFormat formats = DateFormat.getDateInstance(style, locale);
109         return formats.parse(text);
110     }
111 
112     protected String toStringImpl(Object value) {
113         Date date = (Date) value;
114         String text = formats.get(0).format(date);
115         return text;
116     }
117 }