Scanner scanner = new Scanner(is);
int lineNumber = 0;
try {
String province = null;
ChinaStation chinaStation = null;
StringBuilder sb = new StringBuilder();
while (scanner.hasNextLine()) {
logger.debug("Processing line " + ++lineNumber);
String line = scanner.nextLine().trim();
if (line.startsWith("#")) {
if (chinaStation != null) {
chinaStation.setDescription(sb.toString());
saveStation(chinaStation);
chinaStation = null;
sb = new StringBuilder();
}
if (line.startsWith("##")) {
province = line.substring(2);
if (province.trim().length() == 0) {
throw logError(new StationImporterException("Found empty provice in line " + lineNumber));
}
logger.info("Provinced changed to: " + province);
} else {
if (province == null) {
throw logError(new StationImporterException("Found station outside province in line "
+ lineNumber));
}
String stationName = line.substring(1);
if (stationName.trim().length() == 0) {
throw logError(new StationImporterException("Found empty station name in line "
+ lineNumber));
}
chinaStation = new ChinaStation();
chinaStation.setName(stationName);
chinaStation.setProvince(province);
}
} else {
if (chinaStation == null) {
throw logError(new StationImporterException(
"Found description does not belong to a station in line " + lineNumber));
}
sb.append(line);
}
}
if (chinaStation != null) {
chinaStation.setDescription(sb.toString());
saveStation(chinaStation);
}
} finally {
scanner.close();
}