Skip to content

Commit

Permalink
Merge pull request #2 from BezrukovM/mets-mediaconch-plugins
Browse files Browse the repository at this point in the history
Mets and mediaconch plugins
  • Loading branch information
carlwilson committed May 29, 2016
2 parents 4c6bc09 + f7d3c5d commit 2da039e
Show file tree
Hide file tree
Showing 11 changed files with 856 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Package Files #
*.jar
!/metsMetadata-plugin/src/main/resources/mets-api.jar
*.war
*.ear

Expand Down
1 change: 0 additions & 1 deletion iccdump-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@

<artifactId>iccdump-plugin</artifactId>


</project>
15 changes: 15 additions & 0 deletions mediaconch/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>verapdf-library-samples</artifactId>
<groupId>org.verapdf</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>mediaconch-plugin</artifactId>


</project>
125 changes: 125 additions & 0 deletions mediaconch/src/main/java/org/verapdf/MediaConchConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
*
*/
package org.verapdf;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.*;

/**
* @author Maksim Bezrukov
*/
@XmlRootElement(namespace = "http://www.verapdf.org/MediaConchConfig", name = "mediaconchConfig")
final class MediaConchConfig {

@XmlElement
private final String cliPath;
@XmlElement
private final String outFolder;

private MediaConchConfig() {
this("", "");
}

private MediaConchConfig(String outFolder, String cliPath) {
this.outFolder = outFolder;
this.cliPath = cliPath;
}

public String getCliPath() {
return cliPath;
}

public String getOutFolder() {
return outFolder;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

MediaConchConfig that = (MediaConchConfig) o;

if (cliPath != null ? !cliPath.equals(that.cliPath) : that.cliPath != null) return false;
return !(outFolder != null ? !outFolder.equals(that.outFolder) : that.outFolder != null);

}

@Override
public int hashCode() {
int result = cliPath != null ? cliPath.hashCode() : 0;
result = 31 * result + (outFolder != null ? outFolder.hashCode() : 0);
return result;
}

static MediaConchConfig defaultInstance() {
return new MediaConchConfig("mediaconch", null);
}

static MediaConchConfig fromValues(final String cliPath, final String outFolder) {
return new MediaConchConfig(cliPath, outFolder);
}

static String toXml(final MediaConchConfig toConvert, Boolean prettyXml)
throws JAXBException, IOException {
String retVal = "";
try (StringWriter writer = new StringWriter()) {
toXml(toConvert, writer, prettyXml);
retVal = writer.toString();
return retVal;
}
}

static MediaConchConfig fromXml(final String toConvert)
throws JAXBException {
try (StringReader reader = new StringReader(toConvert)) {
return fromXml(reader);
}
}

static void toXml(final MediaConchConfig toConvert,
final OutputStream stream, Boolean prettyXml) throws JAXBException {
Marshaller varMarshaller = getMarshaller(prettyXml);
varMarshaller.marshal(toConvert, stream);
}

static MediaConchConfig fromXml(final InputStream toConvert)
throws JAXBException {
Unmarshaller stringUnmarshaller = getUnmarshaller();
return (MediaConchConfig) stringUnmarshaller.unmarshal(toConvert);
}

static void toXml(final MediaConchConfig toConvert, final Writer writer,
Boolean prettyXml) throws JAXBException {
Marshaller varMarshaller = getMarshaller(prettyXml);
varMarshaller.marshal(toConvert, writer);
}

static MediaConchConfig fromXml(final Reader toConvert)
throws JAXBException {
Unmarshaller stringUnmarshaller = getUnmarshaller();
return (MediaConchConfig) stringUnmarshaller.unmarshal(toConvert);
}

private static Unmarshaller getUnmarshaller() throws JAXBException {
JAXBContext context = JAXBContext
.newInstance(MediaConchConfig.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
return unmarshaller;
}

private static Marshaller getMarshaller(Boolean setPretty)
throws JAXBException {
JAXBContext context = JAXBContext
.newInstance(MediaConchConfig.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, setPretty);
return marshaller;
}
}
144 changes: 144 additions & 0 deletions mediaconch/src/main/java/org/verapdf/MediaConchExtractor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package org.verapdf;

import org.verapdf.core.FeatureParsingException;
import org.verapdf.features.AbstractEmbeddedFileFeaturesExtractor;
import org.verapdf.features.EmbeddedFileFeaturesData;
import org.verapdf.features.tools.FeatureTreeNode;

import javax.xml.bind.JAXBException;
import java.io.*;
import java.util.*;

/**
* @author Maksim Bezrukov
*/
public class MediaConchExtractor extends AbstractEmbeddedFileFeaturesExtractor {

@Override
public List<FeatureTreeNode> getEmbeddedFileFeatures(EmbeddedFileFeaturesData embeddedFileFeaturesData) {
if (!isValidType(embeddedFileFeaturesData.getSubtype())) {
return null;
}
List<FeatureTreeNode> result = new ArrayList<>();
try {
try {
MediaConchConfig config = getConfig(result);
File temp = generateTempFile(embeddedFileFeaturesData.getStream(), embeddedFileFeaturesData.getName());
execCLI(result, config, temp);
} catch (IOException | InterruptedException e) {
FeatureTreeNode node = FeatureTreeNode.createRootNode("error");
node.setValue("Error in execution. Error message: " + e.getMessage());
result.add(node);
}
} catch (FeatureParsingException e) {
throw new IllegalStateException(e);
}
return result;
}

private File generateTempFile(byte[] stream, String name) throws IOException {
File fold = getTempFolder();
File temp = File.createTempFile(name == null ? "" : name, "", fold);
temp.deleteOnExit();
FileOutputStream out = new FileOutputStream(temp);
out.write(stream);
out.close();
return temp;
}

private void execCLI(List<FeatureTreeNode> nodes, MediaConchConfig config, File temp) throws InterruptedException, FeatureParsingException, IOException {
List<FeatureTreeNode> res = new ArrayList<>();
Runtime rt = Runtime.getRuntime();
String cliPath;
String configCliPath = config.getCliPath();
if (configCliPath == null || configCliPath.isEmpty()) {
cliPath = "mediaconch";
} else {
cliPath = configCliPath;
}
File out = getOutFile(config, nodes);
String[] str = new String[]{cliPath, "-mc", "-fx", temp.getCanonicalPath()};
Process pr = rt.exec(str);
FileOutputStream outStream = new FileOutputStream(out);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = pr.getInputStream().read(buffer)) != -1)
{
outStream.write(buffer, 0, bytesRead);
}
pr.waitFor();
FeatureTreeNode node = FeatureTreeNode.createRootNode("resultPath");
node.setValue(out.getCanonicalPath());
nodes.add(node);

}

