Replace usages of org.apache.commons.lang.StringEscapeUtils with Guava (#9094)

This commit is contained in:
Basil Crow authored and GitHub committed 2024-03-30 07:26:04 -06:00
1 parent 47306c20ed
commit 9882ec67f2
4 files changed
+97 -4

No files matched your search

@@ -36,8 +36,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.util.SourceCodeEscapers;
import org.apache.commons.io.output.ProxyWriter;
import org.apache.commons.lang.StringEscapeUtils;
import org.kohsuke.stapler.framework.io.WriterOutputStream;
/**
@@ -122,7 +122,7 @@ public class ConsoleAnnotationOutputStream<T> extends LineTransformationOutputSt
}
} catch (IOException | ClassNotFoundException e) {
// if we failed to resurrect an annotation, ignore it.
LOGGER.log(Level.FINE, "Failed to resurrect annotation from \"" + StringEscapeUtils.escapeJava(new String(in, next, rest, Charset.defaultCharset())) + "\"", e);
LOGGER.log(Level.FINE, "Failed to resurrect annotation from \"" + SourceCodeEscapers.javaCharEscaper().escape(new String(in, next, rest, Charset.defaultCharset())) + "\"", e);
}
int bytesUsed = rest - b.available(); // bytes consumed by annotations
@@ -31,7 +31,7 @@ import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringEscapeUtils;
import jenkins.util.SourceCodeEscapers;
/**
* Filters out console notes.
@@ -73,7 +73,7 @@ public class PlainTextConsoleOutputStream extends LineTransformationOutputStream
try {
ConsoleNote.skip(new DataInputStream(b));
} catch (IOException x) {
LOGGER.log(Level.FINE, "Failed to skip annotation from \"" + StringEscapeUtils.escapeJava(new String(in, next, rest, Charset.defaultCharset())) + "\"", x);
LOGGER.log(Level.FINE, "Failed to skip annotation from \"" + SourceCodeEscapers.javaCharEscaper().escape(new String(in, next, rest, Charset.defaultCharset())) + "\"", x);
}
int bytesUsed = rest - b.available(); // bytes consumed by annotations
@@ -0,0 +1,51 @@
package jenkins.util;
import com.google.common.escape.ArrayBasedCharEscaper;
import com.google.common.escape.CharEscaper;
import com.google.common.escape.Escaper;
import java.util.Map;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
@Restricted(NoExternalUse.class)
public final class SourceCodeEscapers {
// Suppress default constructor for noninstantiability
private SourceCodeEscapers() {
throw new AssertionError();
}
private static final Map<Character, String> REPLACEMENTS = Map.of(
'\b', "\\b",
'\f', "\\f",
'\n', "\\n",
'\r', "\\r",
'\t', "\\t",
'\"', "\\\"",
'\\', "\\\\");
private static final char PRINTABLE_ASCII_MIN = 0x20;
private static final char PRINTABLE_ASCII_MAX = 0x7E;
private static final CharEscaper JAVA_CHAR_ESCAPER = new JavaCharEscaper();
/**
* Returns an {@link Escaper} instance that escapes special characters in a string so it can
* safely be included in either a Java character literal or string literal.
*/
public static CharEscaper javaCharEscaper() {
return JAVA_CHAR_ESCAPER;
}
private static class JavaCharEscaper extends ArrayBasedCharEscaper {
JavaCharEscaper() {
super(REPLACEMENTS, PRINTABLE_ASCII_MIN, PRINTABLE_ASCII_MAX);
}
@Override
protected char[] escapeUnsafe(char c) {
return String.format("\\u%04X", (int) c).toCharArray();
}
}
}