001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2013-2014 ForgeRock AS
015 */
016
017package org.forgerock.doc.maven.pre;
018
019import org.apache.maven.plugin.MojoExecutionException;
020import org.apache.maven.plugin.MojoFailureException;
021import org.forgerock.doc.maven.AbstractDocbkxMojo;
022import org.twdata.maven.mojoexecutor.MojoExecutor;
023
024/**
025 * Use the <a href="https://github.com/markcraig/xcite-maven-plugin"
026 * >XCite Maven plugin</a> to quote text files.
027 *
028 * <p>
029 *
030 * This class generates source including the citations.
031 * For example, if your DocBook source file includes
032 * the following &lt;programlisting&gt;:
033 *
034 * <pre>
035 * &lt;programlisting language=&quot;java&quot;
036 * &gt;[jcp:org.forgerock.doc.jcite.test.Test:--- mainMethod]&lt;/programlisting&gt;
037 * </pre>
038 *
039 * <p>
040 *
041 * Then this class replaces the citation with the code
042 * in between {@code // --- mainMethod} comments,
043 * suitable for inclusion in XML,
044 * and leaves the new file with the modifiable copy of the sources
045 * for further processing.
046 */
047public class XCite {
048
049    /**
050     * The Mojo that holds configuration and related methods.
051     */
052    private AbstractDocbkxMojo m;
053
054    /**
055     * The Executor for the XCite plugin.
056     */
057    private final Executor exec;
058
059    /**
060     * XCite plugin version.
061     */
062    private final String xCiteVersion;
063
064    /**
065     * Plexus utils version.
066     */
067    private final String plexusUtilsVersion;
068
069    /**
070     * Source directory for sources to XCite.
071     */
072    private final String sourceDir;
073
074    /**
075     * Escape XML characters in quotes from other files.
076     */
077    private final boolean escapeXml = true;
078
079    /**
080     * Resolve quotations only in XML files.
081     */
082    private final String includes = "**/*.xml";
083
084    /**
085     * Constructor setting the Mojo that holds the configuration.
086     *
087     * @param mojo The Mojo that holds the configuration.
088     */
089    public XCite(final AbstractDocbkxMojo mojo) {
090        m = mojo;
091        this.exec               = new Executor();
092        this.xCiteVersion       = m.getXCiteVersion();
093        this.plexusUtilsVersion = m.getPlexusUtilsVersion();
094        this.sourceDir          = m.path(m.getDocbkxModifiableSourcesDirectory());
095    }
096
097    /**
098     * Run XCite on the XML source files.
099     *
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}