From 70a450bf01ed3d2fe9862d19a938038be41e96b1 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Thu, 25 May 2023 16:42:26 +1000 Subject: [PATCH] more json and xml utils --- .../fhir/utilities/json/model/JsonNumber.java | 4 + .../fhir/utilities/json/model/JsonObject.java | 7 ++ .../hl7/fhir/utilities/xml/XmlEscaper.java | 76 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlEscaper.java diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNumber.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNumber.java index 6bf55ad87..bb552e634 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNumber.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonNumber.java @@ -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() { } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonObject.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonObject.java index d39bc6261..ff24e28c8 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonObject.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/model/JsonObject.java @@ -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"); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlEscaper.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlEscaper.java new file mode 100644 index 000000000..378642886 --- /dev/null +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlEscaper.java @@ -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("<"); + else if (c == '>') + write(">"); + else if (c == '&') + write("&"); + else if (c == '"') + write("""); + else + target.write((byte) i); + } + } + + private void write(String s) throws IOException { + target.write(s.getBytes(StandardCharsets.UTF_8)); + } + +} +