Class OpenEscape
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>"); // "<script>"
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 Summary
Modifier and TypeMethodDescriptionstatic Stringstatic Stringstatic Stringstatic StringescapeHtml(String str) static StringescapeHtmlStrict(String str) Strict HTML escape that additionally encodes the forward slash/as/, on top of the five characters handled byescapeHtml(String).static StringescapeJava(String str) static StringescapeJson(String str) static StringescapeJsonStrict(String str) RFC 8259 compliant JSON string escape.static StringescapeRegex(String str) static StringescapeShell(String str) static Stringstatic Stringstatic StringunescapeCsv(String str) static StringunescapeHtml(String str) static StringunescapeJava(String str) static StringunescapeJson(String str) static StringunescapeXml(String str)
-
Method Details
-
escapeHtml
-
unescapeHtml
-
escapeHtmlStrict
Strict HTML escape that additionally encodes the forward slash/as/, on top of the five characters handled byescapeHtml(String). 严格 HTML 转义:在escapeHtml(String)处理的 5 个字符基础上, 额外将正斜杠/编码为/。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 benull| 输入字符串,可为null- Returns:
- strictly-escaped string (5 base chars +
/), ornullif input isnull| 严格转义后的字符串(5 个基础字符 +/),输入为null时返回null - Since:
- opencode-base-string V1.0.4
-
escapeXml
-
unescapeXml
-
escapeJava
-
unescapeJava
-
escapeJson
-
unescapeJson
-
escapeJsonStrict
RFC 8259 compliant JSON string escape. 符合 RFC 8259 规范的 JSON 字符串转义。Unlike
escapeJson(String)(which aliasesescapeJava(String)for historical reasons), this variant follows the JSON specification strictly: 与escapeJson(String)(出于历史原因别名为escapeJava(String))不同, 本方法严格遵循 JSON 规范:- Escapes
\\,",\b,\f,\n,\r,\tas their JSON short escapes. 转义\\、"、\b、\f、\n、\r、\t为对应的 JSON 短转义。 - Escapes all other control characters in the range
U+0000-U+001Fas\\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 ofescapeJava(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.
Insomniaworkspace export, generatedpackage.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 benull| 输入字符串,可为null- Returns:
- RFC 8259 compliant escaped string, or
nullif input isnull - Since:
- opencode-base-string V1.0.4
- Escapes
-
escapeSql
-
encodeUrl
-
decodeUrl
-
escapeCsv
-
unescapeCsv
-
escapeRegex
-
escapeShell
-