KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2005 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 org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22
23 import org.apache.cocoon.environment.ObjectModelHelper;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 /**
30  * RealPathModule provides a real filesystem path for a virtual
31  * context-relative path. If this mapping cannot be performed (e.g. Cocoon is
32  * running in a .war file), <code>null</code> will be returned.
33  *
34  * @author <a HREF="mailto:jefft@apache.org">Jeff Turner</a>
35  * @version $Id: RealPathModule.java 292265 2005-09-28 19:19:18Z vgritsenko $
36  */

37
38 /*
39  * Note: the primary use for this is to support external code that wants a
40  * filesystem path. For example, The FOP 0.20.x serializer doesn't like
41  * relative image paths, and doesn't understand Cocoon URLs (context:, cocoon:
42  * etc). So we pass the *2fo.xsl stylesheet a real filesystem path to where we
43  * keep our images:
44  *
45  * <map:transform SRC="skins/{forrest:skin}/xslt/fo/document2fo.xsl">
46  * <map:parameter name="basedir" value="{realpath:resources}/"/>
47  * </map:transform>
48  *
49  * And then prepend this to all image paths:
50  * ...
51  * <xsl:param name="basedir" select="''"/>
52  * ...
53  * <xsl:template match="img">
54  * <xsl:variable name="imgpath" select="concat($basedir, @src)"/>
55  * <fo:external-graphic SRC="{$imgpath}" ...
56  * ...
57  * </xsl:template>
58  */

59 public class RealPathModule extends AbstractInputModule implements ThreadSafe {
60
61     private final static Vector JavaDoc returnNames;
62     static {
63         Vector JavaDoc tmp = new Vector JavaDoc();
64         tmp.add("realPath");
65         returnNames = tmp;
66     }
67
68     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
69     throws ConfigurationException {
70         String JavaDoc uri = ObjectModelHelper.getContext(objectModel).getRealPath(name);
71         if (uri == null) {
72             return null;
73         }
74
75         int lastCharPos = uri.length() - 1;
76         if (uri.charAt(lastCharPos) == '\\') {
77             uri = uri.substring(0, lastCharPos);
78         }
79         return uri;
80     }
81
82     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
83     throws ConfigurationException {
84         return RealPathModule.returnNames.iterator();
85     }
86
87     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
88     throws ConfigurationException {
89         return new Object JavaDoc[] { getAttribute(name, modeConf, objectModel) };
90     }
91 }
92
Popular Tags