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 2015 ForgeRock AS.
015 */
016package org.forgerock.doc.maven.pre;
017
018import org.apache.commons.io.FileUtils;
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.io.IOException;
025import java.util.ArrayList;
026import java.util.List;
027
028/**
029 * Copies arbitrary resources from sources to pre-site output for HTML formats.
030 */
031public class ArbitraryResourceCopier {
032
033    /**
034     * The Mojo that holds configuration and related methods.
035     */
036    private AbstractDocbkxMojo m;
037
038    /**
039     * The Executor to run the resources plugin.
040     */
041    private final Executor executor;
042
043    /**
044     * Constructor setting the Mojo that holds the configuration.
045     *
046     * @param mojo The Mojo that holds the configuration.
047     */
048    public ArbitraryResourceCopier(final AbstractDocbkxMojo mojo) {
049        m = mojo;
050        this.executor = new Executor();
051    }
052
053    /**
054     * Copies arbitrary resources from sources to pre-site output for HTML formats.
055     *
056     * @throws MojoExecutionException   Failed to copy files
057     */
058    public void execute() throws MojoExecutionException {
059        executor.copy();
060    }
061
062    /**
063     * Enclose methods to run plugins.
064     */
065    class Executor extends MojoExecutor {
066
067        /**
068         * Copies arbitrary resources from sources to pre-site output for HTML formats.
069         *
070         * @throws MojoExecutionException   Failed to copy files
071         */
072        public void copy() throws MojoExecutionException {
073            if (m.doCopyResourceFiles() && m.getResourcesDirectory().exists()) {
074
075                List<String> directories = new ArrayList<>();
076                if (m.getFormats().contains(AbstractDocbkxMojo.Format.bootstrap)) {
077                    directories.add("bootstrap");
078                }
079
080                if (m.getFormats().contains(AbstractDocbkxMojo.Format.html)) {
081                    directories.add("html");
082                }
083
084                if (m.getFormats().contains(AbstractDocbkxMojo.Format.webhelp)) {
085                    directories.add("webhelp");
086                }
087
088                if (m.getFormats().contains(AbstractDocbkxMojo.Format.xhtml5)) {
089                    directories.add("xhtml");
090                }
091
092                try {
093                    for (String directory : directories) {
094                        File targetDirectory = FileUtils.getFile(m.getDocbkxOutputDirectory(), directory);
095                        FileUtils.copyDirectoryToDirectory(m.getResourcesDirectory(), targetDirectory);
096                    }
097                } catch (IOException e) {
098                    throw new MojoExecutionException("Failed to copy resources", e);
099                }
100            }
101
102        }
103    }
104}