01: import java.net.*;
02: import java.io.*;
03: /**
04:  * URLReader class is used to get the content of a given 
05:  * Internet address. 
06:  * @author Cagatay CEBI - cagataycebi@gmail.com
07:  * @version 1.0
08:  */
09: public class URLReader {
10:         private URL my_adress;
11:         private StringBuffer my_content;
12: 
13:         /**
14:          * Reads the content of a web address. 
15:          * @param site - Web Address
16:          * @throws Exception - Address not found exception 
17:          */
18:         public void read_new_url ( String web_address ) 
19:         {
20:                 if( my_content != null ) {
21:                         my_content.delete( 0, my_content.length() );
22:                 }
23:                 my_content = new StringBuffer( );
24:                 try {
25:                         my_adress = new URL( web_address );
26:                         BufferedReader in = new BufferedReader(
27:                                         new InputStreamReader(
28:                                         my_adress.openStream( ) ) );
29: 
30:                 String inputLine;
31: 
32:                 while( (inputLine = in.readLine()) != null )
33:                         my_content.append( inputLine + "\n" );
34:                 
35:                 in.close();
36:                 
37:                 }
38:                 catch( UnknownHostException  exception ) {
39:                         // ADDRESS NOT FOUND EXCEPTION
40:                         System.err.println( "ERROR: ADDRESS IS NOT FOUND IN 'URLReader' CLASS!" );
41:                         System.err.println( "EXCEPTION DETAIL:\n" + exception.toString( ) + "\n");
42:                 } 
43:                 catch( MalformedURLException exception ) {
44:                         // MALFORMED URL EXCEPTION
45:                         System.err.println( "ERROR: ADDRESS IS MALFORMED IN 'URLReader' CLASS!" );
46:                         System.err.println( "EXCEPTION DETAIL:\n" + exception.toString( ) + "\n");
47:                 } 
48:                 catch (IOException exception ) {
49:                         // INPUT/OUTPUT EXCEPTION
50:                         System.err.println( "ERROR: INPUT/OUTPUT EXCEPTION IN 'URLReader' CLASS!" );
51:                         System.err.println( "EXCEPTION DETAIL:\n" + exception.toString( ) + "\n");          
52:                 }
53:         }
54:                 
55:         /**
56:          * Returns the content of the given web address. 
57:          * ( The web address must be given in constructor. )   
58:          * @return Web Content
59:          */
60:         public String get_content( )
61:         {
62:                 return my_content.toString( );
63:         }
64:         
65:         /**
66:          * Returns given web address.
67:          * @return Web Address
68:          */
69:         public String get_address( )
70:         {
71:                 return my_adress.toString();
72:         }
73:         
74: }