View Javadoc

1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package gr.abiss.mvn.plugins.jstools;
15  
16  import java.io.File;
17  import java.io.FileInputStream;
18  import java.io.IOException;
19  import java.util.HashSet;
20  import java.util.Iterator;
21  import java.util.Locale;
22  import java.util.ResourceBundle;
23  import java.util.Set;
24  import org.apache.maven.project.MavenProject;
25  import org.apache.maven.project.MavenProjectHelper;
26  import org.apache.maven.reporting.AbstractMavenReport;
27  import org.apache.maven.reporting.MavenReportException;
28  import org.codehaus.doxia.site.renderer.SiteRenderer;
29  import org.codehaus.plexus.archiver.manager.ArchiverManager;
30  import org.codehaus.plexus.util.DirectoryScanner;
31  
32  /***
33   * @version $Id$
34   * @author manos
35   */
36  public abstract class AbstractBaseJstoolsReport extends AbstractMavenReport{
37  	
38  	/***
39  	 * The output directory.
40  	 * 
41  	 * @parameter expression="${project.build.directory}/site/"
42  	 */
43  	protected String outputBaseDirectory;
44  
45  	/***
46  	 * <i>Maven Internal</i>: The Project descriptor.
47  	 * 
48  	 * @parameter expression="${project}"
49  	 * @readonly
50  	 */
51  	protected MavenProject project;
52  
53  	/***
54  	 * <i>Maven Internal</i>: A Project Helper instance.
55  	 * 
56  	 * @component
57  	 */
58  	protected MavenProjectHelper helper;
59  
60  	/***
61  	 * <i>Maven Internal</i>: The Doxia Site Renderer.
62  	 * 
63  	 * @component
64  	 */
65  	protected SiteRenderer siteRenderer;
66  
67  	/***
68  	 * <i>Maven Internal</i>: To look up Archiver/UnArchiver implementations
69  	 * 
70  	 * @parameter expression="${component.org.codehaus.plexus.archiver.manager.ArchiverManager}"
71  	 * @required
72  	 * @readonly
73  	 */
74  	protected ArchiverManager archiverManager;
75  
76  	/***
77  	 * <i>Maven Internal</i>: The build directory. Default is
78  	 * ${project.build.directory}
79  	 * 
80  	 * @parameter expression="${project.build.directory}"
81  	 * @required
82  	 */
83  	protected String buildDir;
84  
85  	/***
86  	 * <i>Maven Internal</i>: The base directory. Default is ${basedir}
87  	 * 
88  	 * @parameter expression="${basedir}"
89  	 * @required
90  	 */
91  	protected File baseDir;
92  
93  	/***
94  	 * The path to the JavaScript source directory (appended to ${basedir}).
95  	 * Default is src/main/js
96  	 * 
97  	 * @parameter expression="src/main/js"
98  	 */
99  	private String jsDir;
100 
101 	/***
102 	 * The include pattern used to select javascript files for processing. Default is all (recursive) files with a .js
103 	 * extention
104      * @parameter
105 	 */
106 	protected String includes;
107 
108 	/***
109 	 * The excluded files pattern. Default is empty.
110      * @parameter expression=""
111 	 */
112 	protected String excludes;
113 	
114 	/***
115 	 * Whether the file selection patterns should be case sensitive. Default is <code>true</code>.
116 	 * @parameter expression="true"
117 	 */
118 	protected boolean caseSensitive;
119 	
120 	/***
121 	 * @return
122 	 */
123 	public abstract String getBundleKey();
124 
125 	/***
126 	 * {@inheritDoc}
127 	 * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
128 	 */
129 	protected SiteRenderer getSiteRenderer() {
130 		return this.siteRenderer;
131 	}
132 
133 	/***
134 	 * {@inheritDoc}
135 	 * @see org.apache.maven.reporting.AbstractMavenReport#getDescription(java.util.Locale)
136 	 */
137 	public String getDescription(Locale locale) {
138 		return this.getBundle(locale).getString(
139 				"report." + getBundleKey() + ".description");
140 	}
141 
142 	/***
143 	 * {@inheritDoc}
144 	 * @see org.apache.maven.reporting.AbstractMavenReport#getName(java.util.Locale)
145 	 */
146 	public String getName(Locale locale) {
147 		return this.getBundle(locale).getString(
148 				"report." + getBundleKey() + ".name");
149 	}
150 
151 	/***
152 	 * @see org.apache.maven.reporting.AbstractMavenReport#getOutputName()
153 	 */
154 	public String getOutputName() {
155 		return getBundleKey() + "/index";
156 	}
157 
158 	/***
159 	 * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
160 	 */
161 	protected String getOutputDirectory() {
162 		return this.outputBaseDirectory + "/" + getBundleKey();
163 	}
164 
165 	/***
166 	 * Loads the locale dependent mojo configuration
167 	 * 
168 	 * @param locale
169 	 * @return the bundle corresponding to the given locale if available or the
170 	 *         default locale otherwise
171 	 */
172 	protected ResourceBundle getBundle(Locale locale) {
173 		return ResourceBundle.getBundle("jstools", locale, this.getClass()
174 				.getClassLoader());
175 	}
176 
177 	/***
178 	 * {@inheritDoc}
179 	 * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
180 	 */
181 	protected MavenProject getProject() {
182 		return this.project;
183 	}
184 
185 	/***
186 	 * Call the subclass implementations of setUp(java.util.Locale),
187 	 * doGenerateReport(java.util.Locale) and tearDown(java.util.Locale)
188 	 * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
189 	 */
190 	public final void executeReport(Locale defaultLocale)
191 			throws MavenReportException {
192 		if (this.canGenerateReport()) {
193 			// call subclass methods
194 			this.setUp(defaultLocale);
195 			this.doGenerateReport(defaultLocale);
196 			this.tearDown(defaultLocale);
197 		}
198 	}
199 
200 	/***
201 	 * Subclasses must implement this method instead of
202 	 * org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
203 	 */
204 	public abstract void doGenerateReport(Locale defaultLocale)
205 			throws MavenReportException;
206 
207 	/***
208 	 * @return the text of a file as a String
209 	 * @throws IOException
210 	 */
211 	protected String getFileAsString(String path) throws IOException {
212 		String s = "";
213 		// Read jslint code
214 		FileInputStream f = new FileInputStream(path);
215 		int x = f.available();
216 		byte b[] = new byte[x];
217 		f.read(b);
218 		s = new String(b);
219 		return s;
220 	}
221 
222 	/***
223 	 * Get the list of JavaScript source files for this project
224 	 * @returnvthe list of JavaScript source files for this project
225 	 * @throws MavenReportException
226 	 */
227 	protected Set<File> getJavaScriptFiles(){ 
228 		DirectoryScanner ds = new DirectoryScanner();
229 		// base dir
230 		if(this.jsDir == null || this.jsDir.length() == 0){
231 			this.jsDir = "src/main/js";
232 		}
233 		ds.setBasedir(this.jsDir);
234 		// includes
235 		if(this.includes == null || this.includes.length() == 0){
236 			this.includes = "**/*.js";
237 		}
238 		ds.setIncludes(this.includes.split(" "));
239 		// excludes
240 		if(this.excludes != null && this.excludes.length() > 0){
241 			ds.setExcludes(this.excludes.split(" "));
242 		}
243 		ds.addDefaultExcludes();
244 		// case sensitivity
245 	   ds.setCaseSensitive(true);
246 	   // scan
247 	   ds.scan();
248 	   // build a file set.	   
249 	   Set<File> files = new HashSet<File>();
250 	   // paths are relative to baseDir
251 	   String[] relPaths = ds.getIncludedFiles();
252 	   for(int i=0;i < relPaths.length;i++){
253 		   File file = new File(this.jsDir, relPaths[i]);
254 		   files.add(file);
255 		   this.getLog().debug("File to process: "+file.getAbsolutePath());
256 	   }
257 	   return files;
258     }
259 	/***
260 	 * Perform any required initialization
261 	 * @param defaultLocale
262 	 * @throws MavenReportException
263 	 */
264 	protected abstract void setUp(Locale defaultLocale)
265 			throws MavenReportException;
266 
267 	/***
268 	 * Perform any required cleanup
269 	 * @param defaultLocale
270 	 * @throws MavenReportException
271 	 */
272 	protected abstract void tearDown(Locale defaultLocale)
273 			throws MavenReportException;
274 }