Java String Escape escapeJavaStyleString(StringBuilder out, String str, boolean escapeSingleQuote, boolean escapeForwardSlash)

Here you can find the source of escapeJavaStyleString(StringBuilder out, String str, boolean escapeSingleQuote, boolean escapeForwardSlash)

Description

escape Java Style String

License

Apache License

Declaration

private static void escapeJavaStyleString(StringBuilder out, String str, boolean escapeSingleQuote,
            boolean escapeForwardSlash) 

Method Source Code

//package com.java2s;
/*/*w  ww.java 2  s  .c  o  m*/
 * Copyright 2007-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    private static void escapeJavaStyleString(StringBuilder out, String str, boolean escapeSingleQuote,
            boolean escapeForwardSlash) {
        if (out == null) {
            throw new IllegalArgumentException("The Writer must not be null");
        }
        if (str == null) {
            return;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            char ch = str.charAt(i);
            if (ch > 0xfff) {
                out.append("\\u").append(hex(ch));
            } else if (ch > 0xff) {
                out.append("\\u0").append(hex(ch));
            } else if (ch > 0x7f) {
                out.append("\\u00").append(hex(ch));
            } else if (ch < 32) {
                switch (ch) {
                case '\b':
                    out.append('\\');
                    out.append('b');
                    break;
                case '\n':
                    out.append('\\');
                    out.append('n');
                    break;
                case '\t':
                    out.append('\\');
                    out.append('t');
                    break;
                case '\f':
                    out.append('\\');
                    out.append('f');
                    break;
                case '\r':
                    out.append('\\');
                    out.append('r');
                    break;
                default:
                    if (ch > 0xf) {
                        out.append("\\u00").append(hex(ch));
                    } else {
                        out.append("\\u000").append(hex(ch));
                    }
                    break;
                }
            } else {
                switch (ch) {
                case '\'':
                    if (escapeSingleQuote) {
                        out.append('\\');
                    }
                    out.append('\'');
                    break;
                case '"':
                    out.append('\\');
                    out.append('"');
                    break;
                case '\\':
                    out.append('\\');
                    out.append('\\');
                    break;
                case '/':
                    if (escapeForwardSlash) {
                        out.append('\\');
                    }
                    out.append('/');
                    break;
                default:
                    out.append(ch);
                    break;
                }
            }
        }
    }

    private static String hex(char ch) {
        return Integer.toHexString(ch).toUpperCase(Locale.ENGLISH);
    }
}

Related

  1. escapeHTML(String s)
  2. escapeHTML(String source)
  3. escapeHTMLTags(String in)
  4. escapeInstanceIdentifier(String instanceIdentifier)
  5. escapeJavaString(String str)
  6. escapeQuotes(String string)
  7. escapeRegex(String regex)
  8. escapeRegexChars(String str, char... ignores)
  9. escapeRegexp(final String str)