ru.histone.HistoneOptimizeAstMojo.java Source code

Java tutorial

Introduction

Here is the source code for ru.histone.HistoneOptimizeAstMojo.java

Source

/**
 *    Copyright 2014 MegaFon
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package ru.histone;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
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.DirectoryScanner;
import ru.histone.optimizer.ASTTreeElementsCounter;
import ru.histone.optimizer.OptimizationTypes;
import ru.histone.utils.StringUtils;

import java.io.File;
import java.io.FileReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Goal which optimizes histone template AST files
 */
@Mojo(name = "optimize-ast", defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
public class HistoneOptimizeAstMojo extends AbstractMojo {

    @Parameter(defaultValue = "${basedir}", property = "outputDir", required = true)
    public String baseDir;

    @Parameter(required = true)
    private String[] includes;

    @Parameter(defaultValue = "", required = true)
    public String[] optimizationTypes;

    public void execute() throws MojoExecutionException {
        try {

            getLog().info(MessageFormat.format("Running DirectoryScanner with params: baseDir={0}, includes=[{1}]",
                    baseDir, StringUtils.join(includes, ", ")));
            DirectoryScanner scanner = new DirectoryScanner();
            scanner.setBasedir(baseDir);
            scanner.setIncludes(includes);
            scanner.scan();

            List<OptimizationTypes> optimizationTypeses = new ArrayList<OptimizationTypes>();
            for (String optimizationType : optimizationTypes) {
                optimizationTypeses.add(OptimizationTypes.valueOf(optimizationType));
            }
            OptimizationTypes[] array = optimizationTypeses
                    .toArray(new OptimizationTypes[optimizationTypeses.size()]);
            getLog().info(MessageFormat.format("Optimizations to be run: [{0}]",
                    "" + StringUtils.join(optimizationTypes, ", ")));

            String[] includedFiles = scanner.getIncludedFiles();
            getLog().info(MessageFormat.format("Files found:\n{0}", StringUtils.join(includedFiles, "\n")));

            HistoneBuilder builder = new HistoneBuilder();
            Histone histone = builder.build();
            ObjectMapper jackson = new ObjectMapper();
            JsonFactory jsonFactory = new JsonFactory();

            for (String includedFile : includedFiles) {
                ArrayNode ast = histone.parseTemplateToAST(new FileReader(new File(baseDir, includedFile)));

                ObjectNode context = builder.getNodeFactory().jsonObject();

                int astCount1 = ASTTreeElementsCounter.count(ast);
                ArrayNode optimizedAst = histone.optimizeAST(ast, context, array);
                int astCount2 = ASTTreeElementsCounter.count(ast);

                getLog().info(MessageFormat.format("Processing {0}, before: {1}, after: {2}", includedFile,
                        astCount1, astCount2));

                JsonGenerator jsonGenerator = jsonFactory.createGenerator(new File(baseDir, includedFile + ".ast"),
                        JsonEncoding.UTF8);
                jackson.writeTree(jsonGenerator, optimizedAst);
            }
        } catch (Exception e) {
            throw new MojoExecutionException("Error parsing templates to AST", e);
        }

    }
}