more unicode work

This commit is contained in:
Grahame Grieve 2024-01-10 10:14:43 +11:00
parent eb95ed00b6
commit 33f43da3d7
3 changed files with 54 additions and 41 deletions

View File

@ -528,7 +528,6 @@ public class Utilities {
return value.replace("\r\n", "\r").replace("\n", "\r").replace("\r", "\r\n");
}
public static String unescapeXml(String xml) throws FHIRException {
if (xml == null)
return null;
@ -601,7 +600,7 @@ public class Utilities {
break;
case 'u':
String hex = json.substring(i + 1, i + 5);
b.append((char) Integer.parseInt(hex, 16));
b.append(Character.toString(Integer.parseInt(hex, 16)));
break;
default:
throw new FHIRException("Unknown JSON escape \\" + ch);

View File

@ -137,52 +137,64 @@ public class XhtmlComposer {
String src = node.getContent();
int i = 0;
while (i < src.length()) {
char c = src.charAt(i);
if (autoLinks && c == 'h' && Utilities.startsWithInList(src.substring(i), "http://", "https://")) {
int j = i;
while (i < src.length() && isValidUrlChar(src.charAt(i))) {
i++;
}
String url = src.substring(j, i);
if (url.endsWith(".") || url.endsWith(",")) {
i--;
url = url.substring(0, url.length()-1);
}
url = Utilities.escapeXml(url);
dst.append("<a href=\""+url+"\">"+ url +"</a>");
int ci = src.codePointAt(i);
if (ci > 65535) {
dst.append("&#x");
dst.append(Integer.toHexString(ci).toUpperCase());
dst.append(";");
i += Character.charCount(ci);
} else {
i++;
if (c == '&') {
dst.append("&amp;");
} else if (c == '<') {
dst.append("&lt;");
} else if (c == '>') {
dst.append("&gt;");
} else if (xml) {
if (c == '"')
dst.append("&quot;");
else
dst.append(c);
char c = (char) ci;
if (autoLinks && c == 'h' && Utilities.startsWithInList(src.substring(i), "http://", "https://")) {
int j = i;
while (i < src.length() && isValidUrlChar(src.charAt(i))) {
i++;
}
String url = src.substring(j, i);
if (url.endsWith(".") || url.endsWith(",")) {
i--;
url = url.substring(0, url.length()-1);
}
url = Utilities.escapeXml(url);
dst.append("<a href=\""+url+"\">"+ url +"</a>");
} else {
if (c == XhtmlNode.NBSP.charAt(0))
dst.append("&nbsp;");
else if (c == (char) 0xA7)
dst.append("&sect;");
else if (c == (char) 169)
dst.append("&copy;");
else if (c == (char) 8482)
dst.append("&trade;");
else if (c == (char) 956)
dst.append("&mu;");
else if (c == (char) 174)
dst.append("&reg;");
else
dst.append(c);
i++;
if (c == '&') {
dst.append("&amp;");
} else if (c == '<') {
dst.append("&lt;");
} else if (c == '>') {
dst.append("&gt;");
} else if (xml) {
if (c == '"')
dst.append("&quot;");
else
dst.append(c);
} else {
if (c == XhtmlNode.NBSP.charAt(0))
dst.append("&nbsp;");
else if (c == (char) 0xA7)
dst.append("&sect;");
else if (c == (char) 169)
dst.append("&copy;");
else if (c == (char) 8482)
dst.append("&trade;");
else if (c == (char) 956)
dst.append("&mu;");
else if (c == (char) 174)
dst.append("&reg;");
else
dst.append(c);
}
}
}
}
}
boolean isTwoCharUnicodeCodePoint(char c1, char c2) {
return false;
}
private void writeComment(String indent, XhtmlNode node, boolean noPrettyOverride) throws IOException {
dst.append(indent + "<!-- " + node.getContent().trim() + " -->" + (pretty && !noPrettyOverride ? "\r\n" : ""));
}

View File

@ -217,6 +217,8 @@ public class XhtmlNodeTest {
public void testEntityNumberGreaterThanFFFF_Hex() throws IOException {
XhtmlNode x = new XhtmlParser().parse("<div>&#x1F637;</div>", "div");
Assertions.assertEquals("\uD83D\uDE37", x.getFirstElement().getChildNodes().get(0).getContent());
String html = new XhtmlComposer(false).compose(x);
Assertions.assertEquals("<div>&#x1F637;</div>", html);
}