Skip to content

Commit

Permalink
fix import xml bug
Browse files Browse the repository at this point in the history
  • Loading branch information
avibn committed May 4, 2023
1 parent 3871cdb commit 734f2a5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.team44.runwayredeclarationapp.model.Runway;
import com.team44.runwayredeclarationapp.utility.xml.XMLHandler;
import com.team44.runwayredeclarationapp.utility.xml.XMLWrapper;
import com.team44.runwayredeclarationapp.view.component.alert.ErrorAlert;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -68,6 +69,16 @@ public void uploadXMLFile(File file, Boolean reset) {
try {
// Parse the XML file
uploadedData = xmlHandler.readXML(file);

// Check for error
if (uploadedData == null) {
ErrorAlert errorAlert = new ErrorAlert("File could not be found!",
"There seems to be an unexpected error.", "Error: " + file.getName() +
" could not be found/read.");
errorAlert.show();
return;
}

var airports = uploadedData.getAirports();
var obstacles = uploadedData.getObstacles();

Expand All @@ -89,9 +100,9 @@ public void uploadXMLFile(File file, Boolean reset) {
// Call the listener to update the GUI
if (reset) {
callSetListener(airports, obstacles);
return;
} else {
callAddListener(airports, obstacles);
}
callAddListener(airports, obstacles);

// Call the listener for upload success
if (fileUploadSuccessfulListener != null) {
Expand All @@ -105,6 +116,10 @@ public void uploadXMLFile(File file, Boolean reset) {
"Please ensure that the uploaded XML file matches the schema specified.\n\nError:\n"
+ e.getMessage().replace("cvc-complex-type.2.4.a: ", ""));
}
} catch (Exception e) {
ErrorAlert errorAlert = new ErrorAlert("Unexpected Error",
"There seems to be an unexpected error.", "Error: " + e.getMessage());
errorAlert.show();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -60,7 +61,7 @@ public class XMLHandler {
/**
* The XSD validation file
*/
private File validationFile;
private URL validationFile;

/**
* Create an XML handler
Expand All @@ -70,8 +71,7 @@ public XMLHandler() {

// Set the validation schema
try {
this.validationFile = new File(
XMLValidator.class.getResource("/xml/validate.xsd").toURI());
this.validationFile = XMLValidator.class.getResource("/xml/validate.xsd");
} catch (Exception e) {
logger.error("Validation file could not be found: " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;
Expand Down Expand Up @@ -32,7 +33,7 @@ public class XMLValidator {
* @throws SAXException when there is a parsing error
* @throws IOException when there are other input/output errors from reading the file
*/
public void validateWithSchema(File xsdFile, File xmlFile) throws SAXException, IOException {
public void validateWithSchema(URL xsdFile, File xmlFile) throws SAXException, IOException {
// Try validate
try {
// Create the validator with the schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.team44.runwayredeclarationapp.TestConstants;
Expand Down Expand Up @@ -95,7 +96,7 @@ void uploadXMLFile_Valid() {
// Verify the listeners that should be called
verify(dataAddListener).load(any(Airport[].class), any(Obstacle[].class));
verify(dataSetListener).load(any(Airport[].class), any(Obstacle[].class));
verify(fileUploadSuccessfulListener).uploadSuccessful();
verify(fileUploadSuccessfulListener, times(2)).uploadSuccessful();

// Verify the error listeners that should NOT be called
verify(errorListener, never()).alert(anyString(), anyString(), anyString());
Expand Down

0 comments on commit 734f2a5

Please sign in to comment.