|
| 1 | +package org.infinispan.json; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +public class EscapeMain { |
| 9 | + |
| 10 | + /** |
| 11 | + * <p> |
| 12 | + * Exposes some internal methods that are useful for {@link JsonMain.Factory} implementations or other extension/layers |
| 13 | + * of the library. |
| 14 | + * </p> |
| 15 | + * |
| 16 | + * @author Borislav Iordanov |
| 17 | + */ |
| 18 | + public static class help { |
| 19 | + /** |
| 20 | + * <p> |
| 21 | + * Perform JSON escaping so that ", <, >, etc. characters are properly encoded in the JSON string representation |
| 22 | + * before returning to the client code. This is useful when serializing property names or string values. |
| 23 | + * </p> |
| 24 | + */ |
| 25 | + public static String escape(String string) { |
| 26 | + return escaper.escapeJsonString(string); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // ------------------------------------------------------------------------ |
| 31 | + // Extra utilities, taken from around the internet: |
| 32 | + // ------------------------------------------------------------------------ |
| 33 | + |
| 34 | + /* |
| 35 | + * Copyright (C) 2008 Google Inc. |
| 36 | + * |
| 37 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 38 | + * you may not use this file except in compliance with the License. |
| 39 | + * You may obtain a copy of the License at |
| 40 | + * |
| 41 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 42 | + * |
| 43 | + * Unless required by applicable law or agreed to in writing, software |
| 44 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 45 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 46 | + * See the License for the specific language governing permissions and |
| 47 | + * limitations under the License. |
| 48 | + */ |
| 49 | + |
| 50 | + /** |
| 51 | + * A utility class that is used to perform JSON escaping so that ", <, >, etc. characters are properly encoded in the |
| 52 | + * JSON string representation before returning to the client code. |
| 53 | + * |
| 54 | + * <p>This class contains a single method to escape a passed in string value: |
| 55 | + * <pre> |
| 56 | + * String jsonStringValue = "beforeQuote\"afterQuote"; |
| 57 | + * String escapedValue = Escaper.escapeJsonString(jsonStringValue); |
| 58 | + * </pre></p> |
| 59 | + * |
| 60 | + * @author Inderjeet Singh |
| 61 | + * @author Joel Leitch |
| 62 | + */ |
| 63 | + static Escaper escaper = new Escaper(false); |
| 64 | + |
| 65 | + final static class Escaper { |
| 66 | + |
| 67 | + private static final char[] HEX_CHARS = { |
| 68 | + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| 69 | + }; |
| 70 | + |
| 71 | + private static final Set<Character> JS_ESCAPE_CHARS; |
| 72 | + private static final Set<Character> HTML_ESCAPE_CHARS; |
| 73 | + |
| 74 | + static { |
| 75 | + Set<Character> mandatoryEscapeSet = new HashSet<Character>(); |
| 76 | + mandatoryEscapeSet.add('"'); |
| 77 | + mandatoryEscapeSet.add('\\'); |
| 78 | + JS_ESCAPE_CHARS = Collections.unmodifiableSet(mandatoryEscapeSet); |
| 79 | + |
| 80 | + Set<Character> htmlEscapeSet = new HashSet<Character>(); |
| 81 | + htmlEscapeSet.add('<'); |
| 82 | + htmlEscapeSet.add('>'); |
| 83 | + htmlEscapeSet.add('&'); |
| 84 | + htmlEscapeSet.add('='); |
| 85 | + htmlEscapeSet.add('\''); |
| 86 | + HTML_ESCAPE_CHARS = Collections.unmodifiableSet(htmlEscapeSet); |
| 87 | + } |
| 88 | + |
| 89 | + private final boolean escapeHtmlCharacters; |
| 90 | + |
| 91 | + Escaper(boolean escapeHtmlCharacters) { |
| 92 | + this.escapeHtmlCharacters = escapeHtmlCharacters; |
| 93 | + } |
| 94 | + |
| 95 | + public String escapeJsonString(CharSequence plainText) { |
| 96 | + StringBuilder escapedString = new StringBuilder(plainText.length() + 20); |
| 97 | + try { |
| 98 | + escapeJsonString(plainText, escapedString); |
| 99 | + } catch (IOException e) { |
| 100 | + throw new RuntimeException(e); |
| 101 | + } |
| 102 | + return escapedString.toString(); |
| 103 | + } |
| 104 | + |
| 105 | + private void escapeJsonString(CharSequence plainText, Appendable out) throws IOException { |
| 106 | + int pos = 0; // Index just past the last char in plainText written to out. |
| 107 | + int len = plainText.length(); |
| 108 | + |
| 109 | + for (int charCount, i = 0; i < len; i += charCount) { |
| 110 | + int codePoint = Character.codePointAt(plainText, i); |
| 111 | + charCount = Character.charCount(codePoint); |
| 112 | + |
| 113 | + if (!isControlCharacter(codePoint) && !mustEscapeCharInJsString(codePoint)) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + out.append(plainText, pos, i); |
| 118 | + pos = i + charCount; |
| 119 | + switch (codePoint) { |
| 120 | + case '\b': |
| 121 | + out.append("\\b"); |
| 122 | + break; |
| 123 | + case '\t': |
| 124 | + out.append("\\t"); |
| 125 | + break; |
| 126 | + case '\n': |
| 127 | + out.append("\\n"); |
| 128 | + break; |
| 129 | + case '\f': |
| 130 | + out.append("\\f"); |
| 131 | + break; |
| 132 | + case '\r': |
| 133 | + out.append("\\r"); |
| 134 | + break; |
| 135 | + case '\\': |
| 136 | + out.append("\\\\"); |
| 137 | + break; |
| 138 | + case '/': |
| 139 | + out.append("\\/"); |
| 140 | + break; |
| 141 | + case '"': |
| 142 | + out.append("\\\""); |
| 143 | + break; |
| 144 | + default: |
| 145 | + appendHexJavaScriptRepresentation(codePoint, out); |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + out.append(plainText, pos, len); |
| 150 | + } |
| 151 | + |
| 152 | + private boolean mustEscapeCharInJsString(int codepoint) { |
| 153 | + if (!Character.isSupplementaryCodePoint(codepoint)) { |
| 154 | + char c = (char) codepoint; |
| 155 | + return JS_ESCAPE_CHARS.contains(c) |
| 156 | + || (escapeHtmlCharacters && HTML_ESCAPE_CHARS.contains(c)); |
| 157 | + } |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + private static boolean isControlCharacter(int codePoint) { |
| 162 | + // JSON spec defines these code points as control characters, so they must be escaped |
| 163 | + return codePoint < 0x20 |
| 164 | + || codePoint == 0x2028 // Line separator |
| 165 | + || codePoint == 0x2029 // Paragraph separator |
| 166 | + || (codePoint >= 0x7f && codePoint <= 0x9f); |
| 167 | + } |
| 168 | + |
| 169 | + private static void appendHexJavaScriptRepresentation(int codePoint, Appendable out) |
| 170 | + throws IOException { |
| 171 | + if (Character.isSupplementaryCodePoint(codePoint)) { |
| 172 | + // Handle supplementary unicode values which are not representable in |
| 173 | + // javascript. We deal with these by escaping them as two 4B sequences |
| 174 | + // so that they will round-trip properly when sent from java to javascript |
| 175 | + // and back. |
| 176 | + char[] surrogates = Character.toChars(codePoint); |
| 177 | + appendHexJavaScriptRepresentation(surrogates[0], out); |
| 178 | + appendHexJavaScriptRepresentation(surrogates[1], out); |
| 179 | + return; |
| 180 | + } |
| 181 | + out.append("\\u") |
| 182 | + .append(HEX_CHARS[(codePoint >>> 12) & 0xf]) |
| 183 | + .append(HEX_CHARS[(codePoint >>> 8) & 0xf]) |
| 184 | + .append(HEX_CHARS[(codePoint >>> 4) & 0xf]) |
| 185 | + .append(HEX_CHARS[codePoint & 0xf]); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments