Changeset 672


Ignore:
Timestamp:
2008-10-06 15:02:40 (5 years ago)
Author:
bruno
Message:

DirectoryRouter?: introduce support for double extensions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/modules/kauri-routing/kauri-routing-impl/src/main/java/org/kauriproject/routing/impl/components/DirectoryRouter.java

    r671 r672  
    1212import org.kauriproject.runtime.rapi.ModuleSource.Resource; 
    1313import org.kauriproject.template.service.TemplateService; 
     14import org.apache.commons.logging.LogFactory; 
     15import org.apache.commons.logging.Log; 
    1416 
    1517import java.util.*; 
     18import java.util.regex.Pattern; 
     19import java.util.regex.Matcher; 
    1620 
    1721/** 
     
    2630    private TemplateService templateService; 
    2731    private Context restletContext; 
     32    private Log log = LogFactory.getLog(getClass()); 
     33 
     34    private static Pattern DOUBLE_EXT = Pattern.compile("^(.*)\\.([^.]*)\\.([^.]+)$"); 
    2835 
    2936    public DirectoryRouter(String path, Context restletContext, KauriModule module, TemplateService templateService) { 
     
    4148 
    4249    private void build() { 
    43         List<String> paths = new ArrayList<String>(); 
    44         scan(path, "", paths); 
     50        List<Page> pages = new ArrayList<Page>(); 
     51        scan(path, "", pages); 
    4552 
    4653        // The "best" matching algorithm of Restlet's Router cannot meaningfully 
     
    4956        // therefore we sort the paths on beforehand on length, since Restlet 
    5057        // will use the first route with best/maximal score. 
    51         Collections.sort(paths, new Comparator<String>() { 
    52             public int compare(String o1, String o2) { 
    53                 if (o1.length() == o2.length()) 
     58        Collections.sort(pages, new Comparator<Page>() { 
     59            public int compare(Page p1, Page p2) { 
     60                if (p1.patternSignificantLength == p2.patternSignificantLength) 
    5461                    return 0; 
    5562                else 
    56                     return o1.length() < o2.length() ? 1 : -1; 
     63                    return p1.patternSignificantLength < p2.patternSignificantLength ? 1 : -1; 
    5764            } 
    5865        }); 
    5966 
    6067        Router router = new Router(); 
    61         for (String path : paths) { 
    62             String templatePath = "kms:/" + this. path + path; 
    63             router.attach(path, new TemplateRestlet(templateService, restletContext, templatePath)); 
     68        for (Page page : pages) { 
     69            if (log.isDebugEnabled()) { 
     70                log.info("Attaching URI pattern " + page.uriPattern + " for template " + page.templatePath); 
     71            } 
     72            router.attach(page.uriPattern, new TemplateRestlet(templateService, restletContext, page.templatePath)); 
    6473        } 
    6574        this.router = router; 
    6675    } 
    6776 
    68     private void scan(String parent, String mountPath, Collection<String> allPaths) { 
     77    private void scan(String parent, String mountPath, Collection<Page> pages) { 
    6978        Resource res = moduleSource.getResource(parent); 
    7079 
     
    7483            Resource childRes = moduleSource.getResource(childPath); 
    7584            if (childRes.isDirectory()) { 
    76                 scan(childPath, mountPath + "/" + child, allPaths); 
     85                scan(childPath, mountPath + "/" + child, pages); 
    7786            } else { 
    78                 allPaths.add(mountPath + "/" + child); 
     87                pages.add(createPage(mountPath + "/" + child)); 
    7988            } 
    8089        } 
     
    101110        } 
    102111    } 
     112 
     113    private Page createPage(String filePath) { 
     114        String templatePath = "kms:/" + this.path + filePath; 
     115        Matcher matcher = DOUBLE_EXT.matcher(filePath); 
     116        if (matcher.matches()) { 
     117            String base = matcher.group(1); 
     118            String publicExt = matcher.group(2); 
     119            String uriPattern = publicExt.length() > 0 ? base + "." + publicExt : base; 
     120            return new Page(uriPattern, templatePath); 
     121        } else { 
     122            return new Page(filePath, templatePath); 
     123        } 
     124    } 
     125 
     126    private static class Page { 
     127        String uriPattern; 
     128        String templatePath; 
     129        int patternSignificantLength; 
     130 
     131        public Page(String uriPattern, String templatePath) { 
     132            this.uriPattern = uriPattern; 
     133            this.templatePath = templatePath; 
     134            this.patternSignificantLength = uriPattern.replaceAll("\\{[^{}]*}", Matcher.quoteReplacement("{}")).length(); 
     135        } 
     136    } 
    103137} 
Note: See TracChangeset for help on using the changeset viewer.