diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriter.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriter.java index 396f017f0..6145ed60a 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriter.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriter.java @@ -27,6 +27,9 @@ import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathFactory; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -159,6 +162,7 @@ public class EpubMetadataWriter implements MetadataWriter { } if (hasChanges[0]) { + removeEmptyTextNodes(opfDoc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); @@ -318,6 +322,7 @@ public class EpubMetadataWriter implements MetadataWriter { applyCoverImageToEpub(tempDir, opfDoc, coverData); + removeEmptyTextNodes(opfDoc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); @@ -625,4 +630,18 @@ public class EpubMetadataWriter implements MetadataWriter { return true; } + + private void removeEmptyTextNodes(Document doc) { + try { + XPath xpath = XPathFactory.newInstance().newXPath(); + NodeList emptyTextNodes = (NodeList) xpath.evaluate("//text()[normalize-space(.)='']", doc, XPathConstants.NODESET); + + for (int i = 0; i < emptyTextNodes.getLength(); i++) { + Node emptyTextNode = emptyTextNodes.item(i); + emptyTextNode.getParentNode().removeChild(emptyTextNode); + } + } catch (Exception e) { + log.warn("Failed to remove empty text nodes", e); + } + } } diff --git a/booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriterTest.java b/booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriterTest.java index 97c179e67..1dc62049b 100644 --- a/booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriterTest.java +++ b/booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriterTest.java @@ -142,6 +142,53 @@ class EpubMetadataWriterTest { } } + @Nested + @DisplayName("Whitespace Tests") + class WhitespaceTests { + @Test + @DisplayName("Should not add extra whitespace lines on repeated saves") + void saveMetadataToFile_repeatedSaves_shouldNotInflateWhitespace() throws IOException { + String initialOpfContent = """ + + + + Original Title + Original Author + + """; + + File epubFile = createEpubWithOpf(initialOpfContent, "test-whitespace-" + System.nanoTime() + ".epub"); + + BookMetadataEntity newMeta = new BookMetadataEntity(); + newMeta.setTitle("Updated Title"); + AuthorEntity author = new AuthorEntity(); + author.setName("Updated Author"); + newMeta.setAuthors(Collections.singleton(author)); + + writer.saveMetadataToFile(epubFile, newMeta, null, new MetadataClearFlags()); + String contentAfterFirstSave = readOpfContent(epubFile); + + newMeta.setTitle("Updated Title 2"); // Change title to force write + writer.saveMetadataToFile(epubFile, newMeta, null, new MetadataClearFlags()); + String contentAfterSecondSave = readOpfContent(epubFile); + + long lines1 = contentAfterFirstSave.lines().count(); + long lines2 = contentAfterSecondSave.lines().count(); + + assertTrue(Math.abs(lines2 - lines1) <= 2, "Line count should be stable"); + assertTrue(!contentAfterSecondSave.contains("\n\n"), "Should not contain double newlines"); + } + } + + private String readOpfContent(File epubFile) throws IOException { + try (ZipFile zf = new ZipFile(epubFile)) { + ZipEntry ze = zf.getEntry("OEBPS/content.opf"); + try (InputStream is = zf.getInputStream(ze)) { + return new String(is.readAllBytes(), StandardCharsets.UTF_8); + } + } + } + private File createEpubWithOpf(String opfContent, String filename) throws IOException { File epubFile = tempDir.resolve(filename).toFile();