Skip to content

Commit a25b1f4

Browse files
authored
Move unescapeName to U.fromXml(string).
1 parent 89fdf65 commit a25b1f4

File tree

3 files changed

+63
-52
lines changed

3 files changed

+63
-52
lines changed

src/main/java/com/github/underscore/lodash/Json.java

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public static void writeJson(Map map, JsonStringBuilder builder) {
333333
while (iter.hasNext()) {
334334
Map.Entry entry = (Map.Entry) iter.next();
335335
builder.fillSpaces().append(builder.type.getWrapLine());
336-
builder.append(JsonValue.unescapeName(String.valueOf(entry.getKey())));
336+
builder.append(JsonValue.escape(String.valueOf(entry.getKey())));
337337
builder.append(builder.type.getWrapLine());
338338
builder.append(':');
339339
if (builder.getIdentStep() != JsonStringBuilder.Step.COMPACT) {
@@ -398,56 +398,6 @@ public static void writeJson(Object value, JsonStringBuilder builder) {
398398
}
399399
}
400400

401-
public static String unescapeName(final String name) {
402-
final int length = name.length();
403-
if (length == 0 || "__EE__EMPTY__EE__".equals(name)) {
404-
return "";
405-
}
406-
if ("-__EE__EMPTY__EE__".equals(name)) {
407-
return "-";
408-
}
409-
if (!name.contains("__")) {
410-
return name;
411-
}
412-
StringBuilder result = new StringBuilder();
413-
int underlineCount = 0;
414-
StringBuilder lastChars = new StringBuilder();
415-
outer:
416-
for (int i = 0; i < length; ++i) {
417-
char ch = name.charAt(i);
418-
if (ch == '_') {
419-
lastChars.append(ch);
420-
} else {
421-
if (lastChars.length() == 2) {
422-
StringBuilder nameToDecode = new StringBuilder();
423-
for (int j = i; j < length; ++j) {
424-
if (name.charAt(j) == '_') {
425-
underlineCount += 1;
426-
if (underlineCount == 2) {
427-
try {
428-
result.append(JsonValue.escape(Base32.decode(nameToDecode.toString())));
429-
} catch (Base32.DecodingException ex) {
430-
result.append("__").append(JsonValue.escape(nameToDecode.toString()))
431-
.append(lastChars);
432-
}
433-
i = j;
434-
underlineCount = 0;
435-
lastChars.setLength(0);
436-
continue outer;
437-
}
438-
} else {
439-
nameToDecode.append(name.charAt(j));
440-
underlineCount = 0;
441-
}
442-
}
443-
}
444-
result.append(lastChars).append(ch);
445-
lastChars.setLength(0);
446-
}
447-
}
448-
return result.append(lastChars).toString();
449-
}
450-
451401
public static String escape(String s) {
452402
if (s == null) {
453403
return null;

src/main/java/com/github/underscore/lodash/Xml.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,59 @@ static String getAttributes(final int sourceIndex, final String source) {
11401140
return "";
11411141
}
11421142

1143+
private static String unescapeName(final String name) {
1144+
if (name == null) {
1145+
return name;
1146+
}
1147+
final int length = name.length();
1148+
if ("__EE__EMPTY__EE__".equals(name)) {
1149+
return "";
1150+
}
1151+
if ("-__EE__EMPTY__EE__".equals(name)) {
1152+
return "-";
1153+
}
1154+
if (!name.contains("__")) {
1155+
return name;
1156+
}
1157+
StringBuilder result = new StringBuilder();
1158+
int underlineCount = 0;
1159+
StringBuilder lastChars = new StringBuilder();
1160+
outer:
1161+
for (int i = 0; i < length; ++i) {
1162+
char ch = name.charAt(i);
1163+
if (ch == '_') {
1164+
lastChars.append(ch);
1165+
} else {
1166+
if (lastChars.length() == 2) {
1167+
StringBuilder nameToDecode = new StringBuilder();
1168+
for (int j = i; j < length; ++j) {
1169+
if (name.charAt(j) == '_') {
1170+
underlineCount += 1;
1171+
if (underlineCount == 2) {
1172+
try {
1173+
result.append(Base32.decode(nameToDecode.toString()));
1174+
} catch (Base32.DecodingException ex) {
1175+
result.append("__").append(nameToDecode.toString())
1176+
.append(lastChars);
1177+
}
1178+
i = j;
1179+
underlineCount = 0;
1180+
lastChars.setLength(0);
1181+
continue outer;
1182+
}
1183+
} else {
1184+
nameToDecode.append(name.charAt(j));
1185+
underlineCount = 0;
1186+
}
1187+
}
1188+
}
1189+
result.append(lastChars).append(ch);
1190+
lastChars.setLength(0);
1191+
}
1192+
}
1193+
return result.append(lastChars).toString();
1194+
}
1195+
11431196
@SuppressWarnings("unchecked")
11441197
private static void addNodeValue(final Map<String, Object> map, final String name, final Object value,
11451198
final BiFunction<Object, Set<String>, String> elementMapper, final Function<Object, Object> nodeMapper,
@@ -1166,7 +1219,7 @@ private static void addNodeValue(final Map<String, Object> map, final String nam
11661219
}
11671220
}
11681221
} else {
1169-
final String elementName = elementMapper.apply(name, namespaces);
1222+
final String elementName = unescapeName(elementMapper.apply(name, namespaces));
11701223
if (elementName != null) {
11711224
map.put(elementName, nodeMapper.apply(getValue(value)));
11721225
}

src/test/java/com/github/underscore/lodash/StringTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,14 @@ public void toJsonFromXml4() {
14351435
+ " \"#omit-xml-declaration\": \"yes\"\n"
14361436
+ "}",
14371437
U.toJson((Map<String, Object>) U.fromXml(xml)));
1438+
final String xml2 = "<__FUa>"
1439+
+ "</__FUa>";
1440+
assertEquals("{\n"
1441+
+ " \"__FUa\": {\n"
1442+
+ " },\n"
1443+
+ " \"#omit-xml-declaration\": \"yes\"\n"
1444+
+ "}",
1445+
U.toJson((Map<String, Object>) U.fromXml(xml2)));
14381446
}
14391447

14401448
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)