From da3bb01e9302628e8a17036efd988e30fa9044b1 Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <andreas.romeyke@slub-dresden.de>
Date: Thu, 23 Feb 2023 10:37:50 +0100
Subject: [PATCH] - encapsulating debug output - refactoring, moved
 printXMLinfo() to XmlInfoRecord

---
 .../plugin/SLUBXmlFormatValidationPlugin.java | 120 +++++++++---------
 .../dps/repository/plugin/XmlInfoRecord.java  |  15 ++-
 2 files changed, 76 insertions(+), 59 deletions(-)

diff --git a/java/org/slub/rosetta/dps/repository/plugin/SLUBXmlFormatValidationPlugin.java b/java/org/slub/rosetta/dps/repository/plugin/SLUBXmlFormatValidationPlugin.java
index c07f028..2bbcd96 100644
--- a/java/org/slub/rosetta/dps/repository/plugin/SLUBXmlFormatValidationPlugin.java
+++ b/java/org/slub/rosetta/dps/repository/plugin/SLUBXmlFormatValidationPlugin.java
@@ -49,7 +49,7 @@ import java.util.Optional;
  */
 public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
     private static final ExLogger log = ExLogger.getExLogger(SLUBXmlFormatValidationPlugin.class);
-
+    private boolean debug = false;
     private boolean valid = false;
     private boolean wellformed = false;
     private static String namespaceSchemaMapFile = "";
