Be robust against empty 'GEO' field
[ical4j.git] / source / net / fortuna / ical4j / model / property / Geo.java
blobf45eac41c41e7cf62b1076abb9825153f841b7b2
1 /**
2 * Copyright (c) 2009, Ben Fortuna
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * o Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * o Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * o Neither the name of Ben Fortuna nor the names of any other contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 package net.fortuna.ical4j.model.property;
34 import java.math.BigDecimal;
36 import net.fortuna.ical4j.model.ParameterList;
37 import net.fortuna.ical4j.model.Property;
38 import net.fortuna.ical4j.model.PropertyFactoryImpl;
39 import net.fortuna.ical4j.model.ValidationException;
40 import net.fortuna.ical4j.util.validation.ValidationResultHandler;
41 import net.fortuna.ical4j.util.validation.ValidationRuleInfo;
43 /**
44 * $Id: Geo.java,v 1.18 2009/11/14 09:22:45 fortuna Exp $
46 * Created: [Apr 6, 2004]
48 * Defines a GEO iCalendar component property.
50 * <pre>
51 * 4.8.1.6 Geographic Position
53 * Property Name: GEO
55 * Purpose: This property specifies information related to the global
56 * position for the activity specified by a calendar component.
58 * Value Type: FLOAT. The value MUST be two SEMICOLON separated FLOAT
59 * values.
61 * Property Parameters: Non-standard property parameters can be
62 * specified on this property.
64 * Conformance: This property can be specified in &quot;VEVENT&quot; or &quot;VTODO&quot;
65 * calendar components.
67 * Description: The property value specifies latitude and longitude, in
68 * that order (i.e., &quot;LAT LON&quot; ordering). The longitude represents the
69 * location east or west of the prime meridian as a positive or negative
70 * real number, respectively. The longitude and latitude values MAY be
71 * specified up to six decimal places, which will allow for accuracy to
72 * within one meter of geographical position. Receiving applications
73 * MUST accept values of this precision and MAY truncate values of
74 * greater precision.
76 * Values for latitude and longitude shall be expressed as decimal
77 * fractions of degrees. Whole degrees of latitude shall be represented
78 * by a two-digit decimal number ranging from 0 through 90. Whole
79 * degrees of longitude shall be represented by a decimal number ranging
80 * from 0 through 180. When a decimal fraction of a degree is specified,
81 * it shall be separated from the whole number of degrees by a decimal
82 * point.
84 * Latitudes north of the equator shall be specified by a plus sign (+),
85 * or by the absence of a minus sign (-), preceding the digits
86 * designating degrees. Latitudes south of the Equator shall be
87 * designated by a minus sign (-) preceding the digits designating
88 * degrees. A point on the Equator shall be assigned to the Northern
89 * Hemisphere.
91 * Longitudes east of the prime meridian shall be specified by a plus
92 * sign (+), or by the absence of a minus sign (-), preceding the digits
93 * designating degrees. Longitudes west of the meridian shall be
94 * designated by minus sign (-) preceding the digits designating
95 * degrees. A point on the prime meridian shall be assigned to the
96 * Eastern Hemisphere. A point on the 180th meridian shall be assigned
97 * to the Western Hemisphere. One exception to this last convention is
98 * permitted. For the special condition of describing a band of latitude
99 * around the earth, the East Bounding Coordinate data element shall be
100 * assigned the value +180 (180) degrees.
102 * Any spatial address with a latitude of +90 (90) or -90 degrees will
103 * specify the position at the North or South Pole, respectively. The
104 * component for longitude may have any legal value.
106 * With the exception of the special condition described above, this
107 * form is specified in Department of Commerce, 1986, Representation of
108 * geographic point locations for information interchange (Federal
109 * Information Processing Standard 70-1): Washington, Department of
110 * Commerce, National Institute of Standards and Technology.
112 * The simple formula for converting degrees-minutes-seconds into
113 * decimal degrees is:
115 * decimal = degrees + minutes/60 + seconds/3600.
117 * Format Definition: The property is defined by the following notation:
119 * geo = &quot;GEO&quot; geoparam &quot;:&quot; geovalue CRLF
121 * geoparam = *(&quot;;&quot; xparam)
123 * geovalue = float &quot;;&quot; float
124 * ;Latitude and Longitude components
126 * Example: The following is an example of this property:
128 * GEO:37.386013;-122.082932
129 * </pre>
131 * @author Ben Fortuna
133 public class Geo extends Property {
135 private static final long serialVersionUID = -902100715801867636L;
137 private static final ValidationRuleInfo VALID_GEO =
138 new ValidationRuleInfo("3.8.1.6");
140 private BigDecimal latitude;
142 private BigDecimal longitude;
145 * Default constructor.
147 public Geo() {
148 super(GEO, PropertyFactoryImpl.getInstance());
149 latitude = BigDecimal.valueOf(0);
150 longitude = BigDecimal.valueOf(0);
154 * Creates a new instance by parsing the specified string representation.
155 * @param value a geo value
157 public Geo(final String value) {
158 super(GEO, PropertyFactoryImpl.getInstance());
159 setValue(value);
163 * @param aList a list of parameters for this component
164 * @param aValue a value string for this component
166 public Geo(final ParameterList aList, final String aValue) {
167 super(GEO, aList, PropertyFactoryImpl.getInstance());
168 setValue(aValue);
172 * @param latitude a latitudinal value
173 * @param longitude a longitudinal value
175 public Geo(final BigDecimal latitude, final BigDecimal longitude) {
176 super(GEO, PropertyFactoryImpl.getInstance());
177 this.latitude = latitude;
178 this.longitude = longitude;
182 * @param aList a list of parameters for this component
183 * @param latitude a latitudinal value
184 * @param longitude a longitudinal value
186 public Geo(final ParameterList aList, final BigDecimal latitude,
187 final BigDecimal longitude) {
188 super(GEO, aList, PropertyFactoryImpl.getInstance());
189 this.latitude = latitude;
190 this.longitude = longitude;
194 * @return Returns the latitude.
196 public final BigDecimal getLatitude() {
197 return latitude;
201 * @return Returns the longitude.
203 public final BigDecimal getLongitude() {
204 return longitude;
208 * {@inheritDoc}
210 public final void setValue(final String aValue) {
211 String latitudeString = aValue.substring(0, aValue.indexOf(';'));
212 if (latitudeString.length() != 0)
214 latitude = new BigDecimal(latitudeString);
215 longitude = new BigDecimal(aValue.substring(aValue.indexOf(';') + 1));
220 * {@inheritDoc}
222 public final String getValue() {
223 return String.valueOf(getLatitude()) + ";"
224 + String.valueOf(getLongitude());
228 * @param latitude The latitude to set.
230 public final void setLatitude(final BigDecimal latitude) {
231 this.latitude = latitude;
235 * @param longitude The longitude to set.
237 public final void setLongitude(final BigDecimal longitude) {
238 this.longitude = longitude;
242 * {@inheritDoc}
244 public final void validate() throws ValidationException {
245 if (latitude == null)
247 ValidationResultHandler.onValidationResult("Latitude is mandatory", VALID_GEO);
249 if (longitude == null)
251 ValidationResultHandler.onValidationResult("Longitude is mandatory", VALID_GEO);