1 16 package org.apache.jetspeed.util.rewriter; 17 18 import javax.swing.text.*; 20 import javax.swing.text.html.*; 21 import javax.swing.text.html.HTMLEditorKit ; 22 23 import java.io.*; 25 26 import java.util.*; 28 29 import java.net.*; 31 import org.apache.turbine.util.Log; 33 34 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 55 public SwingParserAdaptor(Rewriter rewriter) 56 { 57 this.rewriter = rewriter; 58 lineSeparator = System.getProperty("line.separator", "\r\n"); 59 } 60 61 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 throw new MalformedURLException(e.toString()); 87 } 88 return res; 89 } 90 91 92 96 class ParserGetter extends HTMLEditorKit 97 { 98 99 public HTMLEditorKit.Parser getParser(){ 100 return super.getParser(); 101 } 102 } 103 104 105 110 class Callback extends HTMLEditorKit.ParserCallback 111 { 112 113 114 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 131 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 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 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 229 return true; 230 } 231 232 233 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 271 public void handleError(java.lang.String str,int param) 272 { 273 } 275 276 283 public void handleComment(char[] values,int param) 284 { 285 } 288 289 295 public void handleEndOfLineString(java.lang.String str) 296 { 297 addToResult(str); 298 } 299 300 301 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 329 private Callback addToResult(Object txt) 330 { 331 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 354 private Callback addToResult(char[] txt) 355 { 356 358 try 359 { 360 361 result.write(txt); 362 363 } catch (Exception e) 364 { 365 } 366 return this; 367 } 368 369 374 public String getResult() 375 { 376 try 377 { 378 result.flush(); 379 } catch (Exception e) 380 { 381 } 382 383 String res = " " + result.toString(); 385 386 return res; 387 } 388 389 393 public void flush() throws javax.swing.text.BadLocationException 394 { 395 } 397 398 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 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 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 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 addProxiedConvertedAttribute( tag, HTML.Attribute.VALUE, attrs ); 465 466 } else if (((tag == HTML.Tag.LINK) ) && 467 (attrs.getAttribute(HTML.Attribute.HREF) != null)) 468 { 469 470 addConvertedAttribute( tag, 472 HTML.Attribute.HREF, 473 attrs, 474 rewriter.proxyAllTags()); 475 476 } else if ( tag == HTML.Tag.APPLET ) 477 { 478 479 addConvertedAttribute( tag, 481 HTML.Attribute.CODEBASE, 482 attrs, 483 rewriter.proxyAllTags()); 484 485 } else if ( tag == HTML.Tag.FRAME ) 486 { 487 488 addProxiedConvertedAttribute( tag, HTML.Attribute.SRC, attrs); 490 491 } else if ( tag == HTML.Tag.SCRIPT ) 492 { 493 if (attrs.getAttribute(HTML.Attribute.SRC) != null) 495 { 496 497 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 } 512 513 } else if (tag == HTML.Tag.FORM) 514 { 515 516 inForm = true; 519 if (attrs.getAttribute(HTML.Attribute.ACTION) == null) 520 { 521 attrs.addAttribute(HTML.Attribute.METHOD, "POST"); 523 525 528 } else 529 { 530 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 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 addConvertedAttribute( tag, 550 HTML.Attribute.BACKGROUND, 551 attrs, 552 rewriter.proxyAllTags()); 553 554 } else if (tag == HTML.Tag.TD) 555 { 556 if (! (attrs.getAttribute(HTML.Attribute.BACKGROUND) == null)) 558 { 559 addConvertedAttribute( tag, 560 HTML.Attribute.BACKGROUND, 561 attrs, 562 rewriter.proxyAllTags()); 563 } 564 } 565 566 570 } 571 572 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 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 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 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 return rewriter.generateNewUrl(oldURL, tag, attr); 658 } 659 660 661 } 662 663 } 664 665 666 | Popular Tags |