Refactor CDL row serialization
This commit is contained in:
@@ -183,11 +183,31 @@ public class CDL {
|
|||||||
sb.append(delimiter);
|
sb.append(delimiter);
|
||||||
}
|
}
|
||||||
Object object = ja.opt(i);
|
Object object = ja.opt(i);
|
||||||
if (object != null) {
|
appendRowValue(sb, object, delimiter);
|
||||||
|
}
|
||||||
|
sb.append('\n');
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void appendRowValue(StringBuilder sb, Object object, char delimiter) {
|
||||||
|
if (object == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
String string = object.toString();
|
String string = object.toString();
|
||||||
if (!string.isEmpty() && (string.indexOf(delimiter) >= 0 ||
|
if (shouldQuoteValue(string, delimiter)) {
|
||||||
|
appendQuotedValue(sb, string);
|
||||||
|
} else {
|
||||||
|
sb.append(string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean shouldQuoteValue(String string, char delimiter) {
|
||||||
|
return !string.isEmpty() && (string.indexOf(delimiter) >= 0 ||
|
||||||
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
|
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
|
||||||
string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
|
string.indexOf(0) >= 0 || string.charAt(0) == '"');
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void appendQuotedValue(StringBuilder sb, String string) {
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
int length = string.length();
|
int length = string.length();
|
||||||
for (int j = 0; j < length; j += 1) {
|
for (int j = 0; j < length; j += 1) {
|
||||||
@@ -197,13 +217,6 @@ public class CDL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
} else {
|
|
||||||
sb.append(string);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sb.append('\n');
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user