001: /*
002: * Copyright 2005, 2006 by Lars Torunski
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package com.torunski.crawler.core;
018:
019: import java.util.Collection;
020:
021: import javax.swing.event.EventListenerList;
022:
023: import com.torunski.crawler.events.IParserEventListener;
024: import com.torunski.crawler.events.ParserEvent;
025: import com.torunski.crawler.filter.ILinkFilter;
026: import com.torunski.crawler.link.Link;
027: import com.torunski.crawler.model.ICrawlerModel;
028: import com.torunski.crawler.parser.IParser;
029: import com.torunski.crawler.parser.PageData;
030:
031: /**
032: * Implements the common needed crawler methods.
033: *
034: * @author Lars Torunski
035: * @version $Revision: 1.2 $
036: */
037: public abstract class AbstractCrawler implements ICrawler {
038:
039: /** The loader and parser of the pages */
040: protected IParser parser;
041:
042: /** The crawler model */
043: protected ICrawlerModel model;
044:
045: /** The link filter for the links */
046: protected ILinkFilter linkFilter;
047:
048: /** A list of all registered event listeners for this crawler */
049: private EventListenerList listenerList = new EventListenerList();
050:
051: /**
052: * @see com.torunski.crawler.core.ICrawler#getParser()
053: */
054: public IParser getParser() {
055: return parser;
056: }
057:
058: /**
059: * @see com.torunski.crawler.core.ICrawler#setParser(com.torunski.crawler.parser.IParser)
060: */
061: public void setParser(IParser parser) {
062: this .parser = parser;
063: }
064:
065: /**
066: * @see com.torunski.crawler.core.ICrawler#getModel()
067: */
068: public ICrawlerModel getModel() {
069: return model;
070: }
071:
072: /**
073: * @see com.torunski.crawler.core.ICrawler#setModel(com.torunski.crawler.model.ICrawlerModel)
074: */
075: public void setModel(ICrawlerModel model) {
076: this .model = model;
077: }
078:
079: /**
080: * @see com.torunski.crawler.core.ICrawler#getLinkFilter()
081: */
082: public ILinkFilter getLinkFilter() {
083: return linkFilter;
084: }
085:
086: /**
087: * @see com.torunski.crawler.core.ICrawler#setLinkFilter(com.torunski.crawler.filter.ILinkFilter)
088: */
089: public void setLinkFilter(ILinkFilter linkFilter) {
090: this .linkFilter = linkFilter;
091: }
092:
093: /**
094: * @see com.torunski.crawler.core.ICrawler#addParserListener(com.torunski.crawler.events.IParserEventListener)
095: */
096: public void addParserListener(IParserEventListener l) {
097: listenerList.add(IParserEventListener.class, l);
098: }
099:
100: /**
101: * @see com.torunski.crawler.core.ICrawler#removeParserListener(com.torunski.crawler.events.IParserEventListener)
102: */
103: public void removeParserListener(IParserEventListener l) {
104: listenerList.remove(IParserEventListener.class, l);
105: }
106:
107: /**
108: * Notify all listeners that have registered interest for
109: * notification on this event type.
110: *
111: * @param link the link of the PageData object.
112: * @param pageData the PageData object to fire for.
113: * @param newURIs the outgoing filtered links of the page.
114: */
115: protected void fireParserEvent(Link link, PageData pageData,
116: Collection newURIs) {
117: // create the event
118: ParserEvent event = new ParserEvent(this , link, pageData,
119: newURIs);
120:
121: // Guaranteed to return a non-null array
122: Object[] listeners = listenerList.getListenerList();
123:
124: // Process the listeners last to first, notifying
125: // those that are interested in this event
126: for (int i = listeners.length - 2; i >= 0; i -= 2) {
127: if (listeners[i] == IParserEventListener.class) {
128: ((IParserEventListener) listeners[i + 1]).parse(event);
129: }
130: }
131: }
132:
133: }
|