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.post;
018
019import org.apache.commons.io.IOUtils;
020import org.apache.maven.plugin.MojoExecutionException;
021import org.forgerock.doc.maven.AbstractDocbkxMojo;
022import org.forgerock.doc.maven.utils.HtmlUtils;
023
024import java.io.File;
025import java.io.IOException;
026import java.util.HashMap;
027
028/**
029 * Webhelp post-processor for both single-page and chunked HTML formats.
030 */
031public class WebhelpPost {
032
033    /**
034     * The Mojo that holds configuration and related methods.
035     */
036    private AbstractDocbkxMojo m;
037
038    /**
039     * Constructor setting the Mojo that holds the configuration.
040     *
041     * @param mojo The Mojo that holds the configuration.
042     */
043    public WebhelpPost(final AbstractDocbkxMojo mojo) {
044        m = mojo;
045    }
046
047    /**
048     * Post-processes HTML formats.
049     *
050     * @throws org.apache.maven.plugin.MojoExecutionException Failed to post-process HTML.
051     */
052    public void execute() throws MojoExecutionException {
053
054        final File webhelpDir = new File(m.getDocbkxOutputDirectory(), "webhelp");
055
056        HashMap<String, String> replacements = new HashMap<String, String>();
057
058        try {
059            // See https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag
060            String robots = "<head>" + System.getProperty("line.separator")
061                    + IOUtils.toString(getClass().getResourceAsStream("/robots.txt"), "UTF-8");
062            replacements.put("<head>", robots);
063            HtmlUtils.updateHtml(webhelpDir.getPath(), replacements);
064        } catch (IOException e) {
065            throw new MojoExecutionException("Failed to update webhelp with nofollow,noindex tag", e);
066        }
067    }
068}