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.build;
18  
19  import org.apache.maven.plugin.MojoExecutionException;
20  import org.forgerock.doc.maven.AbstractDocbkxMojo;
21  import org.forgerock.doc.maven.utils.ImageCopier;
22  import org.forgerock.doc.maven.utils.OLinkUtils;
23  import org.twdata.maven.mojoexecutor.MojoExecutor;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  
29  /**
30   * Build single-page HTML output.
31   */
32  public class SingleHtml {
33  
34      /**
35       * The Mojo that holds configuration and related methods.
36       */
37      private AbstractDocbkxMojo m;
38  
39      /**
40       * The Executor to run docbkx-tools.
41       */
42      private final Executor executor;
43  
44      /**
45       * Constructor setting the Mojo that holds the configuration.
46       *
47       * @param mojo The Mojo that holds the configuration.
48       */
49      public SingleHtml(final AbstractDocbkxMojo mojo) {
50          m = mojo;
51          this.executor = new Executor();
52      }
53  
54      /**
55       * Build documents from DocBook XML sources.
56       *
57       * @throws MojoExecutionException Failed to build output.
58       */
59      public void execute() throws MojoExecutionException {
60          executor.prepareOlinkDB();
61          executor.build();
62      }
63  
64      /**
65       * Get absolute path to an Olink target database XML document
66       * that points to the individual generated Olink DB files,
67       * for single-page HTML.
68       *
69       * @return Absolute path to the file.
70       * @throws MojoExecutionException Could not write target DB file.
71       */
72      final String getTargetDB() throws MojoExecutionException {
73          File targetDB = new File(m.getBuildDirectory(), "olinkdb-single-page-html.xml");
74  
75          try {
76              OLinkUtils.createTargetDatabase(targetDB, "html", m);
77          } catch (Exception e) {
78              throw new MojoExecutionException(
79                      "Failed to write link target database: " + targetDB.getPath(), e);
80          }
81  
82          return targetDB.getPath();
83      }
84  
85      /**
86       * Enclose methods to run plugins.
87       */
88      class Executor extends MojoExecutor {
89  
90          /**
91           * Prepare olink target database from DocBook XML sources.
92           *
93           * @throws MojoExecutionException Failed to build target database.
94           */
95          void prepareOlinkDB() throws MojoExecutionException {
96  
97              for (String docName : m.getDocNames()) {
98                  ArrayList<Element> cfg = new ArrayList<MojoExecutor.Element>();
99                  cfg.add(element(name("xincludeSupported"), m.isXincludeSupported()));
100                 cfg.add(element(name("sourceDirectory"), m.path(m.getDocbkxModifiableSourcesDirectory())));
101                 cfg.add(element(name("chunkedOutput"), "false"));
102                 cfg.add(element(name("htmlCustomization"), m.path(m.getSingleHTMLCustomization())));
103                 cfg.add(element(name("collectXrefTargets"), "only"));
104 
105                 cfg.add(element(name("includes"), docName + "/" + m.getDocumentSrcName()));
106                 cfg.add(element(name("targetDirectory"), m.path(m.getDocbkxOutputDirectory()) + "/html"));
107                 cfg.add(element(name("targetsFilename"), m.getDocumentSrcName() + ".html.target.db"));
108 
109                 executeMojo(
110                         plugin(
111                                 groupId("com.agilejava.docbkx"),
112                                 artifactId("docbkx-maven-plugin"),
113                                 version(m.getDocbkxVersion())),
114                         goal("generate-html"),
115                         configuration(cfg.toArray(new Element[cfg.size()])),
116                         executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
117             }
118         }
119 
120         /**
121          * Build documents from DocBook XML sources.
122          *
123          * @throws MojoExecutionException Failed to build the output.
124          */
125         void build() throws MojoExecutionException {
126 
127             try {
128                 ImageCopier.copyImages("html", "", m);
129             } catch (IOException e) {
130                 throw new MojoExecutionException("Failed to copy images", e);
131             }
132 
133             ArrayList<MojoExecutor.Element> cfg = new ArrayList<MojoExecutor.Element>();
134             cfg.addAll(m.getBaseConfiguration());
135             cfg.add(element(name("includes"), "*/" + m.getDocumentSrcName()));
136             cfg.add(element(name("chunkedOutput"), "false"));
137             cfg.add(element(name("htmlCustomization"), m.path(m.getSingleHTMLCustomization())));
138             cfg.add(element(name("targetDatabaseDocument"), getTargetDB()));
139             cfg.add(element(name("targetDirectory"), m.path(m.getDocbkxOutputDirectory()) + "/html"));
140 
141             executeMojo(
142                     plugin(
143                             groupId("com.agilejava.docbkx"),
144                             artifactId("docbkx-maven-plugin"),
145                             version(m.getDocbkxVersion())),
146                     goal("generate-html"),
147                     configuration(cfg.toArray(new Element[cfg.size()])),
148                     executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
149         }
150     }
151 }