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

fr.ird.observe.maven.plugins.toolbox.GenerateI18nValidatorFieldsMojo Maven / Gradle / Ivy

package fr.ird.observe.maven.plugins.toolbox;

/*-
 * #%L
 * ObServe Toolkit :: Maven plugin
 * %%
 * Copyright (C) 2008 - 2018 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.nuiton.plugin.PluginHelper;
import org.nuiton.io.SortedProperties;

/**
 * Pour générer les clefs i18n des champs utilisés dans les validateurs.
 * 

* Created on 31/08/16. * * @author Tony Chemit - [email protected] * @since 5.0 */ @Mojo(name = "generate-i18n-validator-fields", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresDependencyResolution = ResolutionScope.COMPILE) class GenerateI18nValidatorFieldsMojo extends GenerateValidatorMojoSupport { /** * Un flag pour activer le mode verbeux. * * @since 1.0.0 */ @Parameter(property = "generateI18nValidatorFields.verbose", defaultValue = "${maven.verbose}") private boolean verbose; /** * A flag to skip the goal. * * @since 1.0.0 */ @Parameter(property = "generateI18nValidatorFields.skip", defaultValue = "false") private boolean skip; /** * To set the package fully qualified name of the generated class. *

* By default, will use groupId.artifactId (with {@code -} replaced by {@code .}). */ @Parameter(property = "generateI18nValidatorFields.packageName") private String packageName; /** * Name of the generated class. */ @Parameter(property = "generateI18nValidatorFields.className", defaultValue = "I18nValidatorHelper", required = true) private String className; /** * Prefix to add to generated i18n keys. */ @Parameter(property = "generateI18nValidatorFields.prefix") private String prefix; /** * The root directory where to generated. */ @Parameter(property = "generateI18nValidatorFields.outputDirectory", defaultValue = "${basedir}/target/generated-sources/java", required = true) private File outputDirectory; @Parameter private boolean rename; @Parameter private Map inheritanceMapping; @Override protected Path createOutputFile() throws IOException { if (packageName == null) { packageName = getProject().getGroupId() + "." + getProject().getArtifactId().replaceAll("-", "."); } Path directory = PluginHelper.getFile(outputDirectory, packageName.trim().split("\\.")).toPath(); Files.createDirectories(directory); return directory.resolve(className + ".java"); } @Override protected boolean isSkip() { return skip; } @Override public void doAction() throws Exception { if (isVerbose()) { getLog().info("project = " + getProject()); } getLog().info("Generate to " + getOutputFile()); List compileSourceRoots = getProject().getTestCompileSourceRoots(); if (!compileSourceRoots.contains(outputDirectory.getAbsolutePath())) { getLog().info("Add to test compile source root: " + outputDirectory); getProject().addTestCompileSourceRoot(outputDirectory.getAbsolutePath()); } Multimap, String> inheritanceRealMapping = HashMultimap.create(); if (inheritanceMapping != null) { for (Map.Entry entry : inheritanceMapping.entrySet()) { Class aClass = Class.forName(entry.getKey()); for (String fieldName : entry.getValue().split(",")) { inheritanceRealMapping.put(aClass, fieldName); } } } Set> inheritanceTypes = inheritanceRealMapping.keySet(); Set fields = new TreeSet<>(); Map renameMapping = new TreeMap<>(); for (ValidatorsCache.ValidatorInfo validator : getValidators()) { Class type = validator.getType(); Map inheritanceLocalMapping = new TreeMap<>(); for (Class aClass : inheritanceTypes) { if (aClass.isAssignableFrom(type)) { String prefix = aClass.getSimpleName() + "."; for (String fieldName : inheritanceRealMapping.get(aClass)) { inheritanceLocalMapping.put(fieldName, prefix); } } } String dtoType = type.getSimpleName() + "."; for (String field : validator.getFields()) { String key = inheritanceLocalMapping.getOrDefault(field, dtoType) + field; fields.add(key); renameMapping.put(prefix + key, prefix + field); } } getLog().info(fields.size() + " validator files(s) detected."); generate(packageName, className, prefix, fields, getOutputFile()); if (rename) { Path i18nPath = getProject().getBasedir().toPath().resolve("src").resolve("main").resolve("resources").resolve("i18n"); ImmutableSet locales = ImmutableSet.of(Locale.FRANCE, Locale.UK, new Locale("es", "ES")); Collection oldKeys = renameMapping.values(); for (Locale locale : locales) { Path localFile = i18nPath.resolve(String.format("validation_%s_%s.properties", locale.getLanguage(), locale.getCountry())); getLog().info("Will rename in " + localFile); Properties p = new Properties(); try (BufferedReader reader = Files.newBufferedReader(localFile, StandardCharsets.UTF_8)) { p.load(reader); } SortedProperties out = new SortedProperties(); for (Map.Entry entry : renameMapping.entrySet()) { String newKey = entry.getKey(); String oldKey = entry.getValue(); String value = p.getProperty(oldKey); out.put(newKey, value); } for (String oldKey : p.stringPropertyNames()) { if (!oldKeys.contains(oldKey)) { out.put(oldKey, p.getProperty(oldKey)); } } try (BufferedWriter writer = Files.newBufferedWriter(localFile, StandardCharsets.UTF_8)) { out.store(writer, "generated by " + getClass().getName()); } } } } @Override public boolean isVerbose() { return verbose; } @Override public void setVerbose(boolean verbose) { this.verbose = verbose; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy