Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Crash on single file policy check #224

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading