import javax.swing.text.html.HTMLEditorKit.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
import java.io.*;
import java.lang.*;


public class HtmlParser
{
 private StringBuffer out_text = null;
 private int parser_ok = 0;
 public HtmlParser() {}

 public String getConvData(String htmlData)
 {
  String return_val = "";
  out_text = new StringBuffer();
 
  if(htmlData.indexOf("<meta") != -1)
  {
   String temp = "";
   while(htmlData.indexOf("<meta") != -1)
   {
    temp = htmlData.substring(0, htmlData.indexOf("<meta"));
    htmlData = htmlData.substring(htmlData.indexOf(">",htmlData.indexOf("<meta"))+1, htmlData.length());
    htmlData = temp + htmlData;
   }
  }

  try
  {
   HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback ()
   {
    public void handleStartTag(HTML.Tag tag, MutableAttributeSet a, int pos)
    {
     if (tag.equals(HTML.Tag.BODY)) parser_ok = 1;
    }
    public void handleEndTag(HTML.Tag tag, MutableAttributeSet a, int pos)
    {
     if (tag.equals(HTML.Tag.BODY)) parser_ok = 0;
    }
    public void handleText(char[] data, int pos)
    {
     if(parser_ok == 1)
     {
      for(int k = 0; k < data.length; k++)
      {
       if(data[k] == (char)160) data[k] = (char)32;
       else out_text.append(data[k]);
      }
     }
    }
   };

   new ParserDelegator().parse(new StringReader(htmlData), callback, false);
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 
  return out_text;
 }
}


신고

'programing > JAVA' 카테고리의 다른 글

JSP charset  (0) 2009.08.14
HTML A태그, IMG태그 추출  (1) 2009.06.22
HTML 태그제거 소스  (0) 2009.06.22
계층형 게시판  (0) 2009.06.03
File Up & Down  (0) 2009.06.02
Paging  (0) 2009.06.02
Posted by 대절님

댓글을 달아 주세요



티스토리 툴바