package net.sf.jabref.exporter;

import net.sf.jabref.MetaData;
import net.sf.jabref.logic.formatter.BibtexFieldFormatters;
import net.sf.jabref.logic.formatter.CaseChangers;
import net.sf.jabref.logic.formatter.Formatter;
import net.sf.jabref.logic.formatter.IdentityFormatter;
import net.sf.jabref.model.entry.BibEntry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SaveActions {

    private final Map<String, Formatter> actions;

    private List<Formatter> availableFormatters;

    public static final String META_KEY = "saveActions";

    public SaveActions(MetaData metaData) {
        actions = new HashMap<>();
        setAvailableFormatters();

        List<String> formatters = metaData.getData(META_KEY);
        if(formatters==null) {
            // no save actions defined in the meta data
            return;
        } else {
            for (int i = 0; i < formatters.size(); i += 2) {
                try {
                    String field = formatters.get(i);
                    Formatter formatter = getFormatterFromString(formatters.get(i + 1));

                    actions.put(field, formatter);
                } catch (IndexOutOfBoundsException e) {
                    // the meta data string in the file is broken. -> Ignore the last item
                    break;
                }
            }
        }

    }

    private void setAvailableFormatters() {
        availableFormatters = new ArrayList<>();

        availableFormatters.addAll(BibtexFieldFormatters.ALL);
        availableFormatters.addAll(CaseChangers.ALL);
    }

    public BibEntry applySaveActions(BibEntry entry) {
        for (String key : actions.keySet()) {
            Formatter formatter = actions.get(key);

            String fieldContent = entry.getField(key);
            String formattedContent = formatter.format(fieldContent);
            entry.setField(key, formattedContent);
        }

        return entry;
    }

    private Formatter getFormatterFromString(String formatterName) {
        for (Formatter formatter : availableFormatters) {
            if (formatterName.equals(formatter.getKey())) {
                return formatter;
            }
        }
        return new IdentityFormatter();
    }

}

@@ -603,16 +603,46 @@
 ions%22;%0A%0A
+    private boolean enabled;%0A%0A
     publ
@@ -819,16 +819,17 @@
       if
+ 
 (formatt
@@ -835,10 +835,12 @@
 ters
+ 
 ==
+ 
 null
@@ -936,24 +936,218 @@
  else %7B%0A    
+        parseEnabledStatus(formatters);%0A%0A            parseSaveActions(formatters);%0A        %7D%0A%0A    %7D%0A%0A    private void parseSaveActions(List%3CString%3E formatters) %7B%0A        //read concrete actions%0A
         for 
@@ -1203,20 +1203,16 @@
         
-    
 try %7B%0A  
@@ -1217,36 +1217,32 @@
                 
- 
-   
 String field = f
@@ -1267,36 +1267,32 @@
                 
-   
- 
 Formatter format
@@ -1357,28 +1357,24 @@
             
-    
 actions.put(
@@ -1404,20 +1404,16 @@
         
-  
-  
 %7D catch 
@@ -1440,28 +1440,24 @@
 eption e) %7B%0A
-    
             
@@ -1547,20 +1547,16 @@
         
-    
 break;%0A 
@@ -1554,32 +1554,292 @@
  break;%0A        
+    %7D%0A        %7D%0A    %7D%0A%0A    private void parseEnabledStatus(List%3CString%3E formatters) %7B%0A        //read if save actions should be enabled%0A        String enableActions = formatters.get(0);%0A        if (%22enabled%22.equals(enableActions)) %7B%0A            enabled = true;%0A
         %7D%0A      
@@ -1823,32 +1823,39 @@
  true;%0A        %7D
+ else %7B
 %0A            %7D%0A 
@@ -1843,33 +1843,48 @@
 e %7B%0A            
-%7D
+enabled = false;
 %0A        %7D%0A%0A    
@@ -1870,33 +1870,32 @@
 alse;%0A        %7D%0A
-%0A
     %7D%0A%0A    priva
@@ -2170,50 +2170,324 @@
     
-for (String key : actions.keySet()) %7B%0A    
+if (enabled) %7B%0A            applyAllActions(entry);%0A        %7D%0A        return entry;%0A    %7D%0A%0A    private void applyAllActions(BibEntry entry) %7B%0A        for (String key : actions.keySet()) %7B%0A            applyActionForField(entry, key);%0A        %7D%0A    %7D%0A%0A    private void applyActionForField(BibEntry entry, String key) %7B%0A
     
@@ -2527,28 +2527,24 @@
 .get(key);%0A%0A
-    
         Stri
@@ -2582,36 +2582,32 @@
 d(key);%0A        
-   
- 
 String formatted
@@ -2656,20 +2656,16 @@
         
-    
 entry.se
@@ -2699,41 +2699,8 @@
 t);%0A
-        %7D%0A%0A        return entry;%0A
     

Add enablement feature for save actions