Solved

Exception: no protocol ...

Posted on 2004-10-10
9
402 Views
Last Modified: 2012-06-21
Hey everyone,
I've basically made a program that, basically; let's you/the user know whether the links on a webpage have changed/(new ones added/old ones removed).

Here's the code:

//:::::::::::::::::::::::::::::::::::::::  DomainChangeCheck.java :::::::::::::::::::::::::::::::::::::::::\\
import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.text.html.*;
import javax.swing.text.*;
import java.text.*;

public class DomainChangeCheck {
      
      static String sDomain = "";
      static String sPage   = "";
      static int http       = 80;
      
      public static void main ( String [] args ) {
            
            DomainChangeCheck dcc = new DomainChangeCheck ();
            
            // Verify that an argument has been passed:
            if ( args.length == 0 ) {
                  System.out.println("Usage: java EeJavaInformer www.domain.com/page.htm");
                  System.exit( 1 );
            }
            
            String s1 = "",
                     s2 = "";
            
            sDomain = args[0];
            if ( args[1].length() > 0 ) sPage = args[1];
            
            // Verify domain existance:
            boolean exist = false;
          try {
              InetAddress addr = InetAddress.getByName( sDomain );
   
              // This constructor will block until the connection succeeds
              Socket socket = new Socket(addr, http);
              exist = true;
              socket.close();
          } catch (Exception e) {      }
          
          // Exit if domain does not exist.
          if ( /*domain does not*/ !exist ) {
                System.out.println( "Domain: " + sDomain + " cannot be found." );
                System.exit( 1 );
          }
            
            boolean bFirstTime = true;
            
      for (;;) {
            
            s2 = s1;
          
          // Retrieve all links in HTML document:
          String [] links = dcc.getLinks( "http://" + sDomain + sPage );
          
          if ( links.length == 0) {
                System.out.println( "Error: Could not find any links in specified document" );
                System.exit( 1 );
          }
          
          s1 = "";
          
          // Check to see if any changes to the links on the page have been made:
          for ( int i=0; i<links.length; i++ ) {
                s1 += links[i];
          }
          
          if ( !(dcc.doDiff( s1, s2 )) ) {
                if ( bFirstTime ) {
                      System.out.println( "\nStarted Successfully..." );
                      bFirstTime = false;
                } else {
                      System.out.println( "\nA change to the page has been made." );
                }
          }
    }
          
    }
      
      private boolean doDiff ( String s1, String s2 ) {
            // return true if the two strings are the same. Else, false.
            
            if ( s1.length() != s2.length() ) return false;
            
            char [] c1 = s1.toCharArray();
            char [] c2 = s2.toCharArray();
            
            boolean bSame = true;
            
            for ( int i=0; i<c1.length; i++ ) {
                  if ( c1[i]!=c2[i] && bSame ) bSame = false;
            }
            
            return bSame;
            
      }
      
    public static String[] getLinks(String uriStr) {
        List result = new ArrayList();
   
        try {
            // Create a reader on the HTML content
            URL url = new URI(uriStr).toURL();
            URLConnection conn = url.openConnection();
            Reader rd = new InputStreamReader(conn.getInputStream());
                
            // Parse the HTML
            EditorKit kit = new HTMLEditorKit();
            HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
            doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
            kit.read(rd, doc, 0);
                
            // Find all the A elements in the HTML document
            HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
            while (it.isValid()) {
                SimpleAttributeSet s = (SimpleAttributeSet)it.getAttributes();
                      
                String link = (String)s.getAttribute(HTML.Attribute.HREF);
                if (link != null) {
                    // Add the link to the result list
                    result.add(link);
                }
                it.next();
            }
        } catch (Exception e) {
              System.out.println( "Exception: " + e.getMessage() + "\n" );
              e.printStackTrace();
        }
          
        // Return all found links
        return (String[])result.toArray(new String[result.size()]);
    }
      
}
//:::::::::::::::::::::::::::::::::::::::  DomainChangeCheck.java :::::::::::::::::::::::::::::::::::::::::\\

Okay now; When I type something like:

java DomainChangeCheck www.positive-websolutions.co.uk /Index.htm

It works fine, but, let's assume that one puts:
java DomainChangeCheck www.experts-exchange.com /Java/

:o\ -- It doesn't work!! I get the following exception (from within the getLinks() method):
well, the  stackTrace:

java.net.MalformedURLException: no protocol: /Programming/Programming_Langauges/Java/
etc..

Any ideas why?

ThanQ :)
[r.D]
0
Comment
Question by:DrWarezz
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12272005
Are you sure you're not calling getLinks from elsewhere and that the only code you're running is the code you posted?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12272023
Also, instead of, or in addition to:

>>System.out.println( "Exception: " + e.getMessage() + "\n" );

could you do

System.err.println( "URI was " + uriStr );

?


0
 
LVL 9

Author Comment

by:DrWarezz
ID: 12272141
I'm sure that all the code I'm running is what's been posted.

I inserted:
System.err.println( "URI was " + uriStr );
into it, and it outputs:

URI was http://www.experts-exchange.com/Java/

:o\ ?

ThanQ,
[r.D]
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 9

Author Comment

by:DrWarezz
ID: 12272148
According to the stackTrace, it would seem that it's line 106 that the error is occuring on:

Reader rd = new InputStreamReader(conn.getInputStream());

?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12272154
Yes but i'd like to see *all* the error output with that in it too ;-)
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 12272179
:)
Okay, here's EVERYTHING that's outputted:

Exception: no protocol: /Programming/Programming_Languages/Java/

URI was http://www.experts-exchange.com/Java/
java.net.MalformedURLException: no protocol: /Programming/Programming_Languages/Java/
   at java.net.URL.(init)(URL.java.579)
   at java.net.URL.(init)(URL.java.476)
   at java.net.URL.(init)(URL.java.425)
   at sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:1090)
   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:681)
   at DomainChangeCheck.getLinks(DomanChangeCheck.java:106)
   at DomainChangeCheck.getLinks(DomanChangeCheck.java:55)
Error: Could not find any links in specified document

And here's the command I type:
java DomainChangeCheck www.experts-exchange.com /Java/

Thanks.
[r.D]
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12272229
At the risk of repeating myself (i said this yesterday and was accused of being 'unhelpful') the html packages are pretty flaky and/or intolerant when it comes to parsing html, so you'll have to accept uneven results. That

>>http://www.experts-exchange.com/Java/

does a redirect to

'/Programming/Programming_Languages/Java/'

So if you were to enter http://www.experts-exchange.com/Programming/Programming_Languages/Java/ at the outset, you might have more success
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 12272240
lol - You were right! :D -- It's the redirection.

Thanks alot CEHJ :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12272245
8-)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now