use of org.hl7.fhir.utilities.validation.ValidationMessage.Source in project org.hl7.fhir.core by hapifhir.
the class ToolsHelper method executeFragments.
public void executeFragments(String[] args) throws IOException {
try {
File source = new CSFile(args[1]);
if (!source.exists())
throw new FHIRException("Source File \"" + source.getAbsolutePath() + "\" not found");
XmlPullParser xpp = loadXml(new FileInputStream(source));
nextNoWhitespace(xpp);
if (!xpp.getName().equals("tests"))
throw new FHIRFormatError("Unable to parse file - starts with " + xpp.getName());
xpp.next();
nextNoWhitespace(xpp);
StringBuilder s = new StringBuilder();
s.append("<results>\r\n");
int fail = 0;
while (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("test")) {
String id = xpp.getAttributeValue(null, "id");
String type = xpp.getAttributeValue(null, "type");
// test
xpp.next();
nextNoWhitespace(xpp);
// pre
xpp.next();
nextNoWhitespace(xpp);
XmlParser p = new XmlParser();
try {
p.parseFragment(xpp, type);
s.append("<result id=\"" + id + "\" outcome=\"ok\"/>\r\n");
nextNoWhitespace(xpp);
} catch (Exception e) {
s.append("<result id=\"" + id + "\" outcome=\"error\" msg=\"" + Utilities.escapeXml(e.getMessage()) + "\"/>\r\n");
fail++;
}
while (xpp.getEventType() != XmlPullParser.END_TAG || !xpp.getName().equals("pre")) xpp.next();
xpp.next();
nextNoWhitespace(xpp);
xpp.next();
nextNoWhitespace(xpp);
}
s.append("</results>\r\n");
TextFile.stringToFile(s.toString(), args[2]);
} catch (Exception e) {
e.printStackTrace();
TextFile.stringToFile(e.getMessage(), args[2]);
}
}
use of org.hl7.fhir.utilities.validation.ValidationMessage.Source 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.utilities.validation.ValidationMessage.Source 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.utilities.validation.ValidationMessage.Source 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.utilities.validation.ValidationMessage.Source in project org.hl7.fhir.core by hapifhir.
the class RdfParser method composeImplementationGuideImplementationGuidePackageResourceComponent.
protected void composeImplementationGuideImplementationGuidePackageResourceComponent(Complex parent, String parentType, String name, ImplementationGuide.ImplementationGuidePackageResourceComponent element, int index) {
if (element == null)
return;
Complex t;
if (Utilities.noString(parentType))
t = parent;
else {
t = parent.predicate("fhir:" + parentType + '.' + name);
}
composeBackboneElement(t, "resource", name, element, index);
if (element.hasExampleElement())
composeBoolean(t, "ImplementationGuide", "example", element.getExampleElement(), -1);
if (element.hasNameElement())
composeString(t, "ImplementationGuide", "name", element.getNameElement(), -1);
if (element.hasDescriptionElement())
composeString(t, "ImplementationGuide", "description", element.getDescriptionElement(), -1);
if (element.hasAcronymElement())
composeString(t, "ImplementationGuide", "acronym", element.getAcronymElement(), -1);
if (element.hasSource())
composeType(t, "ImplementationGuide", "source", element.getSource(), -1);
if (element.hasExampleFor())
composeReference(t, "ImplementationGuide", "exampleFor", element.getExampleFor(), -1);
}
Aggregations