FileDocCategorySizeDatePackage
UriPatternMatcher.javaAPI DocAndroid 1.5 API4507Wed May 06 22:41:10 BST 2009org.apache.http.protocol

UriPatternMatcher

public class UriPatternMatcher extends Object
Maintains a map of objects keyed by a request URI pattern. Instances can be looked up by request URI.
Patterns may have three formats:
  • *
  • *<uri>
  • <uri>*
author
Oleg Kalnichevski
version
$Revision: 630662 $

Fields Summary
private final Map
handlerMap
Constructors Summary
public UriPatternMatcher()

        super();
        this.handlerMap = new HashMap();
    
Methods Summary
public java.lang.Objectlookup(java.lang.String requestURI)

        if (requestURI == null) {
            throw new IllegalArgumentException("Request URI may not be null");
        }
        //Strip away the query part part if found
        int index = requestURI.indexOf("?");
        if (index != -1) {
            requestURI = requestURI.substring(0, index);
        }

        // direct match?
        Object handler = this.handlerMap.get(requestURI);
        if (handler == null) {
            // pattern match?
            String bestMatch = null;
            for (Iterator it = this.handlerMap.keySet().iterator(); it.hasNext();) {
                String pattern = (String) it.next();
                if (matchUriRequestPattern(pattern, requestURI)) {
                    // we have a match. is it any better?
                    if (bestMatch == null
                            || (bestMatch.length() < pattern.length())
                            || (bestMatch.length() == pattern.length() && pattern.endsWith("*"))) {
                        handler = this.handlerMap.get(pattern);
                        bestMatch = pattern;
                    }
                }
            }
        }
        return handler;
    
protected booleanmatchUriRequestPattern(java.lang.String pattern, java.lang.String requestUri)

        if (pattern.equals("*")) {
            return true;
        } else {
            return
            (pattern.endsWith("*") && requestUri.startsWith(pattern.substring(0, pattern.length() - 1))) ||
            (pattern.startsWith("*") && requestUri.endsWith(pattern.substring(1, pattern.length())));
        }
    
public voidregister(java.lang.String pattern, java.lang.Object handler)

        if (pattern == null) {
            throw new IllegalArgumentException("URI request pattern may not be null");
        }
        if (handler == null) {
            throw new IllegalArgumentException("HTTP request handelr may not be null");
        }
        this.handlerMap.put(pattern, handler);
    
public voidsetHandlers(java.util.Map map)

        if (map == null) {
            throw new IllegalArgumentException("Map of handlers may not be null");
        }
        this.handlerMap.clear();
        this.handlerMap.putAll(map);
    
public voidunregister(java.lang.String pattern)

        if (pattern == null) {
            return;
        }
        this.handlerMap.remove(pattern);