KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.cocoon.components.modules.input;
17
18 import org.apache.avalon.framework.configuration.Configurable;
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.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 /**
32  * ProjectPathModule provides relative and absolute paths with regards to the root of a project.
33  * <p>Config:
34  * <pre>
35  * <component-instance logger="core.modules.input"
36  * name="myproject"
37  * class="org.apache.cocoon.components.modules.input.ProjectPathModule">
38  * <uri-prefix>my/project/</uri-prefix>
39  * </component-instance>
40  * </pre>
41  * </p>
42  * <p>Usage:
43  * <pre>
44  * <map:transform SRC="skins/{forrest:skin}/xslt/fo/document2html.xsl">
45  * <map:parameter name="base" value="{myproject:relative}"/>
46  * </map:transform>
47  *
48  * And then prepend this to all image paths:
49  * ...
50  * <xsl:param name="base"/>
51  * ...
52  * <xsl:template match="img">
53  * <img SRC="{concat($base, @src)}" ...
54  * ...
55  * </xsl:template>
56  * </pre>
57  * Then if you are in my/project/some/folder/page.html, the image will have a relative path bact to the root of the project.
58  * <pre>
59  * <img SRC="../../imagename.png"/>
60  * </pre>
61  * Using 'myproject:path' would have given you: /some/folder/page.html<br/>
62  * Using 'myproject:folder' would have given you: /some/folder/
63  * </p>
64  *
65  */

66 public class ProjectPathModule
67     extends AbstractInputModule
68     implements Configurable, ThreadSafe {
69
70     protected static String JavaDoc PROJECT_PARAM_NAME = "uri-prefix";
71     protected static String JavaDoc PROJECT_PARAM_DEFAULT = "/";
72
73     protected String JavaDoc projectBase;
74
75     final static Vector JavaDoc returnNames;
76     static {
77         Vector JavaDoc tmp = new Vector JavaDoc();
78         tmp.add("relative");
79         tmp.add("path");
80         tmp.add("folder");
81         returnNames = tmp;
82     }
83
84     /**
85      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
86      */

87     public void configure(Configuration conf) throws ConfigurationException {
88         this.projectBase = conf.getChild(PROJECT_PARAM_NAME).getValue();
89         if (getLogger().isDebugEnabled()) {
90             getLogger().debug("Configuration supplied: " + this.projectBase);
91         }
92         if (this.projectBase == null) {
93             this.projectBase = PROJECT_PARAM_DEFAULT;
94             if (getLogger().isWarnEnabled()) {
95                 getLogger().warn("No configuration supplied, using default: " + PROJECT_PARAM_DEFAULT);
96             }
97         }
98         if (this.projectBase.equals("")) {
99             this.projectBase = PROJECT_PARAM_DEFAULT;
100             if (getLogger().isWarnEnabled()) {
101                 getLogger().warn("Empty configuration supplied, using default: " + PROJECT_PARAM_DEFAULT);
102             }
103         }
104     }
105
106     /**
107      * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
108      */

109     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
110     throws ConfigurationException {
111         String JavaDoc uri = ObjectModelHelper.getRequest(objectModel).getServletPath();
112         StringBuffer JavaDoc result = new StringBuffer JavaDoc(uri.length());
113         int baseIndex = uri.indexOf(this.projectBase);
114         if (baseIndex != -1) {
115             uri = uri.substring(baseIndex + this.projectBase.length());
116         } else {
117             throw new ConfigurationException( "No project-base path found in URI");
118         }
119         try {
120             // provide a relative path back to the project
121
if (name.startsWith("relative")) {
122                 int nextIndex = 0;
123                 while ((nextIndex = uri.indexOf('/', nextIndex) + 1) > 0) {
124                     result.append("../");
125                 }
126             } else if (name.startsWith("path")) {
127                 // provide the full path from the project
128
result.append("/");
129                 result.append(uri);
130             } else if (name.startsWith("folder")) {
131                 // provide the folder path from the project
132
result.append("/");
133                 result.append(uri.substring(0,uri.lastIndexOf("/") + 1));
134             } else {
135                  if (getLogger().isWarnEnabled()) {
136                      getLogger().warn("Invalid verb: " + name);
137                  }
138             }
139             return result;
140         } catch( final Exception JavaDoc mue ) {
141             throw new ConfigurationException( "Problems resolving project path.", mue);
142         }
143     }
144
145     /**
146      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration, java.util.Map)
147      */

148     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel )
149     throws ConfigurationException {
150         return ProjectPathModule.returnNames.iterator();
151     }
152
153     /**
154      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
155      */

156     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
157     throws ConfigurationException {
158         List JavaDoc values = new LinkedList JavaDoc();
159         values.add( this.getAttribute(name, modeConf, objectModel) );
160         
161         return values.toArray();
162     }
163 }
164
Popular Tags