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 2013-2014 ForgeRock AS
15   */
16  
17  package org.forgerock.doc.maven.pre;
18  
19  import org.apache.maven.plugin.MojoExecutionException;
20  import org.apache.maven.plugin.MojoFailureException;
21  import org.forgerock.doc.maven.AbstractDocbkxMojo;
22  import org.twdata.maven.mojoexecutor.MojoExecutor;
23  
24  /**
25   * Use the <a href="https://github.com/markcraig/xcite-maven-plugin"
26   * >XCite Maven plugin</a> to quote text files.
27   *
28   * <p>
29   *
30   * This class generates source including the citations.
31   * For example, if your DocBook source file includes
32   * the following &lt;programlisting&gt;:
33   *
34   * <pre>
35   * &lt;programlisting language=&quot;java&quot;
36   * &gt;[jcp:org.forgerock.doc.jcite.test.Test:--- mainMethod]&lt;/programlisting&gt;
37   * </pre>
38   *
39   * <p>
40   *
41   * Then this class replaces the citation with the code
42   * in between {@code // --- mainMethod} comments,
43   * suitable for inclusion in XML,
44   * and leaves the new file with the modifiable copy of the sources
45   * for further processing.
46   */
47  public class XCite {
48  
49      /**
50       * The Mojo that holds configuration and related methods.
51       */
52      private AbstractDocbkxMojo m;
53  
54      /**
55       * The Executor for the XCite plugin.
56       */
57      private final Executor exec;
58  
59      /**
60       * XCite plugin version.
61       */
62      private final String xCiteVersion;
63  
64      /**
65       * Plexus utils version.
66       */
67      private final String plexusUtilsVersion;
68  
69      /**
70       * Source directory for sources to XCite.
71       */
72      private final String sourceDir;
73  
74      /**
75       * Escape XML characters in quotes from other files.
76       */
77      private final boolean escapeXml = true;
78  
79      /**
80       * Resolve quotations only in XML files.
81       */
82      private final String includes = "**/*.xml";
83  
84      /**
85       * Constructor setting the Mojo that holds the configuration.
86       *
87       * @param mojo The Mojo that holds the configuration.
88       */
89      public XCite(final AbstractDocbkxMojo mojo) {
90          m = mojo;
91          this.exec               = new Executor();
92          this.xCiteVersion       = m.getXCiteVersion();
93          this.plexusUtilsVersion = m.getPlexusUtilsVersion();
94          this.sourceDir          = m.path(m.getDocbkxModifiableSourcesDirectory());
95      }
96  
97      /**
98       * Run XCite on the XML source files.
99       *
100      * @throws MojoExecutionException   Could not create output directory.
101      * @throws MojoFailureException     Failed to perform replacements.
102      */
103     public void execute() throws MojoExecutionException, MojoFailureException {
104 
105         // Run the XCite plugin on the files in place.
106         exec.runXCite();
107     }
108 
109     /**
110      * Enclose methods to run plugins.
111      */
112     class Executor extends MojoExecutor {
113 
114         /**
115          * Run XCite on the DocBook XML source files.
116          *
117          * @throws MojoExecutionException   Could not create output directory.
118          * @throws MojoFailureException     Failed to perform replacements.
119          */
120         void runXCite() throws MojoExecutionException, MojoFailureException {
121 
122             executeMojo(
123                     plugin(
124                             groupId("org.wrensecurity.commons"),
125                             artifactId("xcite-maven-plugin"),
126                             version(xCiteVersion),
127                             dependencies(
128                                     dependency(
129                                             groupId("org.codehaus.plexus"),
130                                             artifactId("plexus-utils"),
131                                             version(plexusUtilsVersion)))),
132                     goal("cite"),
133                     configuration(
134                             element(name("sourceDirectory"), sourceDir),
135                             element(name("outputDirectory"), sourceDir),
136                             element(name("escapeXml"), Boolean.toString(escapeXml)),
137                             element(name("includes"),
138                                     element(name("include"), includes))),
139                     executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
140         }
141     }
142 }