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 ForgeRock AS.
15   */
16  
17  package org.forgerock.maven.plugins.xcite;
18  
19  import org.apache.maven.plugin.AbstractMojo;
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.apache.maven.plugin.MojoFailureException;
22  import org.apache.maven.plugins.annotations.LifecyclePhase;
23  import org.apache.maven.plugins.annotations.Mojo;
24  import org.apache.maven.plugins.annotations.Parameter;
25  import org.forgerock.maven.plugins.xcite.utils.FileUtils;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  /**
31   * Copy quotes from source text files into target text files.
32   */
33  @Mojo(name = "cite", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
34  public class XCiteMojo extends AbstractMojo {
35  
36      /**
37       * Whether to escape XML characters in quotes.
38       */
39      @Parameter (defaultValue = "false")
40      private boolean escapeXml;
41  
42      /**
43       * Filter strings specifying files with citations to include.
44       */
45      @Parameter
46      private String[] includes;
47  
48      /**
49       * Filter strings specifying files to exclude.
50       */
51      @Parameter
52      private String[] excludes;
53  
54      /**
55       * Indent quotes this number of spaces from the left margin.
56       */
57      @Parameter (defaultValue = "0")
58      private int reindent;
59  
60      /**
61       * Output directory for files with quotes.
62       */
63      @Parameter (defaultValue = "${project.build.directory}/xcite")
64      private File outputDirectory;
65  
66      /**
67       * Source directory for files with citations.
68       */
69      @Parameter (defaultValue = "${basedir}/src/main")
70      private File sourceDirectory;
71  
72      /**
73       * Replace citations with quotes in included files,
74       * writing the resulting files in the output directory.
75       *
76       * @throws MojoExecutionException   Could not create output directory.
77       * @throws MojoFailureException     Failed to perform replacements.
78       */
79      @Override
80      public void execute() throws MojoExecutionException, MojoFailureException {
81  
82          if (!outputDirectory.exists()) {
83              if (!outputDirectory.mkdirs()) {
84                  throw new MojoExecutionException(
85                          "Failed to create output directory: "
86                                  + outputDirectory.getPath());
87              }
88          }
89  
90          String[] files = FileUtils.getIncludedFiles(sourceDirectory, includes, excludes);
91          Resolver resolver =
92                  new Resolver(outputDirectory, escapeXml, reindent, true);
93          try {
94              resolver.resolve(sourceDirectory, files);
95          } catch (IOException e) {
96              throw new MojoFailureException(e.getMessage());
97          }
98      }
99  }