Skip to content

Commit

Permalink
Merge pull request #224 from openpreserve/fix/212
Browse files Browse the repository at this point in the history
FIX: Crash on single file policy check
  • Loading branch information
carlwilson authored Nov 25, 2024
2 parents 9609a5c + da1fbb7 commit 72f2437
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private ValidationReport validatePath(final Path toValidate) {
private ProfileResult profileReport(final ValidationReport toProfile, final Path path) {
try {
final Profile dnaProfile = Rules.getDnaProfile();
return dnaProfile.check(toProfile.document);
return dnaProfile.check(toProfile);
} catch (IllegalArgumentException e) {
this.logMessage(path, Messages.getMessageInstance("APP-2", Severity.ERROR, e.getMessage()));
} catch (ParseException | ParserConfigurationException | SAXException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.openpreservation.messages.MessageFactory;
import org.openpreservation.messages.Messages;
import org.openpreservation.odf.document.Documents;
import org.openpreservation.odf.document.OpenDocument;
import org.openpreservation.odf.fmt.Formats;
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.pkg.OdfPackages;
Expand Down Expand Up @@ -113,6 +114,17 @@ private ValidationReport validateOpenDocumentXml(final Path toValidate)
throws ParserConfigurationException, SAXException, IOException {
final XmlParser checker = new XmlParser();
ParseResult parseResult = checker.parse(toValidate);
return validateParseResult(toValidate, parseResult);
}

public ValidationReport validateOpenDocument(final OpenDocument toValidate)
throws IOException {
ParseResult parseResult = toValidate.getDocument().getXmlDocument().getParseResult();
return validateParseResult(toValidate.getPath(), parseResult);
}

private ValidationReport validateParseResult(final Path toValidate, ParseResult parseResult)
throws IOException {
final ValidationReport report = (parseResult.isWellFormed())
? ValidationReport.of(toValidate.toString(),
Documents.openDocumentOf(toValidate, Documents.odfDocumentOf(parseResult)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import javax.xml.parsers.ParserConfigurationException;

import org.openpreservation.format.zip.ZipEntry;
import org.openpreservation.odf.document.OpenDocument;
import org.xml.sax.SAXException;

import net.sf.saxon.lib.Validation;

public class Validators {

/**
Expand All @@ -28,6 +31,14 @@ public static boolean isCompressionValid(final ZipEntry entry) {
return ValidatingParserImpl.isCompressionValid(entry);
}

public static final ValidationReport reportOf(final String name) {
return ValidationReport.of(name);
}

public static final ValidationReport reportOf(final String name, final OpenDocument document) {
return ValidationReport.of(name, document);
}

private Validators() {
throw new AssertionError("Utility class 'Validators' should not be instantiated");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openpreservation.odf.validation.rules;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand All @@ -19,6 +20,7 @@
import org.openpreservation.odf.validation.Rule;
import org.openpreservation.odf.validation.ValidatingParser;
import org.openpreservation.odf.validation.ValidationReport;
import org.openpreservation.odf.validation.Validator;
import org.openpreservation.odf.validation.Validators;
import org.xml.sax.SAXException;

Expand All @@ -39,9 +41,14 @@ private ProfileImpl(final String id, final String name, final String description
public ProfileResult check(final OpenDocument document) throws ParseException {
Objects.requireNonNull(document, "document must not be null");
try {
return check(this.validatingParser.validatePackage(document.getPath(), document.getPackage()));
ValidationReport report = document.isPackage()
? this.validatingParser.validatePackage(document.getPath(), document.getPackage())
: new Validator().validateOpenDocument(document);
return check(report);
} catch (FileNotFoundException e) {
throw new ParseException("File not found exception when processing package.", e);
} catch (IOException e) {
throw new ParseException("IO exception when processing package.", e);
}
}

Expand Down

0 comments on commit 72f2437

Please sign in to comment.