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:com.squarespace.template.MapFormat.java

public String apply(Map<String, Object> params) {
    List<Object> values = new ArrayList<>(params.size());
    for (String key : keys) {
        Object obj = params.get(key);
        if (obj == null) {
            obj = nullPlaceholder;/*  www.jav a 2  s  .  c  o  m*/
        }
        values.add(obj);
    }
    return StringEscapeUtils.escapeJava(String.format(format, values.toArray()));
}

From source file:mamo.vanillaVotifier.VotifierServerListener.java

@Override
public void onEvent(@NotNull Event event) {
    if (event instanceof ServerStartingEvent) {
        votifier.getLogger().printlnTranslation("s1");
    } else if (event instanceof ServerStartedEvent) {
        votifier.getLogger().printlnTranslation("s2", new SimpleEntry<String, Object>("key",
                RsaUtils.keyToString(votifier.getConfig().getKeyPair().getPublic())));
    } else if (event instanceof ConnectionEstablishedEvent) {
        ConnectionEstablishedEvent connectionEstablishedEvent = (ConnectionEstablishedEvent) event;
        votifier.getLogger().printlnTranslation("s3",
                new SimpleEntry<String, Object>("ip",
                        connectionEstablishedEvent.getSocket().getInetAddress().getHostAddress()),
                new SimpleEntry<String, Object>("port", connectionEstablishedEvent.getSocket().getPort()));
    } else if (event instanceof VoteEventVotifier) {
        VoteEventVotifier voteEvent = (VoteEventVotifier) event;
        votifier.getLogger().printlnTranslation("s4",
                new SimpleEntry<String, Object>("ip", voteEvent.getSocket().getInetAddress().getHostAddress()),
                new SimpleEntry<String, Object>("port", voteEvent.getSocket().getPort()),
                new SimpleEntry<String, Object>("service-name",
                        "\"" + StringEscapeUtils.escapeJava(voteEvent.getVote().getServiceName()) + "\""),
                new SimpleEntry<String, Object>("user-name",
                        "\"" + StringEscapeUtils.escapeJava(voteEvent.getVote().getUserName()) + "\""),
                new SimpleEntry<String, Object>("address",
                        "\"" + StringEscapeUtils.escapeJava(voteEvent.getVote().getAddress()) + "\""),
                new SimpleEntry<String, Object>("timestamp",
                        "\"" + StringEscapeUtils.escapeJava(voteEvent.getVote().getTimeStamp()) + "\""));
    } else if (event instanceof SendingRconCommandEvent) {
        SendingRconCommandEvent sendingRconCommandEvent = (SendingRconCommandEvent) event;
        votifier.getLogger().printlnTranslation("s5",
                new SimpleEntry<String, Object>("ip",
                        sendingRconCommandEvent.getRconConnection().getInetSocketAddress().getAddress()
                                .getHostName()),
                new SimpleEntry<String, Object>("port",
                        sendingRconCommandEvent.getRconConnection().getInetSocketAddress().getPort()),
                new SimpleEntry<String, Object>("command", sendingRconCommandEvent.getCommand()));
    } else if (event instanceof RconCommandResponseEvent) {
        RconCommandResponseEvent commandResponseEvent = ((RconCommandResponseEvent) event);
        if (commandResponseEvent.getMessage() != null && !commandResponseEvent.getMessage().isEmpty()) {
            votifier.getLogger().printlnTranslation("s6",
                    new SimpleEntry<String, Object>("response", commandResponseEvent.getMessage()));
        } else {/*from ww w  .  j  a  va 2 s .com*/
            votifier.getLogger().printlnTranslation("s53");
        }
    } else if (event instanceof RconExceptionEvent) {
        Exception exception = ((RconExceptionEvent) event).getException();
        if (exception instanceof InvalidRconPasswordException) {
            votifier.getLogger().printlnTranslation("s7");
        } else if (exception instanceof ConnectException) {
            votifier.getLogger().printlnTranslation("s39");
        } else {
            votifier.getLogger().printlnTranslation("s28",
                    new SimpleEntry<String, Object>("exception", exception));
        }
    } else if (event instanceof SendingShellCommandEvent) {
        votifier.getLogger().printlnTranslation("s59",
                new SimpleEntry<String, Object>("command", ((SendingShellCommandEvent) event).getCommand()));
    } else if (event instanceof ShellCommandSentEvent) {
        votifier.getLogger().printlnTranslation("s60");
    } else if (event instanceof ShellCommandExceptionEvent) {
        Exception exception = ((ShellCommandExceptionEvent) event).getException();
        if (exception instanceof IOException) {
            votifier.getLogger().printlnTranslation("s61");
        } else {
            votifier.getLogger().printlnTranslation("s62",
                    new SimpleEntry<String, Object>("exception", exception));
        }
    } else if (event instanceof InvalidRequestEvent) {
        InvalidRequestEvent invalidRequestEvent = (InvalidRequestEvent) event;
        StringBuilder message = new StringBuilder();
        String[] sections = invalidRequestEvent.getMessage().split("\n");
        for (int i = 0; i < sections.length - 1; i++) {
            message.append("\"").append(StringEscapeUtils.escapeJava(sections[i])).append("\" ");
        }
        message.append("\"").append(StringEscapeUtils.escapeJava(sections[sections.length - 1])).append("\"");
        votifier.getLogger().printlnTranslation("s8",
                new SimpleEntry<String, Object>("ip",
                        invalidRequestEvent.getSocket().getInetAddress().getHostAddress()),
                new SimpleEntry<String, Object>("port", invalidRequestEvent.getSocket().getPort()),
                new SimpleEntry<String, Object>("message", message));
    } else if (event instanceof ConnectionClosedEvent) {
        ConnectionClosedEvent connectionClosedEvent = (ConnectionClosedEvent) event;
        votifier.getLogger().printlnTranslation("s10",
                new SimpleEntry<String, Object>("ip",
                        connectionClosedEvent.getSocket().getInetAddress().getHostAddress()),
                new SimpleEntry<String, Object>("port", connectionClosedEvent.getSocket().getPort()));
    } else if (event instanceof ConnectionCloseExceptionEvent) {
        ConnectionCloseExceptionEvent connectionCloseException = (ConnectionCloseExceptionEvent) event;
        votifier.getLogger().printlnTranslation("s11",
                new SimpleEntry<String, Object>("ip",
                        connectionCloseException.getSocket().getInetAddress().getHostAddress()),
                new SimpleEntry<String, Object>("port", connectionCloseException.getSocket().getPort()),
                new SimpleEntry<String, Object>("exception", connectionCloseException.getException()));
    } else if (event instanceof DecryptInputExceptionEvent) {
        votifier.getLogger().printlnTranslation("s46");
    } else if (event instanceof RegularExpressionPatternErrorException) {
        votifier.getLogger().printlnTranslation("s65", new SimpleEntry<String, Object>("exception",
                ((RegularExpressionPatternErrorException) event).getException().getMessage()));
    } else if (event instanceof ReadTimedOutExceptionEvent) {
        ReadTimedOutExceptionEvent readTimedOutExceptionEvent = (ReadTimedOutExceptionEvent) event;
        votifier.getLogger().printlnTranslation("s64",
                new SimpleEntry<String, Object>("ip",
                        readTimedOutExceptionEvent.getSocket().getInetAddress().getHostAddress()),
                new SimpleEntry<String, Object>("port", readTimedOutExceptionEvent.getSocket().getPort()));
    } else if (event instanceof CommunicationExceptionEvent) {
        votifier.getLogger().printlnTranslation("s29", new SimpleEntry<String, Object>("exception",
                ((CommunicationExceptionEvent) event).getException()));
    } else if (event instanceof ConnectionEstablishExceptionEvent) {
        votifier.getLogger().printlnTranslation("s30", new SimpleEntry<String, Object>("exception",
                ((ConnectionEstablishExceptionEvent) event).getException()));
    } else if (event instanceof ServerStoppingEvent) {
        votifier.getLogger().printlnTranslation("s22");
    } else if (event instanceof ServerAwaitingTaskCompletionEvent) {
        votifier.getLogger().printlnTranslation("s54");
    } else if (event instanceof ServerStoppedEvent) {
        votifier.getLogger().printlnTranslation("s14");
    }
}

From source file:FstDotWriter.java

public void initializeDot() {
    try {//w  w  w .  ja v a  2s .c om
        out.write("digraph FST {\n");
        out.write("  rankdir = LR;\n");

        out.write("  size = \"7.5,10\";\n"); // must allow for half-inch margins on page
        out.write("  page = \"8.5,11\";\n");

        //StringEscapeUtils.escapeXml(sigma)
        //StringEscapeUtils.escapeJava(sigma)
        //   for 2.4
        //StringEscapeUtils.escapeHtml(sigma)
        //   in 3.0
        //StringEscapeUtils.escapeHtml4(sigma)
        //StringEscapeUtils.escapeHtml4(sigma)

        String displaySigma = (" " + StringEscapeUtils.escapeJava(sigma))
                .replaceAll(" \\\\\\\\", " __LITERALBACKSLASH") // literal backslash
                .replaceAll(" \\\\", " \\\\\\\\").replaceAll(" __LITERALBACKSLASH", " \\\\\\\\\\\\\\\\");
        // literalize backslashes for the dot source file

        out.write("  label = \"Alphabet: " + displaySigma + "\";\n");
        out.write("  center = \"true\";\n");
        out.write("  encoding = \"" + encoding + "\";\n");

        //out.write("  orientation = landscape;\n") ;
        //out.write("  rotate = \"90\";\n") ;

        out.write("  ranksep = \"0.3\";\n");
        out.write("  nodesep = \"0.2\";\n\n");
        out.write("  node[shape=circle, style=solid, fontsize=10] ;\n");
        out.write("  edge[fontsize=10, arrowhead=open] ;\n\n");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.thruzero.applications.faces.demo.beans.page.EscapeToolBean.java

public String escapeTextAction() {
    if ("xml".equals(escapeType)) {
        escapedText = StringEscapeUtils.escapeXml(inputText);
    } else if ("html4".equals(escapeType)) {
        escapedText = StringEscapeUtils.escapeHtml4(inputText);
    } else if ("java".equals(escapeType)) {
        escapedText = StringEscapeUtils.escapeJava(inputText);
    } else {/*from   w w  w .j  a  v a 2s  .c o  m*/
        throw new RuntimeException("Unknown Escape selection");
    }

    String flashHackKey = FlashUtils.saveFlashAttribute(escapedText);

    return "/apps/demo/escapeTool.jsf?faces-redirect=true&fhk=" + flashHackKey;
}

From source file:com.esri.geoportal.commons.csw.client.impl.CapabilitiesParse.java

@SuppressWarnings("unchecked")
@Override//from  w  ww.  ja va2 s.c  o  m
public void startElement(String uri, String localName, String qName, Attributes attrib) throws SAXException {
    tracking(localName, true);
    text = new StringBuffer();

    if (localName.equals("")) {
        localName = qName;
    }
    LOG.finer("ELEMENT BEGIN " + StringEscapeUtils.escapeJava(localName));

    // Define handling rules 
    text = new StringBuffer();

    if (capabilities && localName.equalsIgnoreCase("operation")) {
        if (chkStr(attrib.getValue("name")).equalsIgnoreCase("GetRecordById")) {
            getRecordById = true;
        } else {
            getRecordById = false;
        }

        if (chkStr(attrib.getValue("name")).equalsIgnoreCase("GetRecords")) {
            getRecords = true;
        } else {
            getRecords = false;
        }
    }

    if (capabilities && getRecordById && localName.equalsIgnoreCase("get")) {
        cap.set_getRecordByIDGetURL(attrib.getValue("xlink:href"));
    }

    if (capabilities && getRecords && localName.equalsIgnoreCase("post")) {
        getRecordsList.push(chkStr(attrib.getValue("xlink:href")));
    }

}

From source file:com.galenframework.reports.GalenTestInfo.java

private static String convertArgumentToString(Object argument) {
    if (argument == null) {
        return "null";
    } else if (argument instanceof String) {
        return "\"" + StringEscapeUtils.escapeJava(argument.toString()) + "\"";
    } else if (argument instanceof Boolean) {
        return Boolean.toString((Boolean) argument);
    } else {/*www  .j a  va  2 s  . com*/
        return argument.toString();
    }
}

From source file:iox.refused.jhp.JHP_Parser.java

private static void compile(StringBuffer sb, String src) {
    Matcher m = TEXTLET.matcher(src);
    while (m.find()) {
        String name = BSLASHES.matcher(StringEscapeUtils.escapeJava(m.group(2))).replaceAll("\\\\\\\\");
        m.appendReplacement(sb, "echo(\"" + name + "\");\n");
    }//from www  .j ava 2 s .c  o  m
    m.appendTail(sb);
}

From source file:edu.ucla.cs.scai.canali.core.index.utils.CreateCompleteTripleFileFromProcessedFiles.java

public void start() throws Exception {
    String regex1 = "<([^<>]*)>\\s<([^<>]*)>\\s<([^<>]*)>";
    String regex2 = "<([^<>]*)>\\s<([^<>]*)>\\s\"(.*)\"";
    Pattern p1 = Pattern.compile(regex1);
    Pattern p2 = Pattern.compile(regex2);
    try (PrintWriter out1 = new PrintWriter(new FileWriter(targetPath + "full_triples.nt", false), true);
            PrintWriter out2 = new PrintWriter(new FileWriter(targetPath + "full_entity_labels.nt", false),
                    true);/*from  w  w  w . j  a  va  2 s.  c o m*/
            PrintWriter out3 = new PrintWriter(new FileWriter(targetPath + "full_entity_classes.nt", false),
                    true);
            PrintWriter out4 = new PrintWriter(new FileWriter(targetPath + "full_class_hierarchy.nt", false),
                    true)) {
        //copy all the triples as they are
        System.out.println("Copying Triples");
        BufferedReader in = new BufferedReader(new FileReader(processedPath + "triples"));
        String l;
        /*
        while ((l = in.readLine()) != null) {
        Matcher m1 = p1.matcher(l);
        if (!m1.find()) {
            Matcher m2 = p2.matcher(l);
            if (!m2.find()) {
                System.out.println("Invalid line: " + l);
            } else {
                out1.println(l);
            }
        } else {
            out1.println(l);
        }
        }
        in.close();
        */
        System.out.println("Copying entity labels");
        in = new BufferedReader(new FileReader(processedPath + "entity_labels"));
        while ((l = in.readLine()) != null) {
            String[] s = l.split("\\t");
            out2.println("<" + s[0] + "> <" + label + "> \"" + StringEscapeUtils.escapeJava(s[1]) + "\"@en .");
        }
        in.close();
        /*
        System.out.println("Copying additional entity labels");
        try {
        in = new BufferedReader(new FileReader(processedPath + "additional_entity_labels"));
        while ((l = in.readLine()) != null) {
            String[] s = l.split("\\t");
            out.println("<" + s[0] + "> <" + label + "> \"" + s[1] + "\"@en .");
        }
        in.close();
        } catch (Exception e) {
        e.printStackTrace();
        }
            */
        System.out.println("Copying entity classes");
        in = new BufferedReader(new FileReader(processedPath + "entity_classes"));
        while ((l = in.readLine()) != null) {
            String[] s = l.split("\\t");
            out3.println("<" + s[0] + "> <" + type + "> <" + s[1] + "> .");
        }
        in.close();
        System.out.println("Copying class hierarchy");
        in = new BufferedReader(new FileReader(processedPath + "class_parents"));
        while ((l = in.readLine()) != null) {
            String[] s = l.split("\\t");
            out4.println("<" + s[0] + "> <" + subClassOf + "> <" + s[1] + "> .");
        }
        in.close();
    }
}

From source file:com.reprezen.kaizen.oasparser.jsonoverlay.gen.Template.java

private static String quote(String s) {
    return s != null ? "\"" + StringEscapeUtils.escapeJava(s) + "\"" : "null";
}

From source file:com.heliosdecompiler.helios.controller.transformers.decompilers.DecompilerController.java

public void decompile(OpenedFile file, String path, BiConsumer<Boolean, String> consumer) {
    backgroundTaskHelper/*from www. ja  v  a2s  .  c o  m*/
            .submit(new BackgroundTask(Message.TASK_DECOMPILE_FILE.format(path, getDisplayName()), true, () -> {
                try {
                    String pre = preDecompile(file, path);
                    if (pre != null) {
                        consumer.accept(false, pre);
                    } else {
                        byte[] data = file.getContent(path);
                        ClassData cd = ClassData.construct(data);

                        TransformationResult<String> transformationResult = decompiler
                                .decompile(Collections.singleton(cd), createSettings(), getClasspath(file));

                        Map<String, String> results = transformationResult.getTransformationData();

                        System.out.println("Results: " + results.keySet());
                        System.out
                                .println("Looking for: " + StringEscapeUtils.escapeJava(cd.getInternalName()));

                        if (results.containsKey(cd.getInternalName())) {
                            consumer.accept(true, results.get(cd.getInternalName()));
                        } else {
                            StringBuilder output = new StringBuilder();
                            output.append("An error has occurred while decompiling this file.\r\n").append(
                                    "If you have not tried another decompiler, try that. Otherwise, you're out of luck.\r\n\r\n")
                                    .append("stdout:\r\n").append(transformationResult.getStdout())
                                    .append("\r\nstderr:\r\n").append(transformationResult.getStderr());
                            consumer.accept(false, output.toString());
                        }
                    }
                } catch (Throwable e) {
                    StringWriter writer = new StringWriter();
                    e.printStackTrace(new PrintWriter(writer));

                    StringBuilder output = new StringBuilder();
                    output.append("An error has occurred while decompiling this file.\r\n").append(
                            "If you have not tried another decompiler, try that. Otherwise, you're out of luck.\r\n\r\n")
                            .append("Exception:\r\n").append(writer.toString());
                    consumer.accept(false, output.toString());
                }
            }, () -> {
                consumer.accept(false, "Decompilation aborted");
            }));
}