From 898afd98b91044737188e86b9f338d6c6e3e3dca Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Thu, 12 Mar 2020 10:13:43 +0100 Subject: [PATCH] [Fix] Don't read XML files starting with .tmp_ our temp file prefix --- .../java/li/strolch/xmlpers/api/FileIo.java | 5 +-- .../li/strolch/xmlpers/api/MetadataDao.java | 34 ++++++++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileIo.java b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileIo.java index bd8843f1b..d8f98933e 100644 --- a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileIo.java +++ b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileIo.java @@ -46,13 +46,14 @@ public class FileIo { public static final String DEFAULT_ENCODING = "utf-8"; //$NON-NLS-1$ private static final Logger logger = LoggerFactory.getLogger(FileIo.class); + public static final String TMP_PREFIX = ".tmp_"; private final File path; private final File tmpPath; public FileIo(File path) { this.path = path; - this.tmpPath = new File(this.path.getParentFile(), ".tmp_" + this.path.getName()); + this.tmpPath = new File(this.path.getParentFile(), TMP_PREFIX + this.path.getName()); } public void writeSax(PersistenceContext ctx) { @@ -160,7 +161,7 @@ public class FileIo { // Transform to file try (Writer ioWriter = new OutputStreamWriter(new FileOutputStream(this.tmpPath), encoding)) { - StreamResult result = new StreamResult(this.tmpPath); + StreamResult result = new StreamResult(ioWriter); Source xmlSource = new DOMSource(document); transformer.transform(xmlSource, result); } diff --git a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/MetadataDao.java b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/MetadataDao.java index a26364f3b..f83f760b2 100644 --- a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/MetadataDao.java +++ b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/MetadataDao.java @@ -15,6 +15,8 @@ */ package li.strolch.xmlpers.api; +import static li.strolch.xmlpers.api.FileIo.TMP_PREFIX; + import java.io.File; import java.text.MessageFormat; import java.util.Collections; @@ -138,10 +140,12 @@ public class MetadataDao { Set keySet = new TreeSet<>(); File[] subTypeFiles = queryPath.listFiles(); - for (File subTypeFile : subTypeFiles) { - if (subTypeFile.isDirectory()) { - String type = subTypeFile.getName(); - keySet.add(type); + if (subTypeFiles != null) { + for (File subTypeFile : subTypeFiles) { + if (subTypeFile.isDirectory()) { + String type = subTypeFile.getName(); + keySet.add(type); + } } } @@ -178,7 +182,7 @@ public class MetadataDao { } for (File subTypeFile : subTypeFiles) { - if (subTypeFile.isFile()) { + if (subTypeFile.isFile() && !subTypeFile.getName().startsWith(TMP_PREFIX)) { String filename = subTypeFile.getName(); String id = FilenameUtility.getId(filename); keySet.add(id); @@ -206,13 +210,15 @@ public class MetadataDao { throw new IllegalArgumentException(msg); } - long numberOfFiles = 0l; + long numberOfFiles = 0L; File[] subTypeFiles = queryPath.listFiles(); - for (File subTypeFile : subTypeFiles) { + if (subTypeFiles != null) { + for (File subTypeFile : subTypeFiles) { - if (subTypeFile.isDirectory()) - numberOfFiles++; + if (subTypeFile.isDirectory()) + numberOfFiles++; + } } return numberOfFiles; } @@ -235,13 +241,15 @@ public class MetadataDao { throw new IllegalArgumentException(msg); } - long numberOfFiles = 0l; + long numberOfFiles = 0L; File[] subTypeFiles = queryPath.listFiles(); - for (File subTypeFile : subTypeFiles) { + if (subTypeFiles != null) { + for (File subTypeFile : subTypeFiles) { - if (subTypeFile.isFile()) - numberOfFiles++; + if (subTypeFile.isFile() && !subTypeFile.getName().startsWith(TMP_PREFIX)) + numberOfFiles++; + } } return numberOfFiles; }