com.ewcms.publication.freemarker.directive.out.LengthDirectiveOut.java Source code

Java tutorial

Introduction

Here is the source code for com.ewcms.publication.freemarker.directive.out.LengthDirectiveOut.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.freemarker.directive.out;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.ewcms.publication.freemarker.FreemarkerUtil;

import freemarker.core.Environment;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModelException;

/**
 * ?
 * 
 * @author wangwei
 */
public class LengthDirectiveOut implements DirectiveOutable {
    private static final Logger logger = LoggerFactory.getLogger(LengthDirectiveOut.class);

    private static final String LENGTH_PARAM_NAME = "length";
    private static final String MARK_PARAM_NAME = "mark";
    private static final int NOT_LIMIT_LENGTH = -1;
    private static final String DEFAULT_MARK = "...";

    private String lengthParam = LENGTH_PARAM_NAME;
    private String markParam = MARK_PARAM_NAME;

    @SuppressWarnings("rawtypes")
    @Override
    public Object loopValue(Object value, Environment env, Map params) throws TemplateException {
        Assert.notNull(value);
        return constructOut(value, env, params);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public String constructOut(Object value, Environment env, Map params) throws TemplateException {
        Assert.notNull(value);
        String s = getString(value);
        int length = getLength(params);
        if (length == NOT_LIMIT_LENGTH) {
            return s;
        } else {
            if (s.length() > length) {
                return s.substring(0, length) + getMark(params);
            }
            return s;
        }
    }

    private String getString(Object value) throws TemplateModelException {
        if (value instanceof String) {
            return (String) value;
        } else {
            logger.error("Value is not string");
            throw new TemplateModelException("LengthDriectiveOut only user string");
        }
    }

    @SuppressWarnings("rawtypes")
    private int getLength(Map params) throws TemplateException {
        Integer length = FreemarkerUtil.getInteger(params, lengthParam);
        return length == null ? NOT_LIMIT_LENGTH : length;
    }

    @SuppressWarnings("rawtypes")
    private String getMark(Map params) throws TemplateException {
        String mark = FreemarkerUtil.getString(params, markParam);
        return mark == null ? DEFAULT_MARK : mark;
    }

    /**
     * ???
     * 
     * @param param ???
     */
    public void setLengthParam(String param) {
        lengthParam = param;
    }

    /**
     * ???? 
     * 
     * @param param ???
     */
    public void setMarkParam(String param) {
        markParam = param;
    }
}