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 2015 ForgeRock AS.
15   */
16  package org.forgerock.doc.maven.pre;
17  
18  import org.apache.commons.io.FileUtils;
19  import org.apache.maven.plugin.MojoExecutionException;
20  import org.forgerock.doc.maven.AbstractDocbkxMojo;
21  import org.twdata.maven.mojoexecutor.MojoExecutor;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   * Copies arbitrary resources from sources to pre-site output for HTML formats.
30   */
31  public class ArbitraryResourceCopier {
32  
33      /**
34       * The Mojo that holds configuration and related methods.
35       */
36      private AbstractDocbkxMojo m;
37  
38      /**
39       * The Executor to run the resources plugin.
40       */
41      private final Executor executor;
42  
43      /**
44       * Constructor setting the Mojo that holds the configuration.
45       *
46       * @param mojo The Mojo that holds the configuration.
47       */
48      public ArbitraryResourceCopier(final AbstractDocbkxMojo mojo) {
49          m = mojo;
50          this.executor = new Executor();
51      }
52  
53      /**
54       * Copies arbitrary resources from sources to pre-site output for HTML formats.
55       *
56       * @throws MojoExecutionException   Failed to copy files
57       */
58      public void execute() throws MojoExecutionException {
59          executor.copy();
60      }
61  
62      /**
63       * Enclose methods to run plugins.
64       */
65      class Executor extends MojoExecutor {
66  
67          /**
68           * Copies arbitrary resources from sources to pre-site output for HTML formats.
69           *
70           * @throws MojoExecutionException   Failed to copy files
71           */
72          public void copy() throws MojoExecutionException {
73              if (m.doCopyResourceFiles() && m.getResourcesDirectory().exists()) {
74  
75                  List<String> directories = new ArrayList<>();
76                  if (m.getFormats().contains(AbstractDocbkxMojo.Format.bootstrap)) {
77                      directories.add("bootstrap");
78                  }
79  
80                  if (m.getFormats().contains(AbstractDocbkxMojo.Format.html)) {
81                      directories.add("html");
82                  }
83  
84                  if (m.getFormats().contains(AbstractDocbkxMojo.Format.webhelp)) {
85                      directories.add("webhelp");
86                  }
87  
88                  if (m.getFormats().contains(AbstractDocbkxMojo.Format.xhtml5)) {
89                      directories.add("xhtml");
90                  }
91  
92                  try {
93                      for (String directory : directories) {
94                          File targetDirectory = FileUtils.getFile(m.getDocbkxOutputDirectory(), directory);
95                          FileUtils.copyDirectoryToDirectory(m.getResourcesDirectory(), targetDirectory);
96                      }
97                  } catch (IOException e) {
98                      throw new MojoExecutionException("Failed to copy resources", e);
99                  }
100             }
101 
102         }
103     }
104 }