Guest User

Untitled

a guest
Apr 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. <div class="author"><a href="/user/1" title="View user profile.">Apple</a> - October 22, 2009 - 01:07</div>
  2.  
  3. class HTMLParseListerInner extends HTMLEditorKit.ParserCallback {
  4. private ArrayList<String> foundDates = new ArrayList<String>();
  5. private boolean isDivLink = false;
  6.  
  7. public void handleText(char[] data, int pos) {
  8. if(isDivLink)
  9. foundDates.add(new String(data)); // Extracts "Apple" instead of the date.
  10. }
  11.  
  12. public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
  13. String divValue = (String)a.getAttribute(HTML.Attribute.CLASS);
  14. if (t.toString() == "div" && divValue != null && divValue.equals("author"))
  15. isDivLink = true;
  16. }
  17. }
  18.  
  19. import java.io.*;
  20. import java.util.*;
  21. import javax.swing.text.*;
  22. import javax.swing.text.html.*;
  23. import javax.swing.text.html.parser.*;
  24.  
  25. public class ParserCallbackDiv extends HTMLEditorKit.ParserCallback
  26. {
  27. private boolean isDivLink = false;
  28. private String divText;
  29.  
  30. public void handleEndTag(HTML.Tag tag, int pos)
  31. {
  32. if (tag.equals(HTML.Tag.DIV))
  33. {
  34. System.out.println( divText );
  35. isDivLink = false;
  36. }
  37. }
  38.  
  39. public void handleStartTag(HTML.Tag tag, MutableAttributeSet a, int pos)
  40. {
  41. if (tag.equals(HTML.Tag.DIV))
  42. {
  43. String divValue = (String)a.getAttribute(HTML.Attribute.CLASS);
  44.  
  45. if ("author".equals(divValue))
  46. isDivLink = true;
  47. }
  48. }
  49.  
  50. public void handleText(char[] data, int pos)
  51. {
  52. divText = new String(data);
  53. }
  54.  
  55. public static void main(String[] args)
  56. throws IOException
  57. {
  58. String file = "<div class="author"><a href="/user/1"" +
  59. "title="View user profile.">Apple</a> - October 22, 2009 - 01:07</div>";
  60. StringReader reader = new StringReader(file);
  61.  
  62. ParserCallbackDiv parser = new ParserCallbackDiv();
  63.  
  64. try
  65. {
  66. new ParserDelegator().parse(reader, parser, true);
  67. }
  68. catch (IOException e)
  69. {
  70. System.out.println(e);
  71. }
  72. }
  73. }
Add Comment
Please, Sign In to add comment