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

FEAT: New Policy Profile #122

Merged
merged 1 commit into from
Dec 7, 2023
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 @@ -3,13 +3,15 @@
import java.io.IOException;

import org.openpreservation.messages.MessageLog;
import org.openpreservation.messages.Message.Severity;
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.xml.OdfXmlDocument;

public interface Rule {
public String getId();
public String getName();
public String getDescription();
public Severity getSeverity();
public boolean isPrerequisite();
public MessageLog check(final OdfXmlDocument document) throws IOException;
public MessageLog check(final OdfPackage odfPackage) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ public OdfPackage parsePackage(final InputStream toParse, final String name) thr
private ValidationReport validate(final OdfPackage odfPackage) {
final ValidationReport report = ValidationReport.of(odfPackage.getName(), Documents.openDocumentOf(odfPackage));
report.add(OdfFormats.MIMETYPE, checkMimeEntry(odfPackage));
if (!odfPackage.hasManifest()) {
report.add(OdfPackages.PATH_MANIFEST, FACTORY.getError("PKG-3"));
} else {
report.add(OdfPackages.PATH_MANIFEST, validateManifest(odfPackage));
}
report.add(OdfPackages.PATH_MANIFEST, validateManifest(odfPackage));
if (!odfPackage.hasThumbnail()) {
report.add(OdfPackages.PATH_THUMBNAIL, FACTORY.getWarning("PKG-7"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

import java.util.Objects;

import org.openpreservation.messages.Message.Severity;
import org.openpreservation.odf.validation.Rule;

abstract class AbstractRule implements Rule {
final String id;
final String name;
final String description;
final Severity severity;
final Boolean isPrerequisite;

AbstractRule(final String id, final String name, final String description, final boolean isPrerequisite) {
AbstractRule(final String id, final String name, final String description, final Severity severity,
final boolean isPrerequisite) {
super();
this.id = id;
this.name = name;
this.description = description;
this.severity = severity;
this.isPrerequisite = Boolean.valueOf(isPrerequisite);
}

Expand All @@ -33,14 +37,19 @@ public String getDescription() {
return this.description;
}

@Override
public Severity getSeverity() {
return this.severity;
}

@Override
public boolean isPrerequisite() {
return this.isPrerequisite.booleanValue();
}

@Override
public final int hashCode() {
return Objects.hash(id, name, description, isPrerequisite);
return Objects.hash(id, name, description, severity, isPrerequisite);
}

@Override
Expand All @@ -52,6 +61,7 @@ public final boolean equals(final Object obj) {
final AbstractRule other = (AbstractRule) obj;
return Objects.equals(id, other.id) && Objects.equals(name, other.name)
&& Objects.equals(description, other.description)
&& Objects.equals(severity, other.severity)
&& Objects.equals(isPrerequisite, other.isPrerequisite);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.IOException;
import java.util.Objects;

import org.openpreservation.messages.Message;
import org.openpreservation.messages.Message.Severity;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.messages.Messages;
import org.openpreservation.odf.pkg.OdfPackage;
Expand All @@ -12,8 +12,14 @@

final class DigitalSignaturesRule extends AbstractRule {

private DigitalSignaturesRule(final String id, final String name, final String description, final boolean isPrerequisite) {
super(id, name, description, isPrerequisite);
static final DigitalSignaturesRule getInstance(final Severity severity) {
return new DigitalSignaturesRule("ODF_9", "Digital Signatures",
"The package MUST NOT contain any digital signatures.", severity, false);
}

private DigitalSignaturesRule(final String id, final String name, final String description, final Severity severity,
final boolean isPrerequisite) {
super(id, name, description, severity, isPrerequisite);
}

@Override
Expand All @@ -28,16 +34,11 @@ public MessageLog check(final OdfPackage odfPackage) throws IOException {
if (odfPackage.hasDsigEntries()) {
for (final String path : odfPackage.getMetaInfMap().keySet()) {
if (OdfPackages.isDsig(path)) {
messageLog.add(path, Messages.getMessageInstance(this.id, Message.Severity.ERROR, this.getName(),
messageLog.add(path, Messages.getMessageInstance(this.id, this.severity, this.getName(),
this.getDescription()));
}
}
}
return messageLog;
}

static final DigitalSignaturesRule getInstance() {
return new DigitalSignaturesRule("ODF_9", "Digital Signatures",
"The package MUST NOT contain any digital signatures.", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.IOException;
import java.util.Objects;

import org.openpreservation.messages.Message;
import org.openpreservation.messages.Message.Severity;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.messages.Messages;
import org.openpreservation.odf.pkg.FileEntry;
Expand All @@ -12,13 +12,14 @@

final class EncryptionRule extends AbstractRule {

static final EncryptionRule getInstance() {
static final EncryptionRule getInstance(final Severity severity) {
return new EncryptionRule("ODF_1", "Encryption",
"The package MUST NOT contain any encrypted entries.", true);
"The package MUST NOT contain any encrypted entries.", severity, true);
}

private EncryptionRule(final String id, final String name, final String description, final boolean isPrerequisite) {
super(id, name, description, isPrerequisite);
private EncryptionRule(final String id, final String name, final String description, final Severity severity,
final boolean isPrerequisite) {
super(id, name, description, severity, isPrerequisite);
}

@Override
Expand All @@ -35,7 +36,7 @@ public MessageLog check(final OdfPackage odfPackage) throws IOException {
}
for (final FileEntry entry : odfPackage.getManifest().getEntries()) {
if (entry.isEncrypted()) {
messageLog.add(entry.getFullPath(), Messages.getMessageInstance(this.id, Message.Severity.ERROR, this.getName(),
messageLog.add(entry.getFullPath(), Messages.getMessageInstance(this.id, this.severity, this.getName(),
this.getDescription()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.IOException;
import java.util.Objects;

import org.openpreservation.messages.Message;
import org.openpreservation.messages.Message.Severity;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.messages.Messages;
import org.openpreservation.odf.pkg.OdfPackage;
Expand All @@ -12,15 +12,17 @@

final class ExtensionMimeTypeRule extends AbstractRule {

static final ExtensionMimeTypeRule getInstance() {
static final ExtensionMimeTypeRule getInstance(final Severity severity) {
return new ExtensionMimeTypeRule("ODF_4", "Extension and MIME type",
"The MIME type value MUST be: \"application/vnd.oasis.opendocument.spreadsheet\" and the file extension MUST be \".ods\"."
+ //
"", false);
"",
severity, false);
}

private ExtensionMimeTypeRule(final String id, final String name, final String description, final boolean isPrerequisite) {
super(id, name, description, isPrerequisite);
private ExtensionMimeTypeRule(final String id, final String name, final String description, final Severity severity,
final boolean isPrerequisite) {
super(id, name, description, severity, isPrerequisite);
}

@Override
Expand All @@ -35,7 +37,7 @@ public MessageLog check(final OdfPackage odfPackage) throws IOException {
if (!odfPackage.hasMimeEntry()
|| !"application/vnd.oasis.opendocument.spreadsheet".equals(odfPackage.getMimeType())
|| !odfPackage.getName().endsWith(".ods")) {
messageLog.add(OdfPackages.MIMETYPE, Messages.getMessageInstance(this.id, Message.Severity.ERROR, this.getName(),
messageLog.add(OdfPackages.MIMETYPE, Messages.getMessageInstance(this.id, this.severity, this.getName(),
this.getDescription()));
}
return messageLog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.IOException;
import java.util.Objects;

import org.openpreservation.messages.Message;
import org.openpreservation.messages.Message.Severity;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.messages.Messages;
import org.openpreservation.odf.pkg.OdfPackage;
Expand All @@ -12,14 +12,15 @@

final class PackageMimeTypeRule extends AbstractRule {

static final PackageMimeTypeRule getInstance() {
static final PackageMimeTypeRule getInstance(final Severity severity) {
return new PackageMimeTypeRule("ODF_3", "Package mimetype entry",
"An ODF package MUST have a mimetype entry as specified in the Section 3.3 of the ODF specification v1.3.",
false);
severity, false);
}

private PackageMimeTypeRule(final String id, final String name, final String description, final boolean isPrerequisite) {
super(id, name, description, isPrerequisite);
private PackageMimeTypeRule(final String id, final String name, final String description, final Severity severity,
final boolean isPrerequisite) {
super(id, name, description, severity, isPrerequisite);
}

@Override
Expand All @@ -32,7 +33,7 @@ public MessageLog check(final OdfPackage odfPackage) throws IOException {
Objects.requireNonNull(odfPackage, "odfPackage must not be null");
final MessageLog messageLog = Messages.messageLogInstance();
if (!odfPackage.hasMimeEntry()) {
messageLog.add(OdfPackages.MIMETYPE, Messages.getMessageInstance(this.id, Message.Severity.ERROR, this.getName(),
messageLog.add(OdfPackages.MIMETYPE, Messages.getMessageInstance(this.id, this.severity, this.getName(),
this.getDescription()));
}
return messageLog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javax.xml.parsers.ParserConfigurationException;

import org.openpreservation.messages.Message.Severity;
import org.openpreservation.odf.validation.Profile;
import org.openpreservation.odf.validation.Rule;
import org.xml.sax.SAXException;
Expand All @@ -15,53 +16,53 @@ public class Rules {
static final String ODF5_SCHEMATRON = "org/openpreservation/odf/core/odf/validation/rules/odf-5.sch";
static final String ODF7_SCHEMATRON = "org/openpreservation/odf/core/odf/validation/rules/odf-7.sch";
static final String ODF8_SCHEMATRON = "org/openpreservation/odf/core/odf/validation/rules/odf-8.sch";
static final List<Rule> SET_RULES = Arrays.asList(odf1(), odf2(), odf3(), odf4(), odf5(), odf7(), odf8(), odf9(), odf10());
public static final Set<Rule> DNA_RULES = new LinkedHashSet<>(SET_RULES);
static final List<Rule> SET_RULES = Arrays.asList(odf1(), odf2(), odf3(), odf4(), odf5(), odf7(), odf8(), odf9());
static final Set<Rule> DNA_RULES = new LinkedHashSet<>(SET_RULES);

public static final Rule odf1() {
return EncryptionRule.getInstance();
return EncryptionRule.getInstance(Severity.ERROR);
}

public static final Rule odf2() {
try {
return ValidPackageRule.getInstance();
return ValidPackageRule.getInstance(Severity.ERROR);
} catch (ParserConfigurationException | SAXException e) {
throw new IllegalStateException(e);
}
}

public static final Rule odf3() {
return PackageMimeTypeRule.getInstance();
return PackageMimeTypeRule.getInstance(Severity.ERROR);
}

public static final Rule odf4() {
return ExtensionMimeTypeRule.getInstance();
return ExtensionMimeTypeRule.getInstance(Severity.ERROR);
}

public static final Rule odf5() {
return SchematronRule.getInstance("ODF_5", "External data check",
"The file MUST NOT have any references to external data.", false,
"The file MUST NOT have any references to external data.", Severity.INFO, false,
ClassLoader.getSystemResource(ODF5_SCHEMATRON));
}

public static final Rule odf7() {
return SchematronRule.getInstance("ODF_7", "Content check",
"The file MUST have values or objects in at least one cell.", false,
"The file MUST have values or objects in at least one cell.", Severity.INFO, false,
ClassLoader.getSystemResource(ODF7_SCHEMATRON));
}

public static final Rule odf8() {
return SchematronRule.getInstance("ODF_8", "Macros check",
"The file MUST NOT contain any macros.", false,
"The file MUST NOT contain any macros.", Severity.ERROR, false,
ClassLoader.getSystemResource(ODF8_SCHEMATRON));
}

public static final Rule odf9() {
return DigitalSignaturesRule.getInstance();
return DigitalSignaturesRule.getInstance(Severity.ERROR);
}

public static final Rule odf10() {
return SubDocumentRule.getInstance();
return SubDocumentRule.getInstance(Severity.WARNING);
}

public static final Profile getDnaProfile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.xml.transform.stream.StreamSource;

import org.openpreservation.messages.Message;
import org.openpreservation.messages.Message.Severity;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.messages.Messages;
import org.openpreservation.odf.pkg.FileEntry;
Expand All @@ -21,14 +22,16 @@
import com.helger.schematron.svrl.jaxb.SchematronOutputType;

final class SchematronRule extends AbstractRule {
static final SchematronRule getInstance(final String id, final String name, final String description, final boolean isPrerequisite, final URL schematronLoc) {
return new SchematronRule(id, name, description, isPrerequisite, schematronLoc);
static final SchematronRule getInstance(final String id, final String name, final String description,
final Severity severity, final boolean isPrerequisite, final URL schematronLoc) {
return new SchematronRule(id, name, description, severity, isPrerequisite, schematronLoc);
}

final SchematronResourcePure schematron;

private SchematronRule(final String id, final String name, final String description, final boolean isPrerequisite, final URL schematronLoc) {
super(id, name, description, isPrerequisite);
private SchematronRule(final String id, final String name, final String description, final Severity severity,
final boolean isPrerequisite, final URL schematronLoc) {
super(id, name, description, severity, isPrerequisite);
Objects.requireNonNull(schematronLoc, "schematronLoc must not be null");
this.schematron = SchematronResourcePure.fromURL(schematronLoc);
}
Expand All @@ -48,10 +51,13 @@ public MessageLog check(final OdfPackage odfPackage) throws IOException {
continue;
}
try (InputStream is = odfPackage.getEntryStream(entry)) {
final SchematronOutputType schResult = this.schematron.applySchematronValidationToSVRL(new StreamSource(is));
for (final AbstractSVRLMessage result : SVRLHelper.getAllFailedAssertionsAndSuccessfulReports(schResult)) {
messageLog.add(entry.getFullPath(), Messages.getMessageInstance(this.id, Message.Severity.ERROR, this.getName(),
result.getText()));
final SchematronOutputType schResult = this.schematron
.applySchematronValidationToSVRL(new StreamSource(is));
for (final AbstractSVRLMessage result : SVRLHelper
.getAllFailedAssertionsAndSuccessfulReports(schResult)) {
messageLog.add(entry.getFullPath(),
Messages.getMessageInstance(this.id, Message.Severity.ERROR, this.getName(),
result.getText()));
}
} catch (final Exception e) {
throw new IOException(e);
Expand Down
Loading