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 2014-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 XHTML output.
31   */
32  public class Xhtml5 {
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 Xhtml5(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 XHTML.
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-xhtml.xml");
74  
75          try {
76              OLinkUtils.createTargetDatabase(targetDB, "xhtml5", 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<Element>();
99                  cfg.add(element(name("chunkedOutput"), "false"));
100                 cfg.add(element(name("collectXrefTargets"), "only"));
101                 cfg.add(element(name("includes"), docName + "/" + m.getDocumentSrcName()));
102                 cfg.add(element(name("sourceDirectory"), m.path(m.getDocbkxModifiableSourcesDirectory())));
103                 cfg.add(element(name("xhtml5Customization"), m.path(m.getXhtml5Customization())));
104                 cfg.add(element(name("xincludeSupported"), m.isXincludeSupported()));
105                 cfg.add(element(name("targetDirectory"), m.path(m.getDocbkxOutputDirectory()) + "/xhtml"));
106                 cfg.add(element(name("targetsFilename"), m.getDocumentSrcName() + ".xhtml.target.db"));
107 
108                 executeMojo(
109                         plugin(
110                                 groupId("com.agilejava.docbkx"),
111                                 artifactId("docbkx-maven-plugin"),
112                                 version(m.getDocbkxVersion())),
113                         goal("generate-xhtml5"),
114                         configuration(cfg.toArray(new Element[cfg.size()])),
115                         executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
116             }
117         }
118 
119         /**
120          * Build documents from DocBook XML sources.
121          *
122          * @throws org.apache.maven.plugin.MojoExecutionException Failed to build the output.
123          */
124         void build() throws MojoExecutionException {
125 
126             try {
127                 ImageCopier.copyImages("xhtml", "", m);
128             } catch (IOException e) {
129                 throw new MojoExecutionException("Failed to copy images", e);
130             }
131 
132             ArrayList<Element> cfg = new ArrayList<Element>();
133             cfg.add(element(name("chunkedOutput"), "false"));
134             cfg.add(element(name("highlightSource"), m.useSyntaxHighlighting()));
135             cfg.add(element(name("includes"), "*/" + m.getDocumentSrcName()));
136             cfg.add(element(name("sectionAutolabel"), m.areSectionsAutolabeled()));
137             cfg.add(element(name("sectionLabelIncludesComponentLabel"), m.doesSectionLabelIncludeComponentLabel()));
138             cfg.add(element(name("sourceDirectory"), m.path(m.getDocbkxModifiableSourcesDirectory())));
139             cfg.add(element(name("targetDatabaseDocument"), getTargetDB()));
140             cfg.add(element(name("targetDirectory"), m.path(m.getDocbkxOutputDirectory()) + "/xhtml"));
141             cfg.add(element(name("xhtml5Customization"), m.path(m.getXhtml5Customization())));
142             cfg.add(element(name("xincludeSupported"), m.isXincludeSupported()));
143 
144             executeMojo(
145                     plugin(
146                             groupId("com.agilejava.docbkx"),
147                             artifactId("docbkx-maven-plugin"),
148                             version(m.getDocbkxVersion())),
149                     goal("generate-xhtml5"),
150                     configuration(cfg.toArray(new Element[cfg.size()])),
151                     executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
152         }
153     }
154 }