View Javadoc

1   package com.terradue.jcatalogue.client.internal.converters;
2   
3   /*
4    *    Copyright 2011-2012 Terradue srl
5    *
6    *    Licensed under the Apache License, Version 2.0 (the "License");
7    *    you may not use this file except in compliance with the License.
8    *    You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *    Unless required by applicable law or agreed to in writing, software
13   *    distributed under the License is distributed on an "AS IS" BASIS,
14   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *    See the License for the specific language governing permissions and
16   *    limitations under the License.
17   */
18  
19  import static java.lang.String.format;
20  import static java.util.Calendar.DATE;
21  import static java.util.Calendar.HOUR_OF_DAY;
22  import static java.util.Calendar.MILLISECOND;
23  import static java.util.Calendar.MINUTE;
24  import static java.util.Calendar.MONTH;
25  import static java.util.Calendar.SECOND;
26  import static java.util.Calendar.YEAR;
27  import static java.util.Calendar.getInstance;
28  import static java.util.TimeZone.getTimeZone;
29  import static java.util.regex.Pattern.compile;
30  
31  import java.util.Calendar;
32  import java.util.Date;
33  import java.util.regex.Matcher;
34  import java.util.regex.Pattern;
35  
36  import org.apache.commons.beanutils.ConversionException;
37  import org.apache.commons.beanutils.Converter;
38  
39  public final class AtomDateConverter
40      implements Converter
41  {
42  
43      private static final Pattern PATTERN = compile(
44      "(\\d{4})(?:-(\\d{2}))?(?:-(\\d{2}))?(?:[Tt](?:(\\d{2}))?(?::(\\d{2}))?(?::(\\d{2}))?(?:\\.(\\d{3}))?)?([Zz])?(?:([+-])(\\d{2}):(\\d{2}))?");
45  
46      public Object convert( @SuppressWarnings( "rawtypes" ) Class type, Object value )
47      {
48          if ( value == null )
49          {
50              throw new ConversionException( "Null values not supported in this version." );
51          }
52  
53          if ( String.class == type )
54          {
55              if ( value instanceof Date )
56              {
57                  StringBuilder sb = new StringBuilder();
58                  Calendar c = getInstance( getTimeZone( "GMT" ) );
59                  c.setTime( (Date) value );
60                  sb.append( c.get( YEAR ) );
61                  sb.append( '-' );
62                  int f = c.get( MONTH );
63                  if ( f < 9 )
64                      sb.append( '0' );
65                  sb.append( f + 1 );
66                  sb.append( '-' );
67                  f = c.get( DATE );
68                  if ( f < 10 )
69                      sb.append( '0' );
70                  sb.append( f );
71                  sb.append( 'T' );
72                  f = c.get( HOUR_OF_DAY );
73                  if ( f < 10 )
74                      sb.append( '0' );
75                  sb.append( f );
76                  sb.append( ':' );
77                  f = c.get( MINUTE );
78                  if ( f < 10 )
79                      sb.append( '0' );
80                  sb.append( f );
81                  sb.append( ':' );
82                  f = c.get( SECOND );
83                  if ( f < 10 )
84                      sb.append( '0' );
85                  sb.append( f );
86                  sb.append( '.' );
87                  f = c.get( MILLISECOND );
88                  if ( f < 100 )
89                      sb.append( '0' );
90                  if ( f < 10 )
91                      sb.append( '0' );
92                  sb.append( f );
93                  sb.append( 'Z' );
94                  return sb.toString();
95              }
96          }
97          else if ( Date.class == type )
98          {
99              if ( value instanceof String )
100             {
101                 Matcher m = PATTERN.matcher( (String) value );
102                 if ( m.find() )
103                 {
104                     Calendar c = Calendar.getInstance( getTimeZone( "GMT" ) );
105                     int hoff = 0, moff = 0, doff = -1;
106                     if ( m.group( 9 ) != null )
107                     {
108                         doff = m.group( 9 ).equals( "-" ) ? 1 : -1;
109                         hoff = doff * ( m.group( 10 ) != null ? Integer.parseInt( m.group( 10 ) ) : 0 );
110                         moff = doff * ( m.group( 11 ) != null ? Integer.parseInt( m.group( 11 ) ) : 0 );
111                     }
112                     c.set( Calendar.YEAR, Integer.parseInt( m.group( 1 ) ) );
113                     c.set( Calendar.MONTH, m.group( 2 ) != null ? Integer.parseInt( m.group( 2 ) ) - 1 : 0 );
114                     c.set( Calendar.DATE, m.group( 3 ) != null ? Integer.parseInt( m.group( 3 ) ) : 1 );
115                     c.set( Calendar.HOUR_OF_DAY, m.group( 4 ) != null ? Integer.parseInt( m.group( 4 ) ) + hoff : 0 );
116                     c.set( Calendar.MINUTE, m.group( 5 ) != null ? Integer.parseInt( m.group( 5 ) ) + moff : 0 );
117                     c.set( Calendar.SECOND, m.group( 6 ) != null ? Integer.parseInt( m.group( 6 ) ) : 0 );
118                     c.set( Calendar.MILLISECOND, m.group( 7 ) != null ? Integer.parseInt( m.group( 7 ) ) : 0 );
119                     return c.getTime();
120                 }
121                 throw new IllegalArgumentException( "Invalid Date Format" );
122             }
123         }
124         throw new ConversionException( format( "type %s and value %s not supported", type, value ) );
125     }
126 
127 }