use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class ToolsHelper method processExamples.
public void processExamples(String rootDir, Collection<String> list) throws FHIRException {
for (String n : list) {
try {
String filename = rootDir + n + ".xml";
// 1. produce canonical XML
CSFileInputStream source = new CSFileInputStream(filename);
FileOutputStream dest = new FileOutputStream(Utilities.changeFileExt(filename, ".canonical.xml"));
XmlParser p = new XmlParser();
Resource r = p.parse(source);
XmlParser cxml = new XmlParser();
cxml.setOutputStyle(OutputStyle.CANONICAL);
cxml.compose(dest, r);
// 2. produce JSON
source = new CSFileInputStream(filename);
dest = new FileOutputStream(Utilities.changeFileExt(filename, ".json"));
r = p.parse(source);
JsonParser json = new JsonParser();
json.setOutputStyle(OutputStyle.PRETTY);
json.compose(dest, r);
json = new JsonParser();
json.setOutputStyle(OutputStyle.CANONICAL);
dest = new FileOutputStream(Utilities.changeFileExt(filename, ".canonical.json"));
json.compose(dest, r);
// 2. produce JSON
dest = new FileOutputStream(Utilities.changeFileExt(filename, ".ttl"));
RdfParserBase rdf = new RdfParser();
rdf.compose(dest, r);
} catch (Exception e) {
e.printStackTrace();
throw new FHIRException("Error Processing " + n + ".xml: " + e.getMessage(), e);
}
}
}
use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class ToolsHelper method testRoundTrip.
public void testRoundTrip(String rootDir, String tmpDir, Collection<String> names) throws Throwable {
try {
System.err.println("Round trip from " + rootDir + " to " + tmpDir + ":" + Integer.toString(names.size()) + " files");
for (String n : names) {
System.err.print(" " + n);
String source = rootDir + n + ".xml";
// String tmpJson = tmpDir + n + ".json";
String tmp = tmpDir + n.replace(File.separator, "-") + ".tmp";
String dest = tmpDir + n.replace(File.separator, "-") + ".java.xml";
FileInputStream in = new FileInputStream(source);
XmlParser xp = new XmlParser();
Resource r = xp.parse(in);
System.err.print(".");
JsonParser jp = new JsonParser();
FileOutputStream out = new FileOutputStream(tmp);
jp.setOutputStyle(OutputStyle.PRETTY);
jp.compose(out, r);
out.close();
r = null;
System.err.print(".");
in = new FileInputStream(tmp);
System.err.print(",");
r = jp.parse(in);
System.err.print(".");
out = new FileOutputStream(dest);
new XmlParser().compose(out, r, true);
System.err.println("!");
out.close();
r = null;
System.gc();
}
} catch (Throwable e) {
System.err.println("Error: " + e.getMessage());
throw e;
}
}
use of org.hl7.fhir.r5.elementmodel.JsonParser 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.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class IntegrityChecker method dumpSD.
private void dumpSD(FileWriter w) throws FHIRFormatError, IOException {
Map<String, StructureDefinition> map = new HashMap<>();
for (String sdn : npm.listResources("StructureDefinition")) {
InputStream s = npm.load(sdn);
StructureDefinition sd = (StructureDefinition) new JsonParser().parse(s);
map.put(sd.getUrl(), sd);
}
msg("Loaded " + map.size() + " Structures");
List<String> structures = new ArrayList<>();
for (StructureDefinition sd : map.values()) {
structures.add(sd.getUrl());
}
Collections.sort(structures);
for (String sdn : structures) {
dumpSD(map.get(sdn), map, w);
}
}
use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.
the class IntegrityChecker method checkSP.
private void checkSP() throws IOException {
List<SearchParameter> list = new ArrayList<>();
for (String sdn : npm.listResources("SearchParameter")) {
InputStream s = npm.load(sdn);
SearchParameter sp = (SearchParameter) new JsonParser().parse(s);
list.add(sp);
}
msg("Loaded " + list.size() + " resources");
Map<String, SearchParameterNode> map = new HashMap<>();
for (SearchParameter sp : list) {
for (CodeType c : sp.getBase()) {
String s = c.primitiveValue();
if (!map.containsKey(s)) {
map.put(s, new SearchParameterNode(s));
}
addNode(sp, sp.getBase().size() == 1, map.get(s));
}
}
for (SearchParameterNode node : sort(map.values())) {
dump(node);
}
}
Aggregations