All pastes #994571 Raw Edit

Untitled

public java v1 · immutable
#994571 ·published 2008-04-23 14:22 UTC
rendered paste body
import java.net.*;import java.io.*;import java.util.*;import java.util.regex.*;public class PageParser{	private URLString url;	private EmailAddress[] emails;	private URLString[] urls;	private String siteContent;	public PageParser(Comparable url)	{		// this.emails = new EmailAddress[500];		// this.urls = new URLString[500];		this.url = (URLString) url;		this.siteContent = "";	}	public void parse() throws IOException	{		URL siteURL = new URL(url.toString());		InputStream netStream = siteURL.openStream();		Scanner siteFile = new Scanner(netStream);		while (siteFile.hasNext())		{			siteContent += siteFile.next() + "\n";		}		findEmail();		findURL();		netStream.close();	}	private void findURL()	{		Pattern urlPattern = Pattern				.compile("(http:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)");		Matcher m = urlPattern.matcher(siteContent);		// Find number of URLs in the site's content		int numOfUrls = 0;		while (m.find())		{			numOfUrls++;		}		urls = new URLString[numOfUrls];		// Put URLs into Array		m.reset();		int i = 0;		while (m.find())		{			urls[i] = new URLString(m.group());			i++;		}		//System.out.println("URLS from " + url + ": " + Arrays.toString(urls));	}	private void findEmail()	{		Pattern emailPattern = Pattern				.compile("\\b[A-z0-9._%+-]+@[A-z0-9.-]+\\.[A-z]{2,4}\\b");		Matcher m = emailPattern.matcher(siteContent);		// Find number of Emails in the site's content		int numOfEmails = 0;		while (m.find())		{			numOfEmails++;		}		emails = new EmailAddress[numOfEmails];		// Put URLs into Array		m.reset();		int i = 0;		while (m.find())		{			emails[i] = new EmailAddress(m.group());			i++;		}		//System.out.println("Emails from " + url + ": "+ Arrays.toString(emails));	}	public EmailAddress[] getEmails()	{		return emails;	}	public URLString[] getUrls()	{		return urls;	}}