You are viewing a plain text version of this content. The canonical link for it is here.
Posted to svn@forrest.apache.org by rg...@apache.org on 2006/11/20 17:33:35 UTC

svn commit: r477242 - /forrest/trunk/whiteboard/forrest2/src/core/org/apache/forrest/core/locationMap/Location.java

Author: rgardler
Date: Mon Nov 20 08:33:34 2006
New Revision: 477242

URL: http://svn.apache.org/viewvc?view=rev&rev=477242
Log:
Enable variable substitution in location URIs

Modified:
    forrest/trunk/whiteboard/forrest2/src/core/org/apache/forrest/core/locationMap/Location.java

Modified: forrest/trunk/whiteboard/forrest2/src/core/org/apache/forrest/core/locationMap/Location.java
URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/forrest2/src/core/org/apache/forrest/core/locationMap/Location.java?view=diff&rev=477242&r1=477241&r2=477242
==============================================================================
--- forrest/trunk/whiteboard/forrest2/src/core/org/apache/forrest/core/locationMap/Location.java (original)
+++ forrest/trunk/whiteboard/forrest2/src/core/org/apache/forrest/core/locationMap/Location.java Mon Nov 20 08:33:34 2006
@@ -22,10 +22,14 @@
 import java.net.URISyntaxException;
 import java.net.URL;
 
+import org.apache.forrest.core.exception.ProcessingException;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
+import com.sun.org.apache.regexp.internal.RE;
+import com.sun.org.apache.regexp.internal.RESyntaxException;
+
 /**
  * A location is a possible source location for a given request URI. There may
  * be more than one location for any request URI, each of the possible locations
@@ -108,13 +112,47 @@
 	 * 
 	 * @return
 	 * @throws MalformedURLException
+	 * @throws ProcessingException
 	 */
-	public URL getResolvedSourceURL() throws MalformedURLException {
+	public URL getResolvedSourceURL(URI requestURI)
+			throws MalformedURLException, ProcessingException {
+
+		URL url;
+		try {
+			url = requestURI.toURL();
+		} catch (final IllegalArgumentException e) {
+			// we'll assume that this is not an absolute URL and therefore
+			// refers to a file
+			url = new URL("file://" + requestURI);
+		}
+		final String urlString = url.toExternalForm();
+
+		RE r;
+		String sourcePath = this.getSourceURI().getPath();
+		try {
+			r = new RE(getRequestPattern());
+		} catch (RESyntaxException re) {
+			throw new ProcessingException(
+					"Unable to extract variable values from request: "
+							+ re.getMessage(), re);
+		}
+
+		if (r.match(urlString)) {
+			String variable;
+			String value;
+			for (int i = 0; i < r.getParenCount(); i++) {
+				variable = "$(" + i + ")";
+				value = r.getParen(i);
+				sourcePath = sourcePath.replace(variable, value);
+			}
+		} else {
+			throw new ProcessingException(
+					"Unable to extract variable values from requestURI");
+		}
+
 		URI uri = getSourceURI();
 		URL resourceURL;
-		final String sourcePath;
 		if (uri.getScheme().equals("classpath")) {
-			sourcePath = uri.getPath();
 			resourceURL = resolveClasspathURI(sourcePath);
 		} else {
 			String strURI = uri.getSchemeSpecificPart();
@@ -164,8 +202,9 @@
 	}
 
 	/**
-	 * Get the scheme used for retrieving this resource.
-	 * The scheme will be the first protocol in the source URI.
+	 * Get the scheme used for retrieving this resource. The scheme will be the
+	 * first protocol in the source URI.
+	 * 
 	 * @return
 	 */
 	public String getScheme() {