use of org.hl7.fhir.r4b.formats.XmlParser 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.XmlParser 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.XmlParser in project org.hl7.fhir.core by hapifhir.
the class ADLImporter method execute.
private void execute() throws Exception {
// load config
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
adlConfig = builder.parse(new FileInputStream(config)).getDocumentElement();
// load ADL
builder = factory.newDocumentBuilder();
adl = builder.parse(new FileInputStream(source)).getDocumentElement();
check("root", adl.getNamespaceURI(), "http://schemas.openehr.org/v1", "Wrong namespace for ADL XML");
check("root", adl.getNodeName(), "archetype", "Wrong XML for ADL XML");
check("root", XMLUtil.getNamedChild(adl, "adl_version").getTextContent(), "1.4", "unsupported ADL version");
String id = XMLUtil.getFirstChild(XMLUtil.getNamedChild(adl, "archetype_id")).getTextContent().split("\\.")[1];
// create structure definition
StructureDefinition sd = new StructureDefinition();
sd.setId(id);
// populate metadata
Element description = XMLUtil.getNamedChild(adl, "description");
Element details = XMLUtil.getNamedChild(description, "details");
sd.setDescription(XMLUtil.getNamedChild(details, "purpose").getTextContent());
sd.setCopyright(XMLUtil.getNamedChild(details, "copyright").getTextContent());
sd.setPurpose("Use:\r\n" + XMLUtil.getNamedChild(details, "use").getTextContent() + "\r\n\r\nMisuse:\r\n" + XMLUtil.getNamedChild(details, "misuse").getTextContent());
List<Element> set = new ArrayList<Element>();
XMLUtil.getNamedChildren(details, "keywords", set);
for (Element e : set) sd.addKeyword().setDisplay(e.getTextContent());
String status = XMLUtil.getNamedChild(description, "lifecycle_state").getTextContent();
if ("CommitteeDraft".equals(status) || "AuthorDraft".equals(status))
sd.setStatus(PublicationStatus.DRAFT);
else
throw new Exception("Unknown life cycle state " + XMLUtil.getNamedChild(description, "lifecycle_state").getTextContent());
// load texts from ontology
Element ontology = XMLUtil.getNamedChild(adl, "ontology");
Element term_definitions = XMLUtil.getNamedChild(ontology, "term_definitions");
set.clear();
XMLUtil.getNamedChildren(term_definitions, "items", set);
for (Element item : set) {
processTextItem(item);
}
// load data and protocol
Element definition = XMLUtil.getNamedChild(adl, "definition");
NodeTreeEntry root = new NodeTreeEntry();
root.setTypeName(XMLUtil.getNamedChild(definition, "rm_type_name").getTextContent());
root.setAtCode(XMLUtil.getNamedChild(definition, "node_id").getTextContent());
root.setName(generateToken(root.getAtCode(), true));
sd.setName(root.getName());
root.setCardinality(readCardinality("root", XMLUtil.getNamedChild(definition, "occurrences")));
set.clear();
XMLUtil.getNamedChildren(definition, "attributes", set);
for (Element item : set) {
// we're actually skipping this level - we don't care about data protocol etc.
// XMLUtil.getNamedChild(XMLUtil.getNamedChild(item, "children"), "attributes");
Element attributes = item;
loadChildren(root.getAtCode(), root, attributes);
}
dumpChildren("", root);
genElements(sd, root.getName(), root);
// save
new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), sd);
System.out.println("done. saved as " + dest);
}
use of org.hl7.fhir.r4b.formats.XmlParser 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.XmlParser in project org.hl7.fhir.core by hapifhir.
the class LoincToDEConvertor method saveBundle.
private void saveBundle() throws FHIRFormatError, IOException, XmlPullParserException {
XmlParser xml = new XmlParser();
FileOutputStream s = new FileOutputStream(dest);
xml.compose(s, bundle, true);
s.close();
}
Aggregations