You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by je...@apache.org on 2005/04/08 21:06:27 UTC

svn commit: r160585 - cocoon/trunk/src/java/org/apache/cocoon/components/modules/input/ProjectPathModule.java

Author: jeremy
Date: Fri Apr  8 12:06:26 2005
New Revision: 160585

URL: http://svn.apache.org/viewcvs?view=rev&rev=160585
Log:
adding ProjectPathModule

Added:
    cocoon/trunk/src/java/org/apache/cocoon/components/modules/input/ProjectPathModule.java

Added: cocoon/trunk/src/java/org/apache/cocoon/components/modules/input/ProjectPathModule.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/modules/input/ProjectPathModule.java?view=auto&rev=160585
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/modules/input/ProjectPathModule.java (added)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/modules/input/ProjectPathModule.java Fri Apr  8 12:06:26 2005
@@ -0,0 +1,143 @@
+/*
+ * 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.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.thread.ThreadSafe;
+
+import org.apache.cocoon.environment.ObjectModelHelper;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Vector;
+
+import org.apache.cocoon.util.HashMap;
+
+/**
+ * ProjectPathModule provides relative and absolute paths with regards to the root of a project.
+ * <p>Config:
+ * <pre>
+ *    <component-instance logger="core.modules.input"
+ *           name="myproject"
+ *           class="org.apache.cocoon.components.modules.input.ProjectPathModule">
+ *      	<uri-prefix>my/project/</uri-prefix>
+ *    </component-instance>
+ * </pre>
+ * </p>
+ * <p>Usage:
+ * <pre>
+ * <map:transform src="skins/{forrest:skin}/xslt/fo/document2html.xsl">
+ *    <map:parameter name="base" value="{myproject:relative}"/>
+ * </map:transform>
+ *
+ * And then prepend this to all image paths:
+ *  ...
+ *  <xsl:param name="base"/>
+ *  ...
+ *  <xsl:template match="img">
+ *      <img src="{concat($base, @src)}" ...
+ *      ...
+ *  </xsl:template>
+ *  </pre>
+ * 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.
+ * <pre>
+ *   <img src="../../imagename.png"/>
+ * </pre>
+ * Using 'projectpath:path' would have given you: /some/folder/page.html<br/>
+ * Using 'projectpath:folder' would have given you: /some/folder/
+ * </p>
+ *
+ */
+public class ProjectPathModule extends AbstractInputModule implements Configurable, ThreadSafe {
+
+		protected static String PROJECT_PARAM_NAME = "uri-prefix";
+		protected static String PROJECT_PARAM_DEFAULT = "/";
+
+		protected String projectBase;
+		
+    final  static Vector returnNames;
+    static {
+			Vector tmp = new Vector();
+			tmp.add("relative");
+			tmp.add("path");
+			tmp.add("folder");
+			returnNames = tmp;
+    }
+
+    public void configure(Configuration conf) throws ConfigurationException {
+			this.projectBase = conf.getChild(PROJECT_PARAM_NAME).getValue();
+			if (getLogger().isDebugEnabled()) getLogger().debug("Configuration supplied: " + this.projectBase);
+			if (this.projectBase == null) {
+       	this.projectBase = PROJECT_PARAM_DEFAULT;
+				if (getLogger().isWarnEnabled()) getLogger().warn("No configuration supplied, using default: " + PROJECT_PARAM_DEFAULT);
+       }
+			if (this.projectBase.equals("")) {
+       	this.projectBase = PROJECT_PARAM_DEFAULT;
+				if (getLogger().isWarnEnabled()) getLogger().warn("Empty configuration supplied, using default: " + PROJECT_PARAM_DEFAULT);
+      }
+    }
+
+    public Object getAttribute(String name, Configuration modeConf, Map objectModel) throws ConfigurationException {
+			String uri = ObjectModelHelper.getRequest(objectModel).getServletPath();
+			StringBuffer result = new StringBuffer(uri.length());
+			int baseIndex = uri.indexOf(this.projectBase);
+			if (baseIndex != -1) {
+				uri = uri.substring(baseIndex + this.projectBase.length());
+			} else {
+				throw new ConfigurationException( "No project-base path found in URI");
+			}
+			try {
+				// provide a relative path back to the project
+				if (name.startsWith("relative")) {
+					int nextIndex = 0;
+	        while ((nextIndex = uri.indexOf('/', nextIndex) + 1) > 0) {
+            result.append("../");
+	        }
+				} else if (name.startsWith("path")) { // provide the full path from the project
+					result.append("/");
+					result.append(uri);
+				} else if (name.startsWith("folder")) { // provide the folder path from the project
+					result.append("/");
+					result.append(uri.substring(0,uri.lastIndexOf("/") + 1));
+				} else {
+					if (getLogger().isWarnEnabled()) getLogger().warn("Invalid verb: " + name);
+				}
+				return result;
+			} catch( final Exception mue ) {
+				throw new ConfigurationException( "Problems resolving project path.", mue);
+			}
+    }
+
+    public Iterator getAttributeNames( Configuration modeConf, Map objectModel ) throws ConfigurationException {
+			return ProjectPathModule.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();
+    }
+}