messageArea.append(\ Bad URL encountered : \ }
catch(IOException e) {
messageArea.append(\\
} yield(); return; } /**
* Stops the search. */
public void stopSearch() {
stopSearch = true; } /**
* Inner class used to html handle parser callbacks */
public class SpiderParserCallback extends HTMLEditorKit.ParserCallback { /** url node being parsed */ private UrlTreeNode node; /** tree node */
private DefaultMutableTreeNode treenode; /** contents of last text element */ private String lastText = \ /**
* Creates a new instance of SpiderParserCallback * @param atreenode search tree node that is being parsed */
public SpiderParserCallback(DefaultMutableTreeNode atreenode) {
IOException,
could
not
access
site
:
26
treenode = atreenode;
node = (UrlTreeNode)treenode.getUserObject(); } /**
* handle HTML tags that don't have a start and end tag * @param t HTML tag * @param a HTML attributes * @param pos Position within file */
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if(t.equals(HTML.Tag.IMG)) {
node.addImages(1); return; }
if(t.equals(HTML.Tag.BASE)) {
Object value = a.getAttribute(HTML.Attribute.HREF); if(value != null)
node.setBase(fixHref(value.toString())); } } /**
* take care of start tags * @param t HTML tag * @param a HTML attributes * @param pos Position within file */
public void handleStartTag(HTML.Tag t,
MutableAttributeSet a, int pos)
27
{
if(t.equals(HTML.Tag.TITLE)) {
lastText=\ return; }
if(t.equals(HTML.Tag.A)) {
Object value = a.getAttribute(HTML.Attribute.HREF); if(value != null) {
node.addLinks(1);
String href = value.toString(); href = fixHref(href); try{
URL referencedURL = new URL(node.getBase(),href); searchWeb(treenode,
referencedURL.getProtocol()+\
}
catch (MalformedURLException e) {
messageArea.append(\ Bad URL encountered : \ return; } } } } /**
* take care of start tags * @param t HTML tag
* @param pos Position within file */
public void handleEndTag(HTML.Tag t,
28
int pos) {
if(t.equals(HTML.Tag.TITLE) && lastText != null) {
node.setTitle(lastText.trim());
DefaultTreeModel tm = (DefaultTreeModel)searchTree.getModel(); tm.nodeChanged(treenode); } } /**
* take care of text between tags, check against keyword list for matches, if * match found, set the node match status to true * @param data Text between tags
* @param pos position of text within web page */
public void handleText(char[] data, int pos) {
lastText = new String(data); node.addChars(lastText.length()); String text = lastText.toUpperCase(); for(int i = 0; i < keywordList.length; i++) {
if(text.indexOf(keywordList[i]) >= 0) {
if(!node.isMatch()) {
sitesFound++; updateStats(); }
node.setMatch(keywordList[i]); return; } } }
29
} } /*
* SpiderControl.java * */
import java.awt.*; import javax.swing.*; import javax.swing.tree.*; import java.util.*; import java.net.*; /**
* User interface to conduct web searches with the Spider object * @author Mark Pendergast */
public class SpiderControl extends javax.swing.JFrame implements VerifierListener{
/** Creates new form SpiderControl */ public SpiderControl() { initComponents(); setSize(650,600);
setTitle(\ //
// center the frame on the screen //
Dimension oursize = getSize();
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screensize.width - oursize.width)/2; int y = (screensize.height- oursize.height)/2; x = Math.max(0,x); // keep the corner on the screen y = Math.max(0,y); //
30