Skip to content

Commit

Permalink
adding better error message and improved charset handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Pieter Van Eeckhout committed Apr 23, 2018
1 parent ac0bd92 commit 1cf2c29
Show file tree
Hide file tree
Showing 7 changed files with 742 additions and 743 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,74 +25,69 @@

import com.honeyedoak.waypointcoverter.controller.file.FileController;
import com.honeyedoak.waypointcoverter.controller.waypoint.WaypointController;
import com.honeyedoak.waypointcoverter.exceptions.ProcessingException;
import com.honeyedoak.waypointcoverter.exceptions.FatalException;
import com.honeyedoak.waypointcoverter.exceptions.FileException;
import com.honeyedoak.waypointcoverter.exceptions.InvalidModelStateException;
import com.honeyedoak.waypointcoverter.exceptions.ParseException;
import com.honeyedoak.waypointcoverter.exceptions.WaypointAlreadyExistsException;
import com.honeyedoak.waypointcoverter.exceptions.WaypointDoesNotExistException;
import com.honeyedoak.waypointcoverter.exceptions.*;
import org.apache.log4j.Logger;

import java.io.IOException;

/**
* DomainFacade.java (UTF-8)
*
* <p>
* <p>Entry point into the domain layer. Delegates and coordinates the tasks to
* the respective controllers</p>
*
* <p>
* 2013/06/21
*
* @author Pieter Van Eeckhout <[email protected]>
* @since 1.0.2
* @version 1.0.2
* @since 1.0.2
*/
public class DomainFacade {

private WaypointController waypointController;
private FileController fileController;
private Logger logger;
private WaypointController waypointController;
private FileController fileController;
private Logger logger;

public DomainFacade(WaypointController waypointController, FileController fileController) {
logger = Logger.getLogger(DomainFacade.class);
this.waypointController = waypointController;
this.fileController = fileController;
}
public DomainFacade(WaypointController waypointController, FileController fileController) {
logger = Logger.getLogger(DomainFacade.class);
this.waypointController = waypointController;
this.fileController = fileController;
}

public WaypointController getWaypointController() {
return waypointController;
}
public WaypointController getWaypointController() {
return waypointController;
}

public void setWaypointController(WaypointController waypointController) {
this.waypointController = waypointController;
}
public void setWaypointController(WaypointController waypointController) {
this.waypointController = waypointController;
}

public FileController getFileController() {
return fileController;
}
public FileController getFileController() {
return fileController;
}

public void setFileController(FileController fileController) {
this.fileController = fileController;
}
public void setFileController(FileController fileController) {
this.fileController = fileController;
}

public void loadFile(String filePath) throws FileException, FatalException, ProcessingException {
try {
waypointController.addWaypoints(fileController.readWaypointsFromFile(filePath));
public void loadFile(String filePath) throws FileException, FatalException, ProcessingException {
try {
waypointController.addWaypoints(fileController.readWaypointsFromFile(filePath));

} catch (WaypointAlreadyExistsException | InvalidModelStateException | ParseException ex) {
throw new ProcessingException(ex.getMessage());
}
}
} catch (WaypointAlreadyExistsException | InvalidModelStateException | ParseException ex) {
throw new ProcessingException(ex.getMessage());
}
}

public void toggleWaypointExport(String waypointName) throws WaypointDoesNotExistException {
waypointController.toggleWaypointExport(waypointName);
}
public void toggleWaypointExport(String waypointName) throws WaypointDoesNotExistException {
waypointController.toggleWaypointExport(waypointName);
}

public void exportWaypoints(String filePath, boolean overwrite) throws FileException, ProcessingException {
try {
fileController.writeOpelWaypointsToFile(filePath, waypointController.getWaypointRepository().getWaypointsToExport(), overwrite);
} catch (InvalidModelStateException e) {
throw new ProcessingException(e.getMessage());
}

}
public void exportWaypoints(String filePath, boolean overwrite) throws IOException, ProcessingException {
try {
fileController.writeOpelWaypointsToFile(filePath, waypointController.getWaypointRepository().getWaypointsToExport(), overwrite);
} catch (InvalidModelStateException e) {
throw new ProcessingException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

/**
* GPSCoordinateControler.java (UTF-8)
Expand All @@ -50,75 +53,66 @@
*/
public class FileController {

private Logger logger;
private FileParser fileParser;

public FileController() {
logger = Logger.getLogger(FileController.class);
}

public List<Waypoint> readWaypointsFromFile(String filePath) throws FileException, FatalException, InvalidModelStateException, ParseException {

if (filePath == null || filePath.isEmpty()) {
logger.error("input file path is empty or null");
throw new InvalidModelStateException("intput filePath cannot be null or empty");
}

if (filePath.endsWith("txt")) {
fileParser = new TxtFileParser();
} else if (filePath.endsWith("gpx")) {
fileParser = new GpxFileParser();
} else if (filePath.endsWith("csv")) {
fileParser = new CsvFileParser();
} else {
throw new ParseException("unsupported File extention");
}

return fileParser.parseFile(new File(filePath));
}

public void writeOpelWaypointsToFile(String filePath, List<Waypoint> waypointList) throws InvalidModelStateException, FileException {
writeOpelWaypointsToFile(filePath, waypointList, false);
}

public void writeOpelWaypointsToFile(String filePath, List<Waypoint> waypointList, boolean overwrite) throws InvalidModelStateException, FileException {

PrintWriter pw = null;

if (filePath == null || filePath.isEmpty()) {
logger.error("output file path is empty or null");
throw new InvalidModelStateException("output filePath cannot be null or empty.");
}

//auto add file extention
if (!filePath.endsWith(".txt")) {
logger.info("file was missing extion, added .txt");
filePath += ".txt";
}

try {
File file = new File(filePath);

if (file.exists() && !overwrite) {
logger.info(filePath + " already exists");
throw new FileAlreadyExistsException(filePath + " already exists");
} else {
logger.info(filePath + " already exists. Cleared for overwrite");
}

logger.debug("writing " + waypointList.size() + " waypoints to file");
FileWriter fw = new FileWriter(file);
pw = new PrintWriter(fw);
for (Waypoint waypoint : waypointList) {
pw.println(waypoint.toOpelWaypoint());
}
} catch (IOException ex) {
logger.error("IO problem: " + ex.getMessage());
throw new FileException("There was an error writing to file");
} finally {
if (pw != null) {
pw.close();
}
}
}
private Logger logger;
private FileParser fileParser;

public FileController() {
logger = Logger.getLogger(FileController.class);
}

public List<Waypoint> readWaypointsFromFile(String filePath) throws FileException, FatalException, InvalidModelStateException, ParseException {

if (filePath == null || filePath.isEmpty()) {
logger.error("input file path is empty or null");
throw new InvalidModelStateException("intput filePath cannot be null or empty");
}

if (filePath.endsWith("txt")) {
fileParser = new TxtFileParser();
} else if (filePath.endsWith("gpx")) {
fileParser = new GpxFileParser();
} else if (filePath.endsWith("csv")) {
fileParser = new CsvFileParser();
} else {
throw new ParseException("unsupported File extention");
}

return fileParser.parseFile(new File(filePath));
}

public void writeOpelWaypointsToFile(String filePath, List<Waypoint> waypointList) throws InvalidModelStateException, IOException {
writeOpelWaypointsToFile(filePath, waypointList, false);
}

public void writeOpelWaypointsToFile(String filePath, List<Waypoint> waypointList, boolean overwrite) throws InvalidModelStateException, IOException {

if (filePath == null || filePath.isEmpty()) {
logger.error("output file path is empty or null");
throw new InvalidModelStateException("output filePath cannot be null or empty.");
}

//auto add file extention
if (!filePath.endsWith(".txt")) {
logger.info("file was missing extion, added .txt");
filePath += ".txt";
}

try (OutputStream outstream = Files.newOutputStream(Paths.get(filePath))) {
File file = new File(filePath);

if (file.exists() && !overwrite) {
logger.info(filePath + " already exists");
throw new FileAlreadyExistsException(filePath + " already exists");
} else {
logger.info(filePath + " already exists. Cleared for overwrite");
}

final String collect = waypointList.stream().map(Waypoint::toOpelWaypoint).collect(Collectors.joining(String.format("%n")));
outstream.write(collect.getBytes(StandardCharsets.ISO_8859_1));

} catch (IOException e) {
logger.error(e);
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Waypoint parseLine(String line, String seperator) throws ParseException {
try {
String[] parts = line.split(seperator);

String name = parts[7].replaceAll("^\"|\"$", "");;
String name = parts[7].replaceAll("^\"|\"$", "");
double latitude = Double.parseDouble(parts[1]);
double longitude = Double.parseDouble(parts[2]);
boolean north, east;
Expand Down
Loading

0 comments on commit 1cf2c29

Please sign in to comment.