From fa51393d64d4476c822d9c9746bf294026b891e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Lenhard?= Date: Thu, 5 Jan 2017 22:26:23 +0100 Subject: [PATCH] Escaping of escape symbols in the MetaData (#2445) * Add failing test for content selector serialization * Unquote content selectors on parsing * Add Changelog entry --- CHANGELOG.md | 1 + .../logic/exporter/MetaDataSerializer.java | 2 +- .../logic/importer/util/MetaDataParser.java | 3 ++- .../exporter/BibtexDatabaseWriterTest.java | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72608708f80..35c03d942e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/net/sf/jabref/logic/exporter/MetaDataSerializer.java b/src/main/java/net/sf/jabref/logic/exporter/MetaDataSerializer.java index 3095e6fb27b..22ff399f30f 100644 --- a/src/main/java/net/sf/jabref/logic/exporter/MetaDataSerializer.java +++ b/src/main/java/net/sf/jabref/logic/exporter/MetaDataSerializer.java @@ -61,7 +61,7 @@ private static Map serializeMetaData(Map> 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) diff --git a/src/main/java/net/sf/jabref/logic/importer/util/MetaDataParser.java b/src/main/java/net/sf/jabref/logic/importer/util/MetaDataParser.java index 3de3a4a55b2..c3d893d1d83 100644 --- a/src/main/java/net/sf/jabref/logic/importer/util/MetaDataParser.java +++ b/src/main/java/net/sf/jabref/logic/importer/util/MetaDataParser.java @@ -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; @@ -53,7 +54,7 @@ public static MetaData parse(MetaData metaData, Map 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; } diff --git a/src/test/java/net/sf/jabref/logic/exporter/BibtexDatabaseWriterTest.java b/src/test/java/net/sf/jabref/logic/exporter/BibtexDatabaseWriterTest.java index 9cde3e5211d..147af2a5f92 100644 --- a/src/test/java/net/sf/jabref/logic/exporter/BibtexDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/logic/exporter/BibtexDatabaseWriterTest.java @@ -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; @@ -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()); + } + }