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 org.apache.commons.beanutils.ConvertUtils.lookup;
21  
22  import org.apache.commons.beanutils.ConversionException;
23  import org.apache.commons.beanutils.Converter;
24  
25  import com.terradue.jcatalogue.client.geo.Line;
26  import com.terradue.jcatalogue.client.geo.Point;
27  import com.terradue.jcatalogue.client.geo.Polygon;
28  
29  public final class GeoConverter
30      implements Converter
31  {
32  
33      private static final String SEPARATOR = " ";
34  
35      private static final int EVEN_VERIFIER = 2;
36  
37      private final Converter doubleConverter = lookup( Double.class );
38  
39      public Object convert( @SuppressWarnings( "rawtypes" ) Class type, Object value )
40      {
41          if ( value == null )
42          {
43              throw new ConversionException( "Null values not supported in this version." );
44          }
45  
46          if ( value instanceof String )
47          {
48              String stringValue = (String) value;
49  
50              if ( stringValue.isEmpty() )
51              {
52                  throw new ConversionException( "empty value cannot be converted to a valid GeoLocation" );
53              }
54  
55              String[] values = ( stringValue ).split( SEPARATOR );
56  
57              if ( ( values.length % EVEN_VERIFIER ) != 0 )
58              {
59                  throw new ConversionException( format( "Cannot convert %s to a Geo representation, coordinates must be even in number (%s)",
60                                                         value, values.length ) );
61              }
62  
63              // extract points
64              Point[] points = new Point[values.length / EVEN_VERIFIER];
65  
66              for ( int i = 0; i < values.length; i += EVEN_VERIFIER )
67              {
68                  Double latitude = (Double) doubleConverter.convert( Double.class, values[i] );
69                  Double longitude = (Double) doubleConverter.convert( Double.class, values[i+1] );
70  
71                  points[i / EVEN_VERIFIER] = new Point( latitude, longitude );
72              }
73  
74              if ( Line.class == type )
75              {
76                  return new Line( points );
77              }
78              else if ( Point.class == type )
79              {
80                  return points[0];
81              }
82              else if ( Polygon.class == type )
83              {
84                  if ( !points[0].equals( points[points.length - 1] ) )
85                  {
86                      throw new ConversionException( "Input points don't descrive a valid Polygon, first and last Points are not equals" );
87                  }
88                  return new Polygon( points );
89              }
90          }
91          throw new ConversionException( format( "type %s and value %s not supported", type, value ) );
92      }
93  
94  }