View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2014-2015 ForgeRock AS.
15   */
16  
17  package org.forgerock.doc.maven.post;
18  
19  import org.apache.commons.io.IOUtils;
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.forgerock.doc.maven.AbstractDocbkxMojo;
22  import org.forgerock.doc.maven.utils.HtmlUtils;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.util.HashMap;
27  
28  /**
29   * Webhelp post-processor for both single-page and chunked HTML formats.
30   */
31  public class WebhelpPost {
32  
33      /**
34       * The Mojo that holds configuration and related methods.
35       */
36      private AbstractDocbkxMojo m;
37  
38      /**
39       * Constructor setting the Mojo that holds the configuration.
40       *
41       * @param mojo The Mojo that holds the configuration.
42       */
43      public WebhelpPost(final AbstractDocbkxMojo mojo) {
44          m = mojo;
45      }
46  
47      /**
48       * Post-processes HTML formats.
49       *
50       * @throws org.apache.maven.plugin.MojoExecutionException Failed to post-process HTML.
51       */
52      public void execute() throws MojoExecutionException {
53  
54          final File webhelpDir = new File(m.getDocbkxOutputDirectory(), "webhelp");
55  
56          HashMap<String, String> replacements = new HashMap<String, String>();
57  
58          try {
59              // See https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag
60              String robots = "<head>" + System.getProperty("line.separator")
61                      + IOUtils.toString(getClass().getResourceAsStream("/robots.txt"), "UTF-8");
62              replacements.put("<head>", robots);
63              HtmlUtils.updateHtml(webhelpDir.getPath(), replacements);
64          } catch (IOException e) {
65              throw new MojoExecutionException("Failed to update webhelp with nofollow,noindex tag", e);
66          }
67      }
68  }