/*
 * Copyright 2002-2005 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.servlet.view.velocity;

import org.springframework.web.servlet.view.AbstractUrlBasedView;

/**
 * Convenience subclass of UrlBasedViewResolver that supports VelocityToolboxView
 * (i.e. Velocity templates w/ toolbox support) and custom subclasses of it.
 *
 * <p>The view class for all views generated by this resolver can be specified
 * via setViewClass. See UrlBasedViewResolver's javadocs for details.
 *
 * <p>Note: When chaining ViewResolvers, a VelocityToolboxViewResolver always needs
 * to be last, as it will attempt to resolve any view name, no matter whether
 * the underlying resource actually exists.
 *
 * @author Shinbou Kawai
 * @since 12.05.2005
 * @see #setViewClass
 * @see #setPrefix
 * @see #setSuffix
 * @see #setRequestContextAttribute
 * @see #setExposeSpringMacroHelpers
 * @see #setVelocityFormatterAttribute
 * @see #setDateToolAttribute
 * @see #setNumberToolAttribute
 * @see VelocityToolboxView
 */
public class VelocityToolboxViewResolver extends VelocityViewResolver {

    private String toolboxConfigLocation = null;

    /**
     * Sets default viewClass to VelocityToolboxView.
     * @see #setViewClass
     */
    public VelocityToolboxViewResolver() {
        setViewClass(VelocityToolboxView.class);
    }
    
    /**
     * Set a Velocity Toolbox config location, for example "/WEB-INF/toolbox.xml",
     * to automatically load a Velocity Tools toolbox definition file and expose
     * all defined tools in the specified scopes. If no config location is
     * specified, no toolbox will be loaded and exposed.
     * <p>The specfied location string needs to refer to a ServletContext
     * resource, as expected by ServletToolboxManager which is part of
     * the view package of Velocity Tools.
     * @see org.apache.velocity.tools.view.servlet.ServletToolboxManager#getInstance
     */
    public void setToolboxConfigLocation(String toolboxConfigLocation) {
        this.toolboxConfigLocation = toolboxConfigLocation;
    }

	protected AbstractUrlBasedView buildView(String viewName) throws Exception {
		VelocityView view = (VelocityView) super.buildView(viewName);
        if (this.toolboxConfigLocation != null) {
            ((VelocityToolboxView) view).setToolboxConfigLocation(this.toolboxConfigLocation);
        }
        return view;
    }

}
