MCPcopy Create free account
hub / github.com/antlr/codebuff / PercentEscaper

Class PercentEscaper

output/java_guava/1.4.17/PercentEscaper.java:52–243  ·  view source on GitHub ↗

A UnicodeEscaper that escapes some set of Java characters using a UTF-8 based percent encoding scheme. The set of safe characters (those which remain unescaped) can be specified on construction. This class is primarily used for creating URI escapers in UrlEscapers but can be used

Source from the content-addressed store, hash-verified

50
51
52@Beta
53@GwtCompatible
54public final class PercentEscaper extends UnicodeEscaper {
55
56 // In some escapers spaces are escaped to '+'
57 private static final char[] PLUS_SIGN = {'+'};
58
59 // Percent escapers output upper case hex digits (uri escapers require this).
60 private static final char[] UPPER_HEX_DIGITS = "0123456789ABCDEF".toCharArray();
61
62 /**
63 * If true we should convert space to the {@code +} character.
64 */
65 private final boolean plusForSpace;
66
67 /**
68 * An array of flags where for any {@code char c} if {@code safeOctets[c]} is true then {@code c}
69 * should remain unmodified in the output. If {@code c > safeOctets.length} then it should be
70 * escaped.
71 */
72 private final boolean[] safeOctets;
73
74 /**
75 * Constructs a percent escaper with the specified safe characters and optional handling of the
76 * space character.
77 *
78 * <p>Not that it is allowed, but not necessarily desirable to specify {@code %} as a safe
79 * character. This has the effect of creating an escaper which has no well defined inverse but it
80 * can be useful when escaping additional characters.
81 *
82 * @param safeChars a non null string specifying additional safe characters for this escaper (the
83 * ranges 0..9, a..z and A..Z are always safe and should not be specified here)
84 * @param plusForSpace true if ASCII space should be escaped to {@code +} rather than {@code %20}
85 * @throws IllegalArgumentException if any of the parameters were invalid
86 */
87
88
89 public PercentEscaper(String safeChars, boolean plusForSpace) {
90 // TODO(user): Switch to static factory methods for creation now that class is final.
91 // TODO(user): Support escapers where alphanumeric chars are not safe.
92 checkNotNull(safeChars); // eager for GWT.
93 // Avoid any misunderstandings about the behavior of this escaper
94 if (safeChars.matches(".*[0-9A-Za-z].*")) {
95 throw new IllegalArgumentException("Alphanumeric characters are always 'safe' and should not be explicitly specified");
96 }
97 safeChars += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
98 // Avoid ambiguous parameters. Safe characters are never modified so if
99 // space is a safe character then setting plusForSpace is meaningless.
100 if (plusForSpace && safeChars.contains(" ")) {
101 throw new IllegalArgumentException("plusForSpace cannot be specified when space is a 'safe' character");
102 }
103 this.plusForSpace = plusForSpace;
104 this.safeOctets = createSafeOctets(safeChars);
105 }
106
107 /**
108 * Creates a boolean array with entries corresponding to the character values specified in
109 * safeChars set to true. The array is as small as is required to hold the given character

Callers

nothing calls this directly

Calls 1

toCharArrayMethod · 0.45

Tested by

no test coverage detected