001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2014 ForgeRock AS.
015 */
016
017package org.forgerock.maven.plugins.xcite;
018
019import org.apache.maven.plugin.AbstractMojo;
020import org.apache.maven.plugin.MojoExecutionException;
021import org.apache.maven.plugin.MojoFailureException;
022import org.apache.maven.plugins.annotations.LifecyclePhase;
023import org.apache.maven.plugins.annotations.Mojo;
024import org.apache.maven.plugins.annotations.Parameter;
025import org.forgerock.maven.plugins.xcite.utils.FileUtils;
026
027import java.io.File;
028import java.io.IOException;
029
030/**
031 * Copy quotes from source text files into target text files.
032 */
033@Mojo(name = "cite", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
034public class XCiteMojo extends AbstractMojo {
035
036    /**
037     * Whether to escape XML characters in quotes.
038     */
039    @Parameter (defaultValue = "false")
040    private boolean escapeXml;
041
042    /**
043     * Filter strings specifying files with citations to include.
044     */
045    @Parameter
046    private String[] includes;
047
048    /**
049     * Filter strings specifying files to exclude.
050     */
051    @Parameter
052    private String[] excludes;
053
054    /**
055     * Indent quotes this number of spaces from the left margin.
056     */
057    @Parameter (defaultValue = "0")
058    private int reindent;
059
060    /**
061     * Output directory for files with quotes.
062     */
063    @Parameter (defaultValue = "${project.build.directory}/xcite")
064    private File outputDirectory;
065
066    /**
067     * Source directory for files with citations.
068     */
069    @Parameter (defaultValue = "${basedir}/src/main")
070    private File sourceDirectory;
071
072    /**
073     * Replace citations with quotes in included files,
074     * writing the resulting files in the output directory.
075     *
076     * @throws MojoExecutionException   Could not create output directory.
077     * @throws MojoFailureException     Failed to perform replacements.
078     */
079    @Override
080    public void execute() throws MojoExecutionException, MojoFailureException {
081
082        if (!outputDirectory.exists()) {
083            if (!outputDirectory.mkdirs()) {
084                throw new MojoExecutionException(
085                        "Failed to create output directory: "
086                                + outputDirectory.getPath());
087            }
088        }
089
090        String[] files = FileUtils.getIncludedFiles(sourceDirectory, includes, excludes);
091        Resolver resolver =
092                new Resolver(outputDirectory, escapeXml, reindent, true);
093        try {
094            resolver.resolve(sourceDirectory, files);
095        } catch (IOException e) {
096            throw new MojoFailureException(e.getMessage());
097        }
098    }
099}