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.commons.io.FileUtils;
020import org.apache.maven.plugin.MojoExecutionException;
021import org.forgerock.doc.maven.AbstractDocbkxMojo;
022import org.forgerock.doc.maven.utils.ImageCopier;
023import org.forgerock.doc.maven.utils.OLinkUtils;
024import org.twdata.maven.mojoexecutor.MojoExecutor;
025
026import java.io.File;
027import java.io.IOException;
028import java.util.ArrayList;
029
030/**
031 * Build webhelp output.
032 */
033public class Webhelp {
034
035    /**
036     * The Mojo that holds configuration and related methods.
037     */
038    private AbstractDocbkxMojo m;
039
040    /**
041     * The Executor to run docbkx-tools.
042     */
043    private final Executor executor;
044
045    /**
046     * Constructor setting the Mojo that holds the configuration.
047     *
048     * @param mojo The Mojo that holds the configuration.
049     */
050    public Webhelp(final AbstractDocbkxMojo mojo) {
051        m = mojo;
052        this.executor = new Executor();
053    }
054
055    /**
056     * Build documents from DocBook XML sources.
057     *
058     * @throws MojoExecutionException Failed to build output.
059     */
060    public void execute() throws MojoExecutionException {
061        executor.prepareOlinkDB();
062        executor.build();
063    }
064
065    /**
066     * Get absolute path to an Olink target database XML document
067     * that points to the individual generated Olink DB files, for webhelp.
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-webhelp.xml");
074
075        try {
076            OLinkUtils.createTargetDatabase(targetDB, "webhelp", 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.addAll(m.getBaseConfiguration());
100
101                cfg.add(element(name("chapterAutolabel"), "1"));
102                cfg.add(element(name("webhelpAutolabel"), "1"));
103                cfg.add(element(name("webhelpCustomization"), m.path(m.getWebHelpCustomization())));
104                cfg.add(element(name("collectXrefTargets"), "only"));
105
106                cfg.add(element(name("currentDocid"), docName));
107                cfg.add(element(name("includes"), docName + "/" + m.getDocumentSrcName()));
108                final File webHelpBase = new File(m.getDocbkxOutputDirectory(), "webhelp");
109                cfg.add(element(name("targetDirectory"), m.path(webHelpBase)));
110                cfg.add(element(name("targetsFilename"), m.getDocumentSrcName() + ".webhelp.target.db"));
111
112                executeMojo(
113                        plugin(
114                                groupId("com.agilejava.docbkx"),
115                                artifactId("docbkx-maven-plugin"),
116                                version(m.getDocbkxVersion())),
117                        goal("generate-webhelp"),
118                        configuration(cfg.toArray(new Element[cfg.size()])),
119                        executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
120            }
121        }
122
123        /**
124         * Build documents from DocBook XML sources.
125         *
126         * @throws MojoExecutionException Failed to build the output.
127         */
128        void build() throws MojoExecutionException {
129
130            try {
131                ImageCopier.copyImages("webhelp", "", m);
132            } catch (IOException e) {
133                throw new MojoExecutionException("Failed to copy images", e);
134            }
135
136            for (String docName : m.getDocNames()) {
137                ArrayList<MojoExecutor.Element> cfg = new ArrayList<MojoExecutor.Element>();
138                cfg.addAll(m.getBaseConfiguration());
139
140                cfg.add(element(name("chapterAutolabel"), "1"));
141                cfg.add(element(name("webhelpAutolabel"), "1"));
142                cfg.add(element(name("webhelpCustomization"),
143                        m.path(m.getWebHelpCustomization())));
144
145                cfg.add(element(name("targetDatabaseDocument"), getTargetDB()));
146
147                final File webHelpBase = new File(m.getDocbkxOutputDirectory(), "webhelp");
148                cfg.add(element(name("targetDirectory"), m.path(webHelpBase)));
149
150                cfg.add(element(name("currentDocid"), docName));
151                cfg.add(element(name("includes"), docName + "/" + m.getDocumentSrcName()));
152
153                executeMojo(
154                        plugin(
155                                groupId("com.agilejava.docbkx"),
156                                artifactId("docbkx-maven-plugin"),
157                                version(m.getDocbkxVersion())),
158                        goal("generate-webhelp"),
159                        configuration(cfg.toArray(new Element[cfg.size()])),
160                        executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
161
162                // Copy CSS and logo into place in the new webhelp output.
163                final File webHelpCss = FileUtils.getFile(
164                        webHelpBase, docName, "common", "css", "positioning.css");
165                final File webHelpLogo = FileUtils.getFile(
166                        webHelpBase, docName, "common", "images", "logo.png");
167                try {
168                    FileUtils.copyFile(m.getWebHelpCss(), webHelpCss);
169                    FileUtils.copyFile(m.getWebHelpLogo(), webHelpLogo);
170                } catch (IOException ie) {
171                    throw new MojoExecutionException("Failed to copy file", ie);
172                }
173            }
174        }
175    }
176}