1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.mgaip;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStreamWriter;
24 import java.io.Writer;
25
26 import org.jdom.Comment;
27 import org.jdom.Document;
28 import org.jdom.Element;
29 import org.jdom.JDOMException;
30 import org.jdom.Namespace;
31 import org.jdom.input.SAXBuilder;
32 import org.jdom.output.Format;
33 import org.jdom.output.XMLOutputter;
34 import org.jdom.xpath.XPath;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public final class Replacer {
52
53
54
55
56 private static final int HORIZONTAL_AD = 2;
57
58
59
60 private static final int LOGO_AD = 4;
61
62
63
64 private static final int UNIQUE_AD = 3;
65
66
67
68 private static final int VERTICAL_AD = 1;
69
70
71
72
73 private AdInserterMojo report;
74
75
76
77
78
79
80
81 Replacer(final AdInserterMojo
82 this.report = caller;
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96 private boolean changeHTMLTag(final Element
97 final String
98 Element parelement = single.getParentElement();
99 single.detach();
100 single.removeChild("img");
101 Element script = new Element("script");
102 script.setAttribute("type", "text/javascript");
103 Comment comment = new Comment(content);
104 script.addContent(comment);
105 parelement.addContent(script);
106 Element common = this.getCommon();
107 parelement.addContent(common);
108 return true;
109 }
110
111
112
113
114
115
116
117
118 private String
119
120
121 String ret = "\n";
122 ret += "google_ad_client = \"" + report.googleId + "\";\n";
123 switch (type) {
124 case HORIZONTAL_AD:
125 ret += "google_ad_slot = \"" + report.horizontalAdId + "\";\n";
126 ret += "google_ad_width = " + report.horizontalAdWidth + ";\n";
127 ret += "google_ad_height = " + report.horizontalAdHeight + ";\n";
128 break;
129 case LOGO_AD:
130 ret += "google_ad_slot = \"" + report.logoAdId + "\";\n";
131 ret += "google_ad_width = " + report.logoAdWidth + ";\n";
132 ret += "google_ad_height = " + report.logoAdHeight + ";\n";
133 break;
134 case UNIQUE_AD:
135 ret += "google_ad_slot = \"" + report.uniqueAdId + "\";\n";
136 ret += "google_ad_width = " + report.uniqueAdWidth + ";\n";
137 ret += "google_ad_height = " + report.uniqueAdHeight + ";\n";
138 break;
139 case VERTICAL_AD:
140 ret += "google_ad_slot = \"" + report.verticalAdId + "\";\n";
141 ret += "google_ad_width = " + report.verticalAdWidth + ";\n";
142 ret += "google_ad_height = " + report.verticalAdHeight + ";\n";
143 break;
144 default:
145 this.report.log(2, "No content assigned.");
146 break;
147 }
148 ret += "//";
149
150 return ret;
151 }
152
153
154
155
156 private Element
157 Element common = new Element("script");
158 common.setAttribute("type", "text/javascript");
159
160 common.setAttribute("src",
161 "http://pagead2.googlesyndication.com/pagead/show_ads.js");
162 common.setText("mgaip");
163 return common;
164 }
165
166
167
168
169
170
171
172
173
174
175
176 private boolean
177 Element parelement = single.getParentElement();
178 single.detach();
179 single.removeChild("img");
180 Element custom = new Element("div");
181
182 custom.addContent(report.customPoweredBy);
183 parelement.addContent(custom);
184 return true;
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225 private boolean
226 String src = "http://www.facebook.com/plugins/like.php";
227 src += "?href=" + report.facebookLink + "&";
228 src += "send=false&layout=box_count&width=150&";
229 src += "show_faces=false&action=like&colorscheme=light&";
230 src += "font&height=90";
231 String style = "border:none; overflow:hidden; width:150px; "
232 + "height:90px;";
233
234 Element parelement = single.getParentElement();
235 single.detach();
236 single.removeChild("img");
237 Element iframe = new Element("iframe");
238 iframe.setAttribute("src", src);
239 iframe.setAttribute("scrolling", "no");
240 iframe.setAttribute("frameborder", "0");
241 iframe.setAttribute("style", style);
242 iframe.setAttribute("allowTransparency", "true");
243 iframe.setText("mgaip");
244 parelement.addContent(iframe);
245 return true;
246 }
247
248
249
250
251
252
253
254
255 private boolean getGooglePlusOne(final Element
256 Element parelement = single.getParentElement();
257 single.detach();
258 single.removeChild("img");
259 Element plusone = new Element("plusone", "g",
260 "http://www.w3.org/1999/xhtml");
261 parelement.addContent(plusone);
262 plusone.setText("mgaip");
263 return true;
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343 private boolean htmlReplacement(final Element
344 throws JDOMException {
345 boolean modified = false;
346
347 Namespace xhtml = Namespace.getNamespace("xhtml",
348 "http://www.w3.org/1999/xhtml");
349
350
351
352
353
354 XPath xpath = XPath.newInstance("//xhtml:div[@id='leftColumn']"
355 + "//xhtml:div[@id='navcolumn']//xhtml:a[@class='poweredBy' "
356 + "and @title='googleAds']");
357 xpath.addNamespace(xhtml);
358 Element single = (Element) xpath.selectSingleNode(elem);
359 if (single != null) {
360 this.report.log(1, "Powered - Vertical");
361 modified = this.changeHTMLTag(single,
362 this.getAdContent(VERTICAL_AD));
363 }
364
365
366 xpath = XPath.newInstance("//xhtml:div[@id='bodyColumn']"
367 + "/xhtml:div[@id='contentBox']/xhtml:div[@class='section']"
368 + "/xhtml:img[@src='googleAds' and @alt='googleAds']");
369 xpath.addNamespace(xhtml);
370 single = (Element) xpath.selectSingleNode(elem);
371 if (single != null) {
372 this.report.log(1, "Horizontal");
373 modified = this.changeHTMLTag(single,
374 this.getAdContent(HORIZONTAL_AD));
375 }
376
377
378 xpath = XPath.newInstance("//xhtml:div[@id='bodyColumn']"
379 + "/xhtml:div[@id='contentBox']"
380 + "/xhtml:div[@class='section']/xhtml:p"
381 + "/xhtml:a[@href='#googleAds']");
382 xpath.addNamespace(xhtml);
383 single = (Element) xpath.selectSingleNode(elem);
384 if (single != null) {
385 this.report.log(1, "Unique");
386 modified = this.changeHTMLTag(single, this.getAdContent(UNIQUE_AD));
387 }
388
389
390 xpath = XPath.newInstance("//xhtml:div[@id='banner']"
391 + "/xhtml:div[@id='bannerRight']"
392 + "/xhtml:img[@src='googleAds' and @alt='googleAds']");
393 xpath.addNamespace(xhtml);
394 single = (Element) xpath.selectSingleNode(elem);
395 if (single != null) {
396 this.report.log(1, "Logo right");
397 modified = this.changeHTMLTag(single, this.getAdContent(LOGO_AD));
398 }
399 xpath = XPath.newInstance("//xhtml:div[@id='banner']"
400 + "/xhtml:div[@id='bannerLeft']"
401 + "/xhtml:img[@src='googleAds' and @alt='googleAds']");
402 xpath.addNamespace(xhtml);
403 single = (Element) xpath.selectSingleNode(elem);
404 if (single != null) {
405 this.report.log(1, "Logo left");
406 modified = this.changeHTMLTag(single, this.getAdContent(LOGO_AD));
407 }
408
409
410 xpath = XPath.newInstance("//xhtml:div[@id='leftColumn']"
411 + "/xhtml:div[@id='navcolumn']/xhtml:a[@class='poweredBy' "
412 + "and @title='facebookILike']");
413 xpath.addNamespace(xhtml);
414 single = (Element) xpath.selectSingleNode(elem);
415 if (single != null) {
416 this.report.log(1, "Powered - Facebook");
417 modified = this.getFacebookILike(single);
418 }
419
420
421 xpath = XPath.newInstance("//xhtml:div[@id='leftColumn']"
422 + "/xhtml:div[@id='navcolumn']/xhtml:a[@class='poweredBy' "
423 + "and @title='googlePlusOne']");
424 xpath.addNamespace(xhtml);
425 single = (Element) xpath.selectSingleNode(elem);
426 if (single != null) {
427 this.report.log(1, "Powered - GooglePlusOne");
428 modified = this.getGooglePlusOne(single);
429 }
430
431
432 xpath = XPath.newInstance("//xhtml:div[@id='leftColumn']"
433 + "/xhtml:div[@id='navcolumn']/xhtml:a[@class='poweredBy' "
434 + "and @title='customPoweredBy']");
435 xpath.addNamespace(xhtml);
436 single = (Element) xpath.selectSingleNode(elem);
437 if (single != null) {
438 this.report.log(1, "Powered - Custom");
439 modified = this.getCustomPoweredBy(single);
440 }
441
442 return modified;
443 }
444
445
446
447
448
449
450
451
452
453
454 protected void replace(final String
455 final String
456
457 this.report.log(2, "Processing file " + filename);
458 FileOutputStream output = null;
459 Writer out = null;
460
461 SAXBuilder saxb = new SAXBuilder();
462 saxb.setValidation(false);
463 saxb.setEntityResolver(new IgnoreEntityResolver());
464
465 try {
466 InputStream file = new FileInputStream(directory
467 + File.separatorChar + filename);
468 Document document = saxb.build(file);
469 Element elem = document.getRootElement();
470 boolean modified = this.htmlReplacement(elem);
471
472
473 if (modified) {
474 this.report.log(1, "HTML re-written");
475 XMLOutputter xmloutputter = new XMLOutputter(
476 Format.getPrettyFormat());
477 output = new FileOutputStream(directory + File.separatorChar
478 + filename);
479 out = new OutputStreamWriter(output, "ISO-8859-1");
480 xmloutputter.output(document.getDocType(), output);
481 output.write('\n');
482 xmloutputter.output(elem, output);
483 }
484 } catch (Throwable e) {
485 this.report.log(4, "General problem: " + e.getMessage());
486 this.report.log(e);
487 } finally {
488 if (out != null) {
489 try {
490 out.close();
491 } catch (IOException e) {
492 this.report.log(4, "IO Exception: " + e.getMessage());
493 this.report.log(e);
494 }
495 }
496 if (output != null) {
497 try {
498 output.flush();
499 output.close();
500 } catch (IOException e) {
501 this.report.log(4, "IO Exception: " + e.getMessage());
502 this.report.log(e);
503 }
504 }
505 }
506 }
507 }