diff --git a/java/org/slub/rosetta/dps/repository/plugin/SLUBTechnicalMetadataExtractorMediaConchPlugin.java b/java/org/slub/rosetta/dps/repository/plugin/SLUBTechnicalMetadataExtractorMediaConchPlugin.java index 3c96db0f8cb9905d0022cbca0b32e8d9a556ee01..2d5c590fb2fe67dc60e565ea289983a624c5cce4 100644 --- a/java/org/slub/rosetta/dps/repository/plugin/SLUBTechnicalMetadataExtractorMediaConchPlugin.java +++ b/java/org/slub/rosetta/dps/repository/plugin/SLUBTechnicalMetadataExtractorMediaConchPlugin.java @@ -22,20 +22,19 @@ import com.exlibris.dps.sdk.techmd.MDExtractorPlugin; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; -import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import java.io.BufferedReader; import java.io.File; @@ -87,12 +86,18 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract this.mediaconch_binary_path = initp.get("mediaconch_binary_path").trim(); this.mediaconch_profile_path = initp.get("mediaconch_profile_path").trim(); this.mediainfo_binary_path = initp.get("mediainfo_binary_path").trim(); - - System.out.println("SLUBTechnicalMetadataExtractorMediaConchPlugin instantiated with " - + " mediaconch_binary_path=" + mediaconch_binary_path - + " mediaconch_profile_path=" + mediaconch_profile_path - + " mediainfo_binary_path=" + mediainfo_binary_path - ); + try { + checkFileExists(this.mediainfo_binary_path); + checkFileExists(this.mediaconch_binary_path); + checkFileExists(this.mediaconch_profile_path); + System.out.println("SLUBTechnicalMetadataExtractorMediaConchPlugin instantiated with " + + " mediaconch_binary_path=" + mediaconch_binary_path + + " mediaconch_profile_path=" + mediaconch_profile_path + + " mediainfo_binary_path=" + mediainfo_binary_path + ); + } catch (Exception e) { + e.printStackTrace(); + } } @Override @@ -109,15 +114,18 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract @Override public void extract(String filePath) throws Exception { - if (StringUtils.isEmptyString(mediaconch_binary_path)) { + if (StringUtils.isEmptyString(this.mediaconch_binary_path)) { throw new Exception("mediaconch_binary_path not found"); } - if (StringUtils.isEmptyString(mediaconch_profile_path)) { + if (StringUtils.isEmptyString(this.mediaconch_profile_path)) { throw new Exception("mediaconch_profile_path not found"); } - if (StringUtils.isEmptyString(mediainfo_binary_path)) { + if (StringUtils.isEmptyString(this.mediainfo_binary_path)) { throw new Exception("mediainfo_binary_path not found"); } + checkFileExists(this.mediaconch_binary_path); + checkFileExists(this.mediaconch_profile_path); + checkFileExists(this.mediainfo_binary_path); // mediaconch validation call_mediaconch(filePath); // mediainfo metadata extraction @@ -127,6 +135,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract 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); + checkFileExists(this.mediainfo_binary_path); InputStreamReader process_out; try { Process p = Runtime.getRuntime().exec(execstring); @@ -135,7 +144,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract } 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()); + throw new Exception("ERROR: (actual) mediainfo not available, path=" + this.mediainfo_binary_path + ", " + e.getMessage()); } BufferedReader reader = new BufferedReader(process_out); String line = reader.readLine(); @@ -162,12 +171,10 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract temp_media_streamwriter.close(); /* xslt transform */ InputStream stylestream = getClass().getResourceAsStream(MEDIAINFO_XSD); - File tmp = new File( - String.valueOf(getClass().getResource(MEDIAINFO_XSD)) - ); - System.out.println("Resource=" + tmp.getPath()); StreamSource stylesource = new StreamSource(stylestream); stylesource.setSystemId("./resources" + MEDIAINFO_XSD); + System.out.println("stylesource, tempfile_raw=" + temp_media_outputfile.getAbsolutePath()); + System.out.println("stylesource, tempfile_transformed=" + temp_media_transformed_outputfile.getAbsolutePath()); System.out.println("stylesource, systemID=" + stylesource.getSystemId()); System.out.println("stylesource, publicID=" + stylesource.getPublicId()); /* media info xml */ @@ -177,21 +184,40 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract 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.setFeature(XMLConstants.USE_CATALOG, false); - Transformer transformer = tFactory.newTransformer(stylesource); - /* ok, mediainfo is loaded correctly, and xslt loaded too */ - /* debug output: */ - StreamResult result = new StreamResult(temp_media_transformed_outputstream); - transformer.transform(mediainfo_source, result); + TransformerFactory tFactory; + try { + tFactory = TransformerFactory.newInstance(); + System.out.println("Factory module name:" + tFactory.getClass().getModule().getName()); + tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); + tFactory.setFeature(XMLConstants.USE_CATALOG, false); + try { + System.out.println("stylesource=" + stylesource); + assert (!stylesource.isEmpty()); + Transformer transformer = tFactory.newTransformer(stylesource); + assert (transformer != null); + System.out.println("transformer=" + transformer); + /* ok, mediainfo is loaded correctly, and xslt loaded too */ + /* debug output: */ + StreamResult result = new StreamResult(temp_media_transformed_outputstream); + System.out.println("result=" + result.getClass()); + transformer.transform(mediainfo_source, result); + } catch (TransformerConfigurationException e) { + System.err.println("TransformerConfigurationException" + e); + e.printStackTrace(); + } + }catch (TransformerFactoryConfigurationError e) { + System.err.println("TransformerConfigurationError" + e); + e.printStackTrace(); + } + temp_media_transformed_outputstream.close(); mediastream.close(); /* TODO: read transformed outputfile and return attributes */ extract_attributes_of_transformed_result(temp_media_transformed_outputfile); } - private void extract_attributes_of_transformed_result(File temp_media_transformed_outputfile) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException { + private void extract_attributes_of_transformed_result(File temp_media_transformed_outputfile) throws Exception { + checkFileExists(String.valueOf(temp_media_transformed_outputfile)); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile("/mdExtractor/attributes/key"); @@ -251,6 +277,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract public String getAgent() { StringBuilder response = new StringBuilder(); response.append("mediaconch:\n"); + String[] executables = { this.mediaconch_binary_path, this.mediainfo_binary_path @@ -259,6 +286,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract String execstring = executable + " --Version"; InputStreamReader process_out = null; try { + checkFileExists( executable); Process p = Runtime.getRuntime().exec(execstring); p.waitFor(); process_out = new InputStreamReader(p.getInputStream()); @@ -267,10 +295,12 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract } catch (InterruptedException e) { Thread.currentThread().interrupt(); e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); } if (process_out != null) { BufferedReader reader = new BufferedReader(process_out); - String line = null; + String line; try { line = reader.readLine(); while (line != null) { @@ -296,7 +326,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract if (attributes.containsKey(attribute)) { return attributes.get(attribute); } - return "not found"; + return "not found"; } @Override @@ -448,6 +478,13 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract return "video/x-matroska"; } + private void checkFileExists (String filename) throws Exception { + File f = new File(filename); + if (! f.exists() ) { + System.out.println("ERROR: path=" + filename + "not available"); + throw new Exception("ERROR: path=" + filename + "not available"); + } + } /** stand alone check, main file to call local installed clamd * @param args list of files which should be scanned */ @@ -463,8 +500,10 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract System.out.println(); for (String file : args) { try { + System.out.println("extracting from " + file); plugin.extract(file); } catch (Exception e) { + System.out.println("CALLERROR:"); e.printStackTrace(); } System.out.println("RESULT: " + plugin.isValid());