Class OpenEscape

java.lang.Object
cloud.opencode.base.string.escape.OpenEscape

public final class OpenEscape extends Object
String Escape Facade - Unified entry point for string escaping operations. 字符串转义门面 - 字符串转义操作的统一入口。

Features | 主要功能:

  • HTML/XML escaping - HTML/XML转义
  • Java/JSON escaping - Java/JSON转义
  • SQL escaping - SQL转义
  • URL encoding/decoding - URL编码/解码
  • CSV escaping - CSV转义
  • Regex and shell escaping - 正则和Shell转义

Usage Examples | 使用示例:

String html = OpenEscape.escapeHtml("<script>"); // "&lt;script&gt;"
String sql = OpenEscape.escapeSql("O'Brien");    // "O''Brien"
String url = OpenEscape.encodeUrl("hello world"); // "hello+world"
String csv = OpenEscape.escapeCsv("a,b");        // "\"a,b\""

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-string V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • escapeHtml

      public static String escapeHtml(String str)
    • unescapeHtml

      public static String unescapeHtml(String str)
    • escapeHtmlStrict

      public static String escapeHtmlStrict(String str)
      Strict HTML escape that additionally encodes the forward slash / as &#x2F;, on top of the five characters handled by escapeHtml(String). 严格 HTML 转义:在 escapeHtml(String) 处理的 5 个字符基础上, 额外将正斜杠 / 编码为 &#x2F;

      Use this variant when the escaped value will be inserted into HTML where a later </script> or </style> sequence in user content could close the surrounding script/style block — a classic XSS vector. Encoding / prevents the closing-tag form from materialising. This matches the OWASP XSS Cheat Sheet recommendation for HTML body context. 当转义后的内容将插入到 HTML 中,且用户内容里后续可能出现 </script></style> 这类闭合序列以提前关闭脚本/样式块时(经典 XSS 攻击向量), 应使用此严格变体。编码 / 可阻断闭合标签形态。该行为符合 OWASP XSS Cheat Sheet 在 HTML body 上下文下的推荐做法。

      Parameters:
      str - input string, may be null | 输入字符串,可为 null
      Returns:
      strictly-escaped string (5 base chars + /), or null if input is null | 严格转义后的字符串(5 个基础字符 + /),输入为 null 时返回 null
      Since:
      opencode-base-string V1.0.4
    • escapeXml

      public static String escapeXml(String str)
    • unescapeXml

      public static String unescapeXml(String str)
    • escapeJava

      public static String escapeJava(String str)
    • unescapeJava

      public static String unescapeJava(String str)
    • escapeJson

      public static String escapeJson(String str)
    • unescapeJson

      public static String unescapeJson(String str)
    • escapeJsonStrict

      public static String escapeJsonStrict(String str)
      RFC 8259 compliant JSON string escape. 符合 RFC 8259 规范的 JSON 字符串转义。

      Unlike escapeJson(String) (which aliases escapeJava(String) for historical reasons), this variant follows the JSON specification strictly: 与 escapeJson(String)(出于历史原因别名为 escapeJava(String))不同, 本方法严格遵循 JSON 规范:

      • Escapes \\, ", \b, \f, \n, \r, \t as their JSON short escapes. 转义 \\"\b\f\n\r\t 为对应的 JSON 短转义。
      • Escapes all other control characters in the range U+0000-U+001F as \\uXXXX (lowercase hex). RFC 8259 requires all such characters to be escaped — strict JSON parsers reject raw control characters inside string literals. 范围 U+0000-U+001F 内的其他控制字符转义为 \\uXXXX(小写十六进制)。 RFC 8259 要求所有控制字符必须转义 — 严格的 JSON parser 会拒绝字符串字面量中的裸控制字符。
      • Does NOT escape the apostrophe '. JSON does not recognise \' as a valid escape sequence; passing output of escapeJava(String) (which emits \') to a strict JSON parser is non-conformant. 不转义单引号 '。JSON 不识别 \' 作为有效转义序列; 将 escapeJava(String) 输出(含 \')传给严格 JSON parser 不合规。
      • Forward slash / is left as-is. The JSON spec permits but does not require \/ — most parsers accept the literal form. 正斜杠 / 原样保留。JSON 规范允许但不要求 \/ 转义 — 多数 parser 接受字面形式。

      Use this method when the output must be valid JSON for strict parsers (e.g. Insomnia workspace export, generated package.json, REST API response bodies). For loose contexts that already accept Java-style escapes (logging, debug dumps), escapeJson(String) remains acceptable. 当输出需要被严格 JSON parser 接受时使用本方法(例如 Insomnia 工作区导出、 生成的 package.json、REST API 响应体)。对已经接受 Java 风格转义的宽松场景 (日志、调试输出),escapeJson(String) 仍可使用。

      Parameters:
      str - input string, may be null | 输入字符串,可为 null
      Returns:
      RFC 8259 compliant escaped string, or null if input is null
      Since:
      opencode-base-string V1.0.4
    • escapeSql

      public static String escapeSql(String str)
    • encodeUrl

      public static String encodeUrl(String str)
    • decodeUrl

      public static String decodeUrl(String str)
    • escapeCsv

      public static String escapeCsv(String str)
    • unescapeCsv

      public static String unescapeCsv(String str)
    • escapeRegex

      public static String escapeRegex(String str)
    • escapeShell

      public static String escapeShell(String str)