Skip to content

Commit

Permalink
finish ToDo in FieldFormatterCleanups (#11750)
Browse files Browse the repository at this point in the history
1. move `parse` into `MetaDataParser`
2. move `getAsStringList` into `MetaDataSerializer`
3. some minor fix
  • Loading branch information
leaf-soba authored Sep 11, 2024
1 parent 14ba56d commit 2de686e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,8 @@ public static String getMetaDataString(List<FieldFormatterCleanup> actionList, S
Map<Field, List<String>> 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<String> formattersForKey = groupedByField.get(key);
Expand Down Expand Up @@ -148,21 +145,6 @@ private List<FieldChange> applyAllActions(BibEntry entry) {
return result;
}

// ToDo: This should reside in MetaDataSerializer
public List<String> getAsStringList(String delimiter) {
List<String> 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<FieldFormatterCleanup> parse(String formatterString) {
if ((formatterString == null) || formatterString.isEmpty()) {
// no save actions defined in the meta data
Expand Down Expand Up @@ -190,19 +172,6 @@ public static List<FieldFormatterCleanup> parse(String formatterString) {
return result;
}

// ToDo: This should reside in MetaDataParser
public static FieldFormatterCleanups parse(List<String> 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)
Expand Down
42 changes: 27 additions & 15 deletions src/main/java/org/jabref/logic/exporter/MetaDataSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,7 +45,7 @@ public static Map<String, String> 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"));
}
Expand Down Expand Up @@ -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<String> getAsStringList(FieldFormatterCleanups fieldFormatterCleanups, String delimiter) {
List<String> 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;
}
}
32 changes: 30 additions & 2 deletions src/main/java/org/jabref/logic/importer/util/MetaDataParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,11 +43,20 @@
*/
public class MetaDataParser {

public static final List<FieldFormatterCleanup> 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;
}
Expand Down Expand Up @@ -115,7 +131,7 @@ public MetaData parse(MetaData metaData, Map<String, String> 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)) {
Expand Down Expand Up @@ -237,4 +253,16 @@ private static Optional<String> getNextUnit(Reader reader) throws IOException {
}
return Optional.empty();
}

public static FieldFormatterCleanups fieldFormatterCleanupsParse(List<String> 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);
}
}
}

0 comments on commit 2de686e

Please sign in to comment.