Commit 3563658f authored by Riegel's avatar Riegel
Browse files

Refactor: Use new FactoryMethods for parsing

2 merge requests!28Version 3.17.0 Release,!26Add ZIP-archive support
Showing with 20 additions and 40 deletions
+20 -40
...@@ -12,7 +12,6 @@ import java.util.ArrayList; ...@@ -12,7 +12,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
...@@ -25,36 +24,14 @@ public class CityGmlZipArchive implements Serializable { ...@@ -25,36 +24,14 @@ public class CityGmlZipArchive implements Serializable {
private List<CityGmlZipEntry> entries; private List<CityGmlZipEntry> entries;
private final Path archivePath; private final Path archivePath;
private final String archiveNameRE;
private ZipFile zipFile; private ZipFile zipFile;
public static CityGmlZipArchive fromZipFile(String zipFile, ParserConfiguration config) { public static CityGmlZipArchive fromZipFile(String zipFile, ParserConfiguration config) {
ArrayList<CityGmlZipEntry> archiveEntries = new ArrayList<>(); CityGmlZipArchive archive = CityGmlZipArchive.register(zipFile);
ZipFile zip = null; archive.mountArchive(config);
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) { logger.info("Zip-archive successfully uncompressed, {} CityGml files detected", archive.getEntries().size());
zip = new ZipFile(zipFile); return archive;
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null )
{
if (ze.isDirectory()) {
continue;
}
if (ze.getName().endsWith(".gml")) {
archiveEntries.add(CityGmlZipEntry.of(ze, config));
}
}
} catch (IOException e) {
logger.error(e);
} finally {
if(zip != null){
try {
zip.close();
} catch (IOException e) {
logger.error(e);
}
}
}
logger.info("Zip-archive successfully uncompressed, {} CityGml files detected", archiveEntries.size());
return new CityGmlZipArchive(archiveEntries, Path.of(zipFile));
} }
public static CityGmlZipArchive register(String zipFile) { public static CityGmlZipArchive register(String zipFile) {
...@@ -68,7 +45,7 @@ public class CityGmlZipArchive implements Serializable { ...@@ -68,7 +45,7 @@ public class CityGmlZipArchive implements Serializable {
continue; continue;
} }
if (ze.getName().endsWith(".gml")) { if (ze.getName().endsWith(".gml")) {
archiveEntries.add(CityGmlZipEntry.register(ze)); archiveEntries.add(CityGmlZipEntry.register(ze, cgmlArchive));
} }
} }
cgmlArchive.setEntries(archiveEntries); cgmlArchive.setEntries(archiveEntries);
...@@ -93,11 +70,7 @@ public class CityGmlZipArchive implements Serializable { ...@@ -93,11 +70,7 @@ public class CityGmlZipArchive implements Serializable {
private CityGmlZipArchive(Path archivePath){ private CityGmlZipArchive(Path archivePath){
this.archivePath = archivePath; this.archivePath = archivePath;
} this.archiveNameRE = archivePath.getFileName().toString().replace(".zip","") + File.separator;
private CityGmlZipArchive(List<CityGmlZipEntry> entries, Path archivePath) {
setEntries(entries);
this.archivePath = archivePath;
} }
private void setEntries(List<CityGmlZipEntry> entries) { private void setEntries(List<CityGmlZipEntry> entries) {
...@@ -111,14 +84,20 @@ public class CityGmlZipArchive implements Serializable { ...@@ -111,14 +84,20 @@ public class CityGmlZipArchive implements Serializable {
public CityGmlZipEntry getEntry(String fileName) { public CityGmlZipEntry getEntry(String fileName) {
fileName = stripArchivePath(fileName);
for(CityGmlZipEntry entry : entries){ for(CityGmlZipEntry entry : entries){
if(entry.getFileName().equals(fileName)){ String entryName = stripArchivePath(entry.getFileName());
if(entryName.equals(fileName)){
return entry; return entry;
} }
} }
return null; return null;
} }
private String stripArchivePath(String path) {
Path systemPath = Path.of(path);
return systemPath.toString().replace(archiveNameRE, "");
}
public Path getArchivePath() { public Path getArchivePath() {
return archivePath; return archivePath;
......
...@@ -27,8 +27,8 @@ public class CityGmlZipEntry { ...@@ -27,8 +27,8 @@ public class CityGmlZipEntry {
private static final long MB = 1024 * 1024L; private static final long MB = 1024 * 1024L;
private ZipEntryErrorType errorType = null; private ZipEntryErrorType errorType = null;
public static CityGmlZipEntry of(ZipEntry entry, ParserConfiguration config){ public static CityGmlZipEntry of(ZipEntry entry,CityGmlZipArchive parentArchive, ParserConfiguration config){
CityGmlZipEntry ze = CityGmlZipEntry.register(entry); CityGmlZipEntry ze = CityGmlZipEntry.register(entry, parentArchive);
ze.loadEntry(config); ze.loadEntry(config);
return ze; return ze;
} }
...@@ -53,8 +53,8 @@ public class CityGmlZipEntry { ...@@ -53,8 +53,8 @@ public class CityGmlZipEntry {
} }
} }
public static CityGmlZipEntry register(ZipEntry entry){ public static CityGmlZipEntry register(ZipEntry entry, CityGmlZipArchive parentArchive){
CityGmlZipEntry cgzEntry = new CityGmlZipEntry(entry, false); CityGmlZipEntry cgzEntry = new CityGmlZipEntry(entry, parentArchive,false);
try{ try{
if (!cgzEntry.entrySizeWithinMemoryLimits()) { if (!cgzEntry.entrySizeWithinMemoryLimits()) {
cgzEntry.errorType = ZipEntryErrorType.EXCESSIVE_FILESIZE; cgzEntry.errorType = ZipEntryErrorType.EXCESSIVE_FILESIZE;
...@@ -85,10 +85,11 @@ public class CityGmlZipEntry { ...@@ -85,10 +85,11 @@ public class CityGmlZipEntry {
} }
protected CityGmlZipEntry(ZipEntry entry, boolean decompressed) { protected CityGmlZipEntry(ZipEntry entry,CityGmlZipArchive parentArchive, boolean decompressed) {
this.fileName = entry.getName(); this.fileName = entry.getName();
this.model = null; this.model = null;
this.decompressed = decompressed; this.decompressed = decompressed;
this.parentArchive = parentArchive;
} }
protected CityGmlZipEntry(String fileName, boolean decompressed) { protected CityGmlZipEntry(String fileName, boolean decompressed) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment