From 9ec29de11703ed32a32b532ea8145e0a34fc8152 Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <andreas.romeyke@slub-dresden.de>
Date: Tue, 8 Aug 2023 16:23:12 +0200
Subject: [PATCH] - refactoring, extracted setValid() - refactoring, extracted
 extractErrorsFromReader() - simplified handling external command calls

---
 ...LUBMatroskaFFV1FormatValidationPlugin.java | 74 +++++++++----------
 1 file changed, 37 insertions(+), 37 deletions(-)

diff --git a/java/org/slub/rosetta/dps/repository/plugin/SLUBMatroskaFFV1FormatValidationPlugin.java b/java/org/slub/rosetta/dps/repository/plugin/SLUBMatroskaFFV1FormatValidationPlugin.java
index fa41a1a..6c60f03 100644
--- a/java/org/slub/rosetta/dps/repository/plugin/SLUBMatroskaFFV1FormatValidationPlugin.java
+++ b/java/org/slub/rosetta/dps/repository/plugin/SLUBMatroskaFFV1FormatValidationPlugin.java
@@ -49,7 +49,6 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
     private boolean iswellformed = false;
     private String md5CurrentProfile;
     private String md5UpcomingProfile;
-    //static final ExLogger log = ExLogger.getExLogger(SLUBTechnicalMetadataExtractorMediaConchPlugin.class, ExLogger.VALIDATIONSTACK);
     private boolean isDifferentProfile = true;
     
     private boolean dumpValidationResultToStdOut = false;
@@ -79,8 +78,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
                     + " mediaconch_upcoming_profile_path=" + mediaconch_upcoming_profile_path
                     + (isDifferentProfile ? "with different profiles" : "with same profile")
             );
-        } catch (Exception e) {
-            e.printStackTrace();
+        } catch (Exception ignored) {
         }
         log.info( "SLUBMatroskaFFV1FormatValidationPlugin instantiated with"
                 + " mediaconch binary='" + mediaconch_binary_path
@@ -110,9 +108,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
     private void callMediaconch(String filePath, String profilePath) {
         String execstring = this.mediaconch_binary_path + " " + filePath + " -p " + profilePath;
         log.info("executing: " + execstring);
-
        	final AtomicBoolean fuse = new AtomicBoolean(true);        	
-                
         try {       	
             Process p = Runtime.getRuntime().exec(execstring);
             BufferedReader procOutputReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
@@ -175,12 +171,12 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
                 validationLog.add(line);
                 log.info( line );
             }
-            
             procOutputReader.close();
         } catch (IOException e) {
             log.error("(actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " , e.getMessage());
             System.out.println("ERROR: (actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " + e.getMessage());
         } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
             log.error("ERROR: call of mediaconch interrupted, path=" + this.mediaconch_binary_path + ", " + e.getMessage());
         } finally {
 			// ensure the while loop of procOutputReaderThread terminates under all circumstances
@@ -188,14 +184,29 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
 		}
     }
 
+    private void setValid(boolean value) {
+        isvalid = value;
+        iswellformed = value;
+    }
+
+    private void extractErrorsFromReader(BufferedReader reader) throws IOException {
+        while (true) {
+            String line = reader.readLine();
+            if (line == null) break;
+            System.out.println("MEDIACONCH line: " + line);
+            validationLog.add(line);
+            log.info(line);
+        }
+    }
+
     @Override
     public boolean validateFormat(String filePath) {
+        setValid(false);
         // mediaconch validation, first using upcoming profile, if invalid then retry with current profile
         callMediaconch(filePath, this.mediaconch_upcoming_profile_path);
         if (this.isDifferentProfile && !isvalid) {
             callMediaconch(filePath, this.mediaconch_current_profile_path);
         }
-
         return isvalid;
     }
 
@@ -203,40 +214,27 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
      *
      * @return string with version and signature version
      */
+    @Override
     public final String getAgent() {
         StringBuilder response = new StringBuilder();
         response.append("mediaconch:\n");
-        InputStreamReader process_out = null;
         try {
             String execstring = this.mediaconch_binary_path + " --Version";
             checkFileExists(this.mediaconch_binary_path);
             Process p = Runtime.getRuntime().exec(execstring);
             p.waitFor();
-            process_out = new InputStreamReader(p.getInputStream());
-        } catch (InterruptedException e) {
-            Thread.currentThread().interrupt();
-            e.printStackTrace();
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        if (process_out != null) {
+            InputStreamReader process_out = new InputStreamReader(p.getInputStream());
             BufferedReader reader = new BufferedReader(process_out);
             String line;
-            try {
+            line = reader.readLine();
+            while (line != null) {
+                System.out.println(line);
+                response.append(line);
                 line = reader.readLine();
-                while (line != null) {
-                    System.out.println(line);
-                    response.append(line);
-                    try {
-                        line = reader.readLine();
-                    } catch (IOException e) {
-                        e.printStackTrace();
-                    }
-                }
-                reader.close();
-            } catch (IOException e) {
-                e.printStackTrace();
             }
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+        } catch (Exception ignored) {
         }
         return response.toString().trim();
     }
@@ -248,10 +246,10 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
 
     @Override
     public final List<String> getErrors() {
-        List<String> truncated = new ArrayList<String>();
+        List<String> truncated = new ArrayList<>();
         int chars=0;
         /* workaround for Rosetta Issue https://support.proquest.com/500Do0000083LJkIAM
-         * truncate to ensure not more than 2000 chars used, to
+         * truncate to ensure not more than 2000 chars used
          */
         for (String e: validationLog) {
             if ( (chars+ e.length()) < 1900 ) {
@@ -286,8 +284,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
             byte[] b = Files.readAllBytes(Paths.get(filename));
             byte[] digest = md.digest(b);
             return new BigInteger(1, digest).toString(16);
-        } catch (Exception e) {
-            e.printStackTrace();
+        } catch (Exception ignored) {
         }
         return "";
     }
@@ -295,8 +292,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
     private String modificationDateOfFile(String filename ) {
         try {
             return Files.getLastModifiedTime(Paths.get(filename)).toString();
-        } catch (IOException e) {
-            e.printStackTrace();
+        } catch (IOException ignored) {
         }
         return "";
     }
@@ -324,11 +320,15 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
         initp.put( "mediaconch_upcoming_profile_path", "/etc/mediaconch/profile.xml");
         plugin.initParams( initp );
         plugin.setDumpValidationResultToStdOut(true);
-        plugin.validateFormat(args[0]);
         System.out.println("----------------------------------");
         System.out.println("Agent: '" + plugin.getAgent() + "'");
         System.out.println();
         System.out.println("Validation RESULT: " + plugin.isValid());
+        for (String file : args) {
+            plugin.validateFormat( file );
+            System.out.println("Validation RESULT: " + plugin.isValid());
+            System.out.println("Errors:" + plugin.validationLog);
+        }
         System.out.println("----------------------------------");
         System.out.println("getAgent:");
         System.out.println( plugin.getAgent());
@@ -337,4 +337,4 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
         System.out.println( plugin.getProfile());
         System.out.println("----------------------------------");
     }
-}
\ No newline at end of file
+}
-- 
GitLab