diff --git a/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java b/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java index 7fb5492c1fd..6b5d2d587d8 100644 --- a/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java +++ b/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java @@ -96,11 +96,8 @@ public static String getMetaDataString(List actionList, S Map> groupedByField = new LinkedHashMap<>(); for (FieldFormatterCleanup cleanup : actionList) { Field key = cleanup.getField(); - // add new list into the hashmap if needed - if (!groupedByField.containsKey(key)) { - groupedByField.put(key, new ArrayList<>()); - } + groupedByField.computeIfAbsent(key, k -> new ArrayList<>()); // add the formatter to the map if it is not already there List formattersForKey = groupedByField.get(key); @@ -148,21 +145,6 @@ private List applyAllActions(BibEntry entry) { return result; } - // ToDo: This should reside in MetaDataSerializer - public List getAsStringList(String delimiter) { - List stringRepresentation = new ArrayList<>(); - - if (enabled) { - stringRepresentation.add(ENABLED); - } else { - stringRepresentation.add(DISABLED); - } - - String formatterString = getMetaDataString(actions, delimiter); - stringRepresentation.add(formatterString); - return stringRepresentation; - } - public static List parse(String formatterString) { if ((formatterString == null) || formatterString.isEmpty()) { // no save actions defined in the meta data @@ -190,19 +172,6 @@ public static List parse(String formatterString) { return result; } - // ToDo: This should reside in MetaDataParser - public static FieldFormatterCleanups parse(List formatterMetaList) { - if ((formatterMetaList != null) && (formatterMetaList.size() >= 2)) { - boolean enablementStatus = FieldFormatterCleanups.ENABLED.equals(formatterMetaList.getFirst()); - String formatterString = formatterMetaList.get(1); - - return new FieldFormatterCleanups(enablementStatus, parse(formatterString)); - } else { - // return default actions - return new FieldFormatterCleanups(false, DEFAULT_SAVE_ACTIONS); - } - } - static Formatter getFormatterFromString(String formatterName) { return Formatters .getFormatterForKey(formatterName) diff --git a/src/main/java/org/jabref/logic/exporter/MetaDataSerializer.java b/src/main/java/org/jabref/logic/exporter/MetaDataSerializer.java index 9c0e2dfe95c..fb26b128025 100644 --- a/src/main/java/org/jabref/logic/exporter/MetaDataSerializer.java +++ b/src/main/java/org/jabref/logic/exporter/MetaDataSerializer.java @@ -7,7 +7,6 @@ import java.util.Map; import java.util.StringJoiner; import java.util.TreeMap; -import java.util.stream.Collectors; import org.jabref.logic.citationkeypattern.AbstractCitationKeyPatterns; import org.jabref.logic.citationkeypattern.CitationKeyPattern; @@ -46,7 +45,7 @@ public static Map getSerializedStringMap(MetaData metaData, metaData.getSaveOrder().ifPresent( saveOrderConfig -> stringyMetaData.put(MetaData.SAVE_ORDER_CONFIG, saveOrderConfig.getAsStringList())); metaData.getSaveActions().ifPresent( - saveActions -> stringyMetaData.put(MetaData.SAVE_ACTIONS, saveActions.getAsStringList(OS.NEWLINE))); + saveActions -> stringyMetaData.put(MetaData.SAVE_ACTIONS, getAsStringList(saveActions, OS.NEWLINE))); if (metaData.isProtected()) { stringyMetaData.put(MetaData.PROTECTED_FLAG_META, Collections.singletonList("true")); } @@ -158,18 +157,31 @@ private static String serializeGroups(GroupTreeNode root) { } public static String serializeCustomEntryTypes(BibEntryType entryType) { - StringBuilder builder = new StringBuilder(); - builder.append(MetaData.ENTRYTYPE_FLAG); - builder.append(entryType.getType().getName()); - builder.append(": req["); - builder.append(FieldFactory.serializeOrFieldsList(entryType.getRequiredFields())); - builder.append("] opt["); - builder.append(FieldFactory.serializeFieldsList( - entryType.getOptionalFields() - .stream() - .map(BibField::field) - .collect(Collectors.toList()))); - builder.append("]"); - return builder.toString(); + return MetaData.ENTRYTYPE_FLAG + + entryType.getType().getName() + + ": req[" + + FieldFactory.serializeOrFieldsList(entryType.getRequiredFields()) + + "] opt[" + + FieldFactory.serializeFieldsList( + entryType.getOptionalFields() + .stream() + .map(BibField::field) + .toList()) + + "]"; + } + + public static List getAsStringList(FieldFormatterCleanups fieldFormatterCleanups, String delimiter) { + List stringRepresentation = new ArrayList<>(); + + if (fieldFormatterCleanups.isEnabled()) { + stringRepresentation.add(FieldFormatterCleanups.ENABLED); + } else { + stringRepresentation.add(FieldFormatterCleanups.DISABLED); + } + + String formatterString = FieldFormatterCleanups.getMetaDataString( + fieldFormatterCleanups.getConfiguredActions(), delimiter); + stringRepresentation.add(formatterString); + return stringRepresentation; } } diff --git a/src/main/java/org/jabref/logic/importer/util/MetaDataParser.java b/src/main/java/org/jabref/logic/importer/util/MetaDataParser.java index 70a17d7d5f5..46b5bf7aeb9 100644 --- a/src/main/java/org/jabref/logic/importer/util/MetaDataParser.java +++ b/src/main/java/org/jabref/logic/importer/util/MetaDataParser.java @@ -13,13 +13,20 @@ import java.util.regex.Pattern; import org.jabref.logic.citationkeypattern.CitationKeyPattern; +import org.jabref.logic.cleanup.FieldFormatterCleanup; import org.jabref.logic.cleanup.FieldFormatterCleanups; +import org.jabref.logic.formatter.bibtexfields.NormalizeDateFormatter; +import org.jabref.logic.formatter.bibtexfields.NormalizeMonthFormatter; +import org.jabref.logic.formatter.bibtexfields.NormalizePagesFormatter; import org.jabref.logic.importer.ParseException; +import org.jabref.logic.layout.format.ReplaceUnicodeLigaturesFormatter; import org.jabref.logic.util.Version; import org.jabref.model.database.BibDatabaseMode; import org.jabref.model.entry.BibEntryType; import org.jabref.model.entry.BibEntryTypeBuilder; import org.jabref.model.entry.field.FieldFactory; +import org.jabref.model.entry.field.InternalField; +import org.jabref.model.entry.field.StandardField; import org.jabref.model.entry.types.EntryType; import org.jabref.model.entry.types.EntryTypeFactory; import org.jabref.model.metadata.ContentSelectors; @@ -36,11 +43,20 @@ */ public class MetaDataParser { + public static final List DEFAULT_SAVE_ACTIONS; private static final Logger LOGGER = LoggerFactory.getLogger(MetaDataParser.class); private static FileUpdateMonitor fileMonitor; - private static final Pattern SINGLE_BACKSLASH = Pattern.compile("[^\\\\]\\\\[^\\\\]"); + static { + DEFAULT_SAVE_ACTIONS = List.of( + new FieldFormatterCleanup(StandardField.PAGES, new NormalizePagesFormatter()), + new FieldFormatterCleanup(StandardField.DATE, new NormalizeDateFormatter()), + new FieldFormatterCleanup(StandardField.MONTH, new NormalizeMonthFormatter()), + new FieldFormatterCleanup(InternalField.INTERNAL_ALL_TEXT_FIELDS_FIELD, + new ReplaceUnicodeLigaturesFormatter())); + } + public MetaDataParser(FileUpdateMonitor fileMonitor) { MetaDataParser.fileMonitor = fileMonitor; } @@ -115,7 +131,7 @@ public MetaData parse(MetaData metaData, Map data, Character key Path path = Path.of(parseDirectory(entry.getValue())).normalize(); metaData.setLatexFileDirectory(user, path); } else if (entry.getKey().equals(MetaData.SAVE_ACTIONS)) { - metaData.setSaveActions(FieldFormatterCleanups.parse(values)); + metaData.setSaveActions(fieldFormatterCleanupsParse(values)); } else if (entry.getKey().equals(MetaData.DATABASE_TYPE)) { metaData.setMode(BibDatabaseMode.parse(getSingleItem(values))); } else if (entry.getKey().equals(MetaData.KEYPATTERNDEFAULT)) { @@ -237,4 +253,16 @@ private static Optional getNextUnit(Reader reader) throws IOException { } return Optional.empty(); } + + public static FieldFormatterCleanups fieldFormatterCleanupsParse(List formatterMetaList) { + if ((formatterMetaList != null) && (formatterMetaList.size() >= 2)) { + boolean enablementStatus = FieldFormatterCleanups.ENABLED.equals(formatterMetaList.getFirst()); + String formatterString = formatterMetaList.get(1); + + return new FieldFormatterCleanups(enablementStatus, FieldFormatterCleanups.parse(formatterString)); + } else { + // return default actions + return new FieldFormatterCleanups(false, DEFAULT_SAVE_ACTIONS); + } + } }