1 /*
2 * Copyright 2011 Andres Gomez Casanova
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.mgaip;
17
18 import java.io.File;
19 import java.io.FilenameFilter;
20 import java.util.Locale;
21
22 import org.apache.maven.doxia.sink.Sink;
23 import org.apache.maven.doxia.siterenderer.Renderer;
24 import org.apache.maven.project.MavenProject;
25 import org.apache.maven.reporting.AbstractMavenReport;
26
27 /**
28 * Inserts Google Ads in the generated Maven site.
29 * <p>
30 * <b>Control Version</b>
31 * <p>
32 * <ul>
33 * <li>1.0 Class creation.</li>
34 * </ul>
35 *
36 * @author Andres Gomez Casanova <a
37 * href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
38 * @version 1.0 20110507
39 * @goal insert
40 * @phase post-site
41 * @since 1.0
42 */
43 public final class AdInserterMojo extends AbstractMavenReport {
44
45 /**
46 * Custom script to insert in the vertical zone as a powered by.
47 * <p>
48 *
49 * @parameter
50 */
51 String customPoweredBy;
52
53 /**
54 * Link to associate in the I like Facebook button.
55 * <p>
56 * The link has to be in this form:
57 * <p>
58 * <code>https%3A%2F%2Fwww.facebook.com%2Fpages%2FZemucan%2F138284808329
59 * </code>
60 * <p>
61 * Replacing the ':' by %3A, '/' by %2F
62 * <p>
63 *
64 * @parameter
65 */
66 String facebookLink;
67
68 /**
69 * Identifier of the Google Account. This is the google_ad_client field.
70 *
71 * @parameter default-value="ca-pub-9663595078690574"
72 */
73 String googleId;
74 /**
75 * Adds a Google Plus one script in the pages.
76 *
77 * @parameter
78 */
79 boolean googlePlusOne;
80
81 /**
82 * Height of the horizontal ad.
83 *
84 * @parameter default-value=90
85 */
86 int horizontalAdHeight;
87 /**
88 * Identifier of the Horizontal Ad. This is the google_ad_slot field.
89 * <p>
90 * This is placed everywhere in the text.
91 *
92 * @parameter default-value="2519400815"
93 */
94 String horizontalAdId;
95 /**
96 * Width of the horizontal ad.
97 *
98 * @parameter default-value=728
99 */
100 int horizontalAdWidth;
101 /**
102 * Height of the logo ad.
103 *
104 * @parameter default-value=200
105 */
106 int logoAdHeight;
107 /**
108 * Identifier of the Logo Ad. This is the google_ad_slot field.
109 * <p>
110 * This could be used as a logo replacement.
111 *
112 * @parameter default-value="3789968358"
113 */
114 String logoAdId;
115 /**
116 * Width of the logo ad.
117 *
118 * @parameter default-value=200
119 */
120 int logoAdWidth;
121 /**
122 * Directory where reports will go.
123 *
124 * @parameter expression="${project.reporting.outputDirectory}"
125 * @required
126 * @readonly
127 */
128 private File outputDirectory;
129 /**
130 * @parameter default-value="${project}"
131 * @required
132 * @readonly
133 */
134 private MavenProject project;
135 /**
136 * @component
137 * @required
138 * @readonly
139 */
140 private Renderer siteRenderer;
141 /**
142 * Height of the unique ad.
143 *
144 * @parameter default-value=60
145 */
146 int uniqueAdHeight;
147 /**
148 * Identifier of the Unique Ad. This is the google_ad_slot field.
149 * <p>
150 * Placed inline the text.
151 *
152 * @parameter default-value="1788050126"
153 */
154 String uniqueAdId;
155 /**
156 * Width of the unique ad.
157 *
158 * @parameter default-value=234
159 */
160 int uniqueAdWidth;
161 /**
162 * Height of the vertical ad.
163 *
164 * @parameter default-value=600
165 */
166 int verticalAdHeight;
167 /**
168 * Identifier of the Vertical Ad. This is the google_ad_slot field.
169 * <p>
170 * This is placed at the vertical left side of the site, as a powered by
171 * icon.
172 *
173 * @parameter default-value="9409358178"
174 */
175 String verticalAdId;
176 /**
177 * Width of the vertical ad.
178 *
179 * @parameter default-value=120
180 */
181 int verticalAdWidth;
182
183 /**
184 * Creates the report via Sink.
185 */
186 private void createReport() {
187 // FIXME when re-executing the plugin, the report becomes corrupt.
188
189 // Retrieves the Sink API.
190 Sink sink = (Sink) getSink();
191
192 // Header.
193 sink.head();
194 sink.title();
195 sink.text("mgaip plugin configuration");
196 sink.title_();
197 sink.head_();
198
199 // Body.
200 // Google Ads
201 sink.body();
202 sink.section1();
203 sink.sectionTitle1();
204 sink.text("Google Ads");
205 sink.sectionTitle1_();
206
207 sink.paragraph();
208 sink.text("These are the configured ads for user ");
209 sink.bold();
210 sink.text(this.googleId);
211 sink.bold_();
212 sink.paragraph_();
213
214 sink.table();
215
216 sink.tableRow();
217 sink.tableHeaderCell();
218 sink.text("Ad type");
219 sink.tableHeaderCell_();
220 sink.tableHeaderCell();
221 sink.text("Slot");
222 sink.tableHeaderCell_();
223 sink.tableHeaderCell();
224 sink.text("Width");
225 sink.tableHeaderCell_();
226 sink.tableHeaderCell();
227 sink.text("Height");
228 sink.tableHeaderCell_();
229 sink.tableRow_();
230
231 sink.tableRow();
232 sink.tableCell();
233 sink.text("Horizontal");
234 sink.tableCell_();
235 sink.tableCell();
236 sink.text(this.horizontalAdId);
237 sink.tableCell_();
238 sink.tableCell();
239 sink.text("" + this.horizontalAdWidth);
240 sink.tableCell_();
241 sink.tableCell();
242 sink.text("" + this.horizontalAdHeight);
243 sink.tableCell_();
244 sink.tableRow_();
245
246 sink.tableRow();
247 sink.tableCell();
248 sink.text("Logo");
249 sink.tableCell_();
250 sink.tableCell();
251 sink.text(this.logoAdId);
252 sink.tableCell_();
253 sink.tableCell();
254 sink.text("" + this.logoAdWidth);
255 sink.tableCell_();
256 sink.tableCell();
257 sink.text("" + this.logoAdHeight);
258 sink.tableCell_();
259 sink.tableRow_();
260
261 sink.tableRow();
262 sink.tableCell();
263 sink.text("Unique");
264 sink.tableCell_();
265 sink.tableCell();
266 sink.text(this.uniqueAdId);
267 sink.tableCell_();
268 sink.tableCell();
269 sink.text("" + this.uniqueAdWidth);
270 sink.tableCell_();
271 sink.tableCell();
272 sink.text("" + this.uniqueAdHeight);
273 sink.tableCell_();
274 sink.tableRow_();
275
276 sink.tableRow();
277 sink.tableCell();
278 sink.text("Vertical");
279 sink.tableCell_();
280 sink.tableCell();
281 sink.text(this.verticalAdId);
282 sink.tableCell_();
283 sink.tableCell();
284 sink.text("" + this.verticalAdWidth);
285 sink.tableCell_();
286 sink.tableCell();
287 sink.text("" + this.verticalAdHeight);
288 sink.tableCell_();
289 sink.tableRow_();
290
291 sink.table_();
292
293 sink.section1_();
294
295 // Facebook I like.
296 if (this.facebookLink != null && !this.facebookLink.equals("")) {
297 sink.section1();
298 sink.sectionTitle1();
299 sink.text("Facebook I Like");
300 sink.sectionTitle1_();
301
302 sink.paragraph();
303 sink.text("This is the configuration for the Facebook I Like "
304 + "buttom.");
305 sink.lineBreak();
306 sink.text("Link: ");
307 sink.bold();
308 sink.link(this.facebookLink);
309 sink.text(this.facebookLink);
310 sink.link_();
311 sink.bold_();
312 sink.paragraph_();
313 sink.section1_();
314 }
315
316 // Custom powered by.
317 if (this.customPoweredBy != null && !this.customPoweredBy.equals("")) {
318 sink.section1();
319 sink.sectionTitle1();
320 sink.text("Custom Powered By");
321 sink.sectionTitle1_();
322
323 sink.paragraph();
324 sink.text("This is the custom configuration:");
325 sink.paragraph_();
326
327 sink.paragraph();
328 sink.monospaced();
329 sink.rawText(this.customPoweredBy);
330 sink.monospaced_();
331 sink.paragraph_();
332 sink.section1_();
333 }
334
335 sink.body_();
336
337 sink.flush();
338 sink.close();
339 }
340
341 /*
342 * (non-Javadoc)
343 *
344 * @see org.apache.maven.plugin.Mojo#execute()
345 */
346 public void executeReport(final Locale/* ! */locale) {
347 // Custom logger for debug messages.
348 // this.setLog(new MgaipLogger());
349
350 this.getLog().info("Starting mgaip");
351 this.getLog().info("Google Id: " + this.googleId);
352 this.getLog().debug("Vertical Ad: " + this.verticalAdId);
353 this.getLog().debug("Horizontal Ad: " + this.horizontalAdId);
354 this.getLog().debug("Unique Ad: " + this.uniqueAdId);
355 this.getLog().debug("Logo Ad: " + this.logoAdId);
356
357 this.createReport();
358
359 this.replaceFiles(this.outputDirectory.getPath());
360 }
361
362 /*
363 * (non-Javadoc)
364 *
365 * @see
366 * org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
367 */
368 public String getDescription(final Locale/* ! */locale) {
369 return "Includes Google publicity in the generated site.";
370 }
371
372 /*
373 * (non-Javadoc)
374 *
375 * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
376 */
377 public String/* ! */getName(final Locale/* ! */locale) {
378 return "Mgaip";
379 }
380
381 /*
382 * (non-Javadoc)
383 *
384 * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
385 */
386 protected String/* ! */getOutputDirectory() {
387 return outputDirectory.getAbsolutePath();
388 }
389
390 /*
391 * (non-Javadoc)
392 *
393 * @see org.apache.maven.reporting.MavenReport#getOutputName()
394 */
395 public String/* ! */getOutputName() {
396 return "mgaip";
397 }
398
399 /*
400 * (non-Javadoc)
401 *
402 * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
403 */
404 protected MavenProject/* ! */getProject() {
405 return this.project;
406 }
407
408 /*
409 * (non-Javadoc)
410 *
411 * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
412 */
413 protected Renderer/* ! */getSiteRenderer() {
414 return this.siteRenderer;
415 }
416
417 /**
418 * Writes in the log in the given level.
419 *
420 * @param level
421 * Level of the log to write: 1 debug, 2 info, 3 warn, 4 error.
422 * @param message
423 * Message to print.
424 */
425 void log(final int level, final String/* ! */message) {
426 switch (level) {
427 case 1:
428 this.getLog().debug(message);
429 break;
430 case 2:
431 this.getLog().info(message);
432 break;
433 case 3:
434 this.getLog().warn(message);
435 break;
436 case 4:
437 this.getLog().error(message);
438 break;
439 default:
440 this.getLog().info(message);
441 break;
442 }
443 }
444
445 /**
446 * Prints the content of an exception.
447 *
448 * @param exception
449 * Exception to print.
450 */
451 void log(final Throwable/* ! */exception) {
452 this.getLog().debug(exception);
453 }
454
455 /**
456 * Creates a list of files to modify and then replace the tags of them.
457 */
458 private void replaceFiles(final String /* ! */directory) {
459 // Retrieves only the HTML files.
460 final File file = new File(directory);
461 String[] files = file.list(new FilenameFilter() {
462
463 /*
464 * (non-Javadoc)
465 *
466 * @see java.io.FilenameFilter#accept(java.io.File,
467 * java.lang.String)
468 */
469 public boolean accept(File dir, String name) {
470 boolean ret = false;
471 if (name.startsWith("mgaip")) {
472 ret = false;
473 } else if (name.endsWith("html")) {
474 ret = true;
475 }
476 return ret;
477 }
478
479 }
480
481 );
482
483 Replacer replacer = new Replacer(this);
484
485 if (files != null) {
486 this.getLog().debug("Files to process:");
487 for (int i = 0; i < files.length; i++) {
488 final String filename = files[i];
489 replacer.replace(directory, filename);
490 }
491 } else {
492 this.getLog().warn("No files to process in " + directory);
493 }
494
495 // Recursive part
496 String[] directories = file.list(new FilenameFilter() {
497
498 /*
499 * (non-Javadoc)
500 *
501 * @see java.io.FilenameFilter#accept(java.io.File,
502 * java.lang.String)
503 */
504 public boolean accept(File dir, String name) {
505 boolean ret = false;
506 File file = new File(dir.getPath() + File.separatorChar + name);
507 if (file.isDirectory()) {
508 ret = true;
509 }
510 return ret;
511 }
512 });
513 int size = directories.length;
514 for (int i = 0; i < size; i++) {
515 this.getLog().info(
516 "Directory to process: " + directory + File.separatorChar
517 + directories[i]);
518 this.replaceFiles(directory + File.separatorChar + directories[i]);
519 }
520 }
521 }