FunctionNameMappingImpl.java 2.13 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.forms.formcalc.support;

import com.adobe.forms.formcalc.api.FunctionNameMapping;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FunctionNameMappingImpl
implements FunctionNameMapping {
    private static Logger logger = LoggerFactory.getLogger(FunctionNameMappingImpl.class);
    private final Map<String, String> formcalcToJSFuncs = new HashMap<String, String>();
    private final Map<String, String> formcalcToJSConst = new HashMap<String, String>();

    public FunctionNameMappingImpl() {
        this.init();
    }

    private void init() {
        try {
            this.loadPropertiesFile();
        }
        catch (IOException e) {
            logger.debug("Error while loading properties file: ", (Throwable)e);
        }
        this.formcalcToJSConst.put("pi", "Math.PI");
    }

    private void loadPropertiesFile() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(FunctionNameMappingImpl.class.getResourceAsStream("/com/adobe/forms/formcalc/support/functionsMapping.properties")));
        String line = br.readLine();
        while (line != null) {
            int sep;
            if (!line.startsWith("#") && (sep = line.indexOf(61)) >= 0) {
                this.formcalcToJSFuncs.put(line.substring(0, sep).toLowerCase(), line.substring(sep + 1, line.length()));
            }
            line = br.readLine();
        }
    }

    @Override
    public String getJSFunctionIfAny(String formCalcFuntion) {
        String jsFun = this.formcalcToJSFuncs.get(formCalcFuntion.toLowerCase());
        if (jsFun != null) {
            return jsFun;
        }
        return formCalcFuntion;
    }

    @Override
    public String getFunctionToConst(String formCalcFuntion) {
        return this.formcalcToJSConst.get(formCalcFuntion.toLowerCase());
    }
}