Author: giacomo Date: Tue Feb 8 06:21:04 2005 New Revision: 152673 URL: http://svn.apache.org/viewcvs?view=rev&rev=152673 Log: Added input module that resolves to a real path similar to the RealPathModules but by use of a SourceResolver instead of the Context object (which always resolves to the context path). This allows to mount apps external of the servlet context path and still have a correct real path resolved in respect to the sitemap calling the module. As I couldn't fix the RealPathModule in a backward compatible way, becasuse {realpath:/} and {realpath:.} both resolve to the context root whereas {contextpath:/} resolves to the context root but {contextpath:.} resolves to the path of the respective sitemap, I've choosen to create a new Module for it.
Added: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/ContextPathModule.java Modified: cocoon/branches/BRANCH_2_1_X/src/webapp/WEB-INF/cocoon.xconf Added: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/ContextPathModule.java URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/ContextPathModule.java?view=auto&rev=152673 ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/ContextPathModule.java (added) +++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/ContextPathModule.java Tue Feb 8 06:21:04 2005 @@ -0,0 +1,135 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cocoon.components.modules.input; +import org.apache.avalon.framework.service.ServiceException; +import org.apache.avalon.framework.service.ServiceManager; +import org.apache.avalon.framework.service.Serviceable; + +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.thread.ThreadSafe; +import org.apache.excalibur.source.SourceResolver; + +import java.io.IOException; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Vector; + +/** + * ContextRealPathModule provides a real filesystem path for a virtual + * context-relative path. If this mapping cannot be performed (e.g. Cocoon is + * running in a .war file), <code>null</code> will be returned. Compared to + * the [EMAIL PROTECTED] RealPathModule} this module is able to provide the "real" absolute + * path even if the application is mounted outside the webapp tree of Cocoon. + * <p> + * Note: the primary use for this is to support external code that wants a + * filesystem path. For example, The FOP 0.20.x serializer doesn't like + * relative image paths, and doesn't understand Cocoon URLs (context:, cocoon: + * etc). So we pass the *2fo.xsl stylesheet a real filesystem path to where we + * keep our images: + * </p> + * <p> + * A absolute path argument like {contextrealpath:/resources} will be resolved + * from the root context path (ie. COCOON_HOME/build/webapp) whereas a relative + * path attribute like {contextrealpath:./resources} will be resolved from the + * location of the sitemap that uses it. If that sitemap is mounted outside the + * usual COCOON_HOME/build/webapp the path resolved with this modules points to + * the correct location. + * </p> + * <p> + * <pre> + * <map:transform src="skins/{forrest:skin}/xslt/fo/document2fo.xsl"> + * <map:parameter name="basedir" value="{contextrealpath:resources}/"/> + * </map:transform> + * + * And then prepend this to all image paths: + * ... + * <xsl:param name="basedir" select="''"/> + * ... + * <xsl:template match="img"> + * <xsl:variable name="imgpath" select="concat($basedir, @src)"/> + * <fo:external-graphic src="{$imgpath}" ... + * ... + * </xsl:template> + * </pre> + * </p> + * + * @author <a href="mailto:giacomo at apache dor org">Giacomo Pati</a> + * @version CVS $Id: RealPathModule.java 30932 2004-07-29 17:35:38Z vgritsenko $ + */ +public class ContextPathModule extends AbstractInputModule implements Serviceable, ThreadSafe { + + private ServiceManager m_manager; + private SourceResolver m_resolver; + + final static Vector returnNames; + static { + Vector tmp = new Vector(); + tmp.add("contextPath"); + returnNames = tmp; + } + + /** (non-Javadoc) + * * @see Serviceable#service(ServiceManager) + * */ + public void service(ServiceManager manager) throws ServiceException { + m_manager = manager; + m_resolver = (SourceResolver) m_manager.lookup(SourceResolver.ROLE); + } + + /** (non-Javadoc) + * + * @see org.apache.avalon.framework.activity.Disposable#dispose() + * + */ + public void dispose() { + super.dispose(); + if ( this.m_manager != null ) { + this.m_manager.release( this.m_resolver ); + this.m_manager = null; + this.m_resolver = null; + } + } + + public Object getAttribute( String name, Configuration modeConf, Map objectModel ) throws ConfigurationException { + try { + if(name.startsWith("/")) { + return m_resolver.resolveURI("context:/"+name).getURI().substring("file:".length()); + } + return m_resolver.resolveURI(name).getURI().substring("file:".length()); + } catch( final IOException mue ) { + throw new ConfigurationException( "Cannot resolve realpath", mue); + } + } + + public Iterator getAttributeNames( Configuration modeConf, Map objectModel ) throws ConfigurationException { + + return ContextPathModule.returnNames.iterator(); + } + + + public Object[] getAttributeValues( String name, Configuration modeConf, Map objectModel ) + throws ConfigurationException { + + List values = new LinkedList(); + values.add( this.getAttribute(name, modeConf, objectModel) ); + + return values.toArray(); + } +} Modified: cocoon/branches/BRANCH_2_1_X/src/webapp/WEB-INF/cocoon.xconf URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/webapp/WEB-INF/cocoon.xconf?view=diff&r1=152672&r2=152673 ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/webapp/WEB-INF/cocoon.xconf (original) +++ cocoon/branches/BRANCH_2_1_X/src/webapp/WEB-INF/cocoon.xconf Tue Feb 8 06:21:04 2005 @@ -188,6 +188,7 @@ </component-instance> <component-instance logger="core.modules.input" name="nullinput" class="org.apache.cocoon.components.modules.input.NullInputModule"/> <component-instance logger="core.modules.input" name="realpath" class="org.apache.cocoon.components.modules.input.RealPathModule"/> + <component-instance logger="core.modules.input" name="contextpath" class="org.apache.cocoon.components.modules.input.ContextPathModule"/> <component-instance logger="core.modules.input" name="naming" class="org.apache.cocoon.components.modules.input.NamingInputModule"> </component-instance> <component-instance logger="core.modules.input" name="cocoon-properties" class="org.apache.cocoon.components.modules.input.PropertiesFileModule">