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 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 SyntaxHighlighter files under the specified directories.
027 */
028public final class SyntaxHighlighterCopier {
029
030    /**
031     * SyntaxHighlighter CSS files.
032     */
033    private final String[] shCssResources = {
034        "/css/shCore.css",
035        "/css/shCoreEclipse.css",
036        "/css/shThemeEclipse.css"
037    };
038
039    /**
040     * SyntaxHighlighter JavaScript files.
041     */
042    private final String[] shJavaScriptResources = {
043        "/js/shCore.js",
044        "/js/shBrushAci.js",
045        "/js/shBrushBash.js",
046        "/js/shBrushCsv.js",
047        "/js/shBrushHttp.js",
048        "/js/shBrushJava.js",
049        "/js/shBrushJScript.js",
050        "/js/shBrushLDIF.js",
051        "/js/shBrushPlain.js",
052        "/js/shBrushProperties.js",
053        "/js/shBrushXml.js",
054        "/js/shAll.js"
055    };
056
057    /**
058     * Directories where SyntaxHighlighter scripts and CSS are to be added.
059     */
060    private String[] outputDirectories;
061
062    /**
063     * Construct a SyntaxHighlighterCopier, specifying output directories.
064     *
065     * @param outputDirectories Full path to directories under which to copy files.
066     */
067    public SyntaxHighlighterCopier(final String[] outputDirectories) {
068        this.outputDirectories = outputDirectories;
069    }
070
071    /**
072     * For each outputDirectory, copy SyntaxHighlighter files under outputDirectory/sh.
073     *
074     * @throws IOException Failed to copy files.
075     */
076    public void copy() throws IOException {
077        addShCss();
078        addShScripts();
079    }
080
081    /**
082     * Add SyntaxHighlighter CSS files in each output directory.
083     *
084     * @throws IOException Failed to add CSS.
085     */
086    private void addShCss() throws IOException {
087        addShResources(shCssResources);
088    }
089
090    /**
091     * Add SyntaxHighlighter JavaScript files in each output directory.
092     *
093     * @throws IOException Failed to add scripts.
094     */
095    private void addShScripts() throws IOException {
096        addShResources(shJavaScriptResources);
097    }
098
099    /**
100     * Add SyntaxHighlighter resource files in each output directory.
101     *
102     * @param resources List of resource files to copy.
103     * @throws IOException Failed to files.
104     */
105    private void addShResources(final String[] resources) throws IOException {
106
107        for (String resource : resources) {
108            URL resourceUrl = getClass().getResource(resource);
109
110            // The html.stylesheet parameter should probably take URLs.
111            // When local files are referenced,
112            // the DocBook XSL stylesheets do not copy the files.
113            // Instead the files must be copied to the output directories.
114
115            for (final String outputDirectory : outputDirectories) {
116                final File styleSheetFile = FileUtils.getFile(outputDirectory, "sh", resource);
117                FileUtils.copyURLToFile(resourceUrl, styleSheetFile);
118            }
119        }
120    }
121}