1 16 package org.apache.cocoon.bean.helpers; 17 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import org.apache.cocoon.bean.Target; 25 import org.apache.cocoon.ProcessingException; 26 27 33 34 public class Crawler { 35 36 private Map allTranslatedLinks; 37 private Map stillNotVisited; 38 private Set visitedAlready; 39 40 public Crawler() { 41 visitedAlready = new HashSet (); 42 stillNotVisited = new HashMap (); 43 allTranslatedLinks = new HashMap (); 44 } 45 46 49 public boolean addTarget(Target target) { 50 String targetString = target.toString(); 51 if (!visitedAlready.contains(targetString)) { 52 if (!stillNotVisited.containsKey(targetString)) { 53 stillNotVisited.put(targetString, target); 54 return true; 55 } 56 } 57 return false; 58 } 59 60 63 public int getRemainingCount() { 64 return stillNotVisited.size(); 65 } 66 67 public int getProcessedCount() { 68 return visitedAlready.size(); 69 } 70 71 public int getTranslatedCount() { 72 return allTranslatedLinks.size(); 73 } 74 75 public void addTranslatedLink(Target target) throws ProcessingException { 76 allTranslatedLinks.put(target.getSourceURI(), target); 77 } 78 79 public boolean hasTranslatedLink(Target link) { 80 return allTranslatedLinks.get(link.getSourceURI())!=null; 81 } 82 83 public Target getTranslatedLink(Target link) { 84 return (Target) allTranslatedLinks.get(link.getSourceURI()); 85 } 86 87 90 public CrawlingIterator iterator() { 91 return new CrawlingIterator(visitedAlready, stillNotVisited); 92 } 93 94 public static class CrawlingIterator implements Iterator { 95 96 private Map stillNotVisited; 97 private Set visitedAlready; 98 99 public CrawlingIterator(Set visitedAlready, Map stillNotVisited) { 100 this.visitedAlready = visitedAlready; 101 this.stillNotVisited = stillNotVisited; 102 } 103 104 109 public boolean hasNext() { 110 return !stillNotVisited.isEmpty(); 111 } 112 113 117 public void remove(){ 118 throw new UnsupportedOperationException (); 119 } 120 121 127 public Object next() { 128 Object nextKey = stillNotVisited.keySet().toArray()[0]; 130 Object nextElement = stillNotVisited.remove(nextKey); 131 visitedAlready.add(nextKey); 132 return nextElement; 133 } 134 } 135 } 136 | Popular Tags |