Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Link Grabber

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    18
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Link Grabber

    I created this program which grabs all <img> links in a single html file. But I can't get it work.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package network;
     
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.text.MutableAttributeSet;
    import javax.swing.text.html.HTML.Attribute;
    import javax.swing.text.html.HTML.Tag;
    import javax.swing.text.html.HTMLEditorKit.ParserCallback;
    import javax.swing.text.html.parser.ParserDelegator;
     
    /**
     *
     * @author ztron
     */
    public class LinkGrabber {
        private LinkGrabber(){}
     
        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.IMG){
                        String address = (String)attribute.getAttribute(Attribute.ALT);
                        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){}
     
                //public void handleError(final java.lang.String errMsg, final int pos){}
            };
     
            parserDelegator.parse(reader, parsercallback, true);
            return list;
        }
     
        public final static void main(String args[]) throws Exception{
     
            FileReader reader = new FileReader("java.html");
            List<String> links = LinkGrabber.extractLinks(reader);
            for(String link : links){
                System.out.println(link);
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Link Grabber

    Can you explain a little more what your problem is?
    Do you get an error? Please post the full text of the error message.
    Is the output incorrect? Please show what you get and explain why it is wrong.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Link Grabber

    Did you really write this yourself?

    As Norm says, what is the problem exactly? What happens when you compile the program?
    The more information you can give us the better. It will save us creating our own html file etc to test this..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. New Unanswered Threads Link
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: May 5th, 2011, 12:26 PM
  2. Forward to a link from Java
    By makdu in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: November 30th, 2010, 06:45 AM
  3. Can Anyone Check This Link
    By arpitgadle in forum Java Servlet
    Replies: 5
    Last Post: October 7th, 2009, 08:56 AM
  4. How to upload a file by clicking a link instead of button?
    By raghuprasad in forum Java Theory & Questions
    Replies: 2
    Last Post: May 3rd, 2009, 05:21 AM
  5. How to Hyper-link pages together in Java?
    By don.java1 in forum Java Applets
    Replies: 5
    Last Post: July 21st, 2008, 05:09 AM

Tags for this Thread