KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > bean > helpers > Crawler


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.bean.helpers;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.cocoon.bean.Target;
25 import org.apache.cocoon.ProcessingException;
26
27 /**
28  * A simple Cocoon crawler
29  *
30  * @author <a HREF="mailto:uv@upaya.co.uk">Upayavira</a>
31  * @version CVS $Id: Crawler.java 124685 2005-01-08 22:20:56Z antonio $
32  */

33
34 public class Crawler {
35
36     private Map JavaDoc allTranslatedLinks;
37     private Map JavaDoc stillNotVisited;
38     private Set JavaDoc visitedAlready;
39     
40     public Crawler() {
41         visitedAlready = new HashSet JavaDoc();
42         stillNotVisited = new HashMap JavaDoc();
43         allTranslatedLinks = new HashMap JavaDoc();
44     }
45     
46     /**
47      * Add a target for future processing
48      */

49     public boolean addTarget(Target target) {
50         String JavaDoc 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     /**
61      * Returns the number of targets for processing
62      */

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     /**
88      * Returns an iterator for reading targets
89      */

90     public CrawlingIterator iterator() {
91         return new CrawlingIterator(visitedAlready, stillNotVisited);
92     }
93     
94     public static class CrawlingIterator implements Iterator JavaDoc {
95
96         private Map JavaDoc stillNotVisited;
97         private Set JavaDoc visitedAlready;
98         
99         public CrawlingIterator(Set JavaDoc visitedAlready, Map JavaDoc stillNotVisited) {
100             this.visitedAlready = visitedAlready;
101             this.stillNotVisited = stillNotVisited;
102         }
103
104         /**
105          * Check if list of not visited URIs is empty
106          *
107          * @return boolean true iff list of not visited URIs is not empty
108          */

109         public boolean hasNext() {
110             return !stillNotVisited.isEmpty();
111         }
112
113         /**
114          * Removing objects is not supported, and will always throw
115          * a <code>UnsupportedOperationException</code>.
116          */

117         public void remove(){
118             throw new UnsupportedOperationException JavaDoc();
119         }
120
121         /**
122          * Get next not visited URIs
123          *
124          * @return object from list of not visited URIs, move it immediatly
125          * to set of visited URIs
126          */

127         public Object JavaDoc next() {
128             // could this be simpler:
129
Object JavaDoc nextKey = stillNotVisited.keySet().toArray()[0];
130             Object JavaDoc nextElement = stillNotVisited.remove(nextKey);
131             visitedAlready.add(nextKey);
132             return nextElement;
133         }
134     }
135 }
136
Popular Tags