1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.kathrynhuxtable.maven.plugins.htmlfiltersite;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27
28 import java.util.List;
29
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.project.MavenProject;
33
34 import org.codehaus.plexus.util.FileUtils;
35
36 import org.w3c.tidy.Configuration;
37 import org.w3c.tidy.Report;
38 import org.w3c.tidy.Tidy;
39
40
41
42
43
44
45
46
47
48
49 public class TidyMojo extends AbstractMojo {
50
51
52
53
54
55
56
57 private File tidySourceDirectory;
58
59
60
61
62
63
64
65
66 private File tidyTargetDirectory;
67
68
69
70
71
72
73
74 private String tidyFilePattern;
75
76
77
78
79
80
81
82
83 protected MavenProject project;
84
85
86
87
88
89
90 public void setTidySourceDirectory(File tidySourceDirectory) {
91 this.tidySourceDirectory = tidySourceDirectory;
92 }
93
94
95
96
97
98
99 public void setTidyTargetDirectory(File tidyTargetDirectory) {
100 this.tidyTargetDirectory = tidyTargetDirectory;
101 }
102
103
104
105
106
107
108 public void setTidyFilePattern(String tidyFilePattern) {
109 this.tidyFilePattern = tidyFilePattern;
110 }
111
112
113
114
115 public void execute() throws MojoExecutionException {
116 Tidy tidy = new Tidy();
117 Configuration configuration = tidy.getConfiguration();
118
119
120 tidy.setXHTML(true);
121 tidy.setCharEncoding(Configuration.UTF8);
122 tidy.setIndentContent(true);
123 tidy.setSmartIndent(true);
124 tidy.setQuiet(true);
125
126
127 configuration.adjust();
128
129 boolean writeBack = (tidySourceDirectory.equals(tidyTargetDirectory));
130 List<String> fileList = getFileList();
131 Errors errors = new Errors();
132
133 for (String file : fileList) {
134 tidyFile(tidy, file, errors, writeBack);
135 }
136
137 if (errors.totalErrors + errors.totalWarnings > 0) {
138 Report.generalInfo(tidy.getErrout());
139 }
140
141 if (tidy.getErrout() != tidy.getStderr()) {
142 tidy.getErrout().close();
143 }
144 }
145
146
147
148
149
150
151 @SuppressWarnings("unchecked")
152 private List<String> getFileList() {
153 List<String> fileList = null;
154
155 try {
156 fileList = FileUtils.getFileNames(tidySourceDirectory, tidyFilePattern, "", false, true);
157 } catch (IOException e) {
158
159 e.printStackTrace();
160 }
161
162 return fileList;
163 }
164
165
166
167
168
169
170
171
172
173
174
175 private void tidyFile(Tidy tidy, String file, Errors errors, boolean writeBack) throws MojoExecutionException {
176 File sourceFile = new File(tidySourceDirectory, file);
177 File targetFile = new File(tidyTargetDirectory, file);
178
179 if (writeBack) {
180 try {
181 targetFile = File.createTempFile("tidy", ".tmp", tidySourceDirectory);
182 } catch (IOException e) {
183 e.printStackTrace();
184 throw new MojoExecutionException("Unable to create temporary Tidy output file for " + targetFile, e);
185 }
186 } else {
187 File targetParentDirectory = targetFile.getParentFile();
188
189 if (!targetParentDirectory.exists()) {
190 targetParentDirectory.mkdirs();
191 }
192 }
193
194 try {
195 tidy.parse(new FileInputStream(sourceFile), new FileOutputStream(targetFile));
196 errors.totalWarnings += tidy.getParseWarnings();
197 errors.totalErrors += tidy.getParseErrors();
198 } catch (FileNotFoundException fnfe) {
199 Report.unknownFile(tidy.getErrout(), "htmlfilter-site:tidy", sourceFile.getAbsolutePath());
200 }
201
202 if (writeBack) {
203 try {
204 FileUtils.rename(targetFile, sourceFile);
205 } catch (IOException e) {
206 e.printStackTrace();
207 throw new MojoExecutionException("Unable to rename temporary Tidy output file for " + sourceFile, e);
208 }
209 }
210 }
211
212
213
214
215 private static class Errors {
216 int totalErrors;
217 int totalWarnings;
218
219
220
221
222 public Errors() {
223 totalErrors = 0;
224 totalWarnings = 0;
225 }
226 }
227 }