thefield.setForeground(Color.black); if(listener != null)
listener.validData(jComponent); return true; // if empty, just return true } else
if(input.length() == 0 && !blankOk) {
reportError(thefield,\ return false; // if empty, just return true } /*
* try to convert to an integer */ try{
number = Integer.parseInt(input); }
catch (NumberFormatException e) {
reportError(thefield,\ return false; } /*
* test if its in the range */
if(number < minValue || number > maxValue) {
reportError(thefield,\must enter a number between \\
return false; } /*
* report good data
16
and */
thefield.setForeground(Color.black);
thefield.setText(\ if(listener != null)
listener.validData(jComponent); return true; // valid input found } /**
* report error to the listener (if any) * @param thefield text field being checked * @param message error message to report */
private void reportError(JTextField thefield, String message) {
thefield.setForeground(Color.red); // paint the text red, return false invalid input if(listener != null)
listener.invalidData(message,thefield); } } /*
* Spider.java * */
import java.util.*; import java.io.*; import java.net.*; import javax.swing.*; import javax.swing.tree.*;
import javax.swing.text.html.parser.*;
import javax.swing.text.html.HTMLEditorKit.*; import javax.swing.text.html.*; import javax.swing.text.*; /**
17
* Object used to search the web (or a subset of given domains) for a list of keywords * @author Mark Pendergast */
public class Spider extends Thread{
/** site visit limit (stops search at some point) */ private int siteLimit = 100; /** search depth limit */ private int depthLimit = 100; /** keyword list for seach */ private String keywordList[]; /** ip type list */
private String ipDomainList[]; /** visited tree */
private JTree searchTree = null;
/** message JTextArea, place to post errors */ private JTextArea messageArea; /** place to put search statistics */ private JLabel statsLabel;
/** keep track of web sites searched */ private int sitesSearched = 0;
/** keep track of web sites found with matching criteria */ private int sitesFound = 0;
/** starting site for the search */ private String startSite;
/** flag used to stop search */ private boolean stopSearch = false; /**
* Creates a new instance of Spider
* @param atree JTree used to display the search space
* @param amessagearea JTextArea used to display error/warning messages * @param astatlabel JLabel to display number of searched sites and hits * @param akeywordlist list of keywords to search for
18
* @param aipdomainlist list of top level domains
* @param asitelimit maximum number of web pages to look at
* @param adepthlimit maximum number of levels down to search (controls recursion) * @param astartsite web site to use to start the search */
public Spider(JTree atree, JTextArea amessagearea,JLabel astatlabel, String astartsite, String[] akeywordlist, String[] aipdomainlist, int asitelimit, int adepthlimit) {
searchTree = atree; // place to display search tree
messageArea = amessagearea; // place to display error messages statsLabel = astatlabel; // place to put run statistics startSite = fixHref(astartsite);
keywordList = new String[akeywordlist.length]; for(int i = 0; i< akeywordlist.length; i++)
keywordList[i] = akeywordlist[i].toUpperCase(); // use all upper case for matching
ipDomainList = new String[aipdomainlist.length]; for(int i = 0; i< aipdomainlist.length; i++)
ipDomainList[i] = aipdomainlist[i].toUpperCase(); // use all upper case for matching
siteLimit = asitelimit; // max number of sites to look at depthLimit = adepthlimit; // max depth of recursion to use DefaultMutableTreeNode UrlTreeNode(\
DefaultTreeModel treeModel = new DefaultTreeModel(root); // create a tree model with a root
searchTree.setModel(treeModel);
searchTree.setCellRenderer(new UrlNodeRenderer()); // use a custom cell renderer } /**
* start running the search in a new thread */
root
=
new
DefaultMutableTreeNode(new
19
public void run() {
DefaultTreeModel treeModel = (DefaultTreeModel)searchTree.getModel(); // get our model
DefaultMutableTreeNode root = (DefaultMutableTreeNode)treeModel.getRoot(); String urllc = startSite.toLowerCase();
if(!urllc.startsWith(\ !urllc.startsWith(\ {
startSite = \ // note you must have 3 slashes ! }
else // http missing ? if(urllc.startsWith(\ {
startSite = \ }
startSite = startSite.replace('\\\\', '/'); // fix bad slashes
sitesFound = 0; sitesSearched = 0; updateStats();
searchWeb(root,startSite); // search the web messageArea.append(\ } /**
* search the url search tree to see if we've already visited the specified url * @param urlstring url to search for * @return true if the url is already in the tree */
public boolean urlHasBeenVisited(String urlstring) {
String teststring = fixHref(urlstring);
DefaultTreeModel treeModel = (DefaultTreeModel)searchTree.getModel(); // get our
20