use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class R5ToR5Loader method loadBundle.
@Override
public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException {
Resource r5 = null;
if (isJson)
r5 = new JsonParser().parse(stream);
else
r5 = new XmlParser().parse(stream);
Bundle b;
if (r5 instanceof Bundle)
b = (Bundle) r5;
else {
b = new Bundle();
b.setId(UUID.randomUUID().toString().toLowerCase());
b.setType(BundleType.COLLECTION);
b.addEntry().setResource(r5).setFullUrl(r5 instanceof CanonicalResource ? ((CanonicalResource) r5).getUrl() : null);
}
for (CodeSystem cs : cslist) {
BundleEntryComponent be = b.addEntry();
be.setFullUrl(cs.getUrl());
be.setResource(cs);
}
if (killPrimitives) {
List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>();
for (BundleEntryComponent be : b.getEntry()) {
if (be.hasResource() && be.getResource() instanceof StructureDefinition) {
StructureDefinition sd = (StructureDefinition) be.getResource();
if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE)
remove.add(be);
}
}
b.getEntry().removeAll(remove);
}
if (patchUrls) {
for (BundleEntryComponent be : b.getEntry()) {
if (be.hasResource() && be.getResource() instanceof StructureDefinition) {
StructureDefinition sd = (StructureDefinition) be.getResource();
sd.setUrl(sd.getUrl().replace(URL_BASE, URL_R4));
sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
for (ElementDefinition ed : sd.getSnapshot().getElement()) patchUrl(ed);
for (ElementDefinition ed : sd.getDifferential().getElement()) patchUrl(ed);
}
}
}
return b;
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class CountryCodesConverter method execute.
private void execute() throws ParserConfigurationException, SAXException, IOException {
Document src = load();
CodeSystem cs1 = new CodeSystem();
CodeSystem cs2 = new CodeSystem();
CodeSystem cs3 = new CodeSystem();
setMetadata(src, cs1, "iso3166", "urn:iso:std:iso:3166", "", "");
setMetadata(src, cs2, "iso3166-2", "urn:iso:std:iso:3166:-2", "Part2", " Part 2");
cs1.addProperty().setCode("canonical").setDescription("The 2 letter code that identifies the same country (so 2/3/numeric codes can be aligned)").setType(PropertyType.CODE);
cs2.addProperty().setCode("country").setDescription("The 2 letter code that identifies the country for the subdivision").setType(PropertyType.CODE);
for (Element e : XMLUtil.getNamedChildren(src.getDocumentElement(), "country")) {
System.out.println(e.getAttribute("id"));
String c2 = XMLUtil.getNamedChildText(e, "alpha-2-code");
String c3 = XMLUtil.getNamedChildText(e, "alpha-3-code");
String cN = XMLUtil.getNamedChildText(e, "numeric-code");
Element n = XMLUtil.getNamedChildByAttribute(e, "short-name", "lang3code", "eng");
if (n == null)
n = XMLUtil.getNamedChildByAttribute(e, "short-name-upper-case", "lang3code", "eng");
if (n == null)
continue;
String name = n.getTextContent();
n = XMLUtil.getNamedChildByAttribute(e, "full-name", "lang3code", "eng");
if (n == null)
n = XMLUtil.getNamedChildByAttribute(e, "full-name-upper-case", "lang3code", "eng");
if (n == null)
n = XMLUtil.getNamedChildByAttribute(e, "short-name", "lang3code", "eng");
if (n == null)
n = XMLUtil.getNamedChildByAttribute(e, "short-name-upper-case", "lang3code", "eng");
String desc = n.getTextContent();
ConceptDefinitionComponent cc = cs1.addConcept();
cc.setCode(c2);
cc.setDisplay(name);
cc.setDefinition(desc);
poplang(e, cc);
if (c3 != null) {
cc = cs1.addConcept();
cc.setCode(c3);
cc.setDisplay(name);
cc.setDefinition(desc);
cc.addProperty().setCode("canonical").setValue(new CodeType(c2));
poplang(e, cc);
}
if (cN != null) {
cc = cs1.addConcept();
cc.setCode(cN);
cc.setDisplay(name);
cc.setDefinition(desc);
cc.addProperty().setCode("canonical").setValue(new CodeType(c2));
poplang(e, cc);
}
for (Element sd : XMLUtil.getNamedChildren(e, "subdivision")) {
cc = cs2.addConcept();
cc.setCode(XMLUtil.getNamedChildText(sd, "subdivision-code"));
Element l = XMLUtil.getNamedChild(sd, "subdivision-locale");
cc.setDisplay(XMLUtil.getNamedChildText(l, "subdivision-locale-name"));
cc.addProperty().setCode("country").setValue(new CodeType(c2));
}
}
cs1.setCount(cs1.getConcept().size());
cs2.setCount(cs2.getConcept().size());
throw new Error("Needs revisiting");
// new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "4.0.1", "package", "CodeSstem-iso3166.json")), cs1);
// new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "3.0.2", "package", "CodeSstem-iso3166.json")), cs1); // format hasn't changed
// new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "4.0.1", "package", "CodeSstem-iso3166-2.json")), cs2);
// new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "3.0.2", "package", "CodeSstem-iso3166-2.json")), cs2); // format hasn't changed
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class ArgonautConverter method saveResource.
private void saveResource(Resource resource, String extraType) throws Exception {
if (!WANT_SAVE)
return;
DomainResource dr = null;
if (resource instanceof DomainResource) {
dr = (DomainResource) resource;
if (!dr.hasText()) {
NarrativeGenerator generator = new NarrativeGenerator("", "", context);
generator.generate(dr);
}
}
XmlParser xparser = new XmlParser();
xparser.setOutputStyle(OutputStyle.PRETTY);
JsonParser jparser = new JsonParser();
jparser.setOutputStyle(OutputStyle.PRETTY);
ByteArrayOutputStream ba = new ByteArrayOutputStream();
xparser.compose(ba, resource);
ba.close();
byte[] srcX = ba.toByteArray();
ba = new ByteArrayOutputStream();
jparser.compose(ba, resource);
ba.close();
byte[] srcJ = ba.toByteArray();
String rn = resource.getResourceType().toString();
if (extraType != null)
rn = rn + extraType;
zipX.addBytes(resource.getId() + ".xml", srcX, false);
zipJ.addBytes(resource.getId() + ".json", srcJ, false);
if (!zipsX.containsKey(rn)) {
zipsX.put(rn, new ZipGenerator(Utilities.path(destFolder, "xml/type", rn + ".xml.zip")));
zipsJ.put(rn, new ZipGenerator(Utilities.path(destFolder, "json/type", rn + ".json.zip")));
stats.put(rn, new Stats());
}
zipsJ.get(rn).addBytes(resource.getId() + ".json", srcJ, false);
zipsX.get(rn).addBytes(resource.getId() + ".xml", srcX, false);
Stats ss = stats.get(rn);
ss.setInstances(ss.getInstances() + 1);
String profile = resource.getUserString("profile");
validate(srcX, profile, resource, ss);
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class BatchLoader method main.
public static void main(String[] args) throws IOException, Exception {
if (args.length < 4) {
System.out.println("Batch uploader takes 4 parameters in order: server base url, file/folder to upload, xml/json, and batch size");
} else {
String server = args[0];
String file = args[1];
// args[2].equals("json") ? new JsonParser() : new XmlParser();
IParser p = new JsonParser();
int size = Integer.parseInt(args[3]);
size = 500;
if (file.endsWith(".xml")) {
throw new FHIRException("Unimplemented file type " + file);
} else if (file.endsWith(".json")) {
throw new FHIRException("Unimplemented file type " + file);
} else if (file.endsWith(".zip")) {
LoadZipFile(server, file, p, size, 0, -1);
} else if (new File(file).isDirectory()) {
LoadDirectory(server, file, p, size);
} else
throw new FHIRException("Unknown file type " + file);
}
}
use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.
the class R2016MayToR5Loader method loadResource.
@Override
public org.hl7.fhir.r5.model.Resource loadResource(InputStream stream, boolean isJson) throws FHIRException, IOException {
Resource r2016may = null;
if (isJson)
r2016may = new JsonParser().parse(stream);
else
r2016may = new XmlParser().parse(stream);
org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_14_50.convertResource(r2016may);
setPath(r5);
if (!advisor.getCslist().isEmpty()) {
throw new FHIRException("Error: Cannot have included code systems");
}
if (killPrimitives) {
throw new FHIRException("Cannot kill primitives when using deferred loading");
}
if (patchUrls) {
if (r5 instanceof StructureDefinition) {
StructureDefinition sd = (StructureDefinition) r5;
sd.setUrl(sd.getUrl().replace(URL_BASE, URL_R4));
sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
for (ElementDefinition ed : sd.getSnapshot().getElement()) patchUrl(ed);
for (ElementDefinition ed : sd.getDifferential().getElement()) patchUrl(ed);
}
}
return r5;
}
Aggregations