/*
* Copyright 2005, 2006 by Lars Torunski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.torunski.crawler.core;
import java.util.Collection;
import javax.swing.event.EventListenerList;
import com.torunski.crawler.events.IParserEventListener;
import com.torunski.crawler.events.ParserEvent;
import com.torunski.crawler.filter.ILinkFilter;
import com.torunski.crawler.link.Link;
import com.torunski.crawler.model.ICrawlerModel;
import com.torunski.crawler.parser.IParser;
import com.torunski.crawler.parser.PageData;
/**
* Implements the common needed crawler methods.
*
* @author Lars Torunski
* @version $Revision: 1.2 $
*/
public abstract class AbstractCrawler implements ICrawler {
/** The loader and parser of the pages */
protected IParser parser;
/** The crawler model */
protected ICrawlerModel model;
/** The link filter for the links */
protected ILinkFilter linkFilter;
/** A list of all registered event listeners for this crawler */
private EventListenerList listenerList = new EventListenerList();
/**
* @see com.torunski.crawler.core.ICrawler#getParser()
*/
public IParser getParser() {
return parser;
}
/**
* @see com.torunski.crawler.core.ICrawler#setParser(com.torunski.crawler.parser.IParser)
*/
public void setParser(IParser parser) {
this.parser = parser;
}
/**
* @see com.torunski.crawler.core.ICrawler#getModel()
*/
public ICrawlerModel getModel() {
return model;
}
/**
* @see com.torunski.crawler.core.ICrawler#setModel(com.torunski.crawler.model.ICrawlerModel)
*/
public void setModel(ICrawlerModel model) {
this.model = model;
}
/**
* @see com.torunski.crawler.core.ICrawler#getLinkFilter()
*/
public ILinkFilter getLinkFilter() {
return linkFilter;
}
/**
* @see com.torunski.crawler.core.ICrawler#setLinkFilter(com.torunski.crawler.filter.ILinkFilter)
*/
public void setLinkFilter(ILinkFilter linkFilter) {
this.linkFilter = linkFilter;
}
/**
* @see com.torunski.crawler.core.ICrawler#addParserListener(com.torunski.crawler.events.IParserEventListener)
*/
public void addParserListener(IParserEventListener l) {
listenerList.add(IParserEventListener.class, l);
}
/**
* @see com.torunski.crawler.core.ICrawler#removeParserListener(com.torunski.crawler.events.IParserEventListener)
*/
public void removeParserListener(IParserEventListener l) {
listenerList.remove(IParserEventListener.class, l);
}
/**
* Notify all listeners that have registered interest for
* notification on this event type.
*
* @param link the link of the PageData object.
* @param pageData the PageData object to fire for.
* @param newURIs the outgoing filtered links of the page.
*/
protected void fireParserEvent(Link link, PageData pageData, Collection newURIs) {
// create the event
ParserEvent event = new ParserEvent(this, link, pageData, newURIs);
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == IParserEventListener.class) {
((IParserEventListener) listeners[i + 1]).parse(event);
}
}
}
}
|