001: // Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
002:
003: package org.xbill.DNS;
004:
005: import java.io.*;
006:
007: /**
008: * Geographical Location - describes the physical location of a host.
009: *
010: * @author Brian Wellington
011: */
012:
013: public class GPOSRecord extends Record {
014:
015: private byte[] latitude, longitude, altitude;
016:
017: GPOSRecord() {
018: }
019:
020: Record getObject() {
021: return new GPOSRecord();
022: }
023:
024: private void validate(double longitude, double latitude)
025: throws IllegalArgumentException {
026: if (longitude < -90.0 || longitude > 90.0) {
027: throw new IllegalArgumentException("illegal longitude "
028: + longitude);
029: }
030: if (latitude < -180.0 || latitude > 180.0) {
031: throw new IllegalArgumentException("illegal latitude "
032: + latitude);
033: }
034: }
035:
036: /**
037: * Creates an GPOS Record from the given data
038: * @param longitude The longitude component of the location.
039: * @param latitude The latitude component of the location.
040: * @param altitude The altitude component of the location (in meters above sea
041: * level).
042: */
043: public GPOSRecord(Name name, int dclass, long ttl,
044: double longitude, double latitude, double altitude) {
045: super (name, Type.GPOS, dclass, ttl);
046: validate(longitude, latitude);
047: this .longitude = Double.toString(longitude).getBytes();
048: this .latitude = Double.toString(latitude).getBytes();
049: this .altitude = Double.toString(altitude).getBytes();
050: }
051:
052: /**
053: * Creates an GPOS Record from the given data
054: * @param longitude The longitude component of the location.
055: * @param latitude The latitude component of the location.
056: * @param altitude The altitude component of the location (in meters above sea
057: * level).
058: */
059: public GPOSRecord(Name name, int dclass, long ttl,
060: String longitude, String latitude, String altitude) {
061: super (name, Type.GPOS, dclass, ttl);
062: try {
063: this .longitude = byteArrayFromString(longitude);
064: this .latitude = byteArrayFromString(latitude);
065: validate(getLongitude(), getLatitude());
066: this .altitude = byteArrayFromString(altitude);
067: } catch (TextParseException e) {
068: throw new IllegalArgumentException(e.getMessage());
069: }
070: }
071:
072: void rrFromWire(DNSInput in) throws IOException {
073: longitude = in.readCountedString();
074: latitude = in.readCountedString();
075: altitude = in.readCountedString();
076: try {
077: validate(getLongitude(), getLatitude());
078: } catch (IllegalArgumentException e) {
079: throw new WireParseException(e.getMessage());
080: }
081: }
082:
083: void rdataFromString(Tokenizer st, Name origin) throws IOException {
084: try {
085: longitude = byteArrayFromString(st.getString());
086: latitude = byteArrayFromString(st.getString());
087: altitude = byteArrayFromString(st.getString());
088: } catch (TextParseException e) {
089: throw st.exception(e.getMessage());
090: }
091: try {
092: validate(getLongitude(), getLatitude());
093: } catch (IllegalArgumentException e) {
094: throw new WireParseException(e.getMessage());
095: }
096: }
097:
098: /** Convert to a String */
099: String rrToString() {
100: StringBuffer sb = new StringBuffer();
101: sb.append(byteArrayToString(longitude, true));
102: sb.append(" ");
103: sb.append(byteArrayToString(latitude, true));
104: sb.append(" ");
105: sb.append(byteArrayToString(altitude, true));
106: return sb.toString();
107: }
108:
109: /** Returns the longitude as a string */
110: public String getLongitudeString() {
111: return byteArrayToString(longitude, false);
112: }
113:
114: /**
115: * Returns the longitude as a double
116: * @throws NumberFormatException The string does not contain a valid numeric
117: * value.
118: */
119: public double getLongitude() {
120: return Double.parseDouble(getLongitudeString());
121: }
122:
123: /** Returns the latitude as a string */
124: public String getLatitudeString() {
125: return byteArrayToString(latitude, false);
126: }
127:
128: /**
129: * Returns the latitude as a double
130: * @throws NumberFormatException The string does not contain a valid numeric
131: * value.
132: */
133: public double getLatitude() {
134: return Double.parseDouble(getLatitudeString());
135: }
136:
137: /** Returns the altitude as a string */
138: public String getAltitudeString() {
139: return byteArrayToString(altitude, false);
140: }
141:
142: /**
143: * Returns the altitude as a double
144: * @throws NumberFormatException The string does not contain a valid numeric
145: * value.
146: */
147: public double getAltitude() {
148: return Double.parseDouble(getAltitudeString());
149: }
150:
151: void rrToWire(DNSOutput out, Compression c, boolean canonical) {
152: out.writeCountedString(longitude);
153: out.writeCountedString(latitude);
154: out.writeCountedString(altitude);
155: }
156:
157: }
|