All pastes #506753 Raw Edit

Mine

public text v1 · immutable
·published 2007-05-24 05:09 UTC
rendered paste body
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WebpageParser {

    private String page = null;
    private List<String> links = null;
    private List<String> images = null;
    private List<String> frames = null;
    private String form = null;

    public WebpageParser() { }

    public static void main(String[] args) {
        WebpageParser parser = new WebpageParser();
        try {
            parser.load(new FileInputStream("example.html"));
            Iterator<String> images = parser.getLinks().iterator();
            while (images.hasNext()) {
                System.out.println(images.next());
            }
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    /**
     * Loads the page from the inputstream
     *
     * @param input inputstream to be read
     */
    public void load(InputStream input) throws IOException{
        int length = input.available();
        byte[] bytes = new byte[length];
        input.read(bytes, 0, length);
        this.page = new String(bytes);
    }

    /**
     * Returns the list of addresses contained in the page
     */
    public List<String> getLinks() {
        if (this.links != null) {
            return this.links;
        }
        if (this.page == null) {
            return null;
        }
        List<String> array = new ArrayList<String>();
        String pattern = "<a[^>]+href=\"[^\"]*\"";
        Matcher matcher = Pattern.compile(pattern).matcher(page);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            String tag = page.substring(start, end);
            int urlStart = tag.indexOf("href=") + 6;
            int urlEnd = tag.indexOf("\"", urlStart);
            String url = tag.substring(urlStart, urlEnd);
            array.add(url);
        }
        return links = array;
    }

    /**
     * Return the list of images contained in the page
     */
    public List<String> getImages() {
        if (this.images != null) {
            return this.images;
        }
        if (this.page == null) {
            return null;
        }
        List<String> array = new ArrayList<String>();
        String pattern =
                "url\\([^\\)]*\\)|<img.+src=\"[^\"]*\"[^>]*>|<(link|input)[^>]*image[^>]*>";
        Matcher matcher = Pattern.compile(pattern).matcher(page);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            String tag = page.substring(start, end);
            // Strip css
            tag = tag.replaceAll("url\\(['\"]*|['\"]*\\)", "");
            // Strip html
            tag = tag.replaceAll("<.*(src|href)=\"|\"[^>]*>", "");
            array.add(tag);
        }
        return images = array;
    }

    /**
     * Returns the list of frames contained in the page
     */
    public List<String> getFrames() {
        if (this.frames != null) {
            return this.links;
        }
        if (this.page == null) {
            return null;
        }
        List<String> array = new ArrayList<String>();
        String pattern = "<[i]??frame[^set][^>]*>";
        Matcher matcher = Pattern.compile(pattern).matcher(page);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            String tag = page.substring(start, end);
            int urlStart = tag.indexOf("src=") + 5;
            int urlEnd = tag.indexOf("\"", urlStart);
            String url = tag.substring(urlStart, urlEnd);
            array.add(url);
        }
        return frames = array;
    }

    public String worldListLink() {
        if (this.form != null) {
            return this.form;
        }
        if (this.page == null) {
            return null;
        }
        int start = page.indexOf("<form");
        int end = page.indexOf("</form>");
        String tempForm = page.substring(start, end + 7);

        String actionPattern = "<form[^>]*action=\"([^\"]*)";
        Matcher matcher = Pattern.compile(actionPattern).matcher(tempForm);
        String action = matcher.find() ? matcher.group(1) : "";


        String pluginNamePattern = "<select[^>]*name=\"([^\"]*)";
        matcher = Pattern.compile(pluginNamePattern).matcher(tempForm);
        String pluginName = matcher.find() ? matcher.group(1) : "";

        String pluginPattern = "<option[^>]*value=\'(\\d)\'[^<]*SUN";
        matcher = Pattern.compile(pluginPattern).matcher(tempForm);
        String plugin = matcher.find() ? matcher.group(1) : "";

        String resPattern = "<input type=\"submit\" name=\"(\\d)\" value=\"High Detail\">";

        return action + "?" + pluginName + "=" + plugin/* + res*/;
    }
}