org.bidib.wizard.server.jsontuils.CrunchifyEscapeCharUtility Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bidibwizard-server Show documentation
Show all versions of bidibwizard-server Show documentation
jBiDiB BiDiB Wizard Server POM
package org.bidib.wizard.server.jsontuils;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.StringCharacterIterator;
/**
* @author Crunchify.com Version: 1.0 Updated: 01.25.2016
*
*/
public class CrunchifyEscapeCharUtility {
// Used to ensure that HTTP query strings are in proper form, by escaping special characters such as spaces.
// Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme
public static String crunchifyURLEscapeUtil(String crunchifyURL) {
String crunchifyNewURL = null;
try {
crunchifyNewURL = URLEncoder.encode(crunchifyURL, StandardCharsets.UTF_8);
}
catch (Exception ex) {
throw new RuntimeException("UTF-8 not supported", ex);
}
return crunchifyNewURL;
}
// JSON Escape Utility
public static String crunchifyJSONEscapeUtil(String crunchifyJSON) {
final StringBuilder crunchifyNewJSON = new StringBuilder();
// StringCharacterIterator class iterates over the entire String
StringCharacterIterator iterator = new StringCharacterIterator(crunchifyJSON);
char myChar = iterator.current();
// DONE = \\uffff (not a character)
while (myChar != StringCharacterIterator.DONE) {
if (myChar == '\"') {
crunchifyNewJSON.append("\\\"");
}
else if (myChar == '\t') {
crunchifyNewJSON.append("\\t");
}
else if (myChar == '\f') {
crunchifyNewJSON.append("\\f");
}
else if (myChar == '\n') {
crunchifyNewJSON.append("\\n");
}
else if (myChar == '\r') {
crunchifyNewJSON.append("\\r");
}
else if (myChar == '\\') {
crunchifyNewJSON.append("\\\\");
}
else if (myChar == '/') {
crunchifyNewJSON.append("\\/");
}
else if (myChar == '\b') {
crunchifyNewJSON.append("\\b");
}
else {
// nothing matched - just as text as it is.
crunchifyNewJSON.append(myChar);
}
myChar = iterator.next();
}
return crunchifyNewJSON.toString();
}
}