All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.edgarespina.handlebars.springmvc.HandlebarsViewResolver Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2012 Edgar Espina
 *
 * This file is part of Handlebars.java.
 *
 * 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 com.github.edgarespina.handlebars.springmvc;

import java.io.IOException;
import java.net.URI;

import org.springframework.util.Assert;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
import org.springframework.web.servlet.view.AbstractUrlBasedView;

import com.github.edgarespina.handlebars.Handlebars;
import com.github.edgarespina.handlebars.Template;
import com.github.edgarespina.handlebars.TemplateLoader;

/**
 * A Handlebars {@link ViewResolver view resolver}.
 *
 * @author edgar.espina
 * @since 0.1
 */
public class HandlebarsViewResolver extends AbstractTemplateViewResolver {

  /**
   * The default content type.
   */
  public static final String DEFAULT_CONTENT_TYPE = "text/html;charset=UTF-8";

  /**
   * The handlebars instance.
   */
  private Handlebars handlebars;

  /**
   * Creates a new {@link HandlebarsViewResolver}.
   *
   * @param handlebars The handlebars object. Required.
   */
  public HandlebarsViewResolver(final Handlebars handlebars) {
    Assert.notNull(handlebars, "The handlebars object is required.");

    this.handlebars = handlebars;
    setViewClass(HandlebarsView.class);
    setContentType(DEFAULT_CONTENT_TYPE);
  }

  @Override
  public void setPrefix(final String prefix) {
    throw new UnsupportedOperationException("Use "
        + TemplateLoader.class.getName() + "#setPrefix");
  }

  @Override
  public void setSuffix(final String suffix) {
    throw new UnsupportedOperationException("Use "
        + TemplateLoader.class.getName() + "#setSuffix");
  }

  /**
   * Creates a new {@link HandlebarsViewResolver}.
   */
  public HandlebarsViewResolver() {
    this(new Handlebars());
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected AbstractUrlBasedView buildView(final String viewName)
      throws Exception {
    return configure((HandlebarsView) super.buildView(viewName));
  }

  /**
   * Configure the handlebars view.
   *
   * @param view The handlebars view.
   * @return The configured view.
   * @throws IOException If a resource cannot be loaded.
   */
  protected AbstractUrlBasedView configure(final HandlebarsView view)
      throws IOException {
    String url = view.getUrl();
    if (!url.startsWith("/")) {
      url = "/" + url;
    }
    URI uri = URI.create(url);
    Template template = handlebars.compile(uri);
    view.setTemplate(template);
    return view;
  }

  /**
   * The required view class.
   *
   * @return The required view class.
   */
  @Override
  protected Class requiredViewClass() {
    return HandlebarsView.class;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy