Example usage for org.apache.commons.lang.text StrBuilder setLength

List of usage examples for org.apache.commons.lang.text StrBuilder setLength

Introduction

In this page you can find the example usage for org.apache.commons.lang.text StrBuilder setLength.

Prototype

public StrBuilder setLength(int length) 

Source Link

Document

Updates the length of the builder by either dropping the last characters or adding filler of unicode zero.

Usage

From source file:com.antsdb.saltedfish.sql.vdm.ExprUtil.java

/**
 * <p>Unescapes any Java literals found in the <code>String</code> to a
 * <code>Writer</code>.</p>
 *
 * <p>For example, it will turn a sequence of <code>'\'</code> and
 * <code>'n'</code> into a newline character, unless the <code>'\'</code>
 * is preceded by another <code>'\'</code>.</p>
 * //from ww w.  ja va  2 s. com
 * <p>A <code>null</code> string input has no effect.</p>
 * 
 * @see http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
 * 
 * @param out  the <code>Writer</code> used to output unescaped characters
 * @param str  the <code>String</code> to unescape, may be null
 * @throws IllegalArgumentException if the Writer is <code>null</code>
 * @throws IOException if error occurs on underlying Writer
 */
public static void unescapeJava(Writer out, String str) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("The Writer must not be null");
    }
    if (str == null) {
        return;
    }
    int sz = str.length();
    StrBuilder unicode = new StrBuilder(4);
    boolean hadSlash = false;
    boolean inUnicode = false;
    for (int i = 0; i < sz; i++) {
        char ch = str.charAt(i);
        if (inUnicode) {
            // if in unicode, then we're reading unicode
            // values in somehow
            unicode.append(ch);
            if (unicode.length() == 4) {
                // unicode now contains the four hex digits
                // which represents our unicode character
                try {
                    int value = Integer.parseInt(unicode.toString(), 16);
                    out.write((char) value);
                    unicode.setLength(0);
                    inUnicode = false;
                    hadSlash = false;
                } catch (NumberFormatException nfe) {
                    throw new NestableRuntimeException("Unable to parse unicode value: " + unicode, nfe);
                }
            }
            continue;
        }
        if (hadSlash) {
            // handle an escaped value
            hadSlash = false;
            switch (ch) {
            case '0':
                out.write(0);
                break;
            case '\'':
                out.write('\'');
                break;
            case '\"':
                out.write('"');
                break;
            case 'b':
                out.write('\b');
                break;
            case 'n':
                out.write('\n');
                break;
            case 'r':
                out.write('\r');
                break;
            case 't':
                out.write('\t');
                break;
            case 'Z':
                out.write(26);
                break;
            case '\\':
                out.write('\\');
                break;
            case '%':
                out.write('%');
                break;
            case '_':
                out.write('_');
                break;
            case 'u': {
                // uh-oh, we're in unicode country....
                inUnicode = true;
                break;
            }
            default:
                out.write(ch);
            }
            continue;
        } else if (ch == '\\') {
            hadSlash = true;
            continue;
        }
        out.write(ch);
    }
    if (hadSlash) {
        // then we're in the weird case of a \ at the end of the
        // string, let's output it anyway.
        out.write('\\');
    }
}

From source file:de.iteratec.iteraplan.presentation.tags.StringEscapeUtilsFunction.java

public static void unescapeJava(Writer out, String str) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("The Writer must not be null");
    }//from  w  ww . j a va2  s .  c  o m
    if (str == null) {
        return;
    }
    int sz = str.length();
    StrBuilder unicode = new StrBuilder(4);
    boolean hadSlash = false;
    boolean inUnicode = false;
    for (int i = 0; i < sz; i++) {
        char ch = str.charAt(i);
        if (inUnicode) {
            // if in unicode, then we're reading unicode
            // values in somehow
            unicode.append(ch);
            if (unicode.length() == 4) {
                // unicode now contains the four hex digits
                // which represents our unicode character
                try {
                    int value = Integer.parseInt(unicode.toString(), 16);
                    out.write((char) value);
                    unicode.setLength(0);
                    inUnicode = false;
                    hadSlash = false;
                } catch (NumberFormatException nfe) {
                    throw new NestableRuntimeException("Unable to parse unicode value: " + unicode, nfe);
                }
            }
            continue;
        }
        if (hadSlash) {
            hadSlash = false;
            switch (ch) {
            case '\\':
                out.write('\\');
                break;
            case '\'':
                out.write('\'');
                break;
            case '\"':
                out.write('"');
                break;
            case 'r':
                out.write('\r');
                break;
            case 'f':
                out.write('\f');
                break;
            case 't':
                out.write('\t');
                break;
            case 'n':
                out.write('\n');
                break;
            case 'b':
                out.write('\b');
                break;
            case 'u': {
                inUnicode = true;
                break;
            }
            default:
                out.write(ch);
                break;
            }
            continue;
        } else if (ch == '\\') {
            hadSlash = true;
            continue;
        }
        out.write(ch);
    }
    if (hadSlash) {
        out.write('\\');
    }
}

From source file:net.ymate.platform.webmvc.util.StringEscapeUtils.java

public static String unescapeJava(String str) {
    if (str == null) {
        return null;
    }/*from   w  w w  .  ja  v  a 2  s .  co  m*/
    int sz = str.length();
    StringBuilder out = new StringBuilder();
    StrBuilder unicode = new StrBuilder(4);
    boolean hadSlash = false;
    boolean inUnicode = false;
    for (int i = 0; i < sz; i++) {
        char ch = str.charAt(i);
        if (inUnicode) {
            unicode.append(ch);
            if (unicode.length() == 4) {
                try {
                    int value = Integer.parseInt(unicode.toString(), 16);
                    out.append((char) value);
                    unicode.setLength(0);
                    inUnicode = false;
                    hadSlash = false;
                } catch (NumberFormatException nfe) {
                    throw new NestableRuntimeException("Unable to parse unicode value: " + unicode, nfe);
                }
            }
            continue;
        }
        if (hadSlash) {
            hadSlash = false;
            switch (ch) {
            case '\\':
                out.append('\\');
                break;
            case '\'':
                out.append('\'');
                break;
            case '\"':
                out.append('"');
                break;
            case 'r':
                out.append('\r');
                break;
            case 'f':
                out.append('\f');
                break;
            case 't':
                out.append('\t');
                break;
            case 'n':
                out.append('\n');
                break;
            case 'b':
                out.append('\b');
                break;
            case 'u': {
                inUnicode = true;
                break;
            }
            default:
                out.append(ch);
                break;
            }
            continue;
        } else if (ch == '\\') {
            hadSlash = true;
            continue;
        }
        out.append(ch);
    }
    if (hadSlash) {
        out.append('\\');
    }
    return out.toString();
}

From source file:org.eclipse.skalli.core.search.LuceneIndex.java

static String getExtendedQuery(String queryString) {
    StrBuilder extendedQuery = new StrBuilder();
    if (StringUtils.isNotBlank(queryString)) {
        StrBuilder term = new StrBuilder();
        boolean isSimpleTerm = true;
        boolean insideQuotes = false;
        boolean insideBrackets = false;
        char openedBracket = '\0';
        int pos = 0;
        int len = queryString.length();
        while (pos < len) {
            char c = queryString.charAt(pos++);
            if (c == '"') {
                isSimpleTerm = false;//from  www . j  a v  a  2 s  .  c  o m
                insideQuotes = !insideQuotes;
                term.append(c);
            } else if (c == '(' || c == '[' || c == '{') {
                isSimpleTerm = false;
                insideBrackets = true;
                openedBracket = c;
                term.append(c);
            } else if (c == ')' || c == ']' || c == '}') {
                isSimpleTerm = false;
                if (c == ')' && openedBracket == '(' || c == ']' && openedBracket == '['
                        || c == '}' && openedBracket == '{') {
                    insideBrackets = false;
                    openedBracket = '\0';
                }
                term.append(c);
            } else if (insideQuotes || insideBrackets) {
                term.append(c);
            } else if (c == '*' || c == '?' || c == '~' || c == '+' || c == '-' || c == '!' || c == ':'
                    || c == '^' || c == '|' || c == '&' || c == '\\') {
                isSimpleTerm = false;
                term.append(c);
            } else if (Character.isWhitespace(c)) {
                addTerm(extendedQuery, term, isSimpleTerm);
                isSimpleTerm = true;
                insideQuotes = false;
                insideBrackets = false;
                openedBracket = '\0';
                term.setLength(0);
            } else {
                term.append(c);
            }
        }
        addTerm(extendedQuery, term, isSimpleTerm);
    }
    return extendedQuery.toString();
}