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

jp.vmi.selenium.selenese.utils.EscapeUtils Maven / Gradle / Ivy

package jp.vmi.selenium.selenese.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.Charsets;

/**
 *  String escape utilities.
 */
public class EscapeUtils {

    private EscapeUtils() {
        // no operation
    }

    /** Regex pattern for HTML escape. */
    public static final Pattern HTML_RE = Pattern.compile("([<>&\"'\\\\])|\r?\n|\r");

    /** Mapping table for HTML escape. */
    public static final Map HTML_ESC_MAP = new HashMap();

    static {
        HTML_ESC_MAP.put("<", "<");
        HTML_ESC_MAP.put(">", ">");
        HTML_ESC_MAP.put("&", "&");
        HTML_ESC_MAP.put("\"", """);
        HTML_ESC_MAP.put("'", "'");
        HTML_ESC_MAP.put("\\", "\");
        HTML_ESC_MAP.put("\r\n", "
\n"); HTML_ESC_MAP.put("\n", "
\n"); HTML_ESC_MAP.put("\r", "
\n"); } /** * Regex pattern for URI encode. * * This pattern equivalent to JavaScript encodeURI(). * see: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURI */ public static Pattern URI_RE = Pattern.compile("[^;,/?:@&=+$A-Za-z0-9\\-_.!~*'()#]+"); /** * HTML escape. * * @param s raw string. * @param newline escape newline to <br>. * @return HTML escaped string. */ public static String escapeHtml(String s, boolean newline) { StringBuilder result = new StringBuilder(); Matcher matcher = HTML_RE.matcher(s); int index = 0; while (matcher.find(index)) { int start = matcher.start(); if (index < start) result.append(s, index, start); if (newline) { result.append(HTML_ESC_MAP.get(matcher.group())); } else { String specials = matcher.group(1); if (specials != null) result.append(HTML_ESC_MAP.get(specials)); else result.append('\n'); } index = matcher.end(); } if (index < s.length()) result.append(s, index, s.length()); return result.toString(); } /** * URI encode. * * @param s raw string. * @return URI encoded string. */ public static String encodeUri(String s) { StringBuilder result = new StringBuilder(); Matcher matcher = URI_RE.matcher(s); int index = 0; while (matcher.find(index)) { int start = matcher.start(); if (index < start) result.append(s, index, start); for (byte b : matcher.group().getBytes(Charsets.UTF_8)) result.append(String.format("%%%02x", b)); index = matcher.end(); } if (index < s.length()) result.append(s, index, s.length()); return result.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy