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: Flat file policies #220

Merged
merged 1 commit into from
Nov 20, 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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# ODF Validator

Latest version is 0.16.2-SNAPSHOT.
=======

## About

Expand Down
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);
return dnaProfile.check(toProfile.document);
} 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
@@ -1,11 +1,19 @@
package org.openpreservation.format.zip;

import java.nio.file.Path;
import java.util.List;

/**
* An interface for accessing the contents of a Zip archive.
*/
public interface ZipArchive {
/**
* Get the path to the archive
*
* @return the <code>Path</code> to the archive
*/
public Path getPath();

/**
* Returns the first physical {@link ZipEntry} in the archive.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -50,4 +51,9 @@ static final ZipArchiveCacheImpl of(final ZipArchive archive, final Map<String,
public ZipEntry getFirstEntry() {
return this.archive.getFirstEntry();
}

@Override
public Path getPath() {
return this.archive.getPath();
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package org.openpreservation.format.zip;

import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.openpreservation.utils.Checks;

final class ZipArchiveImpl implements ZipArchive {
static final ZipArchiveImpl from(final List<ZipEntry> entries) {
static final ZipArchiveImpl from(final Path path, final List<ZipEntry> entries) {
Objects.requireNonNull(path, String.format(Checks.NOT_NULL, "Path", "path"));
Objects.requireNonNull(entries, String.format(Checks.NOT_NULL, "List<ZipEntry>", "entries"));
if (entries.contains(null)) {
throw new IllegalArgumentException("List<ZipEntry> entries cannot contain null entries");
}
return new ZipArchiveImpl(entries);
return new ZipArchiveImpl(path, entries);
}

private final Path path;
private final List<ZipEntry> entries;

private ZipArchiveImpl(final List<ZipEntry> entries) {
private ZipArchiveImpl(final Path path, final List<ZipEntry> entries) {
this.path = path;
this.entries = Collections.unmodifiableList(entries);
}

@Override
public Path getPath() {
return this.path;
}

@Override
public ZipEntry getFirstEntry() {
return this.entries.stream().findFirst().orElse(null);
Expand Down Expand Up @@ -51,6 +60,7 @@ public String toString() {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((path == null) ? 0 : path.hashCode());
result = prime * result + ((entries == null) ? 0 : entries.hashCode());
return result;
}
Expand All @@ -69,6 +79,11 @@ public boolean equals(final Object obj) {
return false;
} else if (!entries.equals(other.entries))
return false;
if (path == null) {
if (other.path != null)
return false;
} else if (!path.equals(other.path))
return false;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.archivers.zip.ZipFile.Builder;
import org.openpreservation.utils.Checks;

/**
Expand Down Expand Up @@ -50,7 +51,7 @@ static ZipFileProcessor of(final Path path) throws IOException {

private final Map<String, ZipArchiveEntry> cache(final Path path) throws IOException {
Map<String, ZipArchiveEntry> result = new HashMap<>();
try (ZipFile zipFile = new ZipFile(path)) {
try (ZipFile zipFile = new Builder().setSeekableByteChannel(Files.newByteChannel(path)).get()) {
Enumeration<ZipArchiveEntry> entries = zipFile.getEntriesInPhysicalOrder();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
Expand All @@ -63,6 +64,11 @@ private final Map<String, ZipArchiveEntry> cache(final Path path) throws IOExcep
return result;
}

@Override
public Path getPath() {
return this.path;
}

@Override
public List<ZipEntry> getZipEntries() {
return this.entryCache.values().stream().map(new Function<ZipArchiveEntry, ZipEntry>() {
Expand Down Expand Up @@ -108,7 +114,8 @@ public InputStream getEntryInputStream(String entryName) throws IOException {
return null;
}
if (!this.byteCache.containsKey(entryName)) {
try (ZipFile zipFile = new ZipFile(this.path)) {
try (ZipFile zipFile = new ZipFile.Builder().setSeekableByteChannel(Files.newByteChannel(this.path))
.get()) {
ZipArchiveEntry entry = zipFile.getEntry(entryName);
if (entry == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;

/**
* Interface for a processor that processes an {@link InputStream} and a factory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -73,7 +74,7 @@ public static final ZipProcessor.Factory factoryInstance() {
processor.process(entry, zis);
}
}
return ZipArchiveImpl.from(entries);
return ZipArchiveImpl.from(Paths.get(""), entries);
}));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.openpreservation.odf.document;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

import org.openpreservation.format.xml.ParseResult;
import org.openpreservation.format.zip.ZipArchive;
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.xml.Metadata;
import org.openpreservation.odf.xml.OdfXmlDocument;
Expand All @@ -13,14 +16,15 @@ private Documents() {
throw new AssertionError("Utility class 'Documents' should not be instantiated");
}

public static final OpenDocument openDocumentOf(OdfDocument document) {
public static final OpenDocument openDocumentOf(final Path path, final OdfDocument document) {
Objects.requireNonNull(document, "OdfDocument parameter document cannot be null");
return OpenDocumentImpl.of(document);
return OpenDocumentImpl.of(path, document);
}

public static final OpenDocument openDocumentOf(OdfPackage pkg) {
public static final OpenDocument openDocumentOf(final Path path, final OdfPackage pkg) {
Objects.requireNonNull(path, "Path path document cannot be null");
Objects.requireNonNull(pkg, "OdfPackage pkg document cannot be null");
return OpenDocumentImpl.of(pkg);
return OpenDocumentImpl.of(path, pkg);
}

public static final OdfDocument odfDocumentOf(final OdfXmlDocument xmlDocument, final Metadata metadata) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package org.openpreservation.odf.document;

import java.nio.file.Path;
import java.util.Collection;

import org.openpreservation.odf.fmt.Formats;
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.xml.Version;

public interface OpenDocument {
/**
* Get the path to the OpenDocument file.
*
* @return the path to the OpenDocument file
*/
public Path getPath();

/**
* Indicates the type of OpenDocument, a zip archive package or a single XML
* file
Expand Down Expand Up @@ -40,14 +48,16 @@ public interface OpenDocument {
public Collection<OdfDocument> getSubDocuments();

/**
* Get the ODF Package for the OpenDocument, this will be null for a single XML file.
* Get the ODF Package for the OpenDocument, this will be null for a single XML
* file.
*
* @return the ODF Package for the OpenDocument
*/
public OdfPackage getPackage();

/**
* Get the format of the OpenDocument, this will be the declared format of the package
* Get the format of the OpenDocument, this will be the declared format of the
* package
* or the parsed format of a single document.
*
* @return the format of the OpenDocument
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openpreservation.odf.document;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -11,23 +12,30 @@
import org.openpreservation.odf.xml.Version;

final class OpenDocumentImpl implements OpenDocument {
private final Path path;
private final OdfDocument document;
private final OdfPackage pkg;

private OpenDocumentImpl(OdfDocument document, OdfPackage pkg) {
private OpenDocumentImpl(final Path path, OdfDocument document, OdfPackage pkg) {
super();
this.path = path;
this.document = document;
this.pkg = pkg;
}

static final OpenDocument of(OdfDocument document) {
static final OpenDocument of(final Path path, OdfDocument document) {
Objects.requireNonNull(document, "OdfDocument parameter document cannot be null");
return new OpenDocumentImpl(document, null);
return new OpenDocumentImpl(path, document, null);
}

static final OpenDocument of(OdfPackage pkg) {
static final OpenDocument of(final Path path, OdfPackage pkg) {
Objects.requireNonNull(pkg, "OdfPackage pkg document cannot be null");
return new OpenDocumentImpl(pkg.getDocument(), pkg);
return new OpenDocumentImpl(path, pkg.getDocument(), pkg);
}

@Override
public Path getPath() {
return this.path;
}

@Override
Expand Down Expand Up @@ -74,7 +82,7 @@ public Version getVersion() {

@Override
public int hashCode() {
return Objects.hash(document, pkg);
return Objects.hash(path, document, pkg);
}

@Override
Expand All @@ -84,7 +92,7 @@ public boolean equals(Object obj) {
if (!(obj instanceof OpenDocumentImpl))
return false;
OpenDocumentImpl other = (OpenDocumentImpl) obj;
return Objects.equals(document, other.document) && Objects.equals(pkg, other.pkg);
return Objects.equals(path, other.path) && Objects.equals(document, other.document) && Objects.equals(pkg, other.pkg);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import java.util.Set;

import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.document.OpenDocument;
import org.openpreservation.odf.pkg.PackageParser.ParseException;
import org.openpreservation.odf.xml.OdfXmlDocument;

public interface Profile {
public String getId();
Expand All @@ -13,11 +12,9 @@ public interface Profile {

public String getDescription();

public ProfileResult check(final OdfXmlDocument document) throws ParseException;
public ProfileResult check(final OpenDocument document) throws ParseException;

public ProfileResult check(final ValidationReport report) throws ParseException;

public ProfileResult check(final OdfPackage odfPackage) throws ParseException;

public Set<Rule> getRules();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import org.openpreservation.messages.Message.Severity;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.document.OpenDocument;
import org.openpreservation.odf.pkg.PackageParser.ParseException;
import org.openpreservation.odf.xml.OdfXmlDocument;

public interface Rule {
public String getId();
Expand All @@ -17,9 +16,7 @@ public interface Rule {

public boolean isPrerequisite();

public MessageLog check(final OdfXmlDocument document) throws ParseException;

public MessageLog check(final OdfPackage odfPackage) throws ParseException;
public MessageLog check(final OpenDocument document) throws ParseException;

public MessageLog check(final ValidationReport report) throws ParseException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.pkg.PackageParser;
Expand All @@ -15,6 +16,6 @@ public interface ValidatingParser extends PackageParser {
* @throws IOException if there's a problem reading package elements from the
* zip file.
*/
public ValidationReport validatePackage(final OdfPackage odfPackage)
public ValidationReport validatePackage(final Path path, final OdfPackage odfPackage)
throws ParseException, FileNotFoundException;
}
Loading
Loading