View Javadoc

1   /*
2    * Copyright (c) 2010 Kathryn Huxtable.
3    *
4    * This file is part of the Image Generator Maven plugin.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   * $Id$
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   * Goal runs Velocity on the files in the specified directory.
42   *
43   * @description                  Runs JTidy on the files in the specified
44   *                               directory.
45   * @goal                         tidy
46   * @phase                        pre-site
47   * @requiresDependencyResolution runtime
48   */
49  public class TidyMojo extends AbstractMojo {
50  
51      /**
52       * Location of the source directory.
53       *
54       * @parameter expression="${htmlfiltersite.tidySourceDirectory}"
55       *            default-value="${basedir}/src/site/html"
56       */
57      private File tidySourceDirectory;
58  
59      /**
60       * Location of the target directory. May be the same as the source
61       * directory, in which case the original files will be overwritten.
62       *
63       * @parameter expression="${htmlfiltersite.tidyTargetDirectory}"
64       *            default-value="${project.build.directory}/generated-site/resources"
65       */
66      private File tidyTargetDirectory;
67  
68      /**
69       * Match pattern for the files to be processed..
70       *
71       * @parameter expression="${htmlfiltersite.tidyFilePattern}"
72       *            default-value="**\/*.html"
73       */
74      private String tidyFilePattern;
75  
76      /**
77       * The maven project.
78       *
79       * @parameter expression="${project}"
80       * @required
81       * @readonly
82       */
83      protected MavenProject project;
84  
85      /**
86       * DOCUMENT ME!
87       *
88       * @param tidySourceDirectory the tidySourceDirectory to set
89       */
90      public void setTidySourceDirectory(File tidySourceDirectory) {
91          this.tidySourceDirectory = tidySourceDirectory;
92      }
93  
94      /**
95       * DOCUMENT ME!
96       *
97       * @param tidyTargetDirectory the tidyTargetDirectory to set
98       */
99      public void setTidyTargetDirectory(File tidyTargetDirectory) {
100         this.tidyTargetDirectory = tidyTargetDirectory;
101     }
102 
103     /**
104      * DOCUMENT ME!
105      *
106      * @param tidyFilePattern the tidyFilePattern to set
107      */
108     public void setTidyFilePattern(String tidyFilePattern) {
109         this.tidyFilePattern = tidyFilePattern;
110     }
111 
112     /**
113      * @see org.apache.maven.plugin.AbstractMojo#execute()
114      */
115     public void execute() throws MojoExecutionException {
116         Tidy          tidy          = new Tidy();
117         Configuration configuration = tidy.getConfiguration();
118 
119         // Configure Tidy.
120         tidy.setXHTML(true);
121         tidy.setCharEncoding(Configuration.UTF8);
122         tidy.setIndentContent(true);
123         tidy.setSmartIndent(true);
124         tidy.setQuiet(true);
125 
126         // Ensure config is self-consistent.
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      * DOCUMENT ME!
148      *
149      * @return
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             // TODO Auto-generated catch block
159             e.printStackTrace();
160         }
161 
162         return fileList;
163     }
164 
165     /**
166      * Run Tidy on a single file.
167      *
168      * @param  tidy      the Tidy instance.
169      * @param  file      the file to be tidied.
170      * @param  errors    the Errors object to hold the error and warning totals.
171      * @param  writeBack TODO
172      *
173      * @throws MojoExecutionException DOCUMENT ME!
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      * Keep totals of errors and warnings.
214      */
215     private static class Errors {
216         int totalErrors;
217         int totalWarnings;
218 
219         /**
220          * Creates a new Errors object.
221          */
222         public Errors() {
223             totalErrors   = 0;
224             totalWarnings = 0;
225         }
226     }
227 }