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.FileUtils;
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 webhelp output.
32   */
33  public class Webhelp {
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 Webhelp(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, for webhelp.
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-webhelp.xml");
74  
75          try {
76              OLinkUtils.createTargetDatabase(targetDB, "webhelp", 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.addAll(m.getBaseConfiguration());
100 
101                 cfg.add(element(name("chapterAutolabel"), "1"));
102                 cfg.add(element(name("webhelpAutolabel"), "1"));
103                 cfg.add(element(name("webhelpCustomization"), m.path(m.getWebHelpCustomization())));
104                 cfg.add(element(name("collectXrefTargets"), "only"));
105 
106                 cfg.add(element(name("currentDocid"), docName));
107                 cfg.add(element(name("includes"), docName + "/" + m.getDocumentSrcName()));
108                 final File webHelpBase = new File(m.getDocbkxOutputDirectory(), "webhelp");
109                 cfg.add(element(name("targetDirectory"), m.path(webHelpBase)));
110                 cfg.add(element(name("targetsFilename"), m.getDocumentSrcName() + ".webhelp.target.db"));
111 
112                 executeMojo(
113                         plugin(
114                                 groupId("com.agilejava.docbkx"),
115                                 artifactId("docbkx-maven-plugin"),
116                                 version(m.getDocbkxVersion())),
117                         goal("generate-webhelp"),
118                         configuration(cfg.toArray(new Element[cfg.size()])),
119                         executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
120             }
121         }
122 
123         /**
124          * Build documents from DocBook XML sources.
125          *
126          * @throws MojoExecutionException Failed to build the output.
127          */
128         void build() throws MojoExecutionException {
129 
130             try {
131                 ImageCopier.copyImages("webhelp", "", m);
132             } catch (IOException e) {
133                 throw new MojoExecutionException("Failed to copy images", e);
134             }
135 
136             for (String docName : m.getDocNames()) {
137                 ArrayList<MojoExecutor.Element> cfg = new ArrayList<MojoExecutor.Element>();
138                 cfg.addAll(m.getBaseConfiguration());
139 
140                 cfg.add(element(name("chapterAutolabel"), "1"));
141                 cfg.add(element(name("webhelpAutolabel"), "1"));
142                 cfg.add(element(name("webhelpCustomization"),
143                         m.path(m.getWebHelpCustomization())));
144 
145                 cfg.add(element(name("targetDatabaseDocument"), getTargetDB()));
146 
147                 final File webHelpBase = new File(m.getDocbkxOutputDirectory(), "webhelp");
148                 cfg.add(element(name("targetDirectory"), m.path(webHelpBase)));
149 
150                 cfg.add(element(name("currentDocid"), docName));
151                 cfg.add(element(name("includes"), docName + "/" + m.getDocumentSrcName()));
152 
153                 executeMojo(
154                         plugin(
155                                 groupId("com.agilejava.docbkx"),
156                                 artifactId("docbkx-maven-plugin"),
157                                 version(m.getDocbkxVersion())),
158                         goal("generate-webhelp"),
159                         configuration(cfg.toArray(new Element[cfg.size()])),
160                         executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
161 
162                 // Copy CSS and logo into place in the new webhelp output.
163                 final File webHelpCss = FileUtils.getFile(
164                         webHelpBase, docName, "common", "css", "positioning.css");
165                 final File webHelpLogo = FileUtils.getFile(
166                         webHelpBase, docName, "common", "images", "logo.png");
167                 try {
168                     FileUtils.copyFile(m.getWebHelpCss(), webHelpCss);
169                     FileUtils.copyFile(m.getWebHelpLogo(), webHelpLogo);
170                 } catch (IOException ie) {
171                     throw new MojoExecutionException("Failed to copy file", ie);
172                 }
173             }
174         }
175     }
176 }