@@ -104,7 +104,9 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
     private final ErrorHandler validationErrorHandler = new ErrorHandler() {
         @Override
         public void warning(SAXParseException e) {
-
+            if (debug) {
+                errors.add(e.getMessage());
+            }
         }
 
         @Override
@@ -128,16 +130,18 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
      * @param initp parameter map
      */
     public void initParams(Map<String, String> initp) {
-        System.out.println( initp);
+        if (debug) {
+            System.out.println(initp);
+        }
         String[] catalogs = new String[] {
             initp.get("catalog").trim()
         };
-        validationCatalogResolver = new ValidationCatalogResolver(catalogs, errors);
+        validationCatalogResolver = new ValidationCatalogResolver(catalogs, errors, debug);
         namespaceSchemaMapFile = initp.get("schemacatalog").trim();
         loadNamespaceSchemaMap();
     }
 
-    private static xmlInfoRecord getXMLinfo(Document doc) {
+    private XmlInfoRecord getXMLinfo(Document doc) {
         String namespaceURI = doc.getNamespaceURI();
         String documentURI = doc.getDocumentURI();
         String xmlVersion = doc.getXmlVersion();
@@ -148,44 +152,36 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
             publicId = docType.getPublicId();
             systemId = docType.getSystemId();
         }
-        if (null == namespaceURI) { /* ugly hackm but works */
+        if (null == namespaceURI) { /* ugly hack but works */
             namespaceURI = doc.getDocumentElement().getNamespaceURI();
         }
-        return new xmlInfoRecord(namespaceURI, documentURI, xmlVersion, systemId, publicId);
-    }
-    private void printXMLinfo(Document doc) {
-        xmlInfoRecord info = getXMLinfo(doc);
-        System.out.println(
-                "\n-------------------------------------------"
-                        + "\n namespaceuri= "  + info.nameSpaceUri
-                        + "\n documenturi = "  + info.documentUri
-                        + "\n xmlVersion  = "  + info.xmlVersion
-                        + "\n systemID    = "  + info.systemID
-                        + "\n publicID    = "  + info.publicID
-                        + "\n"
-        );
+        return new XmlInfoRecord(namespaceURI, documentURI, xmlVersion, systemId, publicId);
     }
 
     private Optional<ValidationSchema> assignSchema(Document doc) {
-        xmlInfoRecord info = getXMLinfo(doc);
+        XmlInfoRecord info = getXMLinfo(doc);
         Optional<ValidationSchema> optEle = Optional.empty();
         if (null == info.nameSpaceUri) {
             /* try if a DTD is assignable */
             var type = assignDtdIfApplicable(doc);
             if (type.equals(ValidationSchemaType.dtd)) {
-                System.out.println("found schema " + type);
+                if (debug) {
+                    System.out.println("found schema " + type);
+                }
                 var ele = new ValidationSchema(null, type, info.systemID);
                 optEle = Optional.of(ele);
             }
         } else {
-            System.out.println("check  info.nameSpaceUri=" +  info.nameSpaceUri);
-            var it = namespaceSchemaMap.iterator();
-            System.out.println("map size=" + namespaceSchemaMap.size());
-            while (it.hasNext()){
-                ValidationSchema v = it.next();
-                System.out.println(v.schemaURL);
-                System.out.println(v.nameSpace);
-                System.out.println(v.schemaType);
+            if (debug) {
+                System.out.println("check  info.nameSpaceUri=" + info.nameSpaceUri);
+                var it = namespaceSchemaMap.iterator();
+                System.out.println("map size=" + namespaceSchemaMap.size());
+                while (it.hasNext()) {
+                    ValidationSchema v = it.next();
+                    System.out.println(v.schemaURL);
+                    System.out.println(v.nameSpace);
+                    System.out.println(v.schemaType);
+                }
             }
             optEle = namespaceSchemaMap.stream()
                     .filter(
@@ -193,12 +189,14 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
                     )
                     .findAny();
         }
-        if ( optEle.isPresent() ) {
-            System.out.println("found namespace  " + optEle.get().nameSpace );
-            System.out.println("found schematype " + optEle.get().schemaType );
-            System.out.println("found schemaURL  " + optEle.get().schemaURL );
-        } else {
-            System.out.println("no element found");
+        if (debug) {
+            if (optEle.isPresent()) {
+                System.out.println("found namespace  " + optEle.get().nameSpace);
+                System.out.println("found schematype " + optEle.get().schemaType);
+                System.out.println("found schemaURL  " + optEle.get().schemaURL);
+            } else {
+                System.out.println("no element found");
+            }
         }
         return optEle;
     }
@@ -224,9 +222,13 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
                         assert(dbf.isValidating());
                         dbf.setFeature(XMLConstants.USE_CATALOG, true);
                         dbf.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
-                        System.out.println("-> dtd detected, use catalog");
+                        if (debug) {
+                            System.out.println("-> dtd detected, use catalog");
+                        }
                     } else if (!schema.get().schemaURL.isBlank()) {
-                        System.out.println("-> set schema to " + schema.get().schemaURL);
+                        if (debug) {
+                            System.out.println("-> set schema to " + schema.get().schemaURL);
+                        }
                         dbf.setSchema(schema.get().schemaInst);
                         assert(dbf.getSchema() != null);
                     }
@@ -241,40 +243,42 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
             reportError("not a XML file, "  + e.getMessage(), filePath);
             e.getStackTrace();
         }
-        // debug
-        System.out.println("errors:" );
-        System.out.println("----");
-        for (var line: errors) {
-            System.out.println("\t" + line);
+        if (debug) {
+            // debug
+            System.out.println("errors:");
+            System.out.println("----");
+            for (var line : errors) {
+                System.out.println("\t" + line);
+            }
+            System.out.println("----");
         }
-        System.out.println("----");
         return valid;
     }
-    private static ValidationSchemaType assignDtdIfApplicable(Document doc) {
+    private ValidationSchemaType assignDtdIfApplicable(Document doc) {
+        ValidationSchemaType result = ValidationSchemaType.nothing;
         var info = getXMLinfo(doc);
         if (null != info.systemID && info.systemID.endsWith(".dtd")) {
-            return ValidationSchemaType.dtd;
-        } else if (null != info.systemID ) {
-
+            result = ValidationSchemaType.dtd;
+        } else if (null != info.systemID) {
+            if (debug) {
+                System.out.println("no dtd applicable");
+            }
         }
-        return ValidationSchemaType.nothing;
+        return result;
     }
 
-
     private boolean checkIfWellformed(Document doc) throws ParserConfigurationException, IOException, SAXException {
         /* detect XML type via NS */
         boolean isWellformedXml = false;
-        xmlInfoRecord info = getXMLinfo(doc);
+        XmlInfoRecord info = getXMLinfo(doc);
         reportDetail("detect XML type via NS:" + info.nameSpaceUri);
-        /* TODO: align corresponding Schema based on systemID */
-
-        printXMLinfo(doc);
+        /* align corresponding Schema based on systemID */
+        info.printXMLinfo();
         if (!info.xmlVersion.equals("1.0")) {
             reportError("not an expected XML 1.0 document, found " + info.xmlVersion);
         } else {
             isWellformedXml = true;
             reportDetail("checked XML is wellformed");
-
         }
         return isWellformedXml;
     }
@@ -285,8 +289,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
         //dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
         dbf.setValidating(false);
         DocumentBuilder db = dbf.newDocumentBuilder();
-        Document doc = db.parse(new File(filePath));
-        return doc;
+        return db.parse(new File(filePath));
     }
 
     private boolean validateAgainstSchema(String filePath) throws ParserConfigurationException, SAXException, IOException {
@@ -310,7 +313,9 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
 
     private void reportError(String msg) {
         errors.add(msg);
-        System.out.println("ERROR: " + msg);
+        if (debug) {
+            System.out.println("ERROR: " + msg);
+        }
         // TODO: log.warn(msg);
     }
     private void reportError(String msg, String filepath) {
@@ -355,6 +360,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
         Map<String, String> initp = new HashMap<>();
         initp.put("catalog", "/etc/xml/catalog");
         initp.put("schemacatalog", "example_catalog/schema_catalog.xml");
+        //initp.put("debug", "true");
         plugin.initParams(initp);
         System.out.println("----------------------------------");
         System.out.println("Agent: '" + plugin.getAgent() + "'");
diff --git a/java/org/slub/rosetta/dps/repository/plugin/XmlInfoRecord.java b/java/org/slub/rosetta/dps/repository/plugin/XmlInfoRecord.java
index 0ee478b..3db544e 100644
--- a/java/org/slub/rosetta/dps/repository/plugin/XmlInfoRecord.java
+++ b/java/org/slub/rosetta/dps/repository/plugin/XmlInfoRecord.java
@@ -1,17 +1,28 @@
 package org.slub.rosetta.dps.repository.plugin;
 
-class xmlInfoRecord {
+class XmlInfoRecord {
     public final String nameSpaceUri;
     public final String documentUri;
     public final String xmlVersion;
     public final String systemID;
     public final String publicID;
 
-    public xmlInfoRecord(String nameSpaceUri, String documentUri, String xmlVersion, String systemID, String publicID) {
+    public XmlInfoRecord(String nameSpaceUri, String documentUri, String xmlVersion, String systemID, String publicID) {
         this.documentUri = documentUri;
         this.nameSpaceUri = nameSpaceUri;
         this.xmlVersion = xmlVersion;
         this.systemID = systemID;
         this.publicID = publicID;
     }
+    public void printXMLinfo() {
+        System.out.println(
+                "\n-------------------------------------------"
+                        + "\n namespaceuri= " + this.nameSpaceUri
+                        + "\n documenturi = " + this.documentUri
+                        + "\n xmlVersion  = " + this.xmlVersion
+                        + "\n systemID    = " + this.systemID
+                        + "\n publicID    = " + this.publicID
+                        + "\n"
+        );
+    }
 }
-- 
GitLab