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 org.apache.avalon.framework.configuration.Configuration;
21: import org.apache.avalon.framework.configuration.ConfigurationException;
22: import org.apache.avalon.framework.thread.ThreadSafe;
23:
24: import org.apache.cocoon.environment.ObjectModelHelper;
25:
26: import java.util.Iterator;
27: import java.util.Map;
28: import java.util.Vector;
29:
30: /**
31: * RealPathModule provides a real filesystem path for a virtual
32: * context-relative path. If this mapping cannot be performed (e.g. Cocoon is
33: * running in a .war file), <code>null</code> will be returned.
34: *
35: * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
36: * @version $Id: RealPathModule.java 433543 2006-08-22 06:22:54Z crossley $
37: */
38:
39: /*
40: * Note: the primary use for this is to support external code that wants a
41: * filesystem path. For example, The FOP 0.20.x serializer doesn't like
42: * relative image paths, and doesn't understand Cocoon URLs (context:, cocoon:
43: * etc). So we pass the *2fo.xsl stylesheet a real filesystem path to where we
44: * keep our images:
45: *
46: * <map:transform src="skins/{forrest:skin}/xslt/fo/document2fo.xsl">
47: * <map:parameter name="basedir" value="{realpath:resources}/"/>
48: * </map:transform>
49: *
50: * And then prepend this to all image paths:
51: * ...
52: * <xsl:param name="basedir" select="''"/>
53: * ...
54: * <xsl:template match="img">
55: * <xsl:variable name="imgpath" select="concat($basedir, @src)"/>
56: * <fo:external-graphic src="{$imgpath}" ...
57: * ...
58: * </xsl:template>
59: */
60: public class RealPathModule extends AbstractInputModule implements
61: ThreadSafe {
62:
63: private final static Vector returnNames;
64: static {
65: Vector tmp = new Vector();
66: tmp.add("realPath");
67: returnNames = tmp;
68: }
69:
70: public Object getAttribute(String name, Configuration modeConf,
71: Map objectModel) throws ConfigurationException {
72: String uri = ObjectModelHelper.getContext(objectModel)
73: .getRealPath(name);
74: if (uri == null) {
75: return null;
76: }
77:
78: int lastCharPos = uri.length() - 1;
79: if (uri.charAt(lastCharPos) == '\\') {
80: uri = uri.substring(0, lastCharPos);
81: }
82: return uri;
83: }
84:
85: public Iterator getAttributeNames(Configuration modeConf,
86: Map objectModel) throws ConfigurationException {
87: return RealPathModule.returnNames.iterator();
88: }
89:
90: public Object[] getAttributeValues(String name,
91: Configuration modeConf, Map objectModel)
92: throws ConfigurationException {
93: return new Object[] { getAttribute(name, modeConf, objectModel) };
94: }
95: }
|