View Javadoc
1   /*
2    *  Copyright 2010, 2011 Chris Pheby
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.jadira.bindings.core.jdk;
17  
18  import java.text.ParseException;
19  import java.text.SimpleDateFormat;
20  import java.util.Calendar;
21  import java.util.GregorianCalendar;
22  import java.util.TimeZone;
23  
24  import org.jadira.bindings.core.api.Binding;
25  import org.jadira.bindings.core.api.BindingException;
26  
27  /**
28   * Binds a Calendar to a String
29   */
30  public class CalendarStringBinding extends AbstractStringBinding<Calendar> implements Binding<Calendar, String> {
31  
32      private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
33          @Override
34          public SimpleDateFormat initialValue() {
35              return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
36          }
37      };
38  
39      /**
40       * {@inheritDoc}
41       */
42      @Override
43      public Calendar unmarshal(String object) {
44  
45          if (object.length() < 31 || object.charAt(26) != ':' || object.charAt(29) != '['
46                  || object.charAt(object.length() - 1) != ']') {
47              throw new IllegalArgumentException("Unable to parse calendar: " + object);
48          }
49          TimeZone zone = TimeZone.getTimeZone(object.substring(30, object.length() - 1));
50  
51          String parseableDateString = object.substring(0, 26) + object.substring(27, 29);
52  
53          GregorianCalendar cal = new GregorianCalendar(zone);
54          cal.setTimeInMillis(0);
55          DATE_FORMAT.get().setCalendar(cal);
56  
57          try {
58              DATE_FORMAT.get().parseObject(parseableDateString);
59              return DATE_FORMAT.get().getCalendar();
60          } catch (ParseException ex) {
61              throw new BindingException(ex.getMessage(), ex);
62          }
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public String marshal(Calendar object) {
70  
71          if (object instanceof GregorianCalendar) {
72  
73              GregorianCalendar cal = (GregorianCalendar) object;
74  
75              DATE_FORMAT.get().setCalendar(cal);
76              String str = DATE_FORMAT.get().format(cal.getTime());
77  
78              String printableDateString = str.substring(0, 26) + ":" + str.substring(26) + "["
79                      + cal.getTimeZone().getID() + "]";
80              return printableDateString;
81          } else {
82              throw new IllegalArgumentException("CalendarStringBinding can only support "
83                      + GregorianCalendar.class.getSimpleName());
84          }
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      /* @Override */
91  	public Class<Calendar> getBoundClass() {
92  		return Calendar.class;
93  	}
94  }