001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.sitemap;
018:
019: import org.apache.avalon.framework.parameters.Parameters;
020: import org.apache.cocoon.ProcessingException;
021: import org.apache.cocoon.Constants;
022: import org.apache.cocoon.caching.CacheableProcessingComponent;
023: import org.apache.cocoon.environment.SourceResolver;
024: import org.apache.cocoon.transformation.Transformer;
025: import org.apache.cocoon.xml.xlink.ExtendedXLinkPipe;
026:
027: import org.apache.excalibur.source.SourceValidity;
028:
029: import org.xml.sax.Attributes;
030: import org.xml.sax.SAXException;
031:
032: import java.io.IOException;
033: import java.util.List;
034: import java.util.Map;
035:
036: /**
037: * @author <a href="mailto:uv@upaya.co.uk">Upayavira</a>
038: * @version CVS $Id: LinkGatherer.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public class LinkGatherer extends ExtendedXLinkPipe implements
041: Transformer, CacheableProcessingComponent {
042: private List links;
043:
044: /**
045: * Set the <code>SourceResolver</code>, objectModel <code>Map</code>,
046: * the source and sitemap <code>Parameters</code> used to process the request.
047: */
048: public void setup(SourceResolver resolver, Map objectModel,
049: String src, Parameters par) throws ProcessingException,
050: SAXException, IOException {
051: this .links = (List) objectModel
052: .get(Constants.LINK_COLLECTION_OBJECT);
053: }
054:
055: /**
056: * Generate the unique key.
057: * This key must be unique inside the space of this component.
058: *
059: * @return The generated key hashes the src
060: */
061: public java.io.Serializable getKey() {
062: return "1";
063: }
064:
065: /**
066: * Generate the validity object.
067: *
068: * @return The generated validity object or <code>null</code> if the
069: * component is currently not cacheable.
070: */
071: public SourceValidity getValidity() {
072: // Whilst the cache does not store gathered links, this component must be non-cacheable
073: // return NOPValidity.SHARED_INSTANCE;
074: return null;
075: }
076:
077: public void simpleLink(String href, String role, String arcrole,
078: String title, String show, String actuate, String uri,
079: String name, String raw, Attributes attr)
080: throws SAXException {
081: if (!this .links.contains(href)) {
082: this .addLink(href);
083: }
084: super .simpleLink(href, role, arcrole, title, show, actuate,
085: uri, name, raw, attr);
086: }
087:
088: public void startLocator(String href, String role, String title,
089: String label, String uri, String name, String raw,
090: Attributes attr) throws SAXException {
091: if (!this .links.contains(href)) {
092: this .addLink(href);
093: }
094: super .startLocator(href, role, title, label, uri, name, raw,
095: attr);
096: }
097:
098: private void addLink(String href) {
099: if (href.length() == 0)
100: return;
101: if (href.charAt(0) == '#')
102: return;
103: if (href.indexOf("://") != -1)
104: return;
105: if (href.startsWith("mailto:"))
106: return;
107: if (href.startsWith("news:"))
108: return;
109: if (href.startsWith("javascript:"))
110: return;
111:
112: int anchorPos = href.indexOf('#');
113: if (anchorPos == -1) {
114: this .links.add(href);
115: } else {
116: this .links.add(href.substring(0, anchorPos));
117: }
118: }
119: }
|