KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package org.apache.cocoon.components.modules.input;
18 import org.apache.avalon.framework.service.ServiceException;
19 import org.apache.avalon.framework.service.ServiceManager;
20 import org.apache.avalon.framework.service.Serviceable;
21
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.thread.ThreadSafe;
25 import org.apache.excalibur.source.SourceResolver;
26
27 import java.io.IOException JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 /**
35  * ContextPathModule provides a real filesystem path for a virtual
36  * context-relative path. If this mapping cannot be performed (e.g. Cocoon is
37  * running in a .war file), <code>null</code> will be returned. Compared to
38  * the {@link RealPathModule} this module is able to provide the "real" absolute
39  * path even if the application is mounted outside the webapp tree of Cocoon.
40  * <p>
41  * Note: the primary use for this is to support external code that wants a
42  * filesystem path. For example, The FOP 0.20.x serializer doesn't like
43  * relative image paths, and doesn't understand Cocoon URLs (context:, cocoon:
44  * etc). So we pass the *2fo.xsl stylesheet a real filesystem path to where we
45  * keep our images:
46  * </p>
47  * <p>
48  * A absolute path argument like {contextpath:/resources} will be resolved
49  * from the root context path (ie. COCOON_HOME/build/webapp) whereas a relative
50  * path attribute like {contextpath:./resources} will be resolved from the
51  * location of the sitemap that uses it. If that sitemap is mounted outside the
52  * usual COCOON_HOME/build/webapp the path resolved with this modules points to
53  * the correct location.
54  * </p>
55  * <p>
56  * <pre>
57  * <map:transform SRC="skins/{forrest:skin}/xslt/fo/document2fo.xsl">
58  * <map:parameter name="basedir" value="{contextpath:resources}/"/>
59  * </map:transform>
60  *
61  * And then prepend this to all image paths:
62  * ...
63  * <xsl:param name="basedir" select="''"/>
64  * ...
65  * <xsl:template match="img">
66  * <xsl:variable name="imgpath" select="concat($basedir, @src)"/>
67  * <fo:external-graphic SRC="{$imgpath}" ...
68  * ...
69  * </xsl:template>
70  * </pre>
71  * </p>
72  *
73  * @author <a HREF="mailto:giacomo at apache dor org">Giacomo Pati</a>
74  * @version $Id$
75  */

76 public class ContextPathModule extends AbstractInputModule implements Serviceable, ThreadSafe {
77
78     private ServiceManager m_manager;
79     private SourceResolver m_resolver;
80
81     final static Vector JavaDoc returnNames;
82     static {
83         Vector JavaDoc tmp = new Vector JavaDoc();
84         tmp.add("contextPath");
85         returnNames = tmp;
86     }
87
88         /** (non-Javadoc)
89      * * @see Serviceable#service(ServiceManager)
90      * */

91     public void service(ServiceManager manager) throws ServiceException {
92         m_manager = manager;
93         m_resolver = (SourceResolver) m_manager.lookup(SourceResolver.ROLE);
94     }
95
96     /** (non-Javadoc)
97      *
98      * @see org.apache.avalon.framework.activity.Disposable#dispose()
99      *
100      */

101     public void dispose() {
102         super.dispose();
103         if ( this.m_manager != null ) {
104             this.m_manager.release( this.m_resolver );
105             this.m_manager = null;
106             this.m_resolver = null;
107         }
108     }
109
110     public Object JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel ) throws ConfigurationException {
111         try {
112             if(name.startsWith("/")) {
113                 return m_resolver.resolveURI("context:/"+name).getURI().substring("file:".length());
114             }
115             return m_resolver.resolveURI(name).getURI().substring("file:".length());
116         } catch( final IOException JavaDoc mue ) {
117             throw new ConfigurationException( "Cannot resolve realpath", mue);
118         }
119     }
120
121     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel ) throws ConfigurationException {
122
123         return ContextPathModule.returnNames.iterator();
124     }
125
126
127     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
128         throws ConfigurationException {
129
130             List JavaDoc values = new LinkedList JavaDoc();
131             values.add( this.getAttribute(name, modeConf, objectModel) );
132
133             return values.toArray();
134     }
135 }
136
Popular Tags