branch develop updated (38d2f08 -> a20c87f)
This is an automated email from the git hooks/post-receive script. New change to branch develop in repository i18n. See https://gitlab.nuiton.org/nuiton/i18n.git from 38d2f08 [jgitflow-maven-plugin]Updating develop poms back to pre merge state new 412c33f Add new mojo to generate I18nEnumHelper new a20c87f Fixes #4006 Merge branch 'feature/4006' into develop The 2 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit a20c87fb0b7c56cd197e026e00cd2e8d2c7ecef7 Merge: 38d2f08 412c33f Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 28 16:25:07 2016 +0200 Fixes #4006 Merge branch 'feature/4006' into develop commit 412c33f70789bb5b3fd0f13d675ff624a188f70b Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 28 16:20:27 2016 +0200 Add new mojo to generate I18nEnumHelper Summary of changes: .../i18n/plugin/GenerateI18nEnumHelperMojo.java | 129 +++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/GenerateI18nEnumHelperMojo.java -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
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 412c33f70789bb5b3fd0f13d675ff624a188f70b Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 28 16:20:27 2016 +0200 Add new mojo to generate I18nEnumHelper --- .../i18n/plugin/GenerateI18nEnumHelperMojo.java | 129 +++++++++++++++++++++ 1 file changed, 129 insertions(+) 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 new file mode 100644 index 0000000..22b0e41 --- /dev/null +++ b/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/GenerateI18nEnumHelperMojo.java @@ -0,0 +1,129 @@ +package org.nuiton.i18n.plugin; + +import com.google.common.base.Charsets; +import com.google.common.io.Files; +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.nuiton.util.FileUtil; + +import java.io.BufferedWriter; +import java.io.File; +import java.util.Date; +import java.util.List; + +/** + * Generate a i18n enum class helper. + * + * Created on 28/08/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.5.3 + */ +@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; + + /** + * To set the package fully qualified name of the generated class. + * + * By default, will use groupId.artifactId (with {@code -} replaced by {@code .}). + */ + @Parameter(property = "i18n.packageName") + protected String packageName; + + /** + * Name of the generated class. + */ + @Parameter(property = "i18n.className", defaultValue = "I18nEnumHelper", required = true) + protected String className; + + /** + * The root directory where to generated. + */ + @Parameter(property = "i18n.outputdirectory", defaultValue = "${basedir}/target/generated-sources/java", required = true) + protected File outputdirectory; + + /** + * List of enums to scan to generate i18n keys. + */ + @Parameter(property = "i18n.enums", required = true) + protected List<String> enums; + + @Override + protected void doAction() throws Exception { + + if (packageName == null) { + + packageName = getProject().getGroupId() + "." + getProject().getArtifactId().replaceAll("-", "."); + + } + File directory = FileUtil.getFileFromFQN(outputdirectory, packageName); + + File file = new File(directory, className + ".java"); + Files.createParentDirs(file); + + getLog().info("Will generate to " + file); + + BufferedWriter writer = Files.newWriter(file, Charsets.UTF_8); + try { + + writer.write("// Generated by " + getClass().getName() + " at " + new Date() + "\n"); + writer.write("package " + packageName + ";\n"); + writer.write("\n"); + writer.write("import java.util.Locale;\n"); + writer.write("\n"); + writer.write("import static org.nuiton.i18n.I18n.l;\n"); + writer.write("import static org.nuiton.i18n.I18n.n;\n"); + writer.write("import static org.nuiton.i18n.I18n.t;\n"); + writer.write("\n"); + writer.write("\n"); + writer.write("public class " + className + " {\n"); + writer.write("\n"); + 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(" Class<? extends Enum> aClass = e.getClass();\n"); + writer.write(" return \"" + prefix + "\" + aClass.getName() + \".\" + aClass.getSimpleName() + \".\" + e.name();\n"); + writer.write(" }\n"); + writer.write("\n"); + writer.write(" static {\n\n"); + + for (String anEnumType : enums) { + + Class aClass = Class.forName(anEnumType); + if (!aClass.isEnum()) { + throw new IllegalStateException("Type " + aClass.getName() + " is not an enum."); + } + + getLog().info("Scan enum: " + aClass.getName()); + for (Object o : aClass.getEnumConstants()) { + Enum e = (Enum) o; + writer.write(" n(\"" + prefix + aClass.getName() + "." + e.name() + "\");\n"); + } + writer.write("\n"); + + } + + writer.write(" }\n"); + writer.write("}\n"); + + writer.close(); + } finally { + IOUtils.closeQuietly(writer); + } + + } +} -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
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 a20c87fb0b7c56cd197e026e00cd2e8d2c7ecef7 Merge: 38d2f08 412c33f Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Aug 28 16:25:07 2016 +0200 Fixes #4006 Merge branch 'feature/4006' into develop .../i18n/plugin/GenerateI18nEnumHelperMojo.java | 129 +++++++++++++++++++++ 1 file changed, 129 insertions(+) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm