import javax.swing.text.html.HTML; import javax.swing.text.html.HTML.Tag; import javax.swing.text.html.HTMLEditorKit; import javax.swing.text.MutableAttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.html.parser.ParserDelegator; import java.io.InputStreamReader; import java.net.URL; import java.net.HttpURLConnection; import java.util.Enumeration; public class Example { // 파서는 콜백 형식으로 되어 있다. 각 태그가 들어 올때 적절한 메소드가 호출됨 private class CallbackHandler extends HTMLEditorKit.ParserCallback { private String urlReg = "\\.\\./bbs/board\\.php\\?bo_table=cs_notice&wr_id=([0-9]){2,4}"; private String dataReg = "([?0-9]){2}-([?0-9]){2}"; private String noticeReg = "(.*[0-9ㄱ-ㅎㅏ-ㅣ가-힣]+.*[^공지사항]){5,}"; @Override public void flush( ) throws BadLocationException { System.out.println( "flush" ); } @Override public void handleComment( char[] data, int pos ) { System.out.println( "Cmt " + new String( data ) ); } @Override public void handleEndOfLineString( String eol ) { System.out.println( "EOL " ); } @Override public void handleEndTag( Tag t, int pos ) { System.out.println( "End " ); } @Override public void handleError( String errorMsg, int pos ) { // System.out.println("ERROR\t" + new String(errorMsg)); } @Override public void handleSimpleTag( Tag t, MutableAttributeSet a, int pos ) { /*System.out.print( "Sim <" + t.toString( ) + ">\n" ); for ( Enumeration e = a.getAttributeNames( ); e.hasMoreElements( ); ) { Object attributeName = e.nextElement( ); System.out.print( "\t" + attributeName + "=" ); System.out.println( a.getAttribute( attributeName ) ); } */ } @Override public void handleStartTag( Tag t, MutableAttributeSet a, int pos ) { if ( t == HTML.Tag.A ) { Object str = a.getAttribute( HTML.Attribute.HREF ); if ( str != null ) { // 문제는 태그가 검출된다 하더라도 속성이 없을수도 있기 때문에 null체크를 해줘야 합니다. if ( str.toString( ).matches( this.urlReg ) ) { System.out.println( "HREF : " + str ); } } } } public void handleText( char[] data, int pos ) { String str = new String( data ); if ( str.matches( this.dataReg ) ) { System.out.println( "Data : " + str ); } else if ( str.matches( this.noticeReg ) ) { System.out.println( "Notice :" + str ); } } } public void parse( String str ) { try { // 입력받은 URL에 연결하여 InputStream을 통해 읽은 후 파싱 한다. URL url = new URL( str ); HttpURLConnection con = (HttpURLConnection) url.openConnection( ); InputStreamReader reader = new InputStreamReader( con.getInputStream( ), "utf-8" ); // callbackHandler 와 함께 reader 을 딜리게이터에 던집니다. new ParserDelegator( ).parse( reader, new CallbackHandler( ), true ); con.disconnect( ); } catch ( Exception e ) { e.printStackTrace( ); } } public static void main( String[] args ) { Example parser = new Example( ); parser.parse( URString ); }
기본적인 parser 뼈대 코드 참고 : http://zyint.tistory.com/352
기본적인 자바 정규식 : http://litlhope.springnote.com/pages/1786498
자바 한글 정규식 참고 : http://ohgyun.tistory.com/132
출처 : http://zyint.tistory.com/352
'Java' 카테고리의 다른 글
생성자(Constructor)도 메소드인가? (0) | 2016.07.26 |
---|---|
자바에서 유닉스 명령어 실행 (0) | 2013.03.27 |
Java 연산자 (0) | 2012.11.28 |
Java Static (0) | 2012.11.28 |
Java Swing HTML Parser# (0) | 2012.03.23 |