use of org.hl7.fhir.dstu2.utils.SimpleWorkerContext in project org.hl7.fhir.core by hapifhir.
the class ResourceChecker method checkIsResource.
// protected static Manager.FhirFormat checkIsResource(SimpleWorkerContext context, boolean debug, String path) throws IOException {
//
// if (Utilities.existsInList(ext, "json"))
// return Manager.FhirFormat.JSON;
// if (Utilities.existsInList(ext, "map"))
// return Manager.FhirFormat.TEXT;
// if (Utilities.existsInList(ext, "txt"))
// return Manager.FhirFormat.TEXT;
// if (Utilities.existsInList(ext, "jwt", "jws"))
// return Manager.FhirFormat.SHC;
//
// return checkIsResource(context, debug, TextFile.fileToBytes(path), path);
// }
public static Manager.FhirFormat checkIsResource(SimpleWorkerContext context, boolean debug, byte[] cnt, String filename, boolean guessFromExtension) {
System.out.println(" ..Detect format for " + filename);
if (cnt.length == 0) {
System.out.println(" " + filename + " is empty");
return null;
}
if (guessFromExtension) {
String ext = Utilities.getFileExtension(filename);
if (Utilities.existsInList(ext, "xml")) {
return FhirFormat.XML;
}
if (Utilities.existsInList(ext, "ttl")) {
return FhirFormat.TURTLE;
}
if (Utilities.existsInList(ext, "map")) {
return Manager.FhirFormat.TEXT;
}
if (Utilities.existsInList(ext, "jwt", "jws")) {
return Manager.FhirFormat.SHC;
}
if (Utilities.existsInList(ext, "json")) {
// no, we have to look inside, and decide.
try {
JsonObject json = JsonTrackingParser.parseJson(cnt);
if (json.has("verifiableCredential")) {
return FhirFormat.SHC;
}
} catch (Exception e) {
}
return FhirFormat.JSON;
}
if (Utilities.existsInList(ext, "txt")) {
try {
String src = TextFile.bytesToString(cnt);
if (src.startsWith("shc:/")) {
return FhirFormat.SHC;
}
} catch (Exception e) {
}
return Manager.FhirFormat.TEXT;
}
}
try {
Manager.parse(context, new ByteArrayInputStream(cnt), Manager.FhirFormat.JSON);
return Manager.FhirFormat.JSON;
} catch (Exception e) {
if (debug) {
System.out.println("Not JSON: " + e.getMessage());
}
}
try {
ValidatorUtils.parseXml(cnt);
return Manager.FhirFormat.XML;
} catch (Exception e) {
if (debug) {
System.out.println("Not XML: " + e.getMessage());
}
}
try {
Manager.parse(context, new ByteArrayInputStream(cnt), Manager.FhirFormat.TURTLE);
return Manager.FhirFormat.TURTLE;
} catch (Exception e) {
if (debug) {
System.out.println("Not Turtle: " + e.getMessage());
}
}
try {
String s = new String(cnt, StandardCharsets.UTF_8);
if (s.startsWith("shc:/"))
s = SHCParser.decodeQRCode(s);
JWT jwt = new SHCParser(context).decodeJWT(s);
return Manager.FhirFormat.SHC;
} catch (Exception e) {
if (debug) {
System.out.println("Not a smart health card: " + e.getMessage());
}
}
try {
new StructureMapUtilities(context, null, null).parse(TextFile.bytesToString(cnt), null);
return Manager.FhirFormat.TEXT;
} catch (Exception e) {
if (debug) {
System.out.println("Not Text: " + e.getMessage());
}
}
if (debug)
System.out.println(" .. not a resource: " + filename);
return null;
}
use of org.hl7.fhir.dstu2.utils.SimpleWorkerContext in project org.hl7.fhir.core by hapifhir.
the class ValidatorUtils method messagesToOutcome.
protected static OperationOutcome messagesToOutcome(List<ValidationMessage> messages, SimpleWorkerContext context, FHIRPathEngine fpe) throws IOException, FHIRException, EOperationOutcome {
OperationOutcome op = new OperationOutcome();
for (ValidationMessage vm : filterMessages(messages)) {
try {
fpe.parse(vm.getLocation());
} catch (Exception e) {
System.out.println("Internal error in location for message: '" + e.getMessage() + "', loc = '" + vm.getLocation() + "', err = '" + vm.getMessage() + "'");
}
op.getIssue().add(OperationOutcomeUtilities.convertToIssue(vm, op));
}
if (!op.hasIssue()) {
op.addIssue().setSeverity(OperationOutcome.IssueSeverity.INFORMATION).setCode(OperationOutcome.IssueType.INFORMATIONAL).getDetails().setText(context.formatMessage(I18nConstants.ALL_OK));
}
RenderingContext rc = new RenderingContext(context, null, null, "http://hl7.org/fhir", "", null, RenderingContext.ResourceRendererMode.END_USER);
RendererFactory.factory(op, rc).render(op);
return op;
}
use of org.hl7.fhir.dstu2.utils.SimpleWorkerContext in project org.hl7.fhir.core by hapifhir.
the class ValidationTestConvertor method main.
/**
* @param args
* @throws FHIRException
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException, FHIRException {
SimpleWorkerContext context = SimpleWorkerContext.fromPack("C:\\work\\org.hl7.fhir\\build\\publish\\validation-min.xml.zip");
for (File f : new File("C:\\work\\org.hl7.fhir\\build\\tests\\validation-examples").listFiles()) {
if (f.getAbsolutePath().endsWith(".xml")) {
File t = new File(Utilities.changeFileExt(f.getAbsolutePath(), ".ttl"));
if (!t.exists()) {
try {
System.out.print("Process " + f.getAbsolutePath());
Element e = Manager.parse(context, new FileInputStream(f), FhirFormat.XML);
Manager.compose(context, e, new FileOutputStream(t), FhirFormat.TURTLE, OutputStyle.PRETTY, null);
System.out.println(" .... success");
} catch (Exception e) {
System.out.println(" .... fail: " + e.getMessage());
}
}
}
if (f.getAbsolutePath().endsWith(".json")) {
if (!new File(Utilities.changeFileExt(f.getAbsolutePath(), ".ttl")).exists()) {
File t = new File(Utilities.changeFileExt(f.getAbsolutePath(), ".ttl"));
if (!t.exists()) {
try {
System.out.print("Process " + f.getAbsolutePath());
Element e = Manager.parse(context, new FileInputStream(f), FhirFormat.JSON);
Manager.compose(context, e, new FileOutputStream(t), FhirFormat.TURTLE, OutputStyle.PRETTY, null);
System.out.println(" .... success");
} catch (Exception e) {
System.out.println(" .... fail: " + e.getMessage());
}
}
}
}
}
}
use of org.hl7.fhir.dstu2.utils.SimpleWorkerContext in project org.hl7.fhir.core by hapifhir.
the class CDARoundTripTests method setUp.
@BeforeAll
public void setUp() throws Exception {
context = new SimpleWorkerContext();
FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
context.loadFromPackage(pcm.loadPackage("hl7.fhir.core", "current"), null, "StructureDefinition");
context.loadFromPackage(pcm.loadPackage("hl7.fhir.cda", "current"), null, "StructureDefinition");
}
use of org.hl7.fhir.dstu2.utils.SimpleWorkerContext in project org.hl7.fhir.core by hapifhir.
the class ConceptMapEngineTest method getConceptMapEngine.
@Nonnull
private ConceptMapEngine getConceptMapEngine(Collection<ConceptMap.SourceElementComponent> elements) throws IOException {
ConceptMap conceptMap = getConceptMap(elements);
SimpleWorkerContext simpleWorkerContext = mock(SimpleWorkerContext.class);
when(simpleWorkerContext.fetchResource(ConceptMap.class, CONCEPT_MAP_URL)).thenReturn(conceptMap);
return new ConceptMapEngine(simpleWorkerContext);
}
Aggregations