This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository i18n. See https://gitlab.nuiton.org/nuiton/i18n.git commit d2ce20fc7c2da4e39bc823c29a05d879f6fc3057 Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Sep 2 14:02:41 2016 +0200 Make enum i18n generation customizable (See #4010) --- .../i18n/plugin/GenerateI18nEnumHelperMojo.java | 237 +++++++++++++-------- 1 file changed, 145 insertions(+), 92 deletions(-) diff --git a/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/GenerateI18nEnumHelperMojo.java b/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/GenerateI18nEnumHelperMojo.java index 6eda54c..fb59435 100644 --- a/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/GenerateI18nEnumHelperMojo.java +++ b/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/GenerateI18nEnumHelperMojo.java @@ -27,12 +27,12 @@ import com.google.common.io.Files; import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.TokenStream; -import org.antlr.v4.runtime.misc.NotNull; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.codehaus.plexus.util.StringUtils; import org.nuiton.i18n.plugin.parser.java.Java8BaseVisitor; import org.nuiton.i18n.plugin.parser.java.Java8Lexer; import org.nuiton.i18n.plugin.parser.java.Java8Parser; @@ -44,8 +44,10 @@ import java.io.File; import java.io.IOException; import java.util.Date; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.StringTokenizer; import java.util.TreeSet; /** @@ -59,11 +61,11 @@ import java.util.TreeSet; @Mojo(name = "generateI18nEnumHelper", defaultPhase = LifecyclePhase.GENERATE_SOURCES) public class GenerateI18nEnumHelperMojo extends AbstractI18nMojo { - /** - * Prefix to add to generated i18n keys. - */ - @Parameter(property = "i18n.prefix") - protected String prefix; +// /** +// * Prefix to add to generated i18n keys. +// */ +// @Parameter(property = "i18n.prefix") +// protected String prefix; /** * To set the package fully qualified name of the generated class. @@ -71,58 +73,60 @@ public class GenerateI18nEnumHelperMojo extends AbstractI18nMojo { * By default, will use groupId.artifactId (with {@code -} replaced by {@code .}). */ @Parameter(property = "i18n.packageName") - protected String packageName; + private String packageName; /** * Name of the generated class. */ @Parameter(property = "i18n.className", defaultValue = "I18nEnumHelper", required = true) - protected String className; + private String className; /** * The root directory where to generated. */ @Parameter(property = "i18n.outputdirectory", defaultValue = "${basedir}/target/generated-sources/java", required = true) - protected File outputdirectory; + private File outputdirectory; /** - * List of enums to scan to generate i18n keys. + * List of enumerations set to scan to generate i18n keys. * - * Example with enum {@code enum E { A,B }}, will generate i18n keys: - * <ul> - * <li>prefixA</li> - * <li>prefixB</li> - * </ul> - */ - @Parameter(property = "i18n.enumsWithLabel", required = true) - protected List<String> enumsWithLabel; - - /** - * List of enums to scan to generate i18n keys plus description keys. + * <pre> + * <enumerationSets> + * <enumerationSet> + * <name>label</name> + * <pattern>myPrefix.@CLASS_NAME@.@NAME@</pattern> + * <enums> + * <org.nuiton.Enum1> + * <org.nuiton.Enum2> + * <...> + * </enums> + * </enumerationSet> + * </enumerationSets> + * </pre> * - * Example with enum {@code enum E { A,B }}, will generate i18n keys: + * Example with enum {@code enum org.nuiton.Enum1 { A,B }}, will generate i18n keys: * <ul> - * <li>prefixA</li> - * <li>prefixA.description</li> - * <li>prefixB</li> - * <li>prefixB.description</li> + * <li>myPrefix.org.nuiton.Enum1.A</li> + * <li>myPrefix.org.nuiton.Enum1.B</li> * </ul> - */ - @Parameter(property = "i18n.enumsWithLabelAndDescription", required = true) - protected List<String> enumsWithLabelAndDescription; - - /** - * List of enums to scan to generate i18n description keys. * - * Example with enum {@code enum E { A,B }}, will generate i18n keys: + * In pattern, you can use variable * <ul> - * <li>prefixA.description</li> - * <li>prefixB.description</li> + * + * <li>{@code @CLASS_NAME@} for enumeration class fully qualified name</li> + * <li>{@code @CLASS_SIMPLE_NAME@} for enumeration class simple name</li> + * <li>{@code @NAME@} for enumeration name</li> + * <li>{@code @ORDINAL@} for enumeration ordinal</li> * </ul> + * + * Moreover, two methods will be also generated according to specified name to translate those keys : + * <pre> + * public static String getLabel(Enum) + * public static String getLabel(Locale, Enum) + * </pre> */ - @Parameter(property = "i18n.enumsWithLabelAndDescription", required = true) - protected List<String> enumsWithDescription; - + @Parameter(required = true) + private List<EnumerationSet> enumerationSets; @Override protected void doAction() throws Exception { @@ -154,69 +158,46 @@ public class GenerateI18nEnumHelperMojo extends AbstractI18nMojo { writer.write("\n"); writer.write("public class " + className + " {\n"); writer.write("\n"); - if (!(enumsWithLabel.isEmpty() && enumsWithLabelAndDescription.isEmpty())) { - writer.write(" public static <E extends Enum<E>> String getLabel(E e) {\n"); - writer.write(" return t(getLabelKey(e));\n"); - writer.write(" }\n"); - writer.write("\n"); - - writer.write(" public static <E extends Enum<E>> String getLabel(Locale locale, E e) {\n"); - writer.write(" return l(locale, getLabelKey(e));\n"); - writer.write(" }\n"); - writer.write("\n"); - writer.write(" protected static <E extends Enum<E>> String getLabelKey(E e) {\n"); - writer.write(" return \"" + prefix + "\" + e.getClass().getName() + \".\" + e.name();\n"); - writer.write(" }\n"); - writer.write("\n"); - } - if (!(enumsWithDescription.isEmpty() && enumsWithLabelAndDescription.isEmpty())) { - writer.write(" public static <E extends Enum<E>> String getDescription(E e) {\n"); - writer.write(" return t(getDescriptionKey(e));\n"); - writer.write(" }\n"); - writer.write("\n"); - - writer.write(" public static <E extends Enum<E>> String getDescription(Locale locale, E e) {\n"); - writer.write(" return l(locale, getDescriptionKey(e));\n"); - writer.write(" }\n"); - writer.write("\n"); - writer.write(" protected static <E extends Enum<E>> String getDescriptionKey(E e) {\n"); - writer.write(" return \"" + prefix + "\" + e.getClass().getName() + \".\" + e.name() + \".description\";\n"); - writer.write(" }\n"); - writer.write("\n"); - } - writer.write(" static {\n\n"); - Set<String> allEnums = new TreeSet<>(); - allEnums.addAll(enumsWithLabel); - allEnums.addAll(enumsWithDescription); - allEnums.addAll(enumsWithLabelAndDescription); + Set<String> names = new HashSet<>(); + for (EnumerationSet enumerationSet : enumerationSets) { - Set<String> allLabelEnums = new TreeSet<>(); - allLabelEnums.addAll(enumsWithLabel); - allLabelEnums.addAll(enumsWithLabelAndDescription); + if (names.add(enumerationSet.getName())) { - Set<String> allDescriptionEnums = new TreeSet<>(); - allDescriptionEnums.addAll(enumsWithDescription); - allDescriptionEnums.addAll(enumsWithLabelAndDescription); + String methodName = "get" + StringUtils.capitalise(enumerationSet.getName()); - for (String anEnumType : allEnums) { + writer.write(" public static <E extends Enum<E>> String " + methodName + "(E e) {\n"); + writer.write(" return t(" + methodName + "Key(e));\n"); + writer.write(" }\n"); + writer.write("\n"); - boolean generateLabel = allLabelEnums.contains(anEnumType); - boolean generateDescription = allDescriptionEnums.contains(anEnumType); + writer.write(" public static <E extends Enum<E>> String " + methodName + "(Locale locale, E e) {\n"); + writer.write(" return l(locale, " + methodName + "Key(e));\n"); + writer.write(" }\n"); + writer.write("\n"); + writer.write(" protected static <E extends Enum<E>> String " + methodName + "Key(E e) {\n"); + writer.write(" return " + enumerationSet.transformToMessage() + ";\n"); + writer.write(" }\n"); + writer.write("\n"); - for (String name : getEnumConstants(anEnumType)) { - if (generateLabel) { - writer.write(" n(\"" + prefix + anEnumType + "." + name + "\");\n"); - } - if (generateDescription) { - writer.write(" n(\"" + prefix + anEnumType + "." + name + ".description\");\n"); + } + } + writer.write(" static {\n\n"); + + for (EnumerationSet enumerationSet : enumerationSets) { + + for (String anEnumType : enumerationSet.getEnums()) { + + int ordinal = 0; + String simpleName = anEnumType.substring(anEnumType.lastIndexOf('.') + 1); + for (String name : getEnumConstants(anEnumType)) { + writer.write(" n(\"" + enumerationSet.transformToKey(anEnumType, simpleName, name, (ordinal++) + "") + "\");\n"); } } - writer.write("\n"); } - writer.write(" }\n"); + writer.write("\n }\n"); writer.write("}\n"); writer.close(); @@ -288,7 +269,7 @@ public class GenerateI18nEnumHelperMojo extends AbstractI18nMojo { private JavaParserVisitor(File file) { this.file = file; - names = new HashSet<String>(); + names = new LinkedHashSet<>(); } public Set<String> getNames() { @@ -296,7 +277,7 @@ public class GenerateI18nEnumHelperMojo extends AbstractI18nMojo { } @Override - public Void visitEnumConstant(@NotNull Java8Parser.EnumConstantContext ctx) { + public Void visitEnumConstant(Java8Parser.EnumConstantContext ctx) { String text = ctx.getChild(0).getText(); names.add(text); return super.visitEnumConstant(ctx); @@ -304,4 +285,76 @@ public class GenerateI18nEnumHelperMojo extends AbstractI18nMojo { } + public static class EnumerationSet { + + private String name; + private String pattern; + private List<String> enums; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPattern() { + return pattern; + } + + public void setPattern(String pattern) { + this.pattern = pattern; + } + + public List<String> getEnums() { + return enums; + } + + public void setEnums(List<String> enums) { + this.enums = enums; + } + + public String transformToKey(String className, String simpleName, String name, String ordinal) { + String result = pattern; + result = result.replace("@CLASS_NAME@", className); + result = result.replace("@CLASS_SIMPLE_NAME@", simpleName); + result = result.replace("@NAME@", name); + result = result.replace("@ORDINAL@", ordinal); + return result; + } + + public String transformToMessage() { + String result = ""; + StringTokenizer stringTokenizer = new StringTokenizer(pattern, "@"); + while (stringTokenizer.hasMoreTokens()) { + if (!result.isEmpty()) { + result += " + "; + } + String token = stringTokenizer.nextToken(); + switch (token) { + case "CLASS_SIMPLE_NAME": + result += "e.getClass().getSimpleName()"; + break; + case "CLASS_NAME": + result += "e.getClass().getName()"; + break; + case "NAME": + result += "e.name()"; + break; + case "ORDINAL": + result += "e.ordinal()"; + break; + default: + result += "\"" + token + "\""; + + } + } + if (result.endsWith("+")) { + result = result.substring(0, result.length() - 1); + } + return result; + } + + } } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.