Skip to content

Commit

Permalink
Escaping of escape symbols in the MetaData (#2445)
Browse files Browse the repository at this point in the history
* Add failing test for content selector serialization

* Unquote content selectors on parsing

* Add Changelog entry
  • Loading branch information
lenhard authored and tobiasdiez committed Jan 5, 2017
1 parent d673383 commit fa51393
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#

### Fixed
- The formatter for normalizing pages now also can treat ACM pages such as `2:1--2:33`.
- Backslashes in content selectors are now corretly escaped. Fixes [#2426](https://github.com/JabRef/jabref/issues/2426).

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static Map<String, String> serializeMetaData(Map<String, List<String>> s
StringBuilder stringBuilder = new StringBuilder();
for (String dataItem : metaItem.getValue()) {
stringBuilder.append(StringUtil.quote(dataItem, MetaData.SEPARATOR_STRING, MetaData.ESCAPE_CHARACTER)).append(MetaData.SEPARATOR_STRING);

//in case of save actions, add an additional newline after the enabled flag
if (metaItem.getKey().equals(MetaData.SAVE_ACTIONS)
&& (FieldFormatterCleanups.ENABLED.equals(dataItem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.sf.jabref.model.metadata.ContentSelectors;
import net.sf.jabref.model.metadata.MetaData;
import net.sf.jabref.model.metadata.SaveOrderConfig;
import net.sf.jabref.model.strings.StringUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -53,7 +54,7 @@ public static MetaData parse(MetaData metaData, Map<String, String> data, Charac
metaData.setUserFileDirectory(user, getSingleItem(value));
continue;
} else if(entry.getKey().startsWith(MetaData.SELECTOR_META_PREFIX)){
metaData.addContentSelector(ContentSelectors.parse(entry.getKey().substring(MetaData.SELECTOR_META_PREFIX.length()), entry.getValue()));
metaData.addContentSelector(ContentSelectors.parse(entry.getKey().substring(MetaData.SELECTOR_META_PREFIX.length()), StringUtil.unquote(entry.getValue(), MetaData.ESCAPE_CHARACTER)));
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.sf.jabref.logic.exporter;

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
Expand Down Expand Up @@ -560,4 +562,20 @@ public void writeEntriesInOriginalOrderWhenNoSaveOrderConfigIsSetInMetadata() th
, session.getStringValue());
}

@Test
public void roundtripWithContentSelectorsAndUmlauts() throws IOException, SaveException {
String fileContent = "% Encoding: UTF-8" + OS.NEWLINE + OS.NEWLINE + "@Comment{jabref-meta: selector_journal:Test {\\\\\"U}mlaut;}" + OS.NEWLINE;
Charset encoding = StandardCharsets.UTF_8;

ParserResult firstParse = new BibtexParser(importFormatPreferences).parse(new StringReader(fileContent));

SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true);
BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData(),
new Defaults(BibDatabaseMode.BIBTEX));

StringSaveSession session = databaseWriter.savePartOfDatabase(context, firstParse.getDatabase().getEntries(), preferences);

assertEquals(fileContent, session.getStringValue());
}

}

0 comments on commit fa51393

Please sign in to comment.