1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.jetspeed.util.rewriter;
17
18
19 import javax.swing.text.*;
20 import javax.swing.text.html.*;
21 import javax.swing.text.html.HTMLEditorKit;
22
23
24 import java.io.*;
25
26
27 import java.util.*;
28
29
30 import java.net.*;
31 import org.apache.turbine.util.Log;
32
33
34
35
36
37
38
39
40
41 public class SwingParserAdaptor implements HTMLParserAdaptor
42 {
43
44 private SwingParserAdaptor.Callback cb = new SwingParserAdaptor.Callback();
45 private String lineSeparator;
46 private boolean skippingImplied = false;
47 private Rewriter rewriter;
48
49
50
51
52
53
54
55 public SwingParserAdaptor(Rewriter rewriter)
56 {
57 this.rewriter = rewriter;
58 lineSeparator = System.getProperty("line.separator", "\r\n");
59 }
60
61
62
63
64
65
66
67
68
69
70
71 public String run(Reader reader)
72 throws MalformedURLException
73 {
74 HTMLEditorKit.Parser parser = new SwingParserAdaptor.ParserGetter().getParser();
75
76 String res ="";
77 try
78 {
79 parser.parse(reader, cb, true);
80 res = cb.getResult();
81 } catch (Exception e)
82 {
83 e.printStackTrace();
84
85
86 throw new MalformedURLException(e.toString());
87 }
88 return res;
89 }
90
91
92
93
94
95
96 class ParserGetter extends HTMLEditorKit
97 {
98
99 public HTMLEditorKit.Parser getParser(){
100 return super.getParser();
101 }
102 }
103
104
105
106
107
108
109
110 class Callback extends HTMLEditorKit.ParserCallback
111 {
112
113
114
115
116 private boolean inForm = false;
117 private boolean inScript = false;
118 private boolean emit = true;
119 private boolean simpleTag = false;
120
121 private StringWriter result = new StringWriter();
122
123 private Callback ()
124 {
125 }
126
127
128
129
130
131
132
133
134
135
136 public void handleText(char[] values,int param)
137 {
138 if (false == emit)
139 return;
140 if (values[0] == '>')
141 return;
142 if (false == rewriter.enterText(values, param))
143 return;
144
145 addToResult(values);
146 }
147
148
149
150
151
152
153
154
155
156 public void handleSimpleTag(HTML.Tag tag,MutableAttributeSet attrs,int param)
157 {
158 simpleTag = true;
159 if (false == rewriter.enterSimpleTagEvent(tag, attrs))
160 return;
161
162 if (false == isValidFragmentTag(tag))
163 return;
164
165 appendTagToResult(tag,attrs);
166 if (tag.toString().equalsIgnoreCase("param") ||
167 tag.toString().equalsIgnoreCase("object") ||
168 tag.toString().equalsIgnoreCase("embed"))
169 {
170 result.write(lineSeparator);
171 }
172 simpleTag = false;
173 String appended = rewriter.exitSimpleTagEvent(tag, attrs);
174 if (null != appended)
175 result.write(appended);
176 }
177
178
179
180
181
182
183
184
185
186 public void handleStartTag(HTML.Tag tag, MutableAttributeSet attrs, int position)
187 {
188 if (false == rewriter.enterStartTagEvent(tag, attrs))
189 return;
190
191 if (tag == HTML.Tag.HEAD)
192 {
193 emit = false;
194 return;
195 }
196
197 if (false == isValidFragmentTag(tag))
198 return;
199
200 appendTagToResult(tag,attrs);
201 formatLine(tag);
202 String appended = rewriter.exitStartTagEvent(tag, attrs);
203 if (null != appended)
204 result.write(appended);
205 }
206
207
208 boolean isValidFragmentTag(HTML.Tag tag)
209 {
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 return true;
230 }
231
232
233
234
235
236
237
238
239
240 public void handleEndTag(HTML.Tag tag, int position)
241 {
242 if (false == rewriter.enterEndTagEvent(tag))
243 return;
244
245 if (tag == HTML.Tag.HEAD)
246 {
247 emit = true;
248 return;
249 }
250
251 if (false == isValidFragmentTag(tag))
252 return;
253
254 addToResult("</").addToResult(tag).addToResult(">");
255
256 formatLine(tag);
257 String appended = rewriter.exitEndTagEvent(tag);
258 if (null != appended)
259 result.write(appended);
260
261 }
262
263
264
265
266
267
268
269
270
271 public void handleError(java.lang.String str,int param)
272 {
273
274 }
275
276
277
278
279
280
281
282
283 public void handleComment(char[] values,int param)
284 {
285
286
287 }
288
289
290
291
292
293
294
295 public void handleEndOfLineString(java.lang.String str)
296 {
297 addToResult(str);
298 }
299
300
301
302
303
304
305
306
307 private void formatLine(HTML.Tag tag)
308 {
309 if (tag.isBlock() ||
310 tag.breaksFlow() ||
311 tag == HTML.Tag.FRAME ||
312 tag == HTML.Tag.FRAMESET ||
313 tag == HTML.Tag.SCRIPT)
314 {
315 result.write(lineSeparator);
316 }
317 }
318
319
320
321
322
323
324
325
326
327
328
329 private Callback addToResult(Object txt)
330 {
331
332
333
334
335 try
336 {
337 result.write(txt.toString());
338 } catch (Exception e)
339 {
340 System.err.println("Error parsing:" + e);
341 }
342 return this;
343 }
344
345
346
347
348
349
350
351
352
353
354 private Callback addToResult(char[] txt)
355 {
356
357
358 try
359 {
360
361 result.write(txt);
362
363 } catch (Exception e)
364 {
365 }
366 return this;
367 }
368
369
370
371
372
373
374 public String getResult()
375 {
376 try
377 {
378 result.flush();
379 } catch (Exception e)
380 {
381 }
382
383
384 String res = " " + result.toString();
385
386 return res;
387 }
388
389
390
391
392
393 public void flush() throws javax.swing.text.BadLocationException
394 {
395
396 }
397
398
399
400
401
402
403
404
405 private void appendTagToResult(HTML.Tag tag, MutableAttributeSet attrs)
406 {
407 convertURLS(tag,attrs);
408 Enumeration e = attrs.getAttributeNames();
409 addToResult("<").addToResult(tag);
410 while (e.hasMoreElements())
411 {
412 Object attr = e.nextElement();
413 String value = attrs.getAttribute(attr).toString();
414 addToResult(" ").addToResult(attr).addToResult("=\"").
415 addToResult(value).addToResult("\"");
416 }
417 if (simpleTag)
418 addToResult("/>");
419 else
420 addToResult(">");
421 }
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436 private void convertURLS( HTML.Tag tag, MutableAttributeSet attrs )
437 {
438 rewriter.convertTagEvent(tag, attrs);
439 if ((tag == HTML.Tag.A) &&
440 (attrs.getAttribute(HTML.Attribute.HREF) != null))
441 {
442
443
444 addProxiedConvertedAttribute( tag, HTML.Attribute.HREF, attrs);
445
446 }
447 else if (((tag == HTML.Tag.IMG ||
448 tag == HTML.Tag.INPUT
449 ) &&
450 (attrs.getAttribute(HTML.Attribute.SRC) != null)
451 ))
452 {
453
454
455 addConvertedAttribute( tag,
456 HTML.Attribute.SRC,
457 attrs,
458 rewriter.proxyAllTags());
459
460 } else if (((tag == HTML.Tag.OPTION) ) &&
461 (attrs.getAttribute(HTML.Attribute.VALUE) != null))
462 {
463
464 addProxiedConvertedAttribute( tag, HTML.Attribute.VALUE, attrs );
465
466 } else if (((tag == HTML.Tag.LINK) ) &&
467 (attrs.getAttribute(HTML.Attribute.HREF) != null))
468 {
469
470
471 addConvertedAttribute( tag,
472 HTML.Attribute.HREF,
473 attrs,
474 rewriter.proxyAllTags());
475
476 } else if ( tag == HTML.Tag.APPLET )
477 {
478
479
480 addConvertedAttribute( tag,
481 HTML.Attribute.CODEBASE,
482 attrs,
483 rewriter.proxyAllTags());
484
485 } else if ( tag == HTML.Tag.FRAME )
486 {
487
488
489 addProxiedConvertedAttribute( tag, HTML.Attribute.SRC, attrs);
490
491 } else if ( tag == HTML.Tag.SCRIPT )
492 {
493
494 if (attrs.getAttribute(HTML.Attribute.SRC) != null)
495 {
496
497
498 String s = attrs.getAttribute(HTML.Attribute.SRC).toString();
499 if (s.indexOf("%3E") == -1)
500 {
501 addConvertedAttribute( tag,
502 HTML.Attribute.SRC,
503 attrs,
504 rewriter.proxyAllTags());
505 }
506
507 } else
508 {
509
510
511 }
512
513 } else if (tag == HTML.Tag.FORM)
514 {
515
516
517 inForm = true;
518
519 if (attrs.getAttribute(HTML.Attribute.ACTION) == null)
520 {
521
522 attrs.addAttribute(HTML.Attribute.METHOD, "POST");
523
524
525
526
527
528 } else
529 {
530
531 attrs.addAttribute(HTML.Attribute.METHOD, "POST");
532 addProxiedConvertedAttribute( tag, HTML.Attribute.ACTION, attrs);
533
534 }
535
536 } else if (((tag == HTML.Tag.AREA) ) &&
537 (attrs.getAttribute(HTML.Attribute.HREF) != null))
538 {
539
540
541 addProxiedConvertedAttribute( tag, HTML.Attribute.HREF,
542 attrs );
543
544 } else if (((tag == HTML.Tag.BODY) ) &&
545 (attrs.getAttribute(HTML.Attribute.BACKGROUND) != null))
546 {
547
548
549 addConvertedAttribute( tag,
550 HTML.Attribute.BACKGROUND,
551 attrs,
552 rewriter.proxyAllTags());
553
554 } else if (tag == HTML.Tag.TD)
555 {
556
557 if (! (attrs.getAttribute(HTML.Attribute.BACKGROUND) == null))
558 {
559 addConvertedAttribute( tag,
560 HTML.Attribute.BACKGROUND,
561 attrs,
562 rewriter.proxyAllTags());
563 }
564 }
565
566
567
568
569
570 }
571
572
573
574
575
576
577
578
579
580
581
582
583 private void addConvertedAttribute( HTML.Tag tag,
584 HTML.Attribute attr,
585 MutableAttributeSet attrs,
586 boolean proxy )
587 {
588 if (proxy)
589 {
590 addProxiedConvertedAttribute(tag, attr,attrs);
591 } else
592 {
593 if ( attrs.getAttribute( attr ) != null )
594 {
595 attrs.addAttribute( attr,
596 generateNewUrl( tag, attrs, attr, false ) );
597 }
598 }
599 }
600
601
602 /***
603 *
604 * Converts the given attribute's URL compatible element to a proxied URL.
605 * This method will always add the proxy host prefix to the rewritten URL.
606 *
607 * @param attr The HTML attribute to be proxied.
608 * @param attrs The mutable HTML attribute set for the current HTML element.
609 *
610 */
611 private void addProxiedConvertedAttribute( HTML.Tag tag,
612 HTML.Attribute attr,
613 MutableAttributeSet attrs ) {
614
615
616
617 if ( attrs.getAttribute( attr ) != null )
618 {
619 String attrSource = attrs.getAttribute( attr ).toString();
620
621
622 if (attrSource.startsWith("mailto:"))
623 {
624 attrs.addAttribute( attr,
625 generateNewUrl( tag, attrs, attr, true ) );
626 } else if (attrSource.startsWith("javascript:"))
627 {
628 attrs.addAttribute( attr,
629 attrSource);
630 } else
631 {
632 attrs.addAttribute( attr,
633 generateNewUrl( tag, attrs, attr, true ) );
634 }
635 }
636 }
637
638
639
640
641
642
643
644
645
646
647
648
649
650 private String generateNewUrl(HTML.Tag tag,
651 MutableAttributeSet attrs,
652 HTML.Attribute attr,
653 boolean proxied)
654 {
655 String oldURL = attrs.getAttribute( attr ).toString();
656
657 return rewriter.generateNewUrl(oldURL, tag, attr);
658 }
659
660
661 }
662
663 }
664
665