1 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 ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Vector ; 30 31 66 public class ProjectPathModule 67 extends AbstractInputModule 68 implements Configurable, ThreadSafe { 69 70 protected static String PROJECT_PARAM_NAME = "uri-prefix"; 71 protected static String PROJECT_PARAM_DEFAULT = "/"; 72 73 protected String projectBase; 74 75 final static Vector returnNames; 76 static { 77 Vector tmp = new Vector (); 78 tmp.add("relative"); 79 tmp.add("path"); 80 tmp.add("folder"); 81 returnNames = tmp; 82 } 83 84 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 109 public Object getAttribute(String name, Configuration modeConf, Map objectModel) 110 throws ConfigurationException { 111 String uri = ObjectModelHelper.getRequest(objectModel).getServletPath(); 112 StringBuffer result = new StringBuffer (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 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 result.append("/"); 129 result.append(uri); 130 } else if (name.startsWith("folder")) { 131 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 mue ) { 141 throw new ConfigurationException( "Problems resolving project path.", mue); 142 } 143 } 144 145 148 public Iterator getAttributeNames( Configuration modeConf, Map objectModel ) 149 throws ConfigurationException { 150 return ProjectPathModule.returnNames.iterator(); 151 } 152 153 156 public Object [] getAttributeValues( String name, Configuration modeConf, Map objectModel ) 157 throws ConfigurationException { 158 List values = new LinkedList (); 159 values.add( this.getAttribute(name, modeConf, objectModel) ); 160 161 return values.toArray(); 162 } 163 } 164 | Popular Tags |