Skip to content

Commit

Permalink
FIX: Missing file error for profile validation
Browse files Browse the repository at this point in the history
- added missing file error message for profile validation;
- tests for the above;
- moved file checking and reporting methods to utiltiy class;
- bumped version to 0.11.0, including batch files; and
- some minor formatting changes.
  • Loading branch information
carlwilson committed May 8, 2024
1 parent 5f5e5d2 commit 97efe12
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 37 deletions.
2 changes: 1 addition & 1 deletion odf-apps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.openpreservation.odf</groupId>
<artifactId>odf-validator</artifactId>
<version>0.10.0</version>
<version>0.11.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CliValidator implements Callable<Integer> {
private static final MessageFactory FACTORY = Messages
.getInstance("org.openpreservation.odf.apps.messages.Messages");

@Option(names = {"-p", "--profile"}, description = "Validate using extended Spreadsheet preservation profile.")
@Option(names = { "-p", "--profile" }, description = "Validate using extended Spreadsheet preservation profile.")
private boolean profileFlag;
@Parameters(paramLabel = "FILE", arity = "1..*", description = "A list of Open Document Format spreadsheet files to validate.")
private File[] toValidateFiles;
Expand Down Expand Up @@ -77,8 +77,10 @@ private ValidationReport validatePath(final Path toValidate) {
private ProfileResult profilePath(final Path toProfile) {
try {
return dnaProfile.check(parser.parsePackage(toProfile));
} catch (IllegalArgumentException | IOException e) {
} catch (IllegalArgumentException | FileNotFoundException e) {
this.logMessage(toProfile, Messages.getMessageInstance("APP-2", Severity.ERROR, e.getMessage()));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Expand All @@ -92,15 +94,17 @@ private Integer results() {
Integer retStatus = 0;
for (Map.Entry<Path, ValidationReport> entry : validationReports.entrySet()) {
if (this.appMessages.containsKey(entry.getKey())) {
retStatus = Math.max(retStatus, processMessageLists(this.appMessages.get(entry.getKey()).getMessages().values()));
retStatus = Math.max(retStatus,
processMessageLists(this.appMessages.get(entry.getKey()).getMessages().values()));
}
if (entry.getValue() != null) {
retStatus = Math.max(retStatus, results(entry.getKey(), entry.getValue()));
}
}
for (Map.Entry<Path, ProfileResult> entry : profileResults.entrySet()) {
if (this.appMessages.containsKey(entry.getKey())) {
retStatus = Math.max(retStatus, processMessageLists(this.appMessages.get(entry.getKey()).getMessages().values()));
retStatus = Math.max(retStatus,
processMessageLists(this.appMessages.get(entry.getKey()).getMessages().values()));
}
if (entry.getValue() != null) {
retStatus = Math.max(retStatus, results(entry.getKey(), entry.getValue()));
Expand Down Expand Up @@ -159,7 +163,9 @@ private Integer results(final Path path, final ProfileResult report) {
if (report.getValidationReport() != null && report.getValidationReport().documentMessages.hasErrors()) {
retStatus = 1;
}
MessageLog profileMessages = (report.getValidationReport() != null) ? report.getValidationReport().documentMessages : Messages.messageLogInstance();
MessageLog profileMessages = (report.getValidationReport() != null)
? report.getValidationReport().documentMessages
: Messages.messageLogInstance();
profileMessages.add(report.getProfileMessages().getMessages());
outputSummary(profileMessages);
return retStatus;
Expand All @@ -185,10 +191,12 @@ private void outputSummary(final MessageLog messageLog) {
messageLog.getWarningCount(), messageLog.getInfoCount()));
} else {
ConsoleFormatter
.info(String.format("VALID, no errors, no warnings and %d info message found.", messageLog.getInfoCount()));
.info(String.format("VALID, no errors, no warnings and %d info message found.",
messageLog.getInfoCount()));
}
ConsoleFormatter.newline();
}

private final void logMessage(final Path path, final Message message) {
this.appMessages.putIfAbsent(path, Messages.messageLogInstance());
this.appMessages.get(path).add(path.toString(), message);
Expand Down
2 changes: 1 addition & 1 deletion odf-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.openpreservation.odf</groupId>
<artifactId>odf-validator</artifactId>
<version>0.10.0</version>
<version>0.11.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public OdfPackage parsePackage(final InputStream toParse, final String name) thr
}

private final OdfPackage parsePackage(final Path toParse, final String name) throws IOException {
Checks.existingFileCheck(toParse);
this.initialise();
try {
this.format = sniff(toParse);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.openpreservation.odf.validation;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -55,6 +54,7 @@ public ValidationReport validateSingleFormat(final File toValidate, final Format
public ValidationReport validateSingleFormat(final Path toValidate, final Formats legal) throws ParserConfigurationException, IOException, SAXException {
Objects.requireNonNull(toValidate, String.format(Checks.NOT_NULL, "Path", TO_VAL_STRING));
Objects.requireNonNull(legal, String.format(Checks.NOT_NULL, "Formats", "legal"));
Checks.existingFileCheck(toValidate);
ValidationReport report = validate(toValidate);
if (report.document == null || report.document.getFormat() == null) {
report.add(toValidate.toString(), FACTORY.getError("DOC-6"));
Expand All @@ -77,7 +77,7 @@ public ValidationReport validate(final Path toValidate)
Objects.requireNonNull(toValidate, String.format(Checks.NOT_NULL, "Path", TO_VAL_STRING));

// Check if the path exists and is not a directory
existingFileCheck(toValidate);
Checks.existingFileCheck(toValidate);

if (OdfPackages.isZip(toValidate)) {
return validatePackage(toValidate);
Expand All @@ -94,14 +94,6 @@ private static final ValidationReport notOdf(final Path toValidate) {
return report;
}

static final void existingFileCheck(final Path toValidate) throws FileNotFoundException {
if (!Files.exists(toValidate)) {
throw new FileNotFoundException(errMessage(toValidate.toString(), " does not exist."));
} else if (Files.isDirectory(toValidate)) {
throw new IllegalArgumentException(errMessage(toValidate.toString(), " is a directory."));
}
}

private ValidationReport validatePackage(final Path toValidate) throws ParserConfigurationException, SAXException, IOException {
ValidatingParser parser = Validators.getValidatingParser();
OdfPackage pckg = parser.parsePackage(toValidate);
Expand Down Expand Up @@ -133,8 +125,4 @@ private ValidationReport validateOpenDocumentXml(final Path toValidate) throws P
report.add(toValidate.toString(), parseResult.getMessages());
return report;
}

private static final String errMessage(final String toValidate, final String subMess) {
return String.format("Supplied Path parameter: %s %s", toValidate, subMess);
}
}
16 changes: 16 additions & 0 deletions odf-core/src/main/java/org/openpreservation/utils/Checks.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.openpreservation.utils;

import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Checks {
public static final String NOT_NULL = "%s parameter: %s must not be null.";
public static final String NOT_EMPTY = "String parameter: %s must not be empty.";
Expand All @@ -8,4 +12,16 @@ public class Checks {
private Checks() {
throw new AssertionError("Should not be instantiated.");
}

public static final void existingFileCheck(final Path toValidate) throws FileNotFoundException {
if (!Files.exists(toValidate)) {
throw new FileNotFoundException(errMessage(toValidate.toString(), " does not exist."));
} else if (Files.isDirectory(toValidate)) {
throw new IllegalArgumentException(errMessage(toValidate.toString(), " is a directory."));
}
}

public static final String errMessage(final String toValidate, final String subMess) {
return String.format("Supplied Path parameter: %s %s", toValidate, subMess);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -38,6 +39,28 @@ public void testParseNullFile() throws ParserConfigurationException, SAXExceptio
});
}

@Test
public void testParseMissingFile() throws ParserConfigurationException, SAXException {
PackageParser parser = OdfPackages.getPackageParser();
File file = new File("missingFileRubbishdhbna");
assertThrows("FileNotFoundException expected",
FileNotFoundException.class,
() -> {
parser.parsePackage(file);
});
}

@Test
public void testParseDirFile() throws ParserConfigurationException, SAXException {
PackageParser parser = OdfPackages.getPackageParser();
File file = new File(".");
assertThrows("IllegalArgumentException expected",
IllegalArgumentException.class,
() -> {
parser.parsePackage(file);
});
}

@Test
public void testParseNullPath() throws ParserConfigurationException, SAXException {
PackageParser parser = OdfPackages.getPackageParser();
Expand All @@ -49,6 +72,28 @@ public void testParseNullPath() throws ParserConfigurationException, SAXExceptio
});
}

@Test
public void testParseMissingPath() throws ParserConfigurationException, SAXException {
PackageParser parser = OdfPackages.getPackageParser();
Path path = Path.of(".", "missingFileRubbishdhbna");
assertThrows("FileNotFoundException expected",
FileNotFoundException.class,
() -> {
parser.parsePackage(path);
});
}

@Test
public void testParseDirPath() throws ParserConfigurationException, SAXException {
PackageParser parser = OdfPackages.getPackageParser();
Path path = Path.of(".");
assertThrows("IllegalArgumentException expected",
IllegalArgumentException.class,
() -> {
parser.parsePackage(path);
});
}

@Test
public void testParseNullStream() throws ParserConfigurationException, SAXException, IOException {
PackageParser parser = OdfPackages.getPackageParser();
Expand All @@ -66,47 +111,54 @@ public void testParseNullStream() throws ParserConfigurationException, SAXExcept
}

@Test
public void testParsePackagePath() throws ParserConfigurationException, SAXException, URISyntaxException, IOException {
public void testParsePackagePath()
throws ParserConfigurationException, SAXException, URISyntaxException, IOException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.EMPTY_ODS.toURI()).getAbsolutePath()));
assertNotNull("Parsed package should not be null", pkg);
assertTrue("Package should have a mimetype entry", pkg.hasMimeEntry());
assertEquals("Mimetype should be Spreadsheet", "application/vnd.oasis.opendocument.spreadsheet", pkg.getMimeType());
assertEquals("Mimetype should be Spreadsheet", "application/vnd.oasis.opendocument.spreadsheet",
pkg.getMimeType());
}

@Test
public void testParsePackageFile() throws ParserConfigurationException, SAXException, URISyntaxException, IOException {
public void testParsePackageFile()
throws ParserConfigurationException, SAXException, URISyntaxException, IOException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(new File(TestFiles.EMPTY_ODS.toURI()));
assertNotNull("Parsed package should not be null", pkg);
assertTrue("Package should have a mimetype entry", pkg.hasMimeEntry());
assertEquals("Mimetype should be Spreadsheet", "application/vnd.oasis.opendocument.spreadsheet", pkg.getMimeType());
assertEquals("Mimetype should be Spreadsheet", "application/vnd.oasis.opendocument.spreadsheet",
pkg.getMimeType());
}

@Test
public void testParsePackageStream() throws ParserConfigurationException, SAXException, URISyntaxException, IOException {
public void testParsePackageStream()
throws ParserConfigurationException, SAXException, URISyntaxException, IOException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(TestFiles.EMPTY_ODS.openStream(), TestFiles.EMPTY_ODS.toString());
assertNotNull("Parsed package should not be null", pkg);
assertTrue("Package should have a mimetype entry", pkg.hasMimeEntry());
assertEquals("Mimetype should be Spreadsheet", "application/vnd.oasis.opendocument.spreadsheet", pkg.getMimeType());
assertEquals("Mimetype should be Spreadsheet", "application/vnd.oasis.opendocument.spreadsheet",
pkg.getMimeType());
}

@Test
public void testDsigParsing() throws ParserConfigurationException, SAXException, IOException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(TestFiles.DSIG_INVALID.openStream(), TestFiles.DSIG_INVALID.toString());
ParseResult result = pkg.getEntryXmlParseResult("META-INF/documentsignatures.xml");
assertNotNull("Dsig file META-INF/documentsignatures.xml result should not be null" , result);
assertTrue("Package should have a well formed dsig for META-INF/documentsignatures.xml" , result.isWellFormed());
assertNotNull("Dsig file META-INF/documentsignatures.xml result should not be null", result);
assertTrue("Package should have a well formed dsig for META-INF/documentsignatures.xml", result.isWellFormed());
}

@Test
public void testManifestNotWF() throws IOException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(TestFiles.MANIFEST_NOT_WF.openStream(), TestFiles.MANIFEST_NOT_WF.toString());
OdfPackage pkg = parser.parsePackage(TestFiles.MANIFEST_NOT_WF.openStream(),
TestFiles.MANIFEST_NOT_WF.toString());
ParseResult result = pkg.getEntryXmlParseResult("META-INF/manifest.xml");
assertNotNull("Dsig file META-INF/documentsignatures.xml result should not be null" , result);
assertFalse("Package should NOT have a well formed META-INF/manifest.xml" , result.isWellFormed());
assertNotNull("Dsig file META-INF/documentsignatures.xml result should not be null", result);
assertFalse("Package should NOT have a well formed META-INF/manifest.xml", result.isWellFormed());
}
}
2 changes: 1 addition & 1 deletion odf-reporting/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.openpreservation.odf</groupId>
<artifactId>odf-validator</artifactId>
<version>0.10.0</version>
<version>0.11.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion odf-validator
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ fi

exec "$JAVACMD" -Dfile.encoding=UTF8 -XX:+IgnoreUnrecognizedVMOptions \
-Dapp.name="ODF Validator" \
-jar odf-apps/target/odf-apps-0.10.0-jar-with-dependencies.jar \
-jar odf-apps/target/odf-apps-0.11.0-jar-with-dependencies.jar \
"$@"
2 changes: 1 addition & 1 deletion odf-validator.bat
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ if NOT "%CLASSPATH_PREFIX%" == "" set CLASSPATH=%CLASSPATH_PREFIX%;%CLASSPATH%
@REM Reaching here means variables are defined and arguments have been captured
:endInit

"%JAVACMD%" -Dfile.encoding=UTF8 -XX:+IgnoreUnrecognizedVMOptions -Dapp.name="ODF Validator" -jar .\odf-apps\target\odf-apps-0.10.0-jar-with-dependencies.jar %CMD_LINE_ARGS%
"%JAVACMD%" -Dfile.encoding=UTF8 -XX:+IgnoreUnrecognizedVMOptions -Dapp.name="ODF Validator" -jar .\odf-apps\target\odf-apps-0.11.0-jar-with-dependencies.jar %CMD_LINE_ARGS%
if %ERRORLEVEL% NEQ 0 goto error
goto end

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>org.openpreservation.odf</groupId>
<artifactId>odf-validator</artifactId>
<version>0.10.0</version>
<version>0.11.0</version>
<packaging>pom</packaging>

<modules>
Expand Down

1 comment on commit 97efe12

@maria-messerschmidt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catch FileNotFound is now working when running validator from command line with profile -p.

C:\odf\odf-validator-fix-missing-file-profile>odf-validator.bat -p testfiler/T001.ods
APP-1: [INFO] Validating testfiler\T001.ods.
APP-2: [ERROR] Supplied Path parameter: testfiler\T001.ods does not exist.

Please sign in to comment.