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.commons.io.FileUtils;
20  import org.apache.commons.io.FilenameUtils;
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.codehaus.plexus.util.xml.Xpp3Dom;
23  import org.forgerock.doc.maven.AbstractDocbkxMojo;
24  import org.forgerock.doc.maven.utils.FilteredFileCopier;
25  import org.twdata.maven.mojoexecutor.MojoExecutor;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  /**
31   * Use <a href="http://arrenbrecht.ch/jcite/">JCite</a> to quote Java code.
32   *
33   * <p>
34   *
35   * This class generates source including the citations.
36   * For example, if your DocBook source file includes
37   * the following &lt;programlisting&gt;:
38   *
39   * <pre>
40   * &lt;programlisting language=&quot;java&quot;
41   * &gt;[jcp:org.forgerock.doc.jcite.test.Test:--- mainMethod]&lt;/programlisting&gt;
42   * </pre>
43   *
44   * <p>
45   *
46   * Then this class replaces the citation with the code
47   * in between {@code // --- mainMethod} comments,
48   * suitable for inclusion in XML,
49   * and leaves the new file with the modifiable copy of the sources
50   * for further processing.
51   */
52  public class JCite {
53  
54      /**
55       * The Mojo that holds configuration and related methods.
56       */
57      private AbstractDocbkxMojo m;
58  
59      /**
60       * The Executor to run JCite.
61       */
62      private final Executor executor;
63  
64      /**
65       * Constructor setting the Mojo that holds the configuration.
66       *
67       * @param mojo The Mojo that holds the configuration.
68       */
69      public JCite(final AbstractDocbkxMojo mojo) {
70          m = mojo;
71          this.executor = new Executor();
72          sourceDir = m.path(m.getDocbkxModifiableSourcesDirectory());
73          tempOutputDirectory = new File(m.getBuildDirectory(), "docbkx-jcite");
74          outputDir = m.path(tempOutputDirectory);
75      }
76  
77      // JCite the sources in the modifiable copy.
78      private final String sourceDir;
79  
80      // JCite to a temporary directory.
81      private final File tempOutputDirectory;
82      private final String outputDir;
83  
84      /**
85       * Run JCite on the XML source files.
86       *
87       * @throws MojoExecutionException Failed to run JCite.
88       */
89      public void execute() throws MojoExecutionException {
90  
91          // JCite to a temporary directory...
92          executor.runJCite();
93  
94          // ...and then overwrite the copy of sources with the new files.
95          try {
96              FilteredFileCopier.copyOthers(
97                      ".xml", m.getDocbkxModifiableSourcesDirectory(), tempOutputDirectory);
98              FileUtils.copyDirectory(tempOutputDirectory, m.getDocbkxModifiableSourcesDirectory());
99              FileUtils.deleteDirectory(tempOutputDirectory);
100         } catch (IOException e) {
101             throw new MojoExecutionException(e.getMessage(), e);
102         }
103     }
104 
105     /**
106      * Enclose methods to run plugins.
107      */
108     class Executor extends MojoExecutor {
109 
110         /**
111          * Run JCite on the DocBook XML source files.
112          *
113          * @throws MojoExecutionException Failed to run JCite.
114          */
115         void runJCite() throws MojoExecutionException {
116 
117             // mojo-executor lacks fluent support for element attributes.
118             // You can hack around this by including attributes
119             // in the name of elements without children.
120             // But the hack does not work for elements with children:
121             // SAX barfs on closing tags containing a bunch of attributes.
122             Xpp3Dom mkdir = new Xpp3Dom("mkdir");
123             mkdir.setAttribute("dir", outputDir);
124 
125             Xpp3Dom taskdef = new Xpp3Dom("taskdef");
126             taskdef.setAttribute("name", "jcite");
127             taskdef.setAttribute("classname", "ch.arrenbrecht.jcite.JCiteTask");
128             taskdef.setAttribute("classpathref", "maven.plugin.classpath");
129 
130             Xpp3Dom jcite = new Xpp3Dom("jcite");
131             jcite.setAttribute("srcdir", sourceDir);
132             jcite.setAttribute("destdir", outputDir);
133 
134             // Might have multiple paths to sources.
135             Xpp3Dom sourcepath = new Xpp3Dom("sourcepath");
136             if (m.getJCiteSourcePaths() != null && !m.getJCiteSourcePaths().isEmpty()) {
137                 for (File sourcePath : m.getJCiteSourcePaths()) {
138                     String location = FilenameUtils
139                             .separatorsToSystem(sourcePath.getPath());
140                     Xpp3Dom pathelement = new Xpp3Dom("pathelement");
141                     pathelement.setAttribute("location", location);
142                     sourcepath.addChild(pathelement);
143                 }
144             } else { // No source path defined. Try src/main/java.
145                 Xpp3Dom pathelement = new Xpp3Dom("pathelement");
146                 pathelement.setAttribute("location", "src/main/java");
147                 sourcepath.addChild(pathelement);
148             }
149             jcite.addChild(sourcepath);
150 
151             Xpp3Dom include = new Xpp3Dom("include");
152             include.setAttribute("name", "**/*.xml");
153             jcite.addChild(include);
154 
155             Xpp3Dom target = new Xpp3Dom("target");
156             target.addChild(mkdir);
157             target.addChild(taskdef);
158             target.addChild(jcite);
159 
160             Xpp3Dom configuration = new Xpp3Dom("configuration");
161             configuration.addChild(target);
162 
163             executeMojo(
164                     plugin(
165                             groupId("org.apache.maven.plugins"),
166                             artifactId("maven-antrun-plugin"),
167                             version("1.7"),
168                             dependencies(
169                                     dependency(
170                                     // See https://code.google.com/r/markcraig-jcite/.
171                                             groupId("org.mcraig"),
172                                             artifactId("jcite"),
173                                             version(m.getJCiteVersion())))),
174                     goal("run"),
175                     configuration,
176                     executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
177         }
178     }
179 }