use of org.hl7.fhir.r4.formats.XmlParser in project org.hl7.fhir.core by hapifhir.
the class ToolsHelper method executeRoundTrip.
public void executeRoundTrip(String[] args) throws IOException, FHIRException {
FileInputStream in;
File source = new CSFile(args[1]);
File dest = new CSFile(args[2]);
if (args.length >= 4) {
Utilities.copyFile(args[1], args[3]);
}
if (!source.exists())
throw new FHIRException("Source File \"" + source.getAbsolutePath() + "\" not found");
in = new CSFileInputStream(source);
XmlParser p = new XmlParser();
JsonParser parser = new JsonParser();
JsonParser pj = parser;
Resource rf = p.parse(in);
ByteArrayOutputStream json = new ByteArrayOutputStream();
parser.setOutputStyle(OutputStyle.PRETTY);
parser.compose(json, rf);
json.close();
TextFile.stringToFile(new String(json.toByteArray()), Utilities.changeFileExt(dest.getAbsolutePath(), ".json"));
rf = pj.parse(new ByteArrayInputStream(json.toByteArray()));
FileOutputStream s = new FileOutputStream(dest);
new XmlParser().compose(s, rf, true);
s.close();
}
use of org.hl7.fhir.r4.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();
}
use of org.hl7.fhir.r4.formats.XmlParser in project org.hl7.fhir.core by hapifhir.
the class SimpleWorkerContext method loadFromFile.
public void loadFromFile(InputStream stream, String name, IContextResourceLoader loader, ILoadFilter filter) throws IOException, FHIRException {
Resource f;
try {
if (loader != null)
f = loader.loadBundle(stream, false);
else {
XmlParser xml = new XmlParser();
f = xml.parse(stream);
}
} catch (DataFormatException e1) {
throw new org.hl7.fhir.exceptions.FHIRFormatError(formatMessage(I18nConstants.ERROR_PARSING_, name, e1.getMessage()), e1);
} catch (Exception e1) {
throw new org.hl7.fhir.exceptions.FHIRFormatError(formatMessage(I18nConstants.ERROR_PARSING_, name, e1.getMessage()), e1);
}
if (f instanceof Bundle) {
Bundle bnd = (Bundle) f;
for (BundleEntryComponent e : bnd.getEntry()) {
if (e.getFullUrl() == null) {
logger.logDebugMessage(LogCategory.CONTEXT, "unidentified resource in " + name + " (no fullUrl)");
}
if (filter == null || filter.isOkToLoad(e.getResource())) {
String path = loader != null ? loader.getResourcePath(e.getResource()) : null;
if (path != null) {
e.getResource().setUserData("path", path);
}
cacheResource(e.getResource());
}
}
} else if (f instanceof CanonicalResource) {
if (filter == null || filter.isOkToLoad(f)) {
String path = loader != null ? loader.getResourcePath(f) : null;
if (path != null) {
f.setUserData("path", path);
}
cacheResource(f);
}
}
}
use of org.hl7.fhir.r4.formats.XmlParser in project org.hl7.fhir.core by hapifhir.
the class SimpleWorkerContext method allStructures.
@Override
public List<StructureDefinition> allStructures() {
List<StructureDefinition> result = new ArrayList<StructureDefinition>();
Set<StructureDefinition> set = new HashSet<StructureDefinition>();
for (StructureDefinition sd : listStructures()) {
if (!set.contains(sd)) {
try {
generateSnapshot(sd);
// new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "snapshot", tail(sd.getUrl())+".xml")), sd);
} catch (Exception e) {
System.out.println("Unable to generate snapshot for " + tail(sd.getUrl()) + " from " + tail(sd.getBaseDefinition()) + " because " + e.getMessage());
if (true) {
e.printStackTrace();
}
}
result.add(sd);
set.add(sd);
}
}
return result;
}
use of org.hl7.fhir.r4.formats.XmlParser in project org.hl7.fhir.core by hapifhir.
the class ByteUtils method resourceToByteArray.
public static <T extends Resource> byte[] resourceToByteArray(T resource, boolean pretty, boolean isJson) {
ByteArrayOutputStream baos = null;
byte[] byteArray = null;
try {
baos = new ByteArrayOutputStream();
IParser parser = null;
if (isJson) {
parser = new JsonParser();
} else {
parser = new XmlParser();
}
parser.setOutputStyle(pretty ? IParser.OutputStyle.PRETTY : IParser.OutputStyle.NORMAL);
parser.compose(baos, resource);
baos.close();
byteArray = baos.toByteArray();
baos.close();
} catch (Exception e) {
try {
baos.close();
} catch (Exception ex) {
throw new EFhirClientException("Error closing output stream", ex);
}
throw new EFhirClientException("Error converting output stream to byte array", e);
}
return byteArray;
}
Aggregations