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