📄 parser.java
字号:
package gps.parser;
import gps.datatypes.Record;
import gps.exceptions.ParseException;
import gps.exceptions.UnsupportedTypeException;
import java.util.NoSuchElementException;
/**
* NMEA-0183 Parser. Parses data sent by GPS receiver. As data is being
* transfered via XML to server, parsing consists in most cases of separating
* fields.
*
* @author Dominik Schmidt
*/
public class Parser {
/**
* Type not supported.
*/
/**
* Parses a string sent by GPS receiver.
*
* @param s
* String to be parsed
* @param record
* Record to store data
* @return Type of record
* @throws UnsupportedTypeException
* If type is not recognized
* @throws ParseException
* If there was an error during parsing
*/
public static boolean parse(String s, Record record)
throws UnsupportedTypeException, ParseException {
// Tokenizer to separate tokens
StringTokenizer tokenizer = new StringTokenizer(s,",");
// Type of record
int type;
String temp ;
try {
if (tokenizer.countTokens()==15) {
tokenizer.nextToken();
temp = tokenizer.nextToken();
// Time of fix
record.dateTimeOfFix = calaTime(temp.substring(0,2))+ ":"
+ temp.substring(2, 4) + ":" + temp.substring(4, 6);
// Lattitude
record.lattitude = calLatitude(tokenizer.nextToken());
// Lattitude direction
tokenizer.nextToken();
// Longitude
record.longitude = calLongitude(tokenizer.nextToken());
// Longitude direction
tokenizer.nextToken();
record.quality = tokenizer.nextToken();
record.satelliteCount = tokenizer.nextToken();
tokenizer.nextToken();//
record.altitude = tokenizer.nextToken();
// Ignore rest
return true;
}
}
// Parsing exception.
catch (NoSuchElementException e) {
throw new ParseException("Unexpected end of input.");
}
return false;
}
/**
*
* @param temp
*/
private static String calaTime(String hour)
{
int i ;
String strHour = null;
i = Integer.parseInt(hour.substring(0,2));
i+=8;
i%=24;
if (i>9)
{
strHour = String.valueOf(i);
}
else
{
strHour ="0"+ String.valueOf(i);
}
return strHour;
}
private static String calLongitude(String str){
String strnew = null;
float i = Float.parseFloat(str.substring(0,3));
float j = Float.parseFloat(str.substring(3,str.length()))/60;
strnew = Float.toString(i+j);
System.out.println(strnew);
return strnew;
}
private static String calLatitude(String str) {
String strLatitude = null;
float i = Float.parseFloat(str.substring(0, 2));
float j = Float.parseFloat(str.substring(2, str.length())) / 60;
strLatitude = Float.toString(i + j);
return strLatitude;
}
// private static String calAtitude(String str){
// String strAtitude = null;
// float i = Float.parseFloat(str.substring(0, 3));
// float j = Float.parseFloat(str.substring(3, str.length())) / 60;
// strAtitude = Float.toString(i + j);
// return strAtitude;
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -