All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.janeluo.easypdf.Util Maven / Gradle / Ivy

The newest version!
/* Copyright (c) 2021 janeluo
 * easy-pdf is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.janeluo.easypdf;

/**
 * 通用工具类
 */
public class Util {
    private static final Object[][] XML_ESCAPE_CHARS = {
            {'"', """}, {'&', "&"}, {'\'', "'"},
            {'<', "<"}, {'>', ">"},
    };

    private static final Object[][] HTML_ESCAPE_CHARS = {
            {'"', """}, {'&', "&"}, {'\'', "'"},
            {'<', "<"}, {'>', ">"}, {' ', " "},
            {'¡', "¡"}, {'¢', "¢"}, {'£', "£"},
            {'¤', "¤"}, {'¥', "¥"}, {'¦', "¦"},
            {'§', "§"}, {'¨', "¨"}, {'©', "©"},
            {'ª', "ª"}, {'«', "«"}, {'¬', "¬"},
            {'®', "®"}, {'¯', "¯"}, {'°', "°"},
            {'±', "±"}, {'²', "²"}, {'³', "³"},
            {'´', "´"}, {'µ', "µ"}, {'¶', "¶"},
            {'·', "·"}, {'¸', "¸"}, {'¹', "¹"},
            {'º', "º"}, {'»', "»"}, {'¼', "¼"},
            {'½', "½"}, {'¾', "¾"}, {'¿', "¿"},
            {'À', "À"}, {'Á', "Á"}, {'Â', "Â"},
            {'Ã', "Ã"}, {'Ä', "Ä"}, {'Å', "Å"},
            {'Æ', "Æ"}, {'Ç', "Ç"}, {'È', "È"},
            {'É', "É"}, {'Ê', "Ê"}, {'Ë', "Ë"},
            {'Ì', "Ì"}, {'Í', "Í"}, {'Î', "Î"},
            {'Ï', "Ï"}, {'Ð', "Ð"}, {'Ñ', "Ñ"},
            {'Ò', "Ò"}, {'Ó', "Ó"}, {'Ô', "Ô"},
            {'Õ', "Õ"}, {'Ö', "Ö"}, {'×', "×"},
            {'Ø', "Ø"}, {'Ù', "Ù"}, {'Ú', "Ú"},
            {'Û', "Û"}, {'Ü', "Ü"}, {'Ý', "Ý"},
            {'Þ', "Þ"}, {'ß', "ß"}, {'à', "à"},
            {'á', "á"}, {'â', "â"}, {'ã', "ã"},
            {'ä', "ä"}, {'å', "å"}, {'æ', "æ"},
            {'ç', "ç"}, {'è', "è"}, {'é', "é"},
            {'ê', "ê"}, {'ë', "ë"}, {'ì', "ì"},
            {'í', "í"}, {'î', "î"}, {'ï', "ï"},
            {'ð', "ð"}, {'ñ', "ñ"}, {'ò', "ò"},
            {'ó', "ó"}, {'ô', "ô"}, {'õ', "õ"},
            {'ö', "ö"}, {'÷', "÷"}, {'ø', "ø"},
            {'ù', "ù"}, {'ú', "ú"}, {'û', "û"},
            {'ü', "ü"}, {'ý', "ý"}, {'þ', "þ"},
            {'ÿ', "ÿ"}, {'Œ', "Œ"}, {'œ', "œ"},
            {'Š', "Š"}, {'š', "š"}, {'Ÿ', "Ÿ"},
            {'ƒ', "ƒ"}, {'ˆ', "ˆ"}, {'˜', "˜"},
            {'Α', "Α"}, {'Β', "Β"}, {'Γ', "Γ"},
            {'Δ', "Δ"}, {'Ε', "Ε"}, {'Ζ', "Ζ"},
            {'Η', "Η"}, {'Θ', "Θ"}, {'Ι', "Ι"},
            {'Κ', "Κ"}, {'Λ', "Λ"}, {'Μ', "Μ"},
            {'Ν', "Ν"}, {'Ξ', "Ξ"}, {'Ο', "Ο"},
            {'Π', "Π"}, {'Ρ', "Ρ"}, {'Σ', "Σ"},
            {'Τ', "Τ"}, {'Υ', "Υ"}, {'Φ', "Φ"},
            {'Χ', "Χ"}, {'Ψ', "Ψ"}, {'Ω', "Ω"},
            {'α', "α"}, {'β', "β"}, {'γ', "γ"},
            {'δ', "δ"}, {'ε', "ε"}, {'ζ', "ζ"},
            {'η', "η"}, {'θ', "θ"}, {'ι', "ι"},
            {'κ', "κ"}, {'λ', "λ"}, {'μ', "μ"},
            {'ν', "ν"}, {'ξ', "ξ"}, {'ο', "ο"},
            {'π', "π"}, {'ρ', "ρ"}, {'ς', "ς"},
            {'σ', "σ"}, {'τ', "τ"}, {'υ', "υ"},
            {'φ', "φ"}, {'χ', "χ"}, {'ψ', "ψ"},
            {'ω', "ω"}, {'ϑ', "ϑ"}, {'ϒ', "ϒ"},
            {'ϖ', "ϖ"}, {' ', " "}, {'–', "–"},
            {'—', "—"}, {'‘', "‘"}, {'’', "’"},
            {'‚', "‚"}, {'“', "“"}, {'”', "”"},
            {'„', "„"}, {'†', "†"}, {'‡', "‡"},
            {'•', "•"}, {'…', "…"}, {'‰', "‰"},
            {'′', "′"}, {'″', "″"}, {'‹', "‹"},
            {'›', "›"}, {'‾', "‾"}, {'⁄', "⁄"},
            {'€', "€"}, {'ℑ', "ℑ"}, {'℘', "℘"},
            {'ℜ', "ℜ"}, {'™', "™"}, {'ℵ', "ℵ"},
            {'←', "←"}, {'↑', "↑"}, {'→', "→"},
            {'↓', "↓"}, {'↔', "↔"}, {'↵', "↵"},
            {'⇐', "⇐"}, {'⇑', "⇑"}, {'⇒', "⇒"},
            {'⇓', "⇓"}, {'⇔', "⇔"}, {'∀', "∀"},
            {'∂', "∂"}, {'∃', "∃"}, {'∅', "∅"},
            {'∇', "∇"}, {'∈', "∈"}, {'∉', "∉"},
            {'∋', "∋"}, {'∏', "∏"}, {'∑', "∑"},
            {'−', "−"}, {'∗', "∗"}, {'√', "√"},
            {'∝', "∝"}, {'∞', "∞"}, {'∠', "∠"},
            {'∧', "∧"}, {'∨', "∨"}, {'∩', "∩"},
            {'∪', "∪"}, {'∫', "∫"}, {'∴', "∴"},
            {'∼', "∼"}, {'≅', "≅"}, {'≈', "≈"},
            {'≠', "≠"}, {'≡', "≡"}, {'≤', "≤"},
            {'≥', "≥"}, {'⊂', "⊂"}, {'⊃', "⊃"},
            {'⊄', "⊄"}, {'⊆', "⊆"}, {'⊇', "⊇"},
            {'⊕', "⊕"}, {'⊗', "⊗"}, {'⊥', "⊥"},
            {'⋅', "⋅"}, {'⌈', "⌈"}, {'⌉', "⌉"},
            {'⌊', "⌊"}, {'⌋', "⌋"}, {'〈', "〈"},
            {'〉', "〉"}, {'◊', "◊"}, {'♠', "♠"},
            {'♣', "♣"}, {'♥', "♥"}, {'♦', "♦"},
    };

    /**
     * 对 XML 中的特殊字符进行 escape 处理
     *
     * @param ch 字符
     * @return 编码后的字符串
     */
    public static String escapeXmlChars(char ch) {
        for (Object[] xmlEscapeChar : XML_ESCAPE_CHARS) {
            if ((Character) xmlEscapeChar[0] == ch) {
                return (String) xmlEscapeChar[1];
            }
        }
        return null;
    }

    /**
     * 对 HTML 中的特殊字符进行 escape 处理
     *
     * @param ch 字符
     * @return 编码后的字符串
     */
    public static String escapeHtmlChars(char ch) {
        for (Object[] htmlEscapeChar : HTML_ESCAPE_CHARS) {
            if ((Character) htmlEscapeChar[0] == ch) {
                return (String) htmlEscapeChar[1];
            }
        }
        return null;
    }

    /**
     * 对 HTML 中的特殊字符进行 escape 处理
     *
     * @param string 字符串
     * @return 编码后的字符串
     */
    public static String escapeHtmlString(String string) {
        if (string == null) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < string.length(); i++) {
            String escape = escapeHtmlChars(string.charAt(i));
            if (escape != null) {
                builder.append(escape);
            } else {
                builder.append(string.charAt(i));
            }
        }
        return builder.toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy