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 2014-2015 ForgeRock AS
015 */
016
017package org.forgerock.doc.maven.utils;
018
019import org.apache.commons.io.FileUtils;
020
021import java.io.File;
022import java.io.IOException;
023import java.net.URL;
024
025/**
026 * Copy Bootstrap files under the specified directories.
027 */
028public final class BootstrapCopier {
029
030    /**
031     * Bootstrap logo files.
032     */
033    private final String[] bootstrapLogoResources = {
034        "/logos/hero-bg-01.png",
035        "/logos/left-shape-4.png",
036        "/logos/Icon_External_Link.png",
037        "/logos/forgerock-header-logo.png",
038        "/logos/FR_logo_horiz_FC_rev.png"
039    };
040
041    /**
042     * Bootstrap SWF files.
043     */
044    private final String[] bootstrapSwfResources = {
045        "/swf/ZeroClipboard.swf"
046    };
047
048    /**
049     * Directories where HtmlForBootstrap scripts, SWF and CSS are to be added.
050     */
051    private String[] outputDirectories;
052
053    /**
054     * Construct a BootstrapCopier, specifying output directories.
055     *
056     * @param outputDirectories Full path to directories under which to copy files.
057     */
058    public BootstrapCopier(final String[] outputDirectories) {
059        this.outputDirectories = outputDirectories;
060    }
061
062    /**
063     * For each outputDirectory, copy HtmlForBootstrap files under outputDirectory/sh.
064     *
065     * @throws java.io.IOException Failed to copy files.
066     */
067    public void copy() throws IOException {
068        addBootstrapLogos();
069        addBootstrapSwf();
070    }
071
072    /**
073     * Add HtmlForBootstrap logo files in each output directory.
074     *
075     * @throws java.io.IOException Failed to add scripts.
076     */
077    private void addBootstrapLogos() throws IOException {
078        addBootstrapResources(bootstrapLogoResources);
079    }
080
081    private void addBootstrapSwf() throws IOException {
082        addBootstrapResources(bootstrapSwfResources);
083    }
084
085    /**
086     * Add Bootstrap resource files in each output directory.
087     *
088     * @param resources List of resource files to copy.
089     * @throws java.io.IOException Failed to files.
090     */
091    private void addBootstrapResources(final String[] resources) throws IOException {
092
093        for (String resource : resources) {
094            URL resourceUrl = getClass().getResource(resource);
095
096            // The html.stylesheet parameter should probably take URLs.
097            // When local files are referenced,
098            // the DocBook XSL stylesheets do not copy the files.
099            // Instead the files must be copied to the output directories.
100
101            if (resourceUrl != null) {
102                for (final String outputDirectory : outputDirectories) {
103                    final File styleSheetFile = FileUtils.getFile(outputDirectory, "includes", resource);
104                    FileUtils.copyURLToFile(resourceUrl, styleSheetFile);
105
106                }
107            } else {
108                System.err.println("WARNING: Resource " + resource + " "
109                    + "cannot be " + "found!");
110            }
111        }
112    }
113}