use of java.text.StringCharacterIterator in project Payara by payara.
the class JSONUtil method javaToJSON.
/**
* <p> This method attempts to convert the given Object into a JSON String
* to given depth. If -1 (or lower) is supplied for depth, it will
* walk upto a default depth of 10 levels of the given Object. If 0
* is supplied, it will simply return "". 1 will encode the current
* Object, but no children. 2 will encode the given Object and its
* direct children (if any), and so on.</p>
*
* <p> Strings, Longs, Float, and primitives are considered to not have
* child Objects. Objects which have a public no-argument getXYZ()
* method are considered to be child Objects. Maps and Collections
* will be walked.</p>
* @param obj
* @param depth
* @return
*/
public static String javaToJSON(Object obj, int depth) {
if (depth == 0) {
// Make sure we do nothing if told to do nothing...
return "";
} else if (depth == -1) {
// To prevent recursion...
depth = 10;
}
String value = "";
if (obj == null) {
value = "null";
} else if (obj instanceof String) {
String chStr;
int len;
StringCharacterIterator it = new StringCharacterIterator((String) obj);
char ch = it.first();
StringBuilder builder = new StringBuilder(((String) obj).length() << 2);
builder.append("\"");
while (ch != StringCharacterIterator.DONE) {
switch(ch) {
case '\t':
builder.append("\\t");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
case '\b':
builder.append("\\b");
break;
case '\f':
builder.append("\\f");
break;
case '&':
case '<':
case '>':
case '(':
case ')':
case '{':
case '}':
case ':':
case '/':
case '\\':
case '\'':
case '"':
builder.append("\\");
builder.append(ch);
break;
default:
// Check if we should unicode escape this...
if ((ch > 0x7e) || (ch < 0x20)) {
builder.append("\\u");
chStr = Integer.toHexString(ch);
len = chStr.length();
for (int idx = 4; idx > len; idx--) {
// Add leading 0's
builder.append('0');
}
builder.append(chStr);
} else {
builder.append(ch);
}
break;
}
ch = it.next();
}
builder.append("\"");
value = builder.toString();
} else if ((obj instanceof Boolean) || (obj instanceof Number)) {
value = obj.toString();
} else if (obj instanceof Object[]) {
StringBuilder builder = new StringBuilder("[");
boolean first = true;
for (Object element : ((Object[]) obj)) {
if (first) {
first = false;
} else {
builder.append(',');
}
if (depth == 1) {
// Treat as String, but don't try to go deeper...
builder.append(javaToJSON(element.toString(), 1));
} else {
// Recurse...
builder.append(javaToJSON(element, depth - 1));
}
}
builder.append("]");
value = builder.toString();
} else if (obj instanceof Map) {
StringBuilder builder = new StringBuilder("{");
String key;
boolean first = true;
Map map = ((Map) obj);
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
if (first) {
first = false;
} else {
builder.append(',');
}
key = it.next().toString();
builder.append(javaToJSON(key, 1)).append(":");
if (depth == 1) {
// Treat as String, but don't try to go deeper...
builder.append(javaToJSON(map.get(key).toString(), 1));
} else {
// Recurse...
builder.append(javaToJSON(map.get(key), depth - 1));
}
}
builder.append("}");
value = builder.toString();
} else if (obj instanceof Collection) {
StringBuilder builder = new StringBuilder("[");
boolean first = true;
Iterator it = ((Collection) obj).iterator();
while (it.hasNext()) {
if (first) {
first = false;
} else {
builder.append(',');
}
if (depth == 1) {
// Treat as String, but don't try to go deeper...
builder.append(javaToJSON(it.next().toString(), 1));
} else {
// Recurse...
builder.append(javaToJSON(it.next(), depth - 1));
}
}
builder.append("]");
value = builder.toString();
} else {
// Object
StringBuilder builder = new StringBuilder("{");
String methodName;
Object result;
boolean first = true;
Iterator<String> it = getGetters(obj).iterator();
while (it.hasNext()) {
if (first) {
first = false;
} else {
builder.append(',');
}
methodName = it.next();
// Drop "get"...
builder.append(javaToJSON(methodName.substring(3), 1)).append(":");
result = invokeGetter(obj, methodName);
if ((result != null) && (depth == 1)) {
// Treat as String, but don't try to go deeper...
builder.append(javaToJSON(result.toString(), 1));
} else {
// Recurse...
builder.append(javaToJSON(result, depth - 1));
}
}
builder.append("}");
value = builder.toString();
}
return value;
}
use of java.text.StringCharacterIterator in project Payara by payara.
the class UtilHandlers method escapePropertyValue.
/* This is copied from within javaToJSON() */
public static String escapePropertyValue(String str) {
String chStr;
int len;
StringCharacterIterator it = new StringCharacterIterator(str);
char ch = it.first();
StringBuilder builder = new StringBuilder(str.length() << 2);
while (ch != StringCharacterIterator.DONE) {
switch(ch) {
case '\t':
builder.append("\\t");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
case '\b':
builder.append("\\b");
break;
case '\f':
builder.append("\\f");
break;
case '&':
case '<':
case '>':
case '(':
case ')':
case '{':
case '}':
case ':':
case '/':
case '\\':
case '\'':
case '"':
case '=':
builder.append("\\");
builder.append(ch);
break;
default:
// Check if we should unicode escape this...
if ((ch > 0x7e) || (ch < 0x20)) {
builder.append("\\u");
chStr = Integer.toHexString(ch);
len = chStr.length();
for (int idx = 4; idx > len; idx--) {
// Add leading 0's
builder.append('0');
}
builder.append(chStr);
} else {
builder.append(ch);
}
break;
}
ch = it.next();
}
return builder.toString();
}
use of java.text.StringCharacterIterator in project perun by CESNET.
the class Utils method utftoasci.
/**
* IMPORTANT: this method not convert utf to ascii, just try to convert some problematic
* chars to UTF and others change to '?'!!!
*
* @param s
* @return converted string from ascii to something near utf
*/
public static synchronized String utftoasci(String s) {
final StringBuffer sb = new StringBuffer(s.length() * 2);
final StringCharacterIterator iterator = new StringCharacterIterator(s);
char ch = iterator.current();
while (ch != StringCharacterIterator.DONE) {
if (Character.getNumericValue(ch) >= 0) {
sb.append(ch);
} else {
boolean f = false;
if (Character.toString(ch).equals("Ê")) {
sb.append("E");
f = true;
}
if (Character.toString(ch).equals("È")) {
sb.append("E");
f = true;
}
if (Character.toString(ch).equals("ë")) {
sb.append("e");
f = true;
}
if (Character.toString(ch).equals("é")) {
sb.append("e");
f = true;
}
if (Character.toString(ch).equals("è")) {
sb.append("e");
f = true;
}
if (Character.toString(ch).equals("Â")) {
sb.append("A");
f = true;
}
if (Character.toString(ch).equals("ä")) {
sb.append("a");
f = true;
}
if (Character.toString(ch).equals("ß")) {
sb.append("ss");
f = true;
}
if (Character.toString(ch).equals("Ç")) {
sb.append("C");
f = true;
}
if (Character.toString(ch).equals("Ö")) {
sb.append("O");
f = true;
}
if (Character.toString(ch).equals("º")) {
sb.append("");
f = true;
}
if (Character.toString(ch).equals("ª")) {
sb.append("");
f = true;
}
if (Character.toString(ch).equals("º")) {
sb.append("");
f = true;
}
if (Character.toString(ch).equals("Ñ")) {
sb.append("N");
f = true;
}
if (Character.toString(ch).equals("É")) {
sb.append("E");
f = true;
}
if (Character.toString(ch).equals("Ä")) {
sb.append("A");
f = true;
}
if (Character.toString(ch).equals("Å")) {
sb.append("A");
f = true;
}
if (Character.toString(ch).equals("Ü")) {
sb.append("U");
f = true;
}
if (Character.toString(ch).equals("ö")) {
sb.append("o");
f = true;
}
if (Character.toString(ch).equals("ü")) {
sb.append("u");
f = true;
}
if (Character.toString(ch).equals("á")) {
sb.append("a");
f = true;
}
if (Character.toString(ch).equals("Ó")) {
sb.append("O");
f = true;
}
if (Character.toString(ch).equals("ě")) {
sb.append("e");
f = true;
}
if (Character.toString(ch).equals("Ě")) {
sb.append("E");
f = true;
}
if (Character.toString(ch).equals("š")) {
sb.append("s");
f = true;
}
if (Character.toString(ch).equals("Š")) {
sb.append("S");
f = true;
}
if (Character.toString(ch).equals("č")) {
sb.append("c");
f = true;
}
if (Character.toString(ch).equals("Č")) {
sb.append("C");
f = true;
}
if (Character.toString(ch).equals("ř")) {
sb.append("r");
f = true;
}
if (Character.toString(ch).equals("Ř")) {
sb.append("R");
f = true;
}
if (Character.toString(ch).equals("ž")) {
sb.append("z");
f = true;
}
if (Character.toString(ch).equals("Ž")) {
sb.append("Z");
f = true;
}
if (Character.toString(ch).equals("ý")) {
sb.append("y");
f = true;
}
if (Character.toString(ch).equals("Ý")) {
sb.append("Y");
f = true;
}
if (Character.toString(ch).equals("í")) {
sb.append("i");
f = true;
}
if (Character.toString(ch).equals("Í")) {
sb.append("I");
f = true;
}
if (Character.toString(ch).equals("ó")) {
sb.append("o");
f = true;
}
if (Character.toString(ch).equals("ú")) {
sb.append("u");
f = true;
}
if (Character.toString(ch).equals("Ú")) {
sb.append("u");
f = true;
}
if (Character.toString(ch).equals("ů")) {
sb.append("u");
f = true;
}
if (Character.toString(ch).equals("Ů")) {
sb.append("U");
f = true;
}
if (Character.toString(ch).equals("Ň")) {
sb.append("N");
f = true;
}
if (Character.toString(ch).equals("ň")) {
sb.append("n");
f = true;
}
if (Character.toString(ch).equals("Ť")) {
sb.append("T");
f = true;
}
if (Character.toString(ch).equals("ť")) {
sb.append("t");
f = true;
}
if (Character.toString(ch).equals(" ")) {
sb.append(" ");
f = true;
}
if (!f) {
sb.append("?");
}
}
ch = iterator.next();
}
return sb.toString();
}
use of java.text.StringCharacterIterator in project gradle by gradle.
the class StartScriptTemplateBindingFactory method escapeWindowsJvmOpt.
private String escapeWindowsJvmOpt(String jvmOpts) {
boolean wasOnBackslash = false;
StringBuilder escapedJvmOpt = new StringBuilder();
CharacterIterator it = new StringCharacterIterator(jvmOpts);
// - use a state machine rather than regexps
for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
String repl = Character.toString(ch);
if (ch == '%') {
repl = "%%";
} else if (ch == '"') {
repl = (wasOnBackslash ? '\\' : "") + "\\\"";
}
wasOnBackslash = ch == '\\';
escapedJvmOpt.append(repl);
}
return escapedJvmOpt.toString();
}
use of java.text.StringCharacterIterator in project translationstudio8 by heartsome.
the class Calculator method checkexpression.
/**
* 表达式正确性规则处理与校验.
* @param str
* 表达式
* @return String
* 如果表达式非法,返回空串"",否则返回表达式
*/
public static String checkexpression(String str) {
if (str == null || "".equals(str.trim())) {
return "";
}
StringCharacterIterator sci = new StringCharacterIterator(str);
char lastChar = sci.last();
switch(lastChar) {
case '+':
return "";
case '*':
return "";
case '-':
return "";
case '/':
return "";
default:
break;
}
if (!Calculator.checkbracket(str)) {
return "";
}
Stack<Character> stackCheck = new Stack<Character>();
Stack<Character> stackTmp = new Stack<Character>();
String strResultOne = "";
// 匹配合法的运算字符"数字,.,+,-,*,/,(,),"
String strRegex = "^[\\.\\d\\+\\-\\*/\\(\\)]+$";
Pattern patternFiltrate = Pattern.compile(strRegex);
Matcher m = patternFiltrate.matcher(str);
boolean booFiltrate = m.matches();
if (!booFiltrate) {
strResultOne = "";
return strResultOne;
}
// 匹配非法的浮点数.
String strErrFloat = ".*(\\.\\d*){2,}.*";
Pattern patternErrFloat = Pattern.compile(strErrFloat);
Matcher matcherErrFloat = patternErrFloat.matcher(str);
boolean booErrFloat = matcherErrFloat.matches();
if (booErrFloat) {
strResultOne = "";
return strResultOne;
}
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (checkfig(ch)) {
if (!stackTmp.isEmpty() && stackTmp.peek() == ')') {
strResultOne = "";
return strResultOne;
}
stackTmp.push(ch);
strResultOne = strResultOne + ch;
}
switch(ch) {
case '(':
if (!stackTmp.isEmpty() && stackTmp.peek() == '.') {
strResultOne = "";
return strResultOne;
}
stackCheck.push(ch);
if (stackTmp.isEmpty() || (!checkfig(stackTmp.peek()) && stackTmp.peek() != ')')) {
strResultOne = strResultOne + ch;
} else {
strResultOne = strResultOne + "*" + ch;
}
stackTmp.push(ch);
break;
case ')':
if (!stackCheck.isEmpty()) {
char chx = stackCheck.pop();
if (ch == ')' && chx != '(') {
strResultOne = "";
return strResultOne;
}
} else {
strResultOne = "";
return strResultOne;
}
if (stackTmp.peek() == '.' || (!checkfig(stackTmp.peek()) && stackTmp.peek() != ')')) {
strResultOne = "";
return strResultOne;
}
stackTmp.push(ch);
strResultOne = strResultOne + ch;
break;
case '+':
case '-':
if (!stackTmp.isEmpty() && (stackTmp.peek() == '+' || stackTmp.peek() == '-' || stackTmp.peek() == '*' || stackTmp.peek() == '/' || stackTmp.peek() == '.')) {
strResultOne = "";
return strResultOne;
}
if (stackTmp.isEmpty() || stackTmp.peek() == '(') {
strResultOne = strResultOne + "0" + ch;
} else {
strResultOne = strResultOne + ch;
}
stackTmp.push(ch);
break;
case '*':
case '/':
if (stackTmp.isEmpty() || stackTmp.peek() == '.' || (!checkfig(stackTmp.peek()) && stackTmp.peek() != ')')) {
strResultOne = "";
return strResultOne;
}
stackTmp.push(ch);
strResultOne = strResultOne + ch;
break;
case '.':
if (stackTmp.isEmpty() || !checkfig(stackTmp.peek())) {
strResultOne = strResultOne + "0" + ch;
} else {
strResultOne = strResultOne + ch;
}
stackTmp.push(ch);
break;
default:
break;
}
}
if (!stackCheck.isEmpty()) {
strResultOne = "";
return strResultOne;
}
return strResultOne;
}
Aggregations