Escapes a java string - Java java.lang

Java examples for java.lang:char Array

Description

Escapes a java string

Demo Code


import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class Main{
    /**//from  w  w w  . j a v  a  2 s  .c om
     * Escapes a java string
     *
     * @param source String to escape
     *
     * @return Escaped string
     */
    public static String javaEscape(String source) {

        try {

            StringWriter sw = new StringWriter();

            javaEscape(source, sw);
            sw.flush();

            return sw.toString();

        } catch (IOException ioe) {

            // should never happen writing to a StringWriter
            ioe.printStackTrace();

            return null;

        }

    }
    /**
     * Prepares a string for output inside a Java string, Example:<pre> input string: He didn't say, "stop!"
     * output string: He didn't say, \"stop!\" </pre>Deals with quotes and control-chars (tab, backslash, cr, ff,
     * etc.)
     *
     * @param source String to escape
     * @param out A writer
     *
     * @throws IOException Thrown on I/O exceptions
     */
    public static void javaEscape(String source, Writer out)
            throws IOException {

        stringEscape(source, out, false);

    }
    /**
     * helper function that writes an escaped string
     *
     * @param source String to escape
     * @param out A writer
     * @param escapeSingleQuote true: write "\\'", false: write "'"
     *
     * @throws IOException Thrown on I/O errors
     */
    private static void stringEscape(String source, Writer out,
            boolean escapeSingleQuote) throws IOException {

        char[] chars = source.toCharArray();

        for (int i = 0; i < chars.length; ++i) {

            char ch = chars[i];

            switch (ch) {

            case '\b': // backspace (ASCII 8)
                out.write("\\b");

                break;

            case '\t': // horizontal tab (ASCII 9)
                out.write("\\t");

                break;

            case '\n': // newline (ASCII 10)
                out.write("\\n");

                break;

            case 11: // vertical tab (ASCII 11)
                out.write("\\v");

                break;

            case '\f': // form feed (ASCII 12)
                out.write("\\f");

                break;

            case '\r': // carriage return (ASCII 13)
                out.write("\\r");

                break;

            case '"': // double-quote (ASCII 34)
                out.write("\\\"");

                break;

            case '\'': // single-quote (ASCII 39)

                if (escapeSingleQuote) {

                    out.write("\\'");

                } else {

                    out.write("'");

                }

                break;

            case '\\': // literal backslash (ASCII 92)
                out.write("\\\\");

                break;

            default:

                if (((int) ch < 32) || ((int) ch > 127)) {

                    out.write("\\u");
                    zeroPad(out, Integer.toHexString(ch).toUpperCase(), 4);

                } else {

                    out.write(ch);

                }

                break;

            }

        }

    }
    /**
     * Little helper function that writes a string (zero padded, for a given width)
     *
     * @param out A Writer object
     * @param s String to write
     * @param width Zero padding length
     *
     * @throws IOException Thrown on I/O exceptions
     */
    private static void zeroPad(Writer out, String s, int width)
            throws IOException {

        while (width > s.length()) {

            out.write('0');
            width--;

        }

        out.write(s);

    }
}

Related Tutorials