From e1e08ee0081a9973cce90e46b79d790460b5b0cb Mon Sep 17 00:00:00 2001
From: Riegel <alexander.riegel@hft-stuttgart.de>
Date: Tue, 10 Dec 2024 11:16:33 +0100
Subject: [PATCH] Feat: Implement parsing of LibraryObjects in zip-files

---
 .../datastructure/LibraryObject.java          | 45 ++++++++++++-------
 1 file changed, 28 insertions(+), 17 deletions(-)

diff --git a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/datastructure/LibraryObject.java b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/datastructure/LibraryObject.java
index 2459a39..7ef980d 100644
--- a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/datastructure/LibraryObject.java
+++ b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/datastructure/LibraryObject.java
@@ -74,14 +74,7 @@ public class LibraryObject extends Geometry {
         if (path.toFile().exists()) {
             try {
                 CityGmlParser.gagLogger(true);
-                CityDoctorModel model = CityGmlParser.parseCityGmlFile(path.toString(), config);
-                List<CityObject> objects = model.createFeatureStream().toList();
-                if (objects.isEmpty()) {
-                    throw new InvalidGmlFileException("Referenced library-object's gml file does not contain a CityGML object!");
-                } else if (objects.size() > 1) {
-                    throw new InvalidGmlFileException("Referenced library-object's gml file contains more than one CityGML object!");
-                }
-                geo = objects.get(0).getHighestLodGeometry();
+                geo = getProtoGeometry(CityGmlParser.parseCityGmlFile(path.toString(), config));
             } catch (CityGmlParseException e) {
                 logger.error(String.format(
                         "Encountered an error while parsing library object %s", path));
@@ -89,7 +82,6 @@ public class LibraryObject extends Geometry {
             } catch (InvalidGmlFileException e) {
                 logger.error(e.getStackTrace());
             } finally {
-                // Failsafe to remove gag should parsing fail
                 CityGmlParser.gagLogger(false);
             }
         } else {
@@ -99,15 +91,34 @@ public class LibraryObject extends Geometry {
     }
 
     private static Geometry parseZipEntry(CityGmlZipEntry entry, ParserConfiguration config) {
-        Geometry geom = null;
-        try (CityGmlZipInputStream cgis = new CityGmlZipInputStream(entry)) {
-            InputStream is = cgis.getInputStream();
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        } catch (Exception e) {
-            throw new RuntimeException(e);
+        Geometry geo = null;
+        try {
+            entry.loadEntry(config);
+            geo = getProtoGeometry(entry.getModel());
+        } catch (InvalidGmlFileException e) {
+            logger.error(e.getStackTrace());
+        } catch (CityGmlParseException e) {
+            logger.error(String.format(
+                    "Encountered an error while parsing library object %s", entry.getFileName()));
+            logger.error(e.getStackTrace());
+        } finally {
+            //Gag logger again for next entry
+            CityGmlParser.gagLogger(true);
+        }
+        return geo;
+    }
+
+    private static Geometry getProtoGeometry(CityDoctorModel model) throws InvalidGmlFileException, CityGmlParseException {
+        if (model == null) {
+            throw new CityGmlParseException("CityDoctorModel of referenced LibraryObject is null");
+        }
+        List<CityObject> objects = model.createFeatureStream().toList();
+        if (objects.isEmpty()) {
+            throw new InvalidGmlFileException("Referenced library-object's gml file does not contain a CityGML object!");
+        } else if (objects.size() > 1) {
+            throw new InvalidGmlFileException("Referenced library-object's gml file contains more than one CityGML object!");
         }
-        return geom;
+        return objects.get(0).getHighestLodGeometry();
     }
 
 }
-- 
GitLab