more json and xml utils

This commit is contained in:
Grahame Grieve 2023-05-25 16:42:26 +10:00
parent d60b9009b2
commit 70a450bf01
3 changed files with 87 additions and 0 deletions

View File

@ -14,6 +14,10 @@ public class JsonNumber extends JsonPrimitive {
this.value = Integer.toString(value);
}
public JsonNumber(long value) {
this.value = Long.toString(value);
}
private JsonNumber() {
}

View File

@ -66,6 +66,13 @@ public class JsonObject extends JsonElement {
return add(name, new JsonNumber(value));
}
public JsonObject add(String name, long value) throws JsonException {
check(name != null, "Name is null");
return add(name, new JsonNumber(value));
}
public JsonObject set(String name, JsonElement value) throws JsonException {
check(name != null, "Name is null");
check(value != null, "Value is null");

View File

@ -0,0 +1,76 @@
package org.hl7.fhir.utilities.xml;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public class XmlEscaper {
private InputStream source;
private OutputStream target;
protected XmlEscaper(InputStream source, OutputStream target) {
super();
this.source = source;
this.target = target;
}
public static void convert(InputStream source, OutputStream target) throws IOException {
XmlEscaper self = new XmlEscaper(source, target);
self.process();
}
public static void convert(String source, String target) throws IOException {
convertAndClose(new FileInputStream(source), new FileOutputStream(target));
}
public static void convertAndClose(InputStream source, OutputStream target) throws IOException {
XmlEscaper self = new XmlEscaper(source, target);
self.process();
source.close();
target.close();
}
public InputStream getSource() {
return source;
}
public void setSource(InputStream source) {
this.source = source;
}
public OutputStream getTarget() {
return target;
}
public void setTarget(OutputStream target) {
this.target = target;
}
public void process() throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(source));
int i = 0;
while ((i = buffer.read()) != -1) {
char c = (char) i;
if (c == '<')
write("&lt;");
else if (c == '>')
write("&gt;");
else if (c == '&')
write("&amp;");
else if (c == '"')
write("&quot;");
else
target.write((byte) i);
}
}
private void write(String s) throws IOException {
target.write(s.getBytes(StandardCharsets.UTF_8));
}
}