1
2
3
4
5
6
7
8
9
10
11
12
13
14 package com.stephenduncanjr.xpathreplacement;
15
16 import java.io.File;
17 import java.io.FileReader;
18 import java.io.FileWriter;
19 import java.util.Map;
20
21 import org.apache.maven.plugin.AbstractMojo;
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.plugin.MojoFailureException;
24
25 /***
26 * Replaces matches of an XPath expression in a file.
27 *
28 * @author stephen.duncan (Stephen C. Duncan Jr.
29 * <stephen.duncan@gmail.com>)
30 * @since 1.0
31 *
32 * @goal process-file
33 * @phase process-resources
34 */
35 public class ProcessFileMojo extends AbstractMojo
36 {
37 /***
38 * File to make replacements to.
39 *
40 * @parameter expression="${xpathReplacement.input}"
41 * @required
42 */
43 private File inputFile;
44
45 /***
46 * Mapping of prefixes to namespaces in the XPath expression.
47 *
48 * @parameter
49 */
50 private Map<String, String> namespaceMappings;
51
52 /***
53 * Directory to place output in.
54 *
55 * @parameter expression="${xpathReplacement.outputDirectory}"
56 * default-value="${project.build.outputDirectory}"
57 */
58 private File outputDirectory;
59
60 /***
61 * Value to replace matches with.
62 *
63 * @parameter expression="${xpathReplacement.value}
64 * @required
65 */
66 private String value;
67
68 /***
69 * XPath expression to replace match.
70 *
71 * @parameter expression="${xpathReplacement.xpath}
72 * @required
73 */
74 private String xpath;
75
76 /***
77 * @see org.apache.maven.plugin.Mojo#execute()
78 */
79 public void execute() throws MojoExecutionException, MojoFailureException
80 {
81 final File outputFile = new File(this.outputDirectory.getAbsolutePath() + File.separator + this.inputFile.getName());
82
83 try
84 {
85 if(this.namespaceMappings == null)
86 {
87 XPathReplacement.replace(this.xpath, this.value, new FileReader(this.inputFile), new FileWriter(outputFile));
88 }
89 else
90 {
91 XPathReplacement.replace(this.xpath, this.namespaceMappings, this.value, new FileReader(this.inputFile), new FileWriter(outputFile));
92 }
93 }
94 catch(final Exception e)
95 {
96 throw new MojoExecutionException("Error performing replacement. Reason: " + e.getMessage(), e);
97 }
98 }
99 }