com.ewcms.publication.uri.UriRules.java Source code

Java tutorial

Introduction

Here is the source code for com.ewcms.publication.uri.UriRules.java

Source

/**
 * Copyright (c)2010-2011 Enterprise Website Content Management System(EWCMS), All rights reserved.
 * EWCMS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * http://www.ewcms.com
 */
package com.ewcms.publication.uri;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.RandomStringUtils;

/**
 * ??uri?
 * 
 * @author wangwei
 */
public class UriRules {
    private final static Map<String, RuleParseable> cacheRuleParse = Collections
            .synchronizedMap(new HashMap<String, RuleParseable>());

    private UriRules() {
        //factory class,so hide.    
    }

    /**
     * uri?
     * 
     * @return
     */
    public static UriRuleable newNull() {
        return new NullUriRule();
    }

    /**
     * Homeuri?
     * 
     * @return
     */
    public static UriRuleable newHome() {
        String patter = "${c.absUrl}/index.html";
        return newUriRuleBy(patter);
    }

    /**
     * Listuri?
     * 
     * @return
     */
    public static UriRuleable newList() {
        String patter = "${c.absUrl}/${p}.html";
        return newUriRuleBy(patter);
    }

    /**
     * Detailuri?
     * 
     * @return
     */
    public static UriRuleable newDetail() {
        String patter = "/document/${a.published}/${a.id}_${p}.html";
        return newUriRuleBy(patter);
    }

    /**
     * ???
     * 
     * @param rule uri
     * @param context ?
     * @return
     */
    private static UriRuleable initResourceParameters(UriRuleable rule, String context) {
        rule.putParameter("context", context);
        rule.putParameter("n", RandomStringUtils.randomNumeric(32));
        return rule;
    }

    /**
     * ?uri?
     * 
     * @return
     */
    public static UriRuleable newResource(String context) {
        String patter = "/${context}/${now}/${n}";
        UriRuleable rule = newUriRuleBy(patter);
        return initResourceParameters(rule, context);
    }

    /**
     * ?uri?
     * 
     * @return
     */
    public static UriRuleable newResourceThumb(String context) {
        String patter = "/${context}/${now}/${n}_thumb";
        UriRuleable rule = newUriRuleBy(patter);
        return initResourceParameters(rule, context);
    }

    /**
     * uri?
     * 
     * @return
     */
    public static UriRuleable newArticlePreview() {
        String patter = "${a.id}.html?view=true&channelId=${c.id}&articleId=${a.id}&page=${p}";
        return newUriRuleBy(patter);
    }

    /**
     * ?uri?
     * b
     * @return
     */
    public static UriRuleable newUriRuleBy(String patter) {
        synchronized (cacheRuleParse) {
            RuleParseable parse = cacheRuleParse.get(patter);
            if (parse == null) {
                parse = new RuleParse(patter);
            }
            return new UriRule(parse);
        }
    }
}