private File getOutFile(MediaConchConfig config, List<FeatureTreeNode> nodes) throws FeatureParsingException, IOException {
if (config.getOutFolder() == null) {
File tempFolder = getTempFolder();
File res = getOutFileInFolder(tempFolder);
return res;
} else {
File outFolder = new File(config.getOutFolder());
if (outFolder.isDirectory()) {
File res = getOutFileInFolder(outFolder);
return res;
} else {
FeatureTreeNode node = FeatureTreeNode.createRootNode("error");
node.setValue("Config file contains out folder path but it doesn't link a directory.");
nodes.add(node);
File tempFolder = getTempFolder();
File res = getOutFileInFolder(tempFolder);
return res;
}
}
}

private File getTempFolder() {
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File tempFolder = new File(tempDir, "veraPDFMediaConchPluginTemp");
if (!tempFolder.exists()) {
tempFolder.mkdir();
}
return tempFolder;
}

private File getOutFileInFolder(File folder) throws IOException {
File out = File.createTempFile("veraPDF_MediaConch_Plugin_out", ".xml", folder);
return out;
}

private MediaConchConfig getConfig(List<FeatureTreeNode> nodes) throws FeatureParsingException {
MediaConchConfig config = MediaConchConfig.defaultInstance();
File conf = getConfigFile();
if (conf.isFile() && conf.canRead()) {
try {
config = MediaConchConfig.fromXml(new FileInputStream(conf));
} catch (JAXBException | FileNotFoundException e) {
FeatureTreeNode node = FeatureTreeNode.createRootNode("error");
node.setValue("Config file contains wrong syntax. Error message: " + e.getMessage());
nodes.add(node);
}
}
return config;
}

private File getConfigFile() {
return new File(getFolderPath().toFile(), "config.xml");
}

private boolean isValidType(String type) {
return type.toLowerCase().startsWith("video/");
}

@Override
public String getID() {
return "8725b233-1597-490e-9b45-b989303d2c5b";
}

@Override
public String getDescription() {
//TODO: check this description
return "Generates mediaconch report of the given embedded video file";
}
}
34 changes: 34 additions & 0 deletions metsMetadata-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>verapdf-library-samples</artifactId>
<groupId>org.verapdf</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>metsMetadata-plugin</artifactId>

<dependencies>
<dependency>
<groupId>au.edu.apsr.mtk</groupId>
<artifactId>mets</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/mets-api.jar</systemPath>
</dependency>
<dependency>
<groupId>org.verapdf</groupId>
<artifactId>feature-report</artifactId>
<version>[0.11.0,0.12.0)</version>
</dependency>
<dependency>
<groupId>org.verapdf</groupId>
<artifactId>verapdf-xmp-core</artifactId>
<version>[0.11.0,0.12.0)</version>
</dependency>
</dependencies>

</project>
Loading

0 comments on commit 2da039e

Please sign in to comment.