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

com.mitchellbosecke.pebble.spring4.extension.function.HrefFunction Maven / Gradle / Ivy

There is a newer version: 3.1.5
Show newest version
package com.mitchellbosecke.pebble.spring4.extension.function;

import com.mitchellbosecke.pebble.extension.Function;
import com.mitchellbosecke.pebble.spring4.util.ViewUtils;
import com.mitchellbosecke.pebble.template.EvaluationContext;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.util.StringUtils;

/**
 * Pebble function which adds the context path to the given url
 *
 * @author Eric Bussieres
 */
public class HrefFunction implements Function {

  public static final String FUNCTION_NAME = "href";

  protected static final String PARAM_URL = "url";

  protected List argumentNames;
  private String contextPath;

  /**
   * Constructor
   */
  public HrefFunction() {
    this.argumentNames = new ArrayList<>();
    this.argumentNames.add(PARAM_URL);
  }

  /**
   * {@inheritDoc}
   *
   * @see com.mitchellbosecke.pebble.extension.Function#execute(Map, PebbleTemplate,
   * EvaluationContext, int)
   */
  @Override
  public Object execute(Map args, PebbleTemplate self, EvaluationContext context,
      int lineNumber) {
    StringBuffer result = new StringBuffer();

    result.append(this.getContextPath());
    this.addUrlParameter(args, result);

    return result.toString();
  }

  private void addUrlParameter(Map args, StringBuffer result) {
    String url = (String) args.get(PARAM_URL);
    if (StringUtils.hasText(url)) {
      result.append(url);
    }
  }

  private String getContextPath() {
    if (this.contextPath == null) {
      this.contextPath = ViewUtils.getRequest().getContextPath();
    }

    return this.contextPath;
  }

  /**
   * {@inheritDoc}
   *
   * @see com.mitchellbosecke.pebble.extension.NamedArguments#getArgumentNames()
   */
  @Override
  public List getArgumentNames() {
    return this.argumentNames;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy