KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > sitemap > LinkGatherer


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.sitemap;
17
18 import org.apache.avalon.framework.parameters.Parameters;
19 import org.apache.cocoon.ProcessingException;
20 import org.apache.cocoon.Constants;
21 import org.apache.cocoon.caching.CacheableProcessingComponent;
22 import org.apache.cocoon.environment.SourceResolver;
23 import org.apache.cocoon.transformation.Transformer;
24 import org.apache.cocoon.xml.xlink.ExtendedXLinkPipe;
25
26 import org.apache.excalibur.source.SourceValidity;
27
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 import java.io.IOException JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 /**
36  * @author <a HREF="mailto:uv@upaya.co.uk">Upayavira</a>
37  * @version CVS $Id: LinkGatherer.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

39 public class LinkGatherer extends ExtendedXLinkPipe implements Transformer, CacheableProcessingComponent {
40     private List JavaDoc links;
41
42
43     /**
44      * Set the <code>SourceResolver</code>, objectModel <code>Map</code>,
45      * the source and sitemap <code>Parameters</code> used to process the request.
46      */

47     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par) throws ProcessingException,
48         SAXException JavaDoc, IOException JavaDoc {
49             this.links = (List JavaDoc)objectModel.get(Constants.LINK_COLLECTION_OBJECT);
50     }
51
52     /**
53      * Generate the unique key.
54      * This key must be unique inside the space of this component.
55      *
56      * @return The generated key hashes the src
57      */

58     public java.io.Serializable JavaDoc getKey() {
59         return "1";
60     }
61
62     /**
63      * Generate the validity object.
64      *
65      * @return The generated validity object or <code>null</code> if the
66      * component is currently not cacheable.
67      */

68     public SourceValidity getValidity() {
69 // Whilst the cache does not store gathered links, this component must be non-cacheable
70
// return NOPValidity.SHARED_INSTANCE;
71
return null;
72     }
73
74     public void simpleLink(String JavaDoc href, String JavaDoc role, String JavaDoc arcrole, String JavaDoc title, String JavaDoc show, String JavaDoc actuate, String JavaDoc uri,
75         String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr) throws SAXException JavaDoc {
76             if (!this.links.contains(href)){
77                 this.addLink(href);
78             }
79             super.simpleLink(href, role, arcrole, title, show, actuate, uri, name, raw, attr);
80     }
81
82     public void startLocator(String JavaDoc href, String JavaDoc role, String JavaDoc title, String JavaDoc label, String JavaDoc uri, String JavaDoc name, String JavaDoc raw,
83         Attributes JavaDoc attr) throws SAXException JavaDoc {
84             if (!this.links.contains(href)){
85                 this.addLink(href);
86             }
87             super.startLocator(href, role, title, label, uri, name, raw, attr);
88     }
89     private void addLink(String JavaDoc href) {
90         if (href.length() == 0) return;
91         if (href.charAt(0) == '#') return;
92         if (href.indexOf("://") != -1) return;
93         if (href.startsWith("mailto:")) return;
94         if (href.startsWith("news:")) return;
95         if (href.startsWith("javascript:")) return;
96
97         int anchorPos = href.indexOf('#');
98         if (anchorPos == -1) {
99             this.links.add(href);
100         } else {
101             this.links.add(href.substring(0, anchorPos));
102         }
103     }
104 }
105
Popular Tags