package de.ivu.elect.jee.faces;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;

@Named("esc")
@RequestScoped
public class Escape implements Serializable {

  public String escapeHtml(String s) {
    if (StringUtils.isBlank(s)) {
      return s;
    }
    return StringEscapeUtils.escapeHtml4(s);
  }

  public String encodeUrl(String string) {
    if (StringUtils.isBlank(string)) {
      return string;
    }
    try {
      return URLEncoder.encode(string, StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException e) {
      return "<NICHT CODIERBAR>";
    }
  }

}
