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 2012-2015 ForgeRock AS.
15   */
16  
17  package org.forgerock.doc.maven.site;
18  
19  import static org.twdata.maven.mojoexecutor.MojoExecutor.element;
20  import static org.twdata.maven.mojoexecutor.MojoExecutor.name;
21  
22  import org.apache.commons.io.FileUtils;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.forgerock.doc.maven.AbstractDocbkxMojo;
25  import org.forgerock.doc.maven.AbstractDocbkxMojo.Format;
26  import org.twdata.maven.mojoexecutor.MojoExecutor;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * Lay out built documents,
35   * by default under {@code ${project.build.directory}/site/doc/}.
36   */
37  public class Layout {
38  
39      /**
40       * The Mojo that holds configuration and related methods.
41       */
42      private AbstractDocbkxMojo m;
43  
44      /**
45       * The Executor to run the resources plugin.
46       */
47      private final Executor executor;
48  
49      /**
50       * Constructor setting the Mojo that holds the configuration.
51       *
52       * @param mojo The Mojo that holds the configuration.
53       */
54      public Layout(final AbstractDocbkxMojo mojo) {
55          m = mojo;
56          this.executor = new Executor();
57      }
58  
59      /**
60       * Lay out built documents.
61       *
62       * @throws MojoExecutionException Failed to layout site.
63       */
64      public void execute() throws MojoExecutionException {
65          executor.layout();
66      }
67  
68      /**
69       * Get element specifying built documents to copy to the site directory.
70       *
71       * <p>
72       *
73       * Man pages are not currently copied anywhere.
74       *
75       * <p>
76       *
77       * Webhelp is handled separately.
78       *
79       * @return Compound element specifying built documents to copy.
80       * @throws MojoExecutionException Something went wrong getting document names.
81       */
82      private MojoExecutor.Element getResources() throws MojoExecutionException {
83  
84          ArrayList<MojoExecutor.Element> r = new ArrayList<MojoExecutor.Element>();
85  
86          final List<Format> formats = m.getFormats();
87          final String outputDir = m.path(m.getDocbkxOutputDirectory());
88  
89          if (formats.contains(Format.epub)) {
90              r.add(element(name("resource"),
91                      element(name("directory"), outputDir + "/epub/"),
92                      element(name("includes"),
93                              element(name("include"), "**/*.epub"))));
94          }
95  
96          if (formats.contains(Format.html)) {
97              r.add(element(name("resource"),
98                      element(name("directory"), outputDir + "/"),
99                      element(name("includes"),
100                             element(name("include"), "html/**/*.*"))));
101         }
102 
103         // Man pages are not currently copied anywhere.
104 
105         if (formats.contains(Format.pdf)) {
106             r.add(element(name("resource"),
107                     element(name("directory"), outputDir + "/pdf/"),
108                     element(name("includes"),
109                             element(name("include"), "**/*.pdf"))));
110         }
111 
112         if (formats.contains(Format.rtf)) {
113             r.add(element(name("resource"),
114                     element(name("directory"), outputDir + "/rtf/"),
115                     element(name("includes"),
116                             element(name("include"), "**/*.rtf"))));
117         }
118 
119         // Webhelp is handled separately.
120 
121         if (formats.contains(Format.xhtml5)) {
122             r.add(element(name("resource"),
123                     element(name("directory"), outputDir + "/"),
124                     element(name("includes"),
125                             element(name("include"), "xhtml/**/*.*"))));
126         }
127 
128         if (formats.contains(Format.bootstrap)) {
129             r.add(element(name("resource"),
130                     element(name("directory"), outputDir + "/"),
131                     element(name("includes"),
132                             element(name("include"), "bootstrap/**/*.*"))));
133         }
134 
135         return element("resources", r.toArray(new MojoExecutor.Element[r.size()]));
136     }
137 
138     /**
139      * Enclose methods to run plugins.
140      */
141     class Executor extends MojoExecutor {
142 
143         /**
144          * Lay out built documents.
145          *
146          * @throws MojoExecutionException Failed to lay out documents.
147          */
148         public void layout() throws MojoExecutionException {
149 
150             final String siteDocDirectory = m.path(m.getSiteDirectory()) + "/doc";
151 
152             executeMojo(
153                     plugin(
154                             groupId("org.apache.maven.plugins"),
155                             artifactId("maven-resources-plugin"),
156                             version(m.getMavenResourcesVersion())),
157                     goal("copy-resources"),
158                     configuration(
159                             element(name("encoding"), "UTF-8"),
160                             element(name("outputDirectory"), siteDocDirectory),
161                             getResources()),
162                     executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
163 
164             // The webhelp directory needs to be copied in its entirety
165             // to avoid overwriting other HTML.
166 
167             if (m.getFormats().contains(Format.webhelp)) {
168 
169                 // 2.0.15 does not allow <webhelpBaseDir> to be set,
170                 // so the output location is hard-coded for now.
171                 final String webHelpDir = m.path(m.getDocbkxOutputDirectory()) + "/webhelp";
172 
173                 executeMojo(
174                         plugin(
175                                 groupId("org.apache.maven.plugins"),
176                                 artifactId("maven-resources-plugin"),
177                                 version(m.getMavenResourcesVersion())),
178                         goal("copy-resources"),
179                         configuration(
180                                 element(name("encoding"), "UTF-8"),
181                                 element(name("outputDirectory"), siteDocDirectory + "/webhelp"),
182                                 element(name("resources"),
183                                         element(name("resource"),
184                                                 element(name("directory"), webHelpDir),
185                                                 element(name("excludes"),
186                                                         element(name("exclude"), "**/*.target.db"))))),
187                         executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
188             }
189 
190             // Optionally copy the entire directory of arbitrary resources
191             // to the directories containing HTML format docs.
192             if (m.doCopyResourceFiles() && m.getResourcesDirectory().exists()) {
193 
194                 List<String> directories = new ArrayList<String>();
195                 if (m.getFormats().contains(Format.bootstrap)) {
196                     directories.add("bootstrap");
197                 }
198 
199                 if (m.getFormats().contains(Format.html)) {
200                     directories.add("html");
201                 }
202 
203                 if (m.getFormats().contains(Format.webhelp)) {
204                     directories.add("webhelp");
205                 }
206 
207                 if (m.getFormats().contains(Format.xhtml5)) {
208                     directories.add("xhtml");
209                 }
210 
211                 try {
212                     for (String directory : directories) {
213                         File targetDirectory = FileUtils.getFile(m.getSiteDirectory(), "doc", directory);
214                         FileUtils.copyDirectoryToDirectory(m.getResourcesDirectory(), targetDirectory);
215                     }
216                 } catch (IOException e) {
217                     throw new MojoExecutionException("Failed to copy resources", e);
218                 }
219             }
220         }
221     }
222 }