Skip to content

Commit

Permalink
Rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Jul 16, 2016
1 parent a8739bd commit 013c27c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -79,7 +79,7 @@ public ParserResult parse() throws IOException {
}

@Benchmark
public String write() throws SaveException {
public String write() throws Exception {
BibtexDatabaseWriter<StringSaveSession> databaseWriter = new BibtexDatabaseWriter<>(StringSaveSession::new);
StringSaveSession saveSession = databaseWriter.savePartOfDatabase(
new BibDatabaseContext(database, new MetaData(), new Defaults()), database.getEntries(),
Expand Down
24 changes: 8 additions & 16 deletions src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ private static List<FieldChange> applySaveActions(List<BibEntry> toChange, MetaD
List<FieldChange> changes = new ArrayList<>();

Optional<FieldFormatterCleanups> 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;
}
Expand Down Expand Up @@ -104,11 +104,11 @@ private static List<Comparator<BibEntry>> 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<BibEntry> getSortedEntries(BibDatabaseContext bibDatabaseContext, List<BibEntry> entriesToSort,
SavePreferences preferences) {
Objects.requireNonNull(bibDatabaseContext);
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
28 changes: 13 additions & 15 deletions src/test/java/net/sf/jabref/exporter/BibtexDatabaseWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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");
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/testbib/complex.bib
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 013c27c

Please sign in to comment.