Skip to content
Snippets Groups Projects
Commit db4726d8 authored by Andreas Romeyke's avatar Andreas Romeyke
Browse files

- refactoring, extracted methods

parent 151639a1
No related branches found
No related tags found
No related merge requests found
......@@ -35,6 +35,7 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -53,11 +54,11 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
private String mediainfo_binary_path;
private final static String MEDIAINFO_XSD = "/transformer.xsl";
private List<String> extractionErrors = new ArrayList<String>();
private List<String> validationLog = new ArrayList<String>();
private List<String> extractionErrors = new ArrayList<>();
private List<String> validationLog = new ArrayList<>();
private boolean isvalid = false;
private boolean iswellformed = false;
private Map<String,String> attributes = new HashMap<String, String>();
private Map<String,String> attributes = new HashMap<>();
//static final ExLogger log = ExLogger.getExLogger(SLUBTechnicalMetadataExtractorMediaConchPlugin.class, ExLogger.VALIDATIONSTACK);
/** constructor */
......@@ -83,59 +84,34 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
@Override
public void extract(String filePath) throws Exception {
if (StringUtils.isEmptyString(mediaconch_binary_path)) {
//log.error("No mediaconch_binary_path defined. Please set the plugin parameter to hold your mediaconch_binary_path.");
throw new Exception("mediaconch_binary_path not found");
}
if (StringUtils.isEmptyString(mediaconch_profile_path)) {
//log.error("No mediaconch_config_path defined. Please set the plugin parameter to hold your mediaconch_config_path.");
throw new Exception("mediaconch_profile_path not found");
}
if (StringUtils.isEmptyString(mediainfo_binary_path)) {
throw new Exception("mediainfo_binary_path not found");
}
// mediaconch validation
try {
String execstring = this.mediaconch_binary_path + " " + filePath + " " + this.mediaconch_profile_path;
System.out.println("executing: " + execstring);
Process p = Runtime.getRuntime().exec(execstring);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
validationLog.add(line);
line = reader.readLine();
}
if (p.exitValue() == 0) {
isvalid = true;
iswellformed=true;
} else { // something wrong
isvalid = false;
iswellformed = false;
extractionErrors = validationLog;
}
} catch (IOException e) {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
System.out.println("ERROR: (actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " + e.getMessage());
throw new Exception("ERROR: (actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " + e.getMessage());
call_mediaconch(filePath);
// mediainfo metadata extraction
call_mediainfo_and_result_processing(filePath);
}
/* ffprobe output of metadata
supports different outputs. we are using the flat-model, see WRITERS section in ffprobe manual
the streams will be mapped as: streams.stream.0.$property
the separator is "="
*/
try {
private void call_mediainfo_and_result_processing(String filePath) throws Exception {
String execstring = this.mediainfo_binary_path + " -f --Output=XML " + filePath;
System.out.println("executing: " + execstring);
InputStreamReader process_out;
try {
Process p = Runtime.getRuntime().exec(execstring);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
process_out = new InputStreamReader(p.getInputStream());
} catch (IOException e) {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
System.out.println("ERROR: (actual) mediainfo not available, path=" + this.mediainfo_binary_path + ", " + e.getMessage());
throw new Exception("ERROR: (actual) mediaconch not available, path=" + this.mediainfo_binary_path + ", " + e.getMessage());
}
BufferedReader reader = new BufferedReader(process_out);
String line = reader.readLine();
StringBuilder mediainfo_output = new StringBuilder();
while (line != null) {
......@@ -148,8 +124,13 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
//mediainfo_output.append(line);
line = reader.readLine();
}
File temp_media_output_file = File.createTempFile("mediainfo_", ".xml");
OutputStream temp_media_outputstream = new FileOutputStream( temp_media_output_file);
File temp_media_outputfile = File.createTempFile("mediainfo_outp", ".xml");
temp_media_outputfile.deleteOnExit();
File temp_media_transformed_outputfile = File.createTempFile("mediainfo_transf_", ".xml");
temp_media_transformed_outputfile.deleteOnExit();
OutputStream temp_media_transformed_outputstream = new FileOutputStream(temp_media_transformed_outputfile);
OutputStream temp_media_outputstream = new FileOutputStream(temp_media_outputfile);
OutputStreamWriter temp_media_streamwriter = new OutputStreamWriter(temp_media_outputstream);
temp_media_streamwriter.append(mediainfo_output);
temp_media_streamwriter.close();
......@@ -160,38 +141,59 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
);
System.out.println("Resource=" + tmp.getPath());
StreamSource stylesource = new StreamSource(stylestream);
//stylesource.setPublicId("https://mediaarea.net/mediainfo");
//stylesource.setSystemId("https://mediaarea.net/mediainfo");
stylesource.setSystemId("./resources" + MEDIAINFO_XSD);
System.out.println("stylesource, systemID=" + stylesource.getSystemId());
System.out.println("stylesource, publicID=" + stylesource.getPublicId());
// Use a Transformer for output
InputStream mediastream = new FileInputStream( temp_media_output_file );
/* media info xml */
InputStream mediastream = new FileInputStream(temp_media_outputfile);
StreamSource mediainfo_source = new StreamSource(mediastream);
//stylesource.setPublicId("https://mediaarea.net/mediainfo");
mediainfo_source.setSystemId( temp_media_output_file);
mediainfo_source.setSystemId(temp_media_outputfile);
System.out.println("mediainfo_source, systemID=" + mediainfo_source.getSystemId());
System.out.println("mediainfo_source, publicID=" + mediainfo_source.getPublicId());
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
//tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
tFactory.setFeature(XMLConstants.USE_CATALOG, false);
Transformer transformer = tFactory.newTransformer(stylesource);
/* ok, mediainfo is loaded correctly, and xslt loaded too */
OutputStream debugFile = new FileOutputStream("DEBUG.xml");
StreamResult result = new StreamResult( debugFile);
// StreamResult result = new StreamResult(System.out); /* FIXME , use StringOutputStream */
/* debug output: */
StreamResult result = new StreamResult(temp_media_transformed_outputstream);
transformer.transform(mediainfo_source, result);
temp_media_output_file.deleteOnExit();
/* TODO: read debugFile and return attributes */
attributes.put("key", "value");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
/* */
temp_media_transformed_outputstream.close();
mediastream.close();
}
private void call_mediaconch(String filePath) throws Exception {
String execstring = this.mediaconch_binary_path + " " + filePath + " " + this.mediaconch_profile_path;
System.out.println("executing: " + execstring);
InputStreamReader process_out;
try {
Process p = Runtime.getRuntime().exec(execstring);
p.waitFor();
process_out = new InputStreamReader(p.getInputStream());
if (p.exitValue() == 0) {
isvalid = true;
iswellformed = true;
} else { // something wrong
isvalid = false;
iswellformed = false;
}
} catch (IOException e) {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
System.out.println("ERROR: (actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " + e.getMessage());
throw new Exception("ERROR: (actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " + e.getMessage());
}
BufferedReader reader = new BufferedReader(process_out);
String line = reader.readLine();
while (line != null) {
System.out.println(line);
validationLog.add(line);
line = reader.readLine();
}
extractionErrors = validationLog;
}
public String getAgentName()
......@@ -206,29 +208,42 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
public String getAgent() {
StringBuilder response = new StringBuilder();
response.append("mediaconch:\n");
try {
String[] executables = {
this.mediaconch_binary_path,
this.mediainfo_binary_path
};
for (String executable : executables) {
String execstring = executable + " --Version";
InputStreamReader process_out = null;
try {
Process p = Runtime.getRuntime().exec(execstring);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
process_out = new InputStreamReader(p.getInputStream());
} catch (IOException e) {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
if (process_out != null) {
BufferedReader reader = new BufferedReader(process_out);
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while (line != null) {
System.out.println(line);
response.append(line);
try {
line = reader.readLine();
}
}
} catch (IOException e) {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
}
}
return response.toString().trim();
}
......@@ -243,7 +258,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
@Override
public List<String> getExtractionErrors() {
List<String> extractionErrors = this.extractionErrors;
return extractionErrors;
return Collections.unmodifiableList(extractionErrors);
}
/* following list is build using:
......@@ -254,7 +269,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
@Override
public List<String> getSupportedAttributeNames() {
//return new ArrayList<String>(attributes.keySet());
List<String> available = new ArrayList<String>();
List<String> available = new ArrayList<>();
available.add("mediainfo.track.Audio.BitDepth");
available.add("mediainfo.track.Audio.BitRate");
available.add("mediainfo.track.Audio.BitRate_Mode");
......@@ -398,7 +413,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
*/
public static void main(String[] args) {
SLUBTechnicalMetadataExtractorMediaConchPlugin plugin = new SLUBTechnicalMetadataExtractorMediaConchPlugin();
Map<String, String> initp = new HashMap<String, String>();
Map<String, String> initp = new HashMap<>();
initp.put( "mediaconch_binary_path", "/usr/bin/mediaconch");
initp.put( "mediaconch_profile_path", "/etc/mediaconch/profile.xml");
initp.put( "mediainfo_binary_path", "/usr/bin/mediainfo");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment