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.site;
018
019import static org.twdata.maven.mojoexecutor.MojoExecutor.element;
020import static org.twdata.maven.mojoexecutor.MojoExecutor.name;
021
022import org.apache.commons.io.FileUtils;
023import org.apache.maven.plugin.MojoExecutionException;
024import org.forgerock.doc.maven.AbstractDocbkxMojo;
025import org.forgerock.doc.maven.AbstractDocbkxMojo.Format;
026import org.twdata.maven.mojoexecutor.MojoExecutor;
027
028import java.io.File;
029import java.io.IOException;
030import java.util.ArrayList;
031import java.util.List;
032
033/**
034 * Lay out built documents,
035 * by default under {@code ${project.build.directory}/site/doc/}.
036 */
037public class Layout {
038
039    /**
040     * The Mojo that holds configuration and related methods.
041     */
042    private AbstractDocbkxMojo m;
043
044    /**
045     * The Executor to run the resources plugin.
046     */
047    private final Executor executor;
048
049    /**
050     * Constructor setting the Mojo that holds the configuration.
051     *
052     * @param mojo The Mojo that holds the configuration.
053     */
054    public Layout(final AbstractDocbkxMojo mojo) {
055        m = mojo;
056        this.executor = new Executor();
057    }
058
059    /**
060     * Lay out built documents.
061     *
062     * @throws MojoExecutionException Failed to layout site.
063     */
064    public void execute() throws MojoExecutionException {
065        executor.layout();
066    }
067
068    /**
069     * Get element specifying built documents to copy to the site directory.
070     *
071     * <p>
072     *
073     * Man pages are not currently copied anywhere.
074     *
075     * <p>
076     *
077     * Webhelp is handled separately.
078     *
079     * @return Compound element specifying built documents to copy.
080     * @throws MojoExecutionException Something went wrong getting document names.
081     */
082    private MojoExecutor.Element getResources() throws MojoExecutionException {
083
084        ArrayList<MojoExecutor.Element> r = new ArrayList<MojoExecutor.Element>();
085
086        final List<Format> formats = m.getFormats();
087        final String outputDir = m.path(m.getDocbkxOutputDirectory());
088
089        if (formats.contains(Format.epub)) {
090            r.add(element(name("resource"),
091                    element(name("directory"), outputDir + "/epub/"),
092                    element(name("includes"),
093                            element(name("include"), "**/*.epub"))));
094        }
095
096        if (formats.contains(Format.html)) {
097            r.add(element(name("resource"),
098                    element(name("directory"), outputDir + "/"),
099                    element(name("includes"),
100                            element(name("include"), "html/**/*.*"))));
101        }
102
103        // Man pages are not currently copied anywhere.
104
105        if (formats.contains(Format.pdf)) {
106            r.add(element(name("resource"),
107                    element(name("directory"), outputDir + "/pdf/"),
108                    element(name("includes"),
109                            element(name("include"), "**/*.pdf"))));
110        }
111
112        if (formats.contains(Format.rtf)) {
113            r.add(element(name("resource"),
114                    element(name("directory"), outputDir + "/rtf/"),
115                    element(name("includes"),
116                            element(name("include"), "**/*.rtf"))));
117        }
118
119        // Webhelp is handled separately.
120
121        if (formats.contains(Format.xhtml5)) {
122            r.add(element(name("resource"),
123                    element(name("directory"), outputDir + "/"),
124                    element(name("includes"),
125                            element(name("include"), "xhtml/**/*.*"))));
126        }
127
128        if (formats.contains(Format.bootstrap)) {
129            r.add(element(name("resource"),
130                    element(name("directory"), outputDir + "/"),
131                    element(name("includes"),
132                            element(name("include"), "bootstrap/**/*.*"))));
133        }
134
135        return element("resources", r.toArray(new MojoExecutor.Element[r.size()]));
136    }
137
138    /**
139     * Enclose methods to run plugins.
140     */
141    class Executor extends MojoExecutor {
142
143        /**
144         * Lay out built documents.
145         *
146         * @throws MojoExecutionException Failed to lay out documents.
147         */
148        public void layout() throws MojoExecutionException {
149
150            final String siteDocDirectory = m.path(m.getSiteDirectory()) + "/doc";
151
152            executeMojo(
153                    plugin(
154                            groupId("org.apache.maven.plugins"),
155                            artifactId("maven-resources-plugin"),
156                            version(m.getMavenResourcesVersion())),
157                    goal("copy-resources"),
158                    configuration(
159                            element(name("encoding"), "UTF-8"),
160                            element(name("outputDirectory"), siteDocDirectory),
161                            getResources()),
162                    executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
163
164            // The webhelp directory needs to be copied in its entirety
165            // to avoid overwriting other HTML.
166
167            if (m.getFormats().contains(Format.webhelp)) {
168
169                // 2.0.15 does not allow <webhelpBaseDir> to be set,
170                // so the output location is hard-coded for now.
171                final String webHelpDir = m.path(m.getDocbkxOutputDirectory()) + "/webhelp";
172
173                executeMojo(
174                        plugin(
175                                groupId("org.apache.maven.plugins"),
176                                artifactId("maven-resources-plugin"),
177                                version(m.getMavenResourcesVersion())),
178                        goal("copy-resources"),
179                        configuration(
180                                element(name("encoding"), "UTF-8"),
181                                element(name("outputDirectory"), siteDocDirectory + "/webhelp"),
182                                element(name("resources"),
183                                        element(name("resource"),
184                                                element(name("directory"), webHelpDir),
185                                                element(name("excludes"),
186                                                        element(name("exclude"), "**/*.target.db"))))),
187                        executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
188            }
189
190            // Optionally copy the entire directory of arbitrary resources
191            // to the directories containing HTML format docs.
192            if (m.doCopyResourceFiles() && m.getResourcesDirectory().exists()) {
193
194                List<String> directories = new ArrayList<String>();
195                if (m.getFormats().contains(Format.bootstrap)) {
196                    directories.add("bootstrap");
197                }
198
199                if (m.getFormats().contains(Format.html)) {
200                    directories.add("html");
201                }
202
203                if (m.getFormats().contains(Format.webhelp)) {
204                    directories.add("webhelp");
205                }
206
207                if (m.getFormats().contains(Format.xhtml5)) {
208                    directories.add("xhtml");
209                }
210
211                try {
212                    for (String directory : directories) {
213                        File targetDirectory = FileUtils.getFile(m.getSiteDirectory(), "doc", directory);
214                        FileUtils.copyDirectoryToDirectory(m.getResourcesDirectory(), targetDirectory);
215                    }
216                } catch (IOException e) {
217                    throw new MojoExecutionException("Failed to copy resources", e);
218                }
219            }
220        }
221    }
222}