001    package biweekly.property.marshaller;
002    
003    import java.util.List;
004    
005    import biweekly.io.CannotParseException;
006    import biweekly.io.xml.XCalElement;
007    import biweekly.parameter.ICalParameters;
008    import biweekly.property.Geo;
009    import biweekly.util.ICalFloatFormatter;
010    
011    /*
012     Copyright (c) 2013, Michael Angstadt
013     All rights reserved.
014    
015     Redistribution and use in source and binary forms, with or without
016     modification, are permitted provided that the following conditions are met: 
017    
018     1. Redistributions of source code must retain the above copyright notice, this
019     list of conditions and the following disclaimer. 
020     2. Redistributions in binary form must reproduce the above copyright notice,
021     this list of conditions and the following disclaimer in the documentation
022     and/or other materials provided with the distribution. 
023    
024     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
025     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
026     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
027     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
028     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
029     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
030     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
031     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
033     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034     */
035    
036    /**
037     * Marshals {@link Geo} properties.
038     * @author Michael Angstadt
039     */
040    public class GeoMarshaller extends ICalPropertyMarshaller<Geo> {
041            public GeoMarshaller() {
042                    super(Geo.class, "GEO");
043            }
044    
045            @Override
046            protected String _writeText(Geo property) {
047                    ICalFloatFormatter formatter = new ICalFloatFormatter();
048                    StringBuilder sb = new StringBuilder();
049    
050                    Double latitude = property.getLatitude();
051                    if (latitude != null) {
052                            sb.append(formatter.format(latitude));
053                    }
054    
055                    sb.append(';');
056    
057                    Double longitude = property.getLongitude();
058                    if (longitude != null) {
059                            sb.append(formatter.format(longitude));
060                    }
061    
062                    return sb.toString();
063            }
064    
065            @Override
066            protected Geo _parseText(String value, ICalParameters parameters, List<String> warnings) {
067                    String split[] = value.split(";");
068    
069                    if (split.length < 2) {
070                            throw new CannotParseException("Could not parse value.");
071                    }
072    
073                    String latitudeStr = split[0];
074                    String longitudeStr = split[1];
075    
076                    return parse(latitudeStr, longitudeStr);
077            }
078    
079            @Override
080            protected void _writeXml(Geo property, XCalElement element) {
081                    ICalFloatFormatter formatter = new ICalFloatFormatter();
082    
083                    Double latitude = property.getLatitude();
084                    if (latitude != null) {
085                            element.append("latitude", formatter.format(latitude));
086                    }
087    
088                    Double longitude = property.getLongitude();
089                    if (longitude != null) {
090                            element.append("longitude", formatter.format(longitude));
091                    }
092            }
093    
094            @Override
095            protected Geo _parseXml(XCalElement element, ICalParameters parameters, List<String> warnings) {
096                    String latitudeStr = element.first("latitude");
097                    String longitudeStr = element.first("longitude");
098                    return parse(latitudeStr, longitudeStr);
099            }
100    
101            private Geo parse(String latitudeStr, String longitudeStr) {
102                    Double latitude = null;
103                    if (latitudeStr != null) {
104                            try {
105                                    latitude = Double.valueOf(latitudeStr);
106                            } catch (NumberFormatException e) {
107                                    throw new CannotParseException("Could not parse latitude: " + latitudeStr);
108                            }
109                    }
110    
111                    Double longitude = null;
112                    if (longitudeStr != null) {
113                            try {
114                                    longitude = Double.valueOf(longitudeStr);
115                            } catch (NumberFormatException e) {
116                                    throw new CannotParseException("Could not parse longitude: " + longitudeStr);
117                            }
118                    }
119    
120                    return new Geo(latitude, longitude);
121            }
122    }