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

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

Introduction

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

Prototype

public static final String escapeJava(final String input) 

Source Link

Document

Escapes the characters in a String using Java String rules.

Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)

So a tab becomes the characters '\\' and 't' .

The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote and forward-slash (/) are escaped.

Example:

 input string: He didn't say, "Stop!" 

Usage

From source file:de.hasait.genesis.base.model.JSrcExpression.java

@Nonnull
public static JSrcExpression stringValue(final @Nullable String pValue) {
    final String code;
    if (pValue == null) {
        code = "null";
    } else {// w w w .j a v a  2 s .co m
        code = "\"" + StringEscapeUtils.escapeJava(pValue) + "\"";
    }

    return customCode(code);
}

From source file:de.grobmeier.jjson.translator.StringEscapeUtilsTest.java

/**
 * Test of escapeJava method, of class StringEscapeUtils.
 */// ww  w. ja  va 2s.  c  om
@Test
public void testEscapeJava() {
    String input = "hello\"test";
    String expResult = "hello\\\"test";
    String result = StringEscapeUtils.escapeJava(input);
    assertEquals(expResult, result);
}

From source file:com.tbodt.jtl.Jtl.java

private static String generateCode(CharStream jtl) {
    JtlParser parser = new JtlParser(new CommonTokenStream(new JtlLexer(jtl)));
    ParseTree tree = parser.template();//www . ja  v a 2 s.c  o  m
    ParseTreeWalker walker = new ParseTreeWalker();
    final StringBuilder builder = new StringBuilder();
    builder.append("StringBuilder _builder = new StringBuilder();\n");
    ParseTreeListener listener = new JtlParserBaseListener() {
        @Override
        public void enterText(JtlParser.TextContext ctx) {
            builder.append("_builder.append(\"");
            builder.append(StringEscapeUtils.escapeJava(ctx.getText()));
            builder.append("\");\n");
        }

        @Override
        public void enterEmbed(JtlParser.EmbedContext ctx) {
            builder.append("_builder.append(");
            builder.append(combineText(ctx.CODE()));
            builder.append(");\n");
        }

        @Override
        public void enterCode(JtlParser.CodeContext ctx) {
            builder.append("\n");
            builder.append(combineText(ctx.CODE()));
            builder.append("\n");
        }
    };
    walker.walk(listener, tree);
    builder.append("return _builder.toString();\n");
    return builder.toString();
}

From source file:me.ryandowling.allmightybot.listeners.SpamListener.java

@Override
public void onMessage(MessageEvent event) throws Exception {
    super.onMessage(event);

    System.out.println("Escaped: " + StringEscapeUtils.escapeJava(event.getMessage()));

    if (event.getUser() == event.getBot().getUserBot() || this.bot.isModerator(event.getUser().getNick())) {
        return;//ww  w . ja va 2 s  . c om
    }

    for (Spam spam : this.bot.getSpams()) {
        if (spam.shouldTakeAction(event)) {
            spam.takeAction(event);
        }
    }
}

From source file:ductive.parse.parsers.EOFParser.java

@Override
public Result<Void> doApply(ParseContext ctx) {
    if (ctx.length() != 0) {
        String info = StringEscapeUtils.escapeJava(StringUtils.abbreviate(ctx.toString(), 60));
        throw new NoMatchException(String.format("Extra content '%s' found while looking for EOF", info), ctx,
                0);//w  w w. jav a2  s  .c om
    }

    return Result.make(null, ctx);
}

From source file:me.ryandowling.allmightybot.data.Spam.java

public boolean shouldTakeAction(MessageEvent event) {
    String message = StringEscapeUtils.escapeJava(event.getMessage());

    if (this.pattern == null) {
        this.pattern = Pattern.compile(Pattern.quote(this.search), Pattern.CASE_INSENSITIVE);
    }/*from   ww  w.  j  a v  a  2s .  c o m*/

    return this.pattern.matcher(message).find();
}

From source file:net.lshift.diffa.assets.JstTemplatesPreProcessor.java

public void process(Resource resource, Reader input, Writer output) throws IOException {
    if (resource.getUri().endsWith(".jst")) {
        String content = IOUtils.toString(input);
        String name = removeSuffix(removePrefix(resource.getUri(), "/js/templates/"), ".jst");

        output.write("window.JST = (window.JST || {});\n");
        output.write(String.format("window.JST['%s'] = _.template(\n", name));

        List<String> result = new ArrayList<String>();
        for (String l : content.split("\n")) {
            result.add("\"" + StringEscapeUtils.escapeJava(l) + "\"");
        }/* w w w.  jav  a  2s  .com*/
        output.write(StringUtils.join(result.iterator(), " + \n"));
        output.write(");\n");
    } else {
        IOUtils.copy(input, output);
    }
}

From source file:net.mindengine.galen.suite.actions.GalenPageActionCookie.java

@Override
public List<ValidationError> execute(Browser browser, GalenPageTest pageTest,
        ValidationListener validationListener) throws Exception {
    if (cookies != null && cookies.size() > 0) {
        StringBuilder js = new StringBuilder();

        for (String cookie : cookies) {
            js.append("document.cookie=\"" + StringEscapeUtils.escapeJava(cookie) + "\";");
        }// ww  w. ja  v a  2s .c  o m
        browser.executeJavascript(js.toString());
        browser.refresh();
    }
    return null;
}

From source file:com.squarespace.template.BaseInstruction.java

/**
 * To facilitate clear error messages, the toString() for all instructions will
 * output the type and non-recursive representation.  Use repr() to get the
 * recursive representation alone.//from   w w  w .  ja  va  2 s .  c o  m
 */
@Override
public String toString() {
    StringBuilder buf = new StringBuilder(getType().toString());
    buf.append(" (").append(lineNumber).append(',').append(charOffset).append(')');
    String res = ReprEmitter.get(this, false);
    if (res.length() > 0) {
        buf.append(' ');
        buf.append(StringEscapeUtils.escapeJava(res));
    }
    return buf.toString();
}

From source file:com.squarespace.less.parse.Stream.java

protected void dump() {
    char ch = (index >= length) ? Chars.EOF : raw.charAt(index);
    String esc = StringEscapeUtils.escapeJava(ch + "");
    System.out.printf("Stream: index=%d len=%d line=%d char=%d ch=\"%s\"\n", index, length, lineOffset,
            charOffset, esc);/*from   ww  w.java2 s . com*/
}