01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.cocoon.components.modules.input;
19:
20: import java.util.Iterator;
21: import java.util.Map;
22: import java.util.Vector;
23:
24: import org.apache.avalon.framework.configuration.Configuration;
25: import org.apache.avalon.framework.configuration.ConfigurationException;
26: import org.apache.avalon.framework.thread.ThreadSafe;
27: import org.apache.cocoon.environment.ObjectModelHelper;
28:
29: /**
30: * BaseLinkModule returns a relative link (<code>../</code>,
31: * <code>../../</code> etc) to the base of the current request or sitemap URI. For
32: * instance, if called within a <map:match pattern="a/b/c.xsp"> pipeline,
33: * <code>{baselink:SitemapBaseLink}</code> would evaluate to <code>../../</code>.
34: *
35: * based on {@link RequestURIModule}
36: *
37: * @author <a href="mailto:tk-cocoon@datas-world.de">Torsten Knodt</a>
38: */
39: public class BaseLinkModule extends AbstractInputModule implements
40: ThreadSafe {
41:
42: final static Vector returnNames = new Vector() {
43: {
44: add("RequestBaseLink");
45: add("SitemapBaseLink");
46: }
47: };
48:
49: public Object getAttribute(final String name,
50: final Configuration modeConf, final Map objectModel)
51: throws ConfigurationException {
52:
53: String uri;
54: if (name.equals("SitemapBaseLink"))
55: uri = ObjectModelHelper.getRequest(objectModel)
56: .getSitemapURI();
57: else if (name.equals("RequestBaseLink"))
58: uri = ObjectModelHelper.getRequest(objectModel)
59: .getRequestURI();
60: else
61: uri = "";
62:
63: if (uri.startsWith("/")) {
64: uri = uri.substring(1);
65: }
66:
67: StringBuffer result = new StringBuffer(uri.length());
68:
69: int nextIndex = 0;
70: while ((nextIndex = uri.indexOf('/', nextIndex) + 1) > 0) {
71: result.append("../");
72: }
73:
74: if (getLogger().isDebugEnabled())
75: getLogger().debug(
76: "Returns " + result + " for uri " + uri
77: + " and attribute " + name);
78:
79: return result.toString();
80: }
81:
82: public Iterator getAttributeNames(final Configuration modeConf,
83: final Map objectModel) throws ConfigurationException {
84:
85: return RequestURIModule.returnNames.iterator();
86: }
87:
88: public Object[] getAttributeValues(final String name,
89: final Configuration modeConf, final Map objectModel)
90: throws ConfigurationException {
91:
92: Object result = new Object[1];
93: result = getAttribute(name, modeConf, objectModel);
94: return (result == null ? null : new Object[] { result });
95: }
96:
97: }
|