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 links = null; private List images = null; private List 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 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 getLinks() { if (this.links != null) { return this.links; } if (this.page == null) { return null; } List array = new ArrayList(); String pattern = "]+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 getImages() { if (this.images != null) { return this.images; } if (this.page == null) { return null; } List array = new ArrayList(); String pattern = "url\\([^\\)]*\\)|]*>|<(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 getFrames() { if (this.frames != null) { return this.links; } if (this.page == null) { return null; } List array = new ArrayList(); 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(""); String tempForm = page.substring(start, end + 7); String actionPattern = "]*action=\"([^\"]*)"; Matcher matcher = Pattern.compile(actionPattern).matcher(tempForm); String action = matcher.find() ? matcher.group(1) : ""; String pluginNamePattern = "]*name=\"([^\"]*)"; matcher = Pattern.compile(pluginNamePattern).matcher(tempForm); String pluginName = matcher.find() ? matcher.group(1) : ""; String pluginPattern = "]*value=\'(\\d)\'[^<]*SUN"; matcher = Pattern.compile(pluginPattern).matcher(tempForm); String plugin = matcher.find() ? matcher.group(1) : ""; String resPattern = ""; return action + "?" + pluginName + "=" + plugin/* + res*/; } }