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 2012-2014 ForgeRock AS
015 */
016
017package org.forgerock.doc.maven.build;
018
019import org.apache.maven.plugin.MojoExecutionException;
020import org.forgerock.doc.maven.AbstractDocbkxMojo;
021import org.twdata.maven.mojoexecutor.MojoExecutor;
022
023import java.io.File;
024import java.util.ArrayList;
025
026/**
027 * Build man page output.
028 */
029public class Manpage {
030
031    /**
032     * The Mojo that holds configuration and related methods.
033     */
034    private AbstractDocbkxMojo m;
035
036    /**
037     * The Executor to run docbkx-tools.
038     */
039    private final Executor executor;
040
041    /**
042     * Constructor setting the Mojo that holds the configuration.
043     *
044     * @param mojo The Mojo that holds the configuration.
045     */
046    public Manpage(final AbstractDocbkxMojo mojo) {
047        m = mojo;
048        this.executor = new Executor();
049    }
050
051    /**
052     * Build documents from DocBook XML sources.
053     *
054     * @throws MojoExecutionException Failed to build output.
055     */
056    public void execute() throws MojoExecutionException {
057        executor.prepareOlinkDB();
058        executor.build();
059    }
060
061    /**
062     * Enclose methods to run plugins.
063     */
064    class Executor extends MojoExecutor {
065
066        /**
067         * Prepare olink target database from DocBook XML sources.
068         *
069         * @throws MojoExecutionException Failed to build target database.
070         */
071        void prepareOlinkDB() throws MojoExecutionException {
072            // No plans to implement this for man pages.
073        }
074
075        /**
076         * Build documents from DocBook XML sources.
077         *
078         * @throws MojoExecutionException Failed to build the output.
079         */
080        void build() throws MojoExecutionException {
081            ArrayList<Element> cfg = new ArrayList<MojoExecutor.Element>();
082            cfg.addAll(m.getBaseConfiguration());
083            cfg.add(element(name("includes"), "*/" + m.getDocumentSrcName()));
084            cfg.add(element(name("manpagesCustomization"), m.path(m.getManpagesCustomization())));
085
086            File manPageOutputDir = new File(m.getDocbkxOutputDirectory(), "manpages");
087            cfg.add(element(name("targetDirectory"), m.path(manPageOutputDir)));
088
089            executeMojo(
090                    plugin(
091                            groupId("com.agilejava.docbkx"),
092                            artifactId("docbkx-maven-plugin"),
093                            version(m.getDocbkxVersion())),
094                    goal("generate-manpages"),
095                    configuration(cfg.toArray(new Element[cfg.size()])),
096                    executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
097
098            // Man page generation replaces spaces in path name with underscores.
099            // If necessary, this is corrected during post-processing.
100            m.getLog().info("Man page output directory: "
101                    + manPageOutputDir.getAbsolutePath().replace(' ', '_'));
102        }
103    }
104}