java - Outputting html image and link tags to text files and displaying to user -
i start saying new stackoverflow, please forgive me if make newbie mistakes.
i have been assigned write java swing program searches url (specified user), pull html code, , display image tags , link tags found in html code. of right now, have gui consists of 1 jtextfield user specify url, 2 jtextarea's output link tags , image tags, , search button. once user has searched url, html code read in , if line contains image tag or link tag written corresponding text file. however, narrow down image tags , link tags written text files, rather writing entire line on found. how go doing this?
any appreciated. here have far.
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import java.net.*; import java.io.*; public class websearch extends jframe { // create jlabel prompt user, 2 jtextarea's output results, // jtextfield take user input, , jbutton run application. private jtextfield inputbox; private jlabel userprompt; private jtextarea imageresults; private jtextarea linkresults; private jbutton startbutton; // create constructor add gui components public websearch() { // set title bar super( "links , images url" ); inputbox = new jtextfield( 33 ); startbutton = new jbutton( "search" ); // add actionlistener detect when action performed startbutton.addactionlistener( new actionlistener() { // override actionperformed method @override public void actionperformed( actionevent event ) { string input = inputbox.gettext(); try { url url = new url( input ); bufferedreader in = new bufferedreader( new inputstreamreader( url.openstream() )); string inputline; printwriter imagewriter = new printwriter( "images.txt", "utf-8" ); printwriter linkwriter = new printwriter( "links.txt", "utf-8" ); filereader imagereader = new filereader( "images.txt" ); filereader linkreader = new filereader( "links.txt" ); while(( inputline = in.readline() ) != null ) { if( inputline.contains( "<a href=" )) { linkwriter.println( inputline ); } if( inputline.contains( "<img src=" )) { imagewriter.println( inputline ); } } imagewriter.close(); linkwriter.close(); imageresults.read( imagereader, "images.txt" ); linkresults.read( linkreader, "links.txt" ); in.close(); } catch( exception exception ) { } } // end actionperformed method } // end innerclass ); // end actionlistener userprompt = new jlabel( "enter url:" ); inputbox.settext( "http://www." ); linkresults = new jtextarea( 8, 33 ); linkresults.seteditable( false ); imageresults = new jtextarea( 8, 33 ); imageresults.seteditable( false ); linkresults.settext( "image tags listed here" ); imageresults.settext( "link tags listed here" ); // create scroll panes jtextarea's jscrollpane imagescrollpane = new jscrollpane( imageresults ); jscrollpane linkscrollpane = new jscrollpane( linkresults ); // add gui components container container container = getcontentpane(); container.setlayout( new flowlayout() ); container.add( userprompt ); container.add( inputbox ); container.add( startbutton ); container.add( imagescrollpane ); container.add( linkscrollpane ); setsize( 400, 400 ); setvisible( true ); setdefaultcloseoperation( jframe.exit_on_close ); } // end constructor // create main method , object of class websearch public static void main( string[] args ) { websearch application = new websearch(); } // end main method } // end class
you might better off relying on html parse library jsoup. may - presuming html formed need parse inside quotes each element. simple way parse between quotes within element:
string valuetofind = "<img src=\""; int index = inputline.indexof(valuetofind ); if ( index != -1 ){ stringbuilder link = new stringbuilder(); index += valuetofind.length(); while ( index < inputline.length() && inputline.charat(index) != '"'){ link.append(inputline.charat(index)); index++; } system.out.println(link); } note not account multiple tags on single line (easily modified however), , not account html not formed (multiple spaces after img or element line wrapped - think regular expression, or why reinvent wheel , use html parser).
Comments
Post a Comment