이걸 하면서 정말 답이 없다는걸 느낍니다.
그것은 HTML은 정말 답이 없다는걸요.
얼마나 Well-Format 한 Documet 가 중요하다는것을...
그것은 HTML은 정말 답이 없다는걸요.
얼마나 Well-Format 한 Documet 가 중요하다는것을...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 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 <!--" + t + "-->" ); } @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://litlhope.springnote.com/pages/1786498
자바 한글 정규식 참고 : http://ohgyun.tistory.com/132
Posted by LaLuna