1// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
2
3package org.xbill.DNS;
4
5import java.io.*;
6
7/**
8 * Geographical Location - describes the physical location of a host.
9 *
10 * @author Brian Wellington
11 */
12
13public class GPOSRecord extends Record {
14
15private static final long serialVersionUID = -6349714958085750705L;
16
17private byte [] latitude, longitude, altitude;
18
19GPOSRecord() {}
20
21Record
22getObject() {
23	return new GPOSRecord();
24}
25
26private void
27validate(double longitude, double latitude) throws IllegalArgumentException
28{
29       if (longitude < -90.0 || longitude > 90.0) {
30               throw new IllegalArgumentException("illegal longitude " +
31                                                  longitude);
32       }
33       if (latitude < -180.0 || latitude > 180.0) {
34               throw new IllegalArgumentException("illegal latitude " +
35                                                  latitude);
36       }
37}
38
39/**
40 * Creates an GPOS Record from the given data
41 * @param longitude The longitude component of the location.
42 * @param latitude The latitude component of the location.
43 * @param altitude The altitude component of the location (in meters above sea
44 * level).
45*/
46public
47GPOSRecord(Name name, int dclass, long ttl, double longitude, double latitude,
48	   double altitude)
49{
50	super(name, Type.GPOS, dclass, ttl);
51	validate(longitude, latitude);
52	this.longitude = Double.toString(longitude).getBytes();
53	this.latitude = Double.toString(latitude).getBytes();
54	this.altitude = Double.toString(altitude).getBytes();
55}
56
57/**
58 * Creates an GPOS Record from the given data
59 * @param longitude The longitude component of the location.
60 * @param latitude The latitude component of the location.
61 * @param altitude The altitude component of the location (in meters above sea
62 * level).
63*/
64public
65GPOSRecord(Name name, int dclass, long ttl, String longitude, String latitude,
66	   String altitude)
67{
68	super(name, Type.GPOS, dclass, ttl);
69	try {
70		this.longitude = byteArrayFromString(longitude);
71		this.latitude = byteArrayFromString(latitude);
72		validate(getLongitude(), getLatitude());
73		this.altitude = byteArrayFromString(altitude);
74	}
75	catch (TextParseException e) {
76		throw new IllegalArgumentException(e.getMessage());
77	}
78}
79
80void
81rrFromWire(DNSInput in) throws IOException {
82	longitude = in.readCountedString();
83	latitude = in.readCountedString();
84	altitude = in.readCountedString();
85	try {
86		validate(getLongitude(), getLatitude());
87	}
88	catch(IllegalArgumentException e) {
89		throw new WireParseException(e.getMessage());
90	}
91}
92
93void
94rdataFromString(Tokenizer st, Name origin) throws IOException {
95	try {
96		longitude = byteArrayFromString(st.getString());
97		latitude = byteArrayFromString(st.getString());
98		altitude = byteArrayFromString(st.getString());
99	}
100	catch (TextParseException e) {
101		throw st.exception(e.getMessage());
102	}
103	try {
104		validate(getLongitude(), getLatitude());
105	}
106	catch(IllegalArgumentException e) {
107		throw new WireParseException(e.getMessage());
108	}
109}
110
111/** Convert to a String */
112String
113rrToString() {
114	StringBuffer sb = new StringBuffer();
115	sb.append(byteArrayToString(longitude, true));
116	sb.append(" ");
117	sb.append(byteArrayToString(latitude, true));
118	sb.append(" ");
119	sb.append(byteArrayToString(altitude, true));
120	return sb.toString();
121}
122
123/** Returns the longitude as a string */
124public String
125getLongitudeString() {
126	return byteArrayToString(longitude, false);
127}
128
129/**
130 * Returns the longitude as a double
131 * @throws NumberFormatException The string does not contain a valid numeric
132 * value.
133 */
134public double
135getLongitude() {
136	return Double.parseDouble(getLongitudeString());
137}
138
139/** Returns the latitude as a string */
140public String
141getLatitudeString() {
142	return byteArrayToString(latitude, false);
143}
144
145/**
146 * Returns the latitude as a double
147 * @throws NumberFormatException The string does not contain a valid numeric
148 * value.
149 */
150public double
151getLatitude() {
152	return Double.parseDouble(getLatitudeString());
153}
154
155/** Returns the altitude as a string */
156public String
157getAltitudeString() {
158	return byteArrayToString(altitude, false);
159}
160
161/**
162 * Returns the altitude as a double
163 * @throws NumberFormatException The string does not contain a valid numeric
164 * value.
165 */
166public double
167getAltitude() {
168	return Double.parseDouble(getAltitudeString());
169}
170
171void
172rrToWire(DNSOutput out, Compression c, boolean canonical) {
173	out.writeCountedString(longitude);
174	out.writeCountedString(latitude);
175	out.writeCountedString(altitude);
176}
177
178}
179