diff --git a/src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java b/src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java index 8a520782088..2d9d5de98e4 100644 --- a/src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java +++ b/src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java @@ -45,7 +45,7 @@ public class Benchmarks { private String htmlConversionString; @Setup - public void init() throws IOException { + public void init() throws Exception { Globals.prefs = JabRefPreferences.getInstance(); Random randomizer = new Random(); @@ -79,7 +79,7 @@ public ParserResult parse() throws IOException { } @Benchmark - public String write() throws SaveException { + public String write() throws Exception { BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter<>(StringSaveSession::new); StringSaveSession saveSession = databaseWriter.savePartOfDatabase( new BibDatabaseContext(database, new MetaData(), new Defaults()), database.getEntries(), diff --git a/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java b/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java index 486d6989d96..0b06ee4bb41 100644 --- a/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java +++ b/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java @@ -69,12 +69,12 @@ private static List applySaveActions(List toChange, MetaD List changes = new ArrayList<>(); Optional saveActions = metaData.getSaveActions(); - if (saveActions.isPresent()) { + saveActions.ifPresent(actions -> { // save actions defined -> apply for every entry for (BibEntry entry : toChange) { - changes.addAll(saveActions.get().applySaveActions(entry)); + changes.addAll(actions.applySaveActions(entry)); } - } + }); return changes; } @@ -104,11 +104,11 @@ private static List> getSaveComparators(SavePreferences pre } /* - * We have begun to use getSortedEntries() for both database save operations - * and non-database save operations. In a non-database save operation - * (such as the exportDatabase call), we do not wish to use the - * global preference of saving in standard order. - */ + * We have begun to use getSortedEntries() for both database save operations + * and non-database save operations. In a non-database save operation + * (such as the exportDatabase call), we do not wish to use the + * global preference of saving in standard order. + */ public static List getSortedEntries(BibDatabaseContext bibDatabaseContext, List entriesToSort, SavePreferences preferences) { Objects.requireNonNull(bibDatabaseContext); @@ -242,14 +242,6 @@ protected void writeMetaData(MetaData metaData) throws SaveException { protected abstract void writePreamble(String preamble) throws SaveException; - private void writeUserCommentsForString(BibtexString string, Writer out) throws IOException { - String userComments = string.getUserComments(); - - if(!userComments.isEmpty()) { - out.write(userComments + Globals.NEWLINE); - } - } - /** * Write all strings in alphabetical order, modified to produce a safe (for * BibTeX) order of the strings if they reference each other. diff --git a/src/main/java/net/sf/jabref/exporter/BibtexDatabaseWriter.java b/src/main/java/net/sf/jabref/exporter/BibtexDatabaseWriter.java index 25838365500..abd599c977a 100644 --- a/src/main/java/net/sf/jabref/exporter/BibtexDatabaseWriter.java +++ b/src/main/java/net/sf/jabref/exporter/BibtexDatabaseWriter.java @@ -87,12 +87,18 @@ protected void writePreamble(String preamble) throws SaveException { @Override protected void writeString(BibtexString bibtexString, boolean isFirstString, int maxKeyLength, Boolean reformatFile) throws SaveException { try { - //if the string has not been modified, write it back as it was + // If the string has not been modified, write it back as it was if (!reformatFile && !bibtexString.hasChanged()) { getWriter().write(bibtexString.getParsedSerialization()); return; } + // Write user comments + String userComments = bibtexString.getUserComments(); + if(!userComments.isEmpty()) { + getWriter().write(userComments + Globals.NEWLINE); + } + if (isFirstString) { getWriter().write(Globals.NEWLINE); } diff --git a/src/test/java/net/sf/jabref/exporter/BibtexDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibtexDatabaseWriterTest.java index 44bcc6a42a9..e9cd0d57662 100644 --- a/src/test/java/net/sf/jabref/exporter/BibtexDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibtexDatabaseWriterTest.java @@ -277,7 +277,7 @@ public void roundtrip() throws Exception { } @Test - public void roundtripWithUserComment() throws IOException { + public void roundtripWithUserComment() throws Exception { Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib"); Charset encoding = StandardCharsets.UTF_8; ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); @@ -286,14 +286,14 @@ public void roundtripWithUserComment() throws IOException { BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX)); - databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); + StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences); try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { - assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); + assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue()); } } @Test - public void roundtripWithUserCommentAndEntryChange() throws IOException { + public void roundtripWithUserCommentAndEntryChange() throws Exception { Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib"); Charset encoding = StandardCharsets.UTF_8; ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); @@ -305,39 +305,37 @@ public void roundtripWithUserCommentAndEntryChange() throws IOException { BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX)); - databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); + StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences); try (Scanner scanner = new Scanner(Paths.get("src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib"),encoding.name())) { - assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); + assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue()); } } @Test - public void roundtripWithUserCommentBeforeStringAndChange() throws IOException { + public void roundtripWithUserCommentBeforeStringAndChange() throws Exception { Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); Charset encoding = StandardCharsets.UTF_8; ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); - BibtexString string = result.getDatabase().getStringValues().iterator().next(); - if(string.getContent().isEmpty()) { - // do nothing - } else { - string.setContent("my first string"); + for (BibtexString string : result.getDatabase().getStringValues()) { + // Mark them as changed + string.setContent(string.getContent()); } SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX)); - databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); + StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences); try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { - assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); + assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue()); } } @Test - public void writeSavedSerializationOfEntryIfUnchanged() throws IOException { + public void writeSavedSerializationOfEntryIfUnchanged() throws Exception { BibEntry entry = new BibEntry(); entry.setType(BibtexEntryTypes.ARTICLE); entry.setField("author", "Mr. author"); diff --git a/src/test/resources/testbib/complex.bib b/src/test/resources/testbib/complex.bib index ee64be35dc5..80e183118f9 100644 --- a/src/test/resources/testbib/complex.bib +++ b/src/test/resources/testbib/complex.bib @@ -5,6 +5,7 @@ @Preamble{preamble This is some arbitrary user comment that should be preserved @String{firstString = {my first string}} +This is some user comment in front of a string, should also be preserved. @String{secondString = {}} @ARTICLE{1102917,