jave code explained

I have the following java code to pull <a href links from a website could someone explain what it does in simple terms it comes from this website but has no explination http://www.rgagnon.com/javadetails/java-0639.html

my aim is to turn it into a method that receives a string and returns the links

import java.io.IOException;
import java.io.FileReader;
import java.io.Reader;
import java.util.List;
import java.util.ArrayList;

import javax.swing.text.html.parser.ParserDelegator;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTML.Attribute;
import javax.swing.text.MutableAttributeSet;

public class HTMLUtils {
  private HTMLUtils() {}
 
  public static List<String> extractLinks(Reader reader) throws IOException {
    final ArrayList<String> list = new ArrayList<String>();
   
    ParserDelegator parserDelegator = new ParserDelegator();
    ParserCallback parserCallback = new ParserCallback() {
      public void handleText(final char[] data, final int pos) { }
      public void handleStartTag(Tag tag, MutableAttributeSet attribute, int pos) {
        if (tag == Tag.A) {
          String address = (String) attribute.getAttribute(Attribute.HREF);
          list.add(address);
        }
      }
      public void handleEndTag(Tag t, final int pos) {  }
      public void handleSimpleTag(Tag t, MutableAttributeSet a, final int pos) { }
      public void handleComment(final char[] data, final int pos) { }
      public void handleError(final java.lang.String errMsg, final int pos) { }
    };
    parserDelegator.parse(reader, parserCallback, false);
    return list;
  }
 
  public final static void main(String[] args) throws Exception{
    FileReader reader = new FileReader("java-new.html");
    List<String> links = HTMLUtils.extractLinks(reader);
    for (String link : links) {
      System.out.println(link);
    }
  }
}
enigma1234567890Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mukundha_expertCommented:
it actualy extracts all the links from the html page.
if you are familiar with sax parser, then its quite simple to understand..

ParserDelegator parses the html file and calls the cal back methods on the ParserCallback about events during parsing like hanfleStarttag - called when a start tag is encountered,

a link in html is <a href-="somre link"/>, here href is a attribute of tag 'A'.

so in handleTagStart method we look for tag 'a' if found then we read the attribute href which is the link..
these links are stored in a list and returned.

hope this helps

"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robthewolfCommented:
You have two static functions. extractLinks and main. Main calls extractLinks with a Reader object as it's argument and it returns a list of links. In main it is hard coded as a specific file. You could make it more flexible by replacing the line     FileReader reader = new FileReader("java-new.html"); with     FileReader reader = new FileReader(arts[0]);  then when you run it from the commandline give it the filename you want as an argument. I haven't checked whether reader will work with a uri.
If you want to use it in a program create a new method that uses basically the same code as main, but passes a variable instead of the hardcoded file name
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.