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

org.nuiton.i18n.plugin.parser.I18nParseMojoSupport 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;

import java.io.File;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.maven.plugins.annotations.Parameter;
import org.nuiton.i18n.plugin.I18nMojoSupport;
import org.nuiton.i18n.spi.GetterFile;
import org.nuiton.io.FileUpdater;
import org.nuiton.plugin.PluginHelper;

/**
 * Abstract implementation for parsing goal.
 *
 * @author Tony Chemit - [email protected]
 */
public abstract class I18nParseMojoSupport extends I18nMojoSupport implements I18nParseMojoConfiguration {

    /** @return the outGetter to use for the instance (java.getter,...) */
    protected abstract String getOutGetter();

    /** @return the default includes to add to directory scanner */
    protected abstract String[] getDefaultIncludes();

    /** @return the default excludes to add to directory scanner */
    protected abstract String[] getDefaultExcludes();

    /** @return the default src directory to use in directory scanner */
    protected abstract File getDefaultBasedir();

    /**
     * @param basedir       basedir of files to scan
     * @param acceptPattern optional pattern to accept incoming keys
     * @return a new file parser to be used in the parser consumer parserExecutor
     * @since 1.2
     */
    public abstract FileParser newFileParser(File basedir, Pattern acceptPattern);

    /**
     * @param entry the incoming source entry to attach to the file updater
     * @return a new file updater to detects files to treate
     */
    public abstract FileUpdater newFileUpdater(SourceEntry entry);

    /**
     * Strict mode to only keep in user i18n detected i18n keys and remove obsolete keys.
     * 

* Note : By default not active. Use this with care since it can * delete keys. Moreover if this flag is activated, then all files will be parsed. */ @Parameter(property = "i18n.strictMode", defaultValue = "false") private boolean strictMode; /** To treat default entry offered by the mojo. */ @Parameter(property = "i18n.treatDefaultEntry", defaultValue = "true") private boolean treatDefaultEntry; /** Source entries (src+includes+excludes) to process. */ @Parameter(property = "i18n.entries") private I18nSourceEntry[] entries; /** * Flag to display touched files while parsing. *

* Note: the value will be always {@code true} if {@link #verbose} is * set at {@code true}. * * @since 0.9 */ @Parameter(property = "i18n.showTouchedFiles", defaultValue = "${maven.verbose}") private boolean showTouchedFiles; /** * To force scanning of all sources. * * @since 1.2 */ @Parameter(property = "i18n.force", defaultValue = "false") private boolean force; /** * A regex pattern to accept incoming keys. *

* Only incoming keys which match the pattern will be kept. * * @since 2.5 */ @Parameter(property = "i18n.acceptKeyFormat") private String acceptKeyFormat; protected Pattern acceptPattern; private GetterFile getterFile; private long t0; private ParserExecutor parserExecutor; boolean isForce() { return force; } @Override public void init() throws Exception { super.init(); t0 = System.nanoTime(); getterFile = new GetterFile(gettersDirectory, getOutGetter()); createDirectoryIfNecessary(outputDirectory); // check there is something to treate if ((entries == null || entries.length == 0) && !treatDefaultEntry) { // nothing to do throw new IllegalStateException( "No entry defined and treatDefaultEntry is false, " + "will skip the goal."); } if (verbose && entries != null && entries.length > 0) { if (getLog().isInfoEnabled()) { getLog().info("detected entries : " + entries.length); for (SourceEntry e : entries) { getLog().info(e.toString() + ", specific goal ? " + e.getSpecificGoal()); } } } if (acceptKeyFormat != null) { acceptPattern = Pattern.compile(acceptKeyFormat); } parserExecutor = new ParserExecutor(this); } @Override protected void doAction() throws Exception { if (!silent && strictMode) { getLog().info("config - strictMode is on (all files will be parsed)."); } if (!silent && force) { getLog().info("config - force is on (all files will be parsed)."); } if (treatDefaultEntry) { addDefaultEntry(); } for (I18nSourceEntry entry : entries) { boolean skip = entry.init(this); if (skip) { if (!silent && verbose) { getLog().info("skip [" + entry + "] - " + entry.getSkipMessage()); } continue; } // launch parser for found files String[] files = entry.getFiles(); if (!silent) { getLog().info("start entry " + entry.toString()); getLog().info(files.length + " file(s) to process (among " + entry.getFoudFiles() + " files)"); } for (String file1 : files) { String fileName = entry.getBasedir().getAbsolutePath() + File.separator + file1; File file = new File(fileName); parserExecutor.addFile(newFileParser(entry.getBasedir(), acceptPattern), file); } } if (getLog().isDebugEnabled()) { getLog().debug("ask to terminate " + parserExecutor); } // all files are send to parserExecutor, we ask termination of parserExecutor. // this termination treat all sending file before really stop parserExecutor.terminatesAndWaits(); List treatedFiles = parserExecutor.getTreatedFiles(); List touchedFiles = parserExecutor.getTouchedFiles(); if (treatedFiles.isEmpty()) { if (!silent) { getLog().info("Nothing was parsed - all files are up to date."); } } else { if (showTouchedFiles) { for (File f : touchedFiles) { getLog().info("touch " + f); } } if (!silent) { int i = touchedFiles.size(); int max = treatedFiles.size(); getLog().info(getLogEntry( String.format("Parsing is done. [treated file(s) : %d/%d]", i, max), max, t0)); } getterFile.store(); } parserExecutor.clear(); } @Override public boolean isShowTouchedFiles() { return showTouchedFiles; } @Override public GetterFile getGetterFile() { return getterFile; } boolean isStrictMode() { return strictMode; } /** * Add the default entry to entries given in configuration. *

* This is a convinient method to simplify the configuration of the plugin. */ private void addDefaultEntry() { if (verbose) { getLog().info("add default entry"); } boolean hasEntries = entries != null && entries.length > 0; I18nSourceEntry[] tmp = new I18nSourceEntry[hasEntries ? entries.length + 1 : 1]; if (hasEntries) { System.arraycopy(entries, 0, tmp, 0, entries.length); } tmp[tmp.length - 1] = new I18nSourceEntry(); entries = tmp; } /** * Construit une chaine de log formatée. * * @param msg le prefix du message * @param nbFiles le nombre de fichiers actuellement traités * @param all le temps de traitement de tous les fichiers * @return la chaine de log formatée */ private static String getLogEntry(String msg, int nbFiles, long all) { long now = System.nanoTime(); String s = msg; if (all > 0) { s += String.format("(total time:%s)", PluginHelper.convertTime(now - all)); } if (nbFiles > 0) { s += String.format(" ( ~ %s / file)", PluginHelper.convertTime((now - all) / nbFiles)); } return s; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy