public static String toCssFriendly(Severity severity) {
    if (severity.equals(SEVERITY_INFO)) {
      return "info";
    } else if (severity.equals(SEVERITY_WARN)) {
      return "warn";
    } else if (severity.equals(SEVERITY_ERROR)) {
      return "error";
    } else if (severity.equals(SEVERITY_FATAL)) {
      return "fatal";
    }

    throw new IllegalStateException("Unexpected message severity: " + severity.toString());
  }
  @Override
  public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
    if (!component.isRendered()) {
      return;
    }

    Growl uiGrowl = (Growl) component;
    ResponseWriter writer = facesContext.getResponseWriter();

    String clientId = uiGrowl.getClientId(facesContext);
    Iterator<FacesMessage> allMessages =
        uiGrowl.isGlobalOnly() ? facesContext.getMessages(null) : facesContext.getMessages();
    Map<String, List<FacesMessage>> messages = new HashMap<String, List<FacesMessage>>();
    messages.put("info", new ArrayList<FacesMessage>()); // Bootstrap info
    messages.put("warn", new ArrayList<FacesMessage>()); // Bootstrap warning
    messages.put("error", new ArrayList<FacesMessage>()); // Bootstrap Error
    messages.put("fatal", new ArrayList<FacesMessage>()); // Bootstrap Success

    while (allMessages.hasNext()) {

      FacesMessage message = allMessages.next();
      Severity severity = message.getSeverity();
      if (message.isRendered() && !uiGrowl.isRedisplay()) {
        continue;
      }

      if (severity.equals(FacesMessage.SEVERITY_INFO)) messages.get("info").add(message);
      else if (severity.equals(FacesMessage.SEVERITY_WARN)) messages.get("warn").add(message);
      else if (severity.equals(FacesMessage.SEVERITY_ERROR)) messages.get("error").add(message);
      else if (severity.equals(FacesMessage.SEVERITY_FATAL)) messages.get("fatal").add(message);
    }

    writer.startElement("script", uiGrowl);
    writer.writeAttribute("id", clientId, "id");
    writer.writeText("$(function() { ", null);
    for (String severity : messages.keySet()) {
      List<FacesMessage> severityMessages = messages.get(severity);
      if (severityMessages.size() > 0) {
        encodeSeverityMessages(facesContext, uiGrowl, severity, severityMessages);
      }
    }
    writer.writeText("});", null);

    writer.endElement("script");
  }