<commit_before>package io.github.ihongs.jsp;

import io.github.ihongs.HongsCause;
import io.github.ihongs.HongsExemption;
import io.github.ihongs.action.ActionDriver;
import io.github.ihongs.action.ActionHelper;
import io.github.ihongs.util.Dawn;
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.jsp.HttpJspPage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 抽象JSP类
 *
 * <pre>
 * 如果在JSP中需要使用框架提供的方法和标签, 需要从这继承:
 * &lt;%@page extends="io.github.ihongs.action.Pagelet"%&gt;
 * </pre>
 *
 * @author Hongs
 */
abstract public class Pagelet extends ActionDriver implements HttpJspPage
{

  ActionHelper helper = null;

  @Override
  public void jspInit()
  {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public void jspDestroy()
  {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public void /**/service(HttpServletRequest req, HttpServletResponse rsp)
    throws ServletException, IOException
  {
    try
    {
      this._jspService(req, rsp);
    }
    catch (ServletException ex )
    {
      Throwable ax = ex.getCause( );
      if (ax == null) { ax = ex ; }

      String er = ax.getLocalizedMessage();
      int eo  = ax instanceof HongsCause ? ( (HongsCause) ax).getState() : 0;
      if (eo <= 400 || eo > 600)
      {
          eo  = HttpServletResponse.SC_INTERNAL_SERVER_ERROR ;
      }

      req.setAttribute("javax.servlet.error.status_code", eo);
      req.setAttribute("javax.servlet.error.message"    , er);
      req.setAttribute("javax.servlet.error.exception"  , ax);
      req.setAttribute("javax.servlet.error.exception_type", ax.getClass().getName());
      rsp.sendError(eo, er);
    }
  }

  /**
   * @see escapeXML 的别名
   * @param str
   * @return
   */
  public static String escape(String str) {
      return escapeXML(str);
  }

  /**
   * 转义XML/HTML文本
   * @param str
   * @return
   */
  public static String escapeXML(String str) {
      if (str == null) return "";
      StringBuilder b = new StringBuilder( );<commit_msg>增加一个常用函数<commit_after>package io.github.ihongs.jsp;

import io.github.ihongs.Core;
import io.github.ihongs.HongsCause;
import io.github.ihongs.HongsExemption;
import io.github.ihongs.action.ActionDriver;
import io.github.ihongs.action.ActionHelper;
import io.github.ihongs.util.Dawn;
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.jsp.HttpJspPage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 抽象JSP类
 *
 * <pre>
 * 如果在JSP中需要使用框架提供的方法和标签, 需要从这继承:
 * &lt;%@page extends="io.github.ihongs.action.Pagelet"%&gt;
 * </pre>
 *
 * @author Hongs
 */
abstract public class Pagelet extends ActionDriver implements HttpJspPage
{

  ActionHelper helper = null;

  @Override
  public void jspInit()
  {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public void jspDestroy()
  {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public void /**/service(HttpServletRequest req, HttpServletResponse rsp)
    throws ServletException, IOException
  {
    try
    {
      this._jspService(req, rsp);
    }
    catch (ServletException ex )
    {
      Throwable ax = ex.getCause( );
      if (ax == null) { ax = ex ; }

      String er = ax.getLocalizedMessage();
      int eo  = ax instanceof HongsCause ? ( (HongsCause) ax).getState() : 0;
      if (eo <= 400 || eo > 600)
      {
          eo  = HttpServletResponse.SC_INTERNAL_SERVER_ERROR ;
      }

      req.setAttribute("javax.servlet.error.status_code", eo);
      req.setAttribute("javax.servlet.error.message"    , er);
      req.setAttribute("javax.servlet.error.exception"  , ax);
      req.setAttribute("javax.servlet.error.exception_type", ax.getClass().getName());
      rsp.sendError(eo, er);
    }
  }

  /**
   * 补全URL, 增加应用前缀
   * @param uri
   * @return 
   */
  public static String fixUri(String uri) {
      if (! uri.matches( "^(\\w+:)?/" ) ) {
          uri = Core.BASE_HREF +"/"+ uri;
      }
      return uri;
  }

  /**
   * @see escapeXML 的别名
   * @param str
   * @return
   */
  public static String escape(String str) {
      return escapeXML(str);
  }

  /**
   * 转义XML/HTML文本
   * @param str
   * @return
   */
  public static String escapeXML(String str) {
      if (str == null) return "";
      StringBuilder b = new StringBuilder( );
{'fraction_non_alphanumeric': 0.12656881514571366, 'fraction_numerical': 0.0029780897681344395, 'mean_word_length': 3.183274021352313, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '414432', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1594, 'n_words': 316}