
org.nuiton.i18n.plugin.parser.impl.ParseValidationFieldsMojo Maven / Gradle / Ivy
/*
* #%L
* I18n :: Maven Plugin
* %%
* Copyright (C) 2007 - 2017 Code Lutin, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser 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 Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package org.nuiton.i18n.plugin.parser.impl;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.opensymphony.xwork2.util.DomHelper;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.xpath.XPath;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.nuiton.i18n.plugin.parser.I18nSourceEntry;
import org.nuiton.i18n.plugin.parser.SourceEntry;
import org.nuiton.io.FileUpdater;
import org.nuiton.io.MirroredFileUpdater;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* Find i18n keys from xworks xml validation fields files.
*
* Note: this goal must always be invoked before the {@code process-resources}
* phase, otherwise all files will be considered as uptodate.
*
* @author Tony Chemit - [email protected]
*/
@Mojo(name = "parse-validation-fields", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
public class ParseValidationFieldsMojo extends I18nParseXmlMojoSupport {
public static final String DEFAULT_INCLUDES = "**/*-validation.xml";
/** Root directory of the default entry. */
@Parameter(property = "i18n.defaultBasedir", defaultValue = "${basedir}/src/main/resources", required = true)
private File defaultBasedir;
/**
* Default included files to process (ant-like expression).
*
* Note: default value is **\/**-validation.xml
*/
@Parameter(property = "i18n.defaultIncludes", defaultValue = DEFAULT_INCLUDES, required = true)
private String defaultIncludes;
/**
* Always use the local xworks dtd to increase performance.
*
* @since 1.6.0
*/
@Parameter(property = "i18n.useLocalResolver", defaultValue = "true")
private boolean useLocalResolver;
/**
* Defines the file name of the getter where to put detected i18n keys
* while getter phase.
*
* @since 2.0
*/
@Parameter(property = "i18n.outputGetter", defaultValue = "validation-fields.getter", required = true)
private String outputGetter;
private Map dtdMappings;
/**
* Prefix to add to generated i18n keys.
*/
@Parameter(property = "i18n.validationFieldPrefix", required = true)
private String validationFieldPrefix;
/**
* Map of inheritance rules for some fields.
*
* Keys are fully qualified name of super type, values are field names.
*
* If a type get a match for a field name, then use the super type in the key. The first matching type is used, so
* order of types is meaningful.
*/
@Parameter
private Map inheritanceMapping;
private Set> inheritanceTypes;
private Multimap, String> inheritanceRealMapping;
private ClassLoader classLoader;
public ParseValidationFieldsMojo() {
dtdMappings = new HashMap<>();
dtdMappings.put("-//Apache Struts//XWork Validator 1.0//EN", "xwork-validator-1.0.dtd");
dtdMappings.put("-//Apache Struts//XWork Validator 1.0.2//EN", "xwork-validator-1.0.2.dtd");
dtdMappings.put("-//Apache Struts//XWork Validator 1.0.3//EN", "xwork-validator-1.0.3.dtd");
}
@Override
public void init() throws Exception {
super.init();
classLoader = initClassLoader(getProject(),
buildOutputDirectory,
false,
false,
false,
true,
true);
inheritanceRealMapping = HashMultimap.create();
if (inheritanceMapping != null) {
for (Map.Entry entry : inheritanceMapping.entrySet()) {
Class> aClass = classLoader.loadClass(entry.getKey());
for (String fieldName : entry.getValue().split(",")) {
inheritanceRealMapping.put(aClass, fieldName);
}
}
}
inheritanceTypes = inheritanceRealMapping.keySet();
}
@Override
public String[] getDefaultIncludes() {
return new String[]{defaultIncludes};
}
@Override
public String[] getDefaultExcludes() {
return I18nSourceEntry.EMPTY_STRING_ARRAY;
}
@Override
public File getDefaultBasedir() {
return defaultBasedir;
}
@Override
public FileUpdater newFileUpdater(SourceEntry entry) {
return new MirroredFileUpdater("", "", entry.getBasedir(), buildOutputDirectory) {
@Override
public File getMirrorFile(File f) {
String file =
f.getAbsolutePath().substring(prefixSourceDirecotory);
return new File(destinationDirectory + File.separator + file);
}
};
}
@Override
protected String getOutGetter() {
return outputGetter;
}
@Override
protected String getCoreRuleFile() {
return "validation-fields.rules";
}
protected XmlFileParser newXmlFileParser(File basedir, XPath xpath, DocumentBuilder builder) {
return new XmlFileParser(getLog(),
encoding,
getAcceptPattern(),
rules,
xpath,
builder,
namespaces,
isVerbose()) {
private final Path rootDirectory = basedir.toPath();
private final Map fileTypeMapping = new TreeMap<>();
@Override
protected Document fileToDocument(File fileToProcess) throws SAXException, IOException {
InputSource in = new InputSource(FileUtils.openInputStream(fileToProcess));
return DomHelper.parse(in, dtdMappings);
}
@Override
public String extract(File file, String i18nString) {
Class type = getFileType(file);
for (Class> aClass : inheritanceTypes) {
if (aClass.isAssignableFrom(type) && inheritanceRealMapping.containsEntry(aClass, i18nString)) {
type = aClass;
break;
}
}
String i18nKey = validationFieldPrefix + type.getSimpleName() + "." + i18nString.trim();
if (isVerbose()) {
getLog().info(String.format("%s = %s", i18nString, i18nKey));
}
return i18nKey;
}
private Class getFileType(File file) {
Class type = fileTypeMapping.get(file);
if (type == null) {
Path relativePath = rootDirectory.relativize(file.toPath());
StringBuilder fullyQualifiedNameBuilder = new StringBuilder();
for (Path path : relativePath) {
fullyQualifiedNameBuilder.append(".").append(path.getName(0));
}
String fullyQualifiedName = fullyQualifiedNameBuilder.substring(1);
int lastIndex = fullyQualifiedName.indexOf("-");
fullyQualifiedName = fullyQualifiedName.substring(0, lastIndex);
try {
type = classLoader.loadClass(fullyQualifiedName);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Can't find type: " + fullyQualifiedName);
}
fileTypeMapping.put(file, type);
}
return type;
}
};
}
}