001/*
002 * Shredzone Commons
003 *
004 * Copyright (C) 2012 Richard "Shred" Körber
005 *   http://commons.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU Library General Public License as
009 * published by the Free Software Foundation, either version 3 of the
010 * License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU Library General Public License
018 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019 */
020
021package org.shredzone.commons.view.util;
022
023import javax.annotation.ParametersAreNonnullByDefault;
024
025import org.shredzone.commons.view.PathContext;
026import org.springframework.expression.EvaluationContext;
027import org.springframework.expression.spel.support.StandardEvaluationContext;
028
029/**
030 * An {@link EvaluationContext} that offers additional string functions.
031 *
032 * <dl>
033 *   <dt><code>simplify</code></dt>
034 *   <dd>simplifies a unicode string so it can be used in URLs (see {@link PathUtils#simplify(String)})</dd>
035 *
036 *   <dt><code>suffix</code></dt>
037 *   <dd>suggests a suffix for a content type (see {@link PathUtils#suffix(String)})</dd>
038 *
039 *   <dt><code>encode</code></dt>
040 *   <dd>url encodes a string (see {@link PathUtils#encode(String)})</dd>
041 * </dl>
042 *
043 * @author Richard "Shred" Körber
044 */
045@ParametersAreNonnullByDefault
046public class ViewPathEvaluationContext extends StandardEvaluationContext {
047
048    /**
049     * Instantiates a new view path evaluation context.
050     *
051     * @param context
052     *            properties and variables to be used
053     */
054    public ViewPathEvaluationContext(PathContext context) {
055        super(context);
056        try {
057            init();
058            setVariables(context.getVariables());
059        } catch (NoSuchMethodException ex) {
060            throw new IllegalStateException("Exception while creating context", ex);
061        }
062    }
063
064    /**
065     * Initializes the evaluation context. Subclasses may override this method to register
066     * more functions.
067     */
068    protected void init() throws NoSuchMethodException {
069        registerFunction(
070                "simplify",
071                PathUtils.class.getDeclaredMethod("simplify", new Class[] { String.class })
072        );
073        registerFunction(
074                "suffix",
075                PathUtils.class.getDeclaredMethod("suffix", new Class[] { String.class })
076        );
077        registerFunction(
078                "encode",
079                PathUtils.class.getDeclaredMethod("encode", new Class[] { String.class })
080        );
081    }
082
083}