Example usage for org.apache.commons.lang3 StringEscapeUtils unescapeJava

List of usage examples for org.apache.commons.lang3 StringEscapeUtils unescapeJava

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringEscapeUtils unescapeJava.

Prototype

public static final String unescapeJava(final String input) 

Source Link

Document

Unescapes any Java literals found in the String .

Usage

From source file:org.apache.nifi.processors.kite.ConvertCSVToAvro.java

private static String unescapeString(String input) {
    if (input.length() > 1) {
        input = StringEscapeUtils.unescapeJava(input);
    }//  ww w  .  j  a  va  2  s. c o m
    return input;
}

From source file:org.ballerinalang.bcl.parser.BConfigLangListener.java

@Override
public void enterBasicString(TomlParser.BasicStringContext context) {
    String stringVal = context.basicStringValue().getText();
    stringVal = StringEscapeUtils.unescapeJava(stringVal);
    currentValue = getResolvedStringValue(stringVal);
}

From source file:org.ballerinalang.util.parser.antlr4.BLangAntlr4Listener.java

protected void createBasicLiteral(BallerinaParser.LiteralValueContext ctx) {
    if (ctx.exception == null) {
        TerminalNode terminalNode = ctx.IntegerLiteral();
        if (terminalNode != null) {
            if (terminalNode.getText().endsWith("l") || terminalNode.getText().endsWith("L")) {
                //dropping the last character L
                String longValue = terminalNode.getText().substring(0, terminalNode.getText().length() - 1);
                modelBuilder.createLongLiteral(longValue, getCurrentLocation(ctx));
            } else {
                modelBuilder.createIntegerLiteral(terminalNode.getText(), getCurrentLocation(ctx));
            }//  ww w  .  java2  s.com
        }

        terminalNode = ctx.FloatingPointLiteral();
        if (terminalNode != null) {
            if (terminalNode.getText().endsWith("d") || terminalNode.getText().endsWith("D")) {
                //dropping the last character D
                String doubleValue = terminalNode.getText().substring(0, terminalNode.getText().length() - 1);
                modelBuilder.createDoubleLiteral(doubleValue, getCurrentLocation(ctx));
            } else {
                modelBuilder.createFloatLiteral(terminalNode.getText(), getCurrentLocation(ctx));
            }
        }

        terminalNode = ctx.QuotedStringLiteral();
        if (terminalNode != null) {
            String stringLiteral = terminalNode.getText();
            stringLiteral = stringLiteral.substring(1, stringLiteral.length() - 1);
            stringLiteral = StringEscapeUtils.unescapeJava(stringLiteral);
            modelBuilder.createStringLiteral(stringLiteral, getCurrentLocation(ctx));
        }

        terminalNode = ctx.BooleanLiteral();
        if (terminalNode != null) {
            modelBuilder.createBooleanLiteral(terminalNode.getText(), getCurrentLocation(ctx));
        }

        terminalNode = ctx.NullLiteral();
        if (terminalNode != null) {
            modelBuilder.createNullLiteral(terminalNode.getText(), getCurrentLocation(ctx));
        }
    }
}

From source file:org.displaytag.export.excel.ExcelUtils.java

/**
 * Escape certain values that are not permitted in excel cells.
 * @param rawValue the object value/*from w  w w  .  ja v a2  s.  co  m*/
 * @return the escaped value
 */
public static String escapeColumnValue(Object rawValue) {
    if (rawValue == null) {
        return null;
    }
    // str = Patterns.replaceAll(str, "(\\r\\n|\\r|\\n|\\n\\r)\\s*", "");
    String returnString = rawValue.toString();
    // escape the String to get the tabs, returns, newline explicit as \t \r \n
    returnString = StringEscapeUtils.escapeJava(StringUtils.trimToEmpty(returnString));
    // remove tabs, insert four whitespaces instead
    returnString = StringUtils.replace(StringUtils.trim(returnString), "\\t", "    ");
    // remove the return, only newline valid in excel
    returnString = StringUtils.replace(StringUtils.trim(returnString), "\\r", " ");
    // unescape so that \n gets back to newline
    returnString = StringEscapeUtils.unescapeJava(returnString);
    return returnString;
}

From source file:org.evosuite.testcase.ConstantInliner.java

/** {@inheritDoc} */
@Override//from ww w. j  ava  2  s  .co m
public void afterStatement(Statement statement, Scope scope, Throwable exception) {
    try {
        for (VariableReference var : statement.getVariableReferences()) {
            if (var.equals(statement.getReturnValue())
                    || var.equals(statement.getReturnValue().getAdditionalVariableReference()))
                continue;
            Object object = var.getObject(scope);

            if (var.isPrimitive()) {
                ConstantValue value = new ConstantValue(test, var.getGenericClass());
                value.setValue(object);
                // logger.info("Statement before inlining: " +
                // statement.getCode());
                statement.replace(var, value);
                // logger.info("Statement after inlining: " +
                // statement.getCode());
            } else if (var.isString() && object != null) {
                ConstantValue value = new ConstantValue(test, var.getGenericClass());
                try {
                    String val = StringEscapeUtils.unescapeJava(new String(object.toString()));
                    if (val.length() < Properties.MAX_STRING) {
                        value.setValue(val);
                        statement.replace(var, value);
                    }
                } catch (IllegalArgumentException e) {
                    // Exceptions may happen if strings are not valid
                    // unicode
                    logger.info("Cannot escape invalid string: " + object);
                }
                // logger.info("Statement after inlining: " +
                // statement.getCode());
            } else if (var.isArrayIndex()) {
                // If this is an array index and there is an object outside
                // the array
                // then replace the array index with that object
                for (VariableReference otherVar : scope.getElements(var.getType())) {
                    Object otherObject = otherVar.getObject(scope);
                    if (otherObject == object && !otherVar.isArrayIndex()
                            && otherVar.getStPosition() < statement.getPosition()) {
                        statement.replace(var, otherVar);
                        break;
                    }
                }
            } else {
                // TODO: Ignoring exceptions during getObject, but keeping
                // the assertion for now
                if (object == null) {
                    if (statement instanceof MethodStatement) {
                        MethodStatement ms = (MethodStatement) statement;
                        if (var.equals(ms.getCallee())) {
                            // Don't put null in callee's, the compiler will not accept it
                            continue;
                        }
                    } else if (statement instanceof FieldStatement) {
                        FieldStatement fs = (FieldStatement) statement;
                        if (var.equals(fs.getSource())) {
                            // Don't put null in source, the compiler will not accept it
                            continue;
                        }
                    }
                    ConstantValue value = new ConstantValue(test, var.getGenericClass());
                    value.setValue(null);
                    // logger.info("Statement before inlining: " +
                    // statement.getCode());
                    statement.replace(var, value);
                    // logger.info("Statement after inlining: " +
                    // statement.getCode());
                }
            }
        }
    } catch (CodeUnderTestException e) {
        logger.warn("Not inlining test: " + e.getCause());
        // throw new AssertionError("This case isn't handled yet: " +
        // e.getCause()
        // + ", " + Arrays.asList(e.getStackTrace()));
    }

}

From source file:org.graylog.plugins.pipelineprocessor.parser.PipelineRuleParser.java

public static String unescape(String string) {
    return StringEscapeUtils.unescapeJava(string);
}

From source file:org.jraf.dbpedia2sqlite.db.Resource.java

public static Resource parse(String line) throws Throwable {
    Resource res = new Resource();
    int spaceIdx = line.indexOf(' ');
    String name = line.substring(0, spaceIdx);
    int slashIdx = name.lastIndexOf('/');
    int gtIdx = name.lastIndexOf('>');
    name = name.substring(slashIdx + 1, gtIdx);
    name = URLDecoder.decode(name, "utf-8");
    name = name.replace('_', ' ');
    res.name = name;/*from  w  w w . j  a v  a2  s.  c o  m*/

    String _abstract = line.substring(spaceIdx + 1);
    int startQuoteIdx = _abstract.indexOf('"');
    int endQuoteIdx = _abstract.lastIndexOf('"');
    if (endQuoteIdx - startQuoteIdx == 1) {
        _abstract = "";
    } else {
        _abstract = _abstract.substring(startQuoteIdx + 1, endQuoteIdx);
        _abstract = StringEscapeUtils.unescapeJava(_abstract);
    }
    res._abstract = _abstract;

    return res;
}

From source file:org.moe.natjgen.EditContext.java

protected String getNAStringValue(Annotation annotation, String key) {
    Expression expr = getNAValue(annotation, key);
    if (expr instanceof StringLiteral) {
        String string = (String) getRewrite().get(expr, StringLiteral.ESCAPED_VALUE_PROPERTY);
        if (string != null && string.length() >= 2) {
            return StringEscapeUtils.unescapeJava(string.substring(1, string.length() - 1));
        }//from www. java 2s  . c o  m
    }
    return null;
}

From source file:org.moe.natjgen.EditContext.java

protected String getSMAStringValue(Annotation annotation) {
    Expression expr = getSMAValue(annotation);
    if (expr instanceof StringLiteral) {
        String string = (String) getRewrite().get(expr, StringLiteral.ESCAPED_VALUE_PROPERTY);
        if (string != null && string.length() >= 2) {
            return StringEscapeUtils.unescapeJava(string.substring(1, string.length() - 1));
        }/*from  ww  w . ja  v a2s .c om*/
    }
    return null;
}

From source file:org.moe.natjgen.test.AbstractNatJGenTest.java

protected static void assertExpressionEqualsValue(Expression expr, Object value) {
    if (value == null) {
        assertEquals(NullLiteral.class, expr.getClass());
    } else if (value instanceof Integer) {
        int v = ((Integer) value).intValue();
        if (v < 0) {
            expr = assertInstanceOf(expr, PrefixExpression.class).getOperand();
            assertNotNull(expr);//from   w w  w  . j a  v  a2  s.  c om
            v = Math.abs(v);
        }
        NumberLiteral literal = assertInstanceOf(expr, NumberLiteral.class);
        assertEquals(Integer.toString(v), literal.getToken());
    } else if (value instanceof Boolean) {
        boolean v = ((Boolean) value).booleanValue();
        BooleanLiteral literal = assertInstanceOf(expr, BooleanLiteral.class);
        assertEquals(v, literal.booleanValue());
    } else if (value instanceof String) {
        StringLiteral literal = assertInstanceOf(expr, StringLiteral.class);
        assertEquals("\"" + value + "\"", StringEscapeUtils.unescapeJava(literal.getEscapedValue()));
    } else if (value instanceof ClassVal) {
        ClassVal clazz = (ClassVal) value;
        assertNotNull(clazz);
        assertNotNull(clazz.get());
        TypeLiteral literal = assertInstanceOf(expr, TypeLiteral.class);
        assertSimpleType(literal.getType(), clazz.get());
    } else {
        fail("Unimplemented value evaluation");
    }
}