KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > BaseLinkModule


1 /*
2  * Copyright 1999-2002,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
17 package org.apache.cocoon.components.modules.input;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.thread.ThreadSafe;
26 import org.apache.cocoon.environment.ObjectModelHelper;
27
28 /**
29  * BaseLinkModule returns a relative link (<code>../</code>,
30  * <code>../../</code> etc) to the base of the current request or sitemap URI. For
31  * instance, if called within a &lt;map:match pattern="a/b/c.xsp"> pipeline,
32  * <code>{baselink:SitemapBaseLink}</code> would evaluate to <code>../../</code>.
33  *
34  * @author <a HREF="mailto:tk-cocoon@datas-world.de">Torsten Knodt</a>
35  * based on RequestURIModule
36  *
37  */

38 public class BaseLinkModule extends AbstractInputModule implements ThreadSafe {
39
40     final static Vector JavaDoc returnNames = new Vector JavaDoc() {
41         {
42             add("RequestBaseLink");
43             add("SitemapBaseLink");
44         }
45     };
46
47     public Object JavaDoc getAttribute(
48         final String JavaDoc name,
49         final Configuration modeConf,
50         final Map JavaDoc objectModel)
51         throws ConfigurationException {
52
53         String JavaDoc uri;
54         if (name.equals("SitemapBaseLink"))
55             uri = ObjectModelHelper.getRequest(objectModel).getSitemapURI();
56         else if (name.equals("RequestBaseLink"))
57             uri = ObjectModelHelper.getRequest(objectModel).getRequestURI();
58         else
59             uri = "";
60
61         if (uri.startsWith("/")) {
62             uri = uri.substring(1);
63         }
64
65         StringBuffer JavaDoc result = new StringBuffer JavaDoc(uri.length());
66
67         int nextIndex = 0;
68         while ((nextIndex = uri.indexOf('/', nextIndex) + 1) > 0) {
69             result.append("../");
70         }
71
72         if (getLogger().isDebugEnabled())
73             getLogger().debug("Returns " + result + " for uri " + uri + " and attribute " + name);
74
75         return result.toString();
76     }
77
78     public Iterator JavaDoc getAttributeNames(final Configuration modeConf, final Map JavaDoc objectModel)
79         throws ConfigurationException {
80
81         return RequestURIModule.returnNames.iterator();
82     }
83
84     public Object JavaDoc[] getAttributeValues(
85         final String JavaDoc name,
86         final Configuration modeConf,
87         final Map JavaDoc objectModel)
88         throws ConfigurationException {
89
90         Object JavaDoc result = new Object JavaDoc[1];
91         result = getAttribute(name, modeConf, objectModel);
92         return (result == null? null : new Object JavaDoc[]{result});
93     }
94
95 }
96
Popular Tags