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-2015 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.forgerock.doc.maven.utils.ImageCopier;
022import org.forgerock.doc.maven.utils.OLinkUtils;
023import org.twdata.maven.mojoexecutor.MojoExecutor;
024
025import java.io.File;
026import java.io.IOException;
027import java.util.ArrayList;
028
029/**
030 * Build single-page HTML output.
031 */
032public class SingleHtml {
033
034    /**
035     * The Mojo that holds configuration and related methods.
036     */
037    private AbstractDocbkxMojo m;
038
039    /**
040     * The Executor to run docbkx-tools.
041     */
042    private final Executor executor;
043
044    /**
045     * Constructor setting the Mojo that holds the configuration.
046     *
047     * @param mojo The Mojo that holds the configuration.
048     */
049    public SingleHtml(final AbstractDocbkxMojo mojo) {
050        m = mojo;
051        this.executor = new Executor();
052    }
053
054    /**
055     * Build documents from DocBook XML sources.
056     *
057     * @throws MojoExecutionException Failed to build output.
058     */
059    public void execute() throws MojoExecutionException {
060        executor.prepareOlinkDB();
061        executor.build();
062    }
063
064    /**
065     * Get absolute path to an Olink target database XML document
066     * that points to the individual generated Olink DB files,
067     * for single-page HTML.
068     *
069     * @return Absolute path to the file.
070     * @throws MojoExecutionException Could not write target DB file.
071     */
072    final String getTargetDB() throws MojoExecutionException {
073        File targetDB = new File(m.getBuildDirectory(), "olinkdb-single-page-html.xml");
074
075        try {
076            OLinkUtils.createTargetDatabase(targetDB, "html", m);
077        } catch (Exception e) {
078            throw new MojoExecutionException(
079                    "Failed to write link target database: " + targetDB.getPath(), e);
080        }
081
082        return targetDB.getPath();
083    }
084
085    /**
086     * Enclose methods to run plugins.
087     */
088    class Executor extends MojoExecutor {
089
090        /**
091         * Prepare olink target database from DocBook XML sources.
092         *
093         * @throws MojoExecutionException Failed to build target database.
094         */
095        void prepareOlinkDB() throws MojoExecutionException {
096
097            for (String docName : m.getDocNames()) {
098                ArrayList<Element> cfg = new ArrayList<MojoExecutor.Element>();
099                cfg.add(element(name("xincludeSupported"), m.isXincludeSupported()));
100                cfg.add(element(name("sourceDirectory"), m.path(m.getDocbkxModifiableSourcesDirectory())));
101                cfg.add(element(name("chunkedOutput"), "false"));
102                cfg.add(element(name("htmlCustomization"), m.path(m.getSingleHTMLCustomization())));
103                cfg.add(element(name("collectXrefTargets"), "only"));
104
105                cfg.add(element(name("includes"), docName + "/" + m.getDocumentSrcName()));
106                cfg.add(element(name("targetDirectory"), m.path(m.getDocbkxOutputDirectory()) + "/html"));
107                cfg.add(element(name("targetsFilename"), m.getDocumentSrcName() + ".html.target.db"));
108
109                executeMojo(
110                        plugin(
111                                groupId("com.agilejava.docbkx"),
112                                artifactId("docbkx-maven-plugin"),
113                                version(m.getDocbkxVersion())),
114                        goal("generate-html"),
115                        configuration(cfg.toArray(new Element[cfg.size()])),
116                        executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
117            }
118        }
119
120        /**
121         * Build documents from DocBook XML sources.
122         *
123         * @throws MojoExecutionException Failed to build the output.
124         */
125        void build() throws MojoExecutionException {
126
127            try {
128                ImageCopier.copyImages("html", "", m);
129            } catch (IOException e) {
130                throw new MojoExecutionException("Failed to copy images", e);
131            }
132
133            ArrayList<MojoExecutor.Element> cfg = new ArrayList<MojoExecutor.Element>();
134            cfg.addAll(m.getBaseConfiguration());
135            cfg.add(element(name("includes"), "*/" + m.getDocumentSrcName()));
136            cfg.add(element(name("chunkedOutput"), "false"));
137            cfg.add(element(name("htmlCustomization"), m.path(m.getSingleHTMLCustomization())));
138            cfg.add(element(name("targetDatabaseDocument"), getTargetDB()));
139            cfg.add(element(name("targetDirectory"), m.path(m.getDocbkxOutputDirectory()) + "/html"));
140
141            executeMojo(
142                    plugin(
143                            groupId("com.agilejava.docbkx"),
144                            artifactId("docbkx-maven-plugin"),
145                            version(m.getDocbkxVersion())),
146                    goal("generate-html"),
147                    configuration(cfg.toArray(new Element[cfg.size()])),
148                    executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
149        }
150    }
151}