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.post;
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.SyntaxHighlighterCopier;
023
024import java.io.IOException;
025
026/**
027 * XHTML post-processor.
028 */
029public class Xhtml {
030
031    /**
032     * The Mojo that holds configuration and related methods.
033     */
034    private AbstractDocbkxMojo m;
035
036    /**
037     * Constructor setting the Mojo that holds the configuration.
038     *
039     * @param mojo The Mojo that holds the configuration.
040     */
041    public Xhtml(final AbstractDocbkxMojo mojo) {
042        m = mojo;
043    }
044
045    /**
046     * Add SyntaxHighlighter files for each XHTML document.
047     *
048     * @throws MojoExecutionException Failed to post-process XHTML.
049     */
050    public void execute() throws MojoExecutionException {
051
052        String[] outputDirectories = new String[m.getDocNames().size()];
053
054        int i = 0;
055        for (final String docName : m.getDocNames()) {
056
057            // Example: ${project.build.directory}/docbkx/xhtml/my-book
058            outputDirectories[i] = FileUtils.getFile(
059                    m.getDocbkxOutputDirectory(), "xhtml", docName).getPath();
060            ++i;
061        }
062
063        SyntaxHighlighterCopier copier =
064                new SyntaxHighlighterCopier(outputDirectories);
065        try {
066            copier.copy();
067        } catch (IOException e) {
068            throw new MojoExecutionException(
069                    "Failed to copy files: " + e.getMessage(), e);
070        }
071    }
072}