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

com.swak.i18n.MessageI18nSourceScanners Maven / Gradle / Ivy

The newest version!
package com.swak.i18n;

import com.swak.common.dto.base.I18nCode;
import com.swak.common.util.OrderedProperties;
import com.swak.core.support.ClassScanners;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;

/**
 * @author colley.ma
 * @since 3.0.0
 */
public class MessageI18nSourceScanners {


    public static String i18nScanner(String[] basePackages, Class clazz, Class... filters) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        Set> scanners = ClassScanners.scanners(basePackages, clazz);
        for (Class scanner : scanners) {
            if (!filter(scanner, filters)) {
                continue;
            }
            OrderedProperties i18nMessage = new OrderedProperties();
            I18nCode[] enumConstants = scanner.getEnumConstants();
            Arrays.stream(enumConstants).forEach(enumConstant -> {
                String className = enumConstant.getClass().getName() + "." + enumConstant;
                i18nMessage.put(className, enumConstant.fallback());
            });
            Arrays.stream(enumConstants).forEach(enumConstant -> {
                String className = enumConstant.getClass().getName() + "." + enumConstant;
                if(StringUtils.isNotBlank(enumConstant.getAlias())){
                    i18nMessage.put(className+".Alias", enumConstant.getAlias());
                }
            });
            StringWriter writer = new StringWriter();
            i18nMessage.store(writer, scanner.getName());
            stringBuilder.append(writer);
        }
        return stringBuilder.toString();
    }

    private static boolean filter(Class scanner, Class... filters) {
        if (ArrayUtils.isEmpty(filters)) {
            return true;
        }
        for (Class filter : filters) {
            if (Objects.equals(filter.getName(), scanner.getName())) {
                return true;
            }
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy