Add Url Reference functionality.

This commit is contained in:
Christian P. MOMON 2010-10-11 14:56:08 +02:00
parent f896b9b0dd
commit 5e939ed560

102
src/fr/devinsy/kiss4web/SimpleServletDispatcher.java Executable file → Normal file
View file

@ -11,6 +11,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
@ -18,7 +20,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import fr.devinsy.util.StringConcatenator;
import fr.devinsy.util.StringList;
/**
@ -28,6 +30,8 @@ public class SimpleServletDispatcher extends HttpServlet
{
private static final long serialVersionUID = -3471226305721330069L;
static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (ServletDispatcher.class);
static protected Pattern URL_PARAMETER_PATTERN = Pattern.compile("^(.+)-\\d+\\.xhtml$");
static protected Pattern URL_PARAMETER_PATTERN2 = Pattern.compile("^.+-(\\d+)\\.xhtml$");
protected String webclassesRootPath;
@ -61,10 +65,13 @@ public class SimpleServletDispatcher extends HttpServlet
/**
* Convert a path in a class name, using easy conventions.
*
* "/" => "Index_xhtml"
* "/good/" => "good.Good_xhtml"
* "/good/morning.xhtml" => "good.Morning_xhtml"
* "/good/day_day.xhtml" => "good.Day_day_xhtml"
* "/good/morning_girl.xhtml" => "good.Morning_girl_xhtml"
* "/good/morning-123.xhtml" => "good.Morning_xhtml" (123 is detected as a parameter, it will be decoded in the class called later).
*/
static public String pathInfoToClassName (String pathInfo)
{
@ -83,7 +90,7 @@ public class SimpleServletDispatcher extends HttpServlet
else
{
String[] tokens = pathInfo.split ("/");
StringConcatenator name = new StringConcatenator();
StringList name = new StringList();
for (int tokenCounter = 1; tokenCounter < tokens.length - 1; tokenCounter++)
{
@ -98,6 +105,13 @@ public class SimpleServletDispatcher extends HttpServlet
}
else
{
Matcher matcher = URL_PARAMETER_PATTERN.matcher(lastToken);
if (matcher.matches())
{
//logger.debug("group 1=[" + matcher.group(1) + "]");
lastToken = matcher.group(1) + ".xhtml";
}
name.append (lastToken.substring(0, 1).toUpperCase()).append (lastToken.substring(1).replace('.', '_'));
}
@ -120,17 +134,19 @@ public class SimpleServletDispatcher extends HttpServlet
{
String result;
String className = pathInfoToClassName(pathInfo);
if (prefix == null)
{
result = pathInfoToClassName(pathInfo);
result = className;
}
else if (prefix.endsWith("."))
{
result = prefix + pathInfoToClassName(pathInfo);
result = prefix + className;
}
else
{
result = prefix + "." + pathInfoToClassName(pathInfo);
result = prefix + "." + className;
}
//
@ -362,6 +378,80 @@ public class SimpleServletDispatcher extends HttpServlet
}
/**
* Extract value from a path.
* Example:
* "/article-123.xhtml" => "123".
*/
static public String rewritedParameter(String path)
{
String result;
Matcher matcher = Pattern.compile("^.+-(\\d+)\\.xhtml$").matcher(path);
if (matcher.matches())
{
//logger.debug("group 1=[" + matcher.group(1) + "]");
result = matcher.group(1);
}
else
{
result = null;
}
//
return(result);
}
/**
*
*/
static public String rewritedParameter(HttpServletRequest request)
{
String result;
//logger.debug("uri=[" + request.getRequestURI() + "]");
Matcher matcher = URL_PARAMETER_PATTERN2.matcher(request.getRequestURI());
if (matcher.matches())
{
//logger.debug("group 1=[" + matcher.group(1) + "]");
result = matcher.group(1);
}
else
{
result = null;
}
//
return(result);
}
/**
* Extract values from a path.
* Example:
* "/article-/123/doors/open.xhtml" => "123", "doors" and "open".
*/
static public String[] rewritedParameters(String path)
{
String[] result;
result = new String[10]; // STU.
Matcher matcher = Pattern.compile("^.+-(\\d+)\\.xhtml$").matcher(path);
if (matcher.matches())
{
//logger.debug("group 1=[" + matcher.group(1) + "]");
result[0] = matcher.group(1); // STU.
}
else
{
result = null;
}
//
return(result);
}
/**
*
*/