org.forgerock.doc.maven.SiteBuildMojo.java Source code

Java tutorial

Introduction

Here is the source code for org.forgerock.doc.maven.SiteBuildMojo.java

Source

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * If applicable, add the following below this MPL 2.0 HEADER, replacing
 * the fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *     Portions Copyright [yyyy] [name of copyright owner]
 *
 *     Copyright 2012-2014 ForgeRock AS
 *
 */

package org.forgerock.doc.maven;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.twdata.maven.mojoexecutor.MojoExecutor;

/**
 * Layout built documentation. The resulting documentation set is found under
 * {@code ${project.build.directory}/site/doc}.
 *
 * @Checkstyle:ignoreFor 2
 * @goal layout
 * @phase site
 */
public class SiteBuildMojo extends AbstractBuildMojo {
    /**
     * File system directory for site content, relative to the build directory.
     *
     * @parameter default-value="site" property="siteDirectory"
     * @required
     */
    private String siteDirectory;

    /**
     * File system directory for site content, relative to the build directory.
     *
     * @return {@link #siteDirectory}
     */
    public final File getSiteDirectory() {
        return new File(getBuildDirectory(), siteDirectory);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final void execute() throws MojoExecutionException {
        Executor exec = new Executor();
        getLog().info("Laying out site...");
        exec.layout();

        getLog().info("Adding .htaccess file...");
        String layoutDir = getSiteDirectory().getPath() + File.separator + "doc";
        File htaccess = new File(getBuildDirectory().getPath() + File.separator + ".htaccess");
        FileUtils.deleteQuietly(htaccess);
        try {
            FileUtils.copyURLToFile(getClass().getResource("/.htaccess"), htaccess);
            HTMLUtils.addHtaccess(layoutDir, htaccess);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to copy .htaccess: " + e.getMessage());
        }

        getLog().info("Add redirect to docs.html under layout directory...");
        try {
            File file = new File(
                    getSiteDirectory().getPath() + File.separator + "doc" + File.separator + "index.html");
            if (!file.exists()) {
                String redirect = IOUtils.toString(getClass().getResourceAsStream("/index.html"), "UTF-8");
                redirect = redirect.replaceAll("PROJECT", getProjectName()).replaceAll("LOWERCASE",
                        getProjectName().toLowerCase());
                FileUtils.write(file, redirect, "UTF-8");
            }
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to copy redirect file: " + e.getMessage());
        }

        // Test links in document source, and generate a report.
        if (!runLinkTester().equalsIgnoreCase("false")) {
            getLog().info("Running linktester...");
            exec.testLinks();
        }
    }

    /**
     * Enclose methods to run plugins.
     */
    class Executor extends MojoExecutor {
        /**
         * Returns element specifying built documents to copy to the site
         * directory. Man pages are not currently copied anywhere.
         *
         * @return Compound element specifying built documents to copy
         * @throws MojoExecutionException Something went wrong getting document names.
         */
        private MojoExecutor.Element getResources() throws MojoExecutionException {

            ArrayList<MojoExecutor.Element> r = new ArrayList<MojoExecutor.Element>();

            Set<String> docNames = DocUtils.getDocumentNames(getDocbkxSourceDirectory(), getDocumentSrcName());
            if (docNames.isEmpty()) {
                throw new MojoExecutionException("No document names found.");
            }

            List<String> formats = getOutputFormats();

            if (formats.contains("epub")) {
                String epubDir = FilenameUtils.separatorsToUnix(getDocbkxOutputDirectory().getPath()) + "/epub/";
                r.add(element(name("resource"), element(name("directory"), epubDir),
                        element(name("includes"), element(name("include"), "**/*.epub"))));
            }

            if (formats.contains("html")) {
                String htmlDir = FilenameUtils.separatorsToUnix(getDocbkxOutputDirectory().getPath()) + "/html/";
                r.add(element(name("resource"), element(name("directory"), htmlDir)));
            }

            if (formats.contains("pdf")) {
                String pdfDir = FilenameUtils.separatorsToUnix(getDocbkxOutputDirectory().getPath()) + "/pdf/";
                r.add(element(name("resource"), element(name("directory"), pdfDir),
                        element(name("includes"), element(name("include"), "**/*.pdf"))));
            }

            if (formats.contains("rtf")) {
                String rtfDir = FilenameUtils.separatorsToUnix(getDocbkxOutputDirectory().getPath()) + "/rtf/";
                r.add(element(name("resource"), element(name("directory"), rtfDir),
                        element(name("includes"), element(name("include"), "**/*.rtf"))));
            }

            // The webhelp is handled separately.

            return element("resources", r.toArray(new Element[r.size()]));
        }

        /**
         * Lay out docs in site directory under <code>target/site/doc</code>.
         *
         * @throws MojoExecutionException Problem during execution.
         */
        public void layout() throws MojoExecutionException {
            if (getSiteDirectory() == null) {
                throw new MojoExecutionException("<siteDirectory> must be set.");
            }

            String siteDocDirectory = FilenameUtils.separatorsToUnix(getSiteDirectory().getPath()) + "/doc";
            executeMojo(
                    plugin(groupId("org.apache.maven.plugins"), artifactId("maven-resources-plugin"),
                            version(getResourcesVersion())),
                    goal("copy-resources"),
                    configuration(element(name("encoding"), "UTF-8"),
                            element(name("outputDirectory"), siteDocDirectory), getResources()),
                    executionEnvironment(getProject(), getSession(), getPluginManager()));

            // The webhelp directory needs to be copied in its entirety
            // to avoid overwriting other HTML.
            if (getOutputFormats().contains("webhelp")) {

                // 2.0.15 does not allow <webhelpBaseDir> to be set,
                // so the output location is hard-coded for now.
                String webHelpDir = FilenameUtils
                        .separatorsToUnix(getDocbkxOutputDirectory().getPath() + "/webhelp");
                executeMojo(
                        plugin(groupId("org.apache.maven.plugins"), artifactId("maven-resources-plugin"),
                                version(getResourcesVersion())),
                        goal("copy-resources"),
                        configuration(element(name("encoding"), "UTF-8"),
                                element(name("outputDirectory"), siteDocDirectory + "/webhelp"),
                                element(name("resources"),
                                        element(name("resource"), element(name("directory"), webHelpDir),
                                                element(name("excludes"),
                                                        element(name("exclude"), "**/*.target.db"))))),
                        executionEnvironment(getProject(), getSession(), getPluginManager()));
            }
        }

        /**
         * Test links in source documentation.
         *
         * @throws MojoExecutionException Problem during execution.
         */
        void testLinks() throws MojoExecutionException {
            String include = "**/" + getDocumentSrcName();

            if (getJCiteOutputDirectory().exists()) {
                include = getJCiteOutputDirectory().getPath() + "/" + include;
            } else if (doUseFilteredSources()) {
                include = getFilteredDocbkxSourceDirectory().getPath() + "/" + include;
            }

            final String log = getDocbkxOutputDirectory().getPath() + File.separator + "linktester.err";

            final String jiraUrlPattern = "^https://bugster.forgerock.org/jira/browse/OPEN(AM|ICF|IDM|IG|DJ)-[0-9]{1,4}$";
            final String rfcUrlPattern = "^http://tools.ietf.org/html/rfc[0-9]+$";

            executeMojo(
                    plugin(groupId("org.forgerock.maven.plugins"), artifactId("linktester-maven-plugin"),
                            version(getLinkTesterVersion())),
                    goal("check"),
                    configuration(element(name("includes"), element(name("include"), include)),
                            element(name("validating"), "true"), element(name("skipUrls"), skipLinkCheck()),
                            element(name("xIncludeAware"), "true"), element(name("failOnError"), "false"),
                            element(name("outputFile"), log),
                            element(name("skipUrlPatterns"), element(name("skipUrlPattern"), jiraUrlPattern),
                                    element(name("skipUrlPattern"), rfcUrlPattern))),
                    executionEnvironment(getProject(), getSession(), getPluginManager()));
        }
    }
}