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