mirror of
https://github.com/hapifhir/org.hl7.fhir.core.git
synced 2025-02-09 06:14:45 +00:00
more json and xml utils
This commit is contained in:
parent
d60b9009b2
commit
70a450bf01
@ -14,6 +14,10 @@ public class JsonNumber extends JsonPrimitive {
|
|||||||
this.value = Integer.toString(value);
|
this.value = Integer.toString(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JsonNumber(long value) {
|
||||||
|
this.value = Long.toString(value);
|
||||||
|
}
|
||||||
|
|
||||||
private JsonNumber() {
|
private JsonNumber() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,6 +66,13 @@ public class JsonObject extends JsonElement {
|
|||||||
return add(name, new JsonNumber(value));
|
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 {
|
public JsonObject set(String name, JsonElement value) throws JsonException {
|
||||||
check(name != null, "Name is null");
|
check(name != null, "Name is null");
|
||||||
check(value != null, "Value is null");
|
check(value != null, "Value is null");
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user