All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gherkin.I18n Maven / Gradle / Ivy

There is a newer version: 2.12.2
Show newest version
package gherkin;

import gherkin.formatter.PrettyFormatter;
import gherkin.formatter.model.Comment;
import gherkin.formatter.model.DataTableRow;
import gherkin.formatter.model.Row;
import gherkin.lexer.Lexer;
import gherkin.lexer.Listener;
import gherkin.util.Mapper;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.SortedSet;
import java.util.TreeSet;

import static gherkin.util.FixJava.join;
import static gherkin.util.FixJava.map;

public class I18n {
    private static final List FEATURE_ELEMENT_KEYWORD_KEYS = Arrays.asList("feature", "background", "scenario", "scenario_outline", "examples");
    private static final List STEP_KEYWORD_KEYS = Arrays.asList("given", "when", "then", "and", "but");
    private static final List KEYWORD_KEYS = new ArrayList();

    static {
        KEYWORD_KEYS.addAll(FEATURE_ELEMENT_KEYWORD_KEYS);
        KEYWORD_KEYS.addAll(STEP_KEYWORD_KEYS);
    }

    private static final Mapper QUOTE_MAPPER = new Mapper() {
        public String map(Object o) {
            return '"' + (String) o + '"';
        }
    };

    private static final Mapper CODE_KEYWORD_MAPPER = new Mapper() {
        public String map(Object keyword) {
            return codeKeywordFor((String) keyword);
        }
    };

    public static String codeKeywordFor(String keyword) {
        return keyword.replaceAll("[\\s',!]", "");
    }

    public static List getAll() throws IOException {
        List result = new ArrayList();
        ResourceBundle bundle = ResourceBundle.getBundle("gherkin.I18n", Locale.US);
        String[] isoCodes = bundle.getString("i18n.isoCodes").split(",");
        for (String isoCode : isoCodes) {
            result.add(new I18n(isoCode));
        }
        return result;
    }

    private final String isoCode;
    private final Locale locale;
    private final Map> keywords;

    public I18n(String isoCode) {
        this.isoCode = isoCode;
        this.locale = localeFor(this.isoCode);
        this.keywords = new HashMap>();

        populateKeywords();
    }

    private void populateKeywords() {
        ResourceBundle keywordBundle = ResourceBundle.getBundle("gherkin.I18nKeywords", locale);
        Enumeration keys = keywordBundle.getKeys();
        while (keys.hasMoreElements()) {
            List keywordList = new ArrayList();
            String key = keys.nextElement();

            String value = keywordBundle.getString(key);
            keywordList.addAll(Arrays.asList(value.split("\\|")));
            keywords.put(key, Collections.unmodifiableList(keywordList));
        }
    }

    public String getIsoCode() {
        return isoCode;
    }

    public Locale getLocale() {
        return locale;
    }

    public String getUnderscoredIsoCode() {
        return getIsoCode().replaceAll("[\\s-]", "_").toLowerCase();
    }

    public Lexer lexer(Listener listener) {
        String qualifiedI18nLexerClassName = "gherkin.lexer.i18n." + localeName().toUpperCase();
        try {
            Class delegateClass = getClass().getClassLoader().loadClass(qualifiedI18nLexerClassName);
            return (Lexer) delegateClass.getConstructor(Listener.class).newInstance(listener);
        } catch (Exception e) {
            throw new RuntimeException("Couldn't load lexer class: " + qualifiedI18nLexerClassName, e);
        }
    }

    /**
     * Workaround for he and id bugs in the JDK.
     * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6457127
     * http://forums.sun.com/thread.jspa?threadID=5335461
     *
     * @return locale name.
     */
    private String localeName() {
        return locale.toString().replaceAll("^iw", "he").replaceAll("^in", "id");
    }

    public List keywords(String key) {
        if (!keywords.containsKey(key)) {
            throw new RuntimeException("No such key: " + key + ". Available keys: " + keywords.keySet());
        }
        return keywords.get(key);
    }

    public List getCodeKeywords() {
        List result = map(getStepKeywords(), CODE_KEYWORD_MAPPER);
        result.remove("*");
        return result;
    }

    public List getStepKeywords() {
        SortedSet result = new TreeSet();
        for (String keyword : STEP_KEYWORD_KEYS) {
            result.addAll(keywords.get(keyword));
        }
        return new ArrayList(result);
    }

    public String getKeywordTable() {
        StringWriter writer = new StringWriter();
        PrettyFormatter pf = new PrettyFormatter(writer, true, false);
        List table = new ArrayList();
        for (String key : KEYWORD_KEYS) {
            List cells = Arrays.asList(key, join(map(keywords(key), QUOTE_MAPPER), ", "));
            table.add(new DataTableRow(Collections.emptyList(), cells, -1));
        }
        for (String key : STEP_KEYWORD_KEYS) {
            List codeKeywordList = new ArrayList(keywords.get(key));
            codeKeywordList.remove("* ");
            String codeKeywords = join(map(map(codeKeywordList, CODE_KEYWORD_MAPPER), QUOTE_MAPPER), ", ");

            List cells = Arrays.asList(key + " (code)", codeKeywords);
            table.add(new DataTableRow(Collections.emptyList(), cells, -1));
        }
        pf.table(table);
        return writer.getBuffer().toString();
    }

    private Locale localeFor(String isoString) {
        String[] languageAndCountry = isoString.split("-");
        if (languageAndCountry.length == 1) {
            return new Locale(isoString);
        } else {
            return new Locale(languageAndCountry[0], languageAndCountry[1]);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy