use of org.hl7.fhir.r4.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.
the class NativeHostServices method unConvertResource.
/**
* Convert a resource from R4 to the specified version
*
* @param r - the source of the resource to convert from
* @param fmt - the format of the content. one of XML, JSON, TURTLE
* @param version - the version to convert to. one of r2, r3
* @return - the converted resource (or an exception if can't be converted)
* @throws FHIRException
* @throws IOException
*/
public byte[] unConvertResource(byte[] r, String fmt, String version) throws FHIRException, IOException {
try {
if ("3.0".equals(version) || "3.0.1".equals(version) || "r3".equals(version)) {
org.hl7.fhir.r5.formats.ParserBase p4 = org.hl7.fhir.r5.formats.FormatUtilities.makeParser(fmt);
org.hl7.fhir.r5.model.Resource res4 = p4.parse(r);
org.hl7.fhir.dstu3.model.Resource res3 = VersionConvertorFactory_30_50.convertResource(res4);
org.hl7.fhir.dstu3.formats.ParserBase p3 = org.hl7.fhir.dstu3.formats.FormatUtilities.makeParser(fmt);
unConvertCount++;
return p3.composeBytes(res3);
} else if ("1.0".equals(version) || "1.0.2".equals(version) || "r2".equals(version)) {
org.hl7.fhir.r5.formats.ParserBase p4 = org.hl7.fhir.r5.formats.FormatUtilities.makeParser(fmt);
org.hl7.fhir.r5.model.Resource res4 = p4.parse(r);
org.hl7.fhir.dstu2.model.Resource res2 = VersionConvertorFactory_10_50.convertResource(res4, conv_10_50_advisor);
org.hl7.fhir.dstu2.formats.ParserBase p2 = org.hl7.fhir.dstu2.formats.FormatUtilities.makeParser(fmt);
unConvertCount++;
return p2.composeBytes(res2);
} else if ("1.4".equals(version) || "1.4.0".equals(version)) {
org.hl7.fhir.r5.formats.ParserBase p4 = org.hl7.fhir.r5.formats.FormatUtilities.makeParser(fmt);
org.hl7.fhir.r5.model.Resource res4 = p4.parse(r);
org.hl7.fhir.dstu2016may.model.Resource res2 = VersionConvertorFactory_14_50.convertResource(res4);
org.hl7.fhir.dstu2016may.formats.ParserBase p2 = org.hl7.fhir.dstu2016may.formats.FormatUtilities.makeParser(fmt);
unConvertCount++;
return p2.composeBytes(res2);
} else
throw new FHIRException("Unsupported version " + version);
} catch (Exception e) {
exceptionCount++;
synchronized (lock) {
lastException = e.getMessage();
}
throw e;
}
}
use of org.hl7.fhir.r4.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.
the class NativeHostServices method validateResource.
/**
* Validate a resource.
*
* Possible options:
* - id-optional : no resource id is required (default)
* - id-required : a resource id is required
* - id-prohibited : no resource id is allowed
* - any-extensions : allow extensions other than those defined by the encountered structure definitions
* - bp-ignore : ignore best practice recommendations (default)
* - bp-hint : treat best practice recommendations as a hint
* - bp-warning : treat best practice recommendations as a warning
* - bp-error : treat best practice recommendations as an error
* - display-ignore : ignore Coding.display and do not validate it (default)
* - display-check : check Coding.display - must be correct
* - display-case-space : check Coding.display but allow case and whitespace variation
* - display-case : check Coding.display but allow case variation
* - display-space : check Coding.display but allow whitespace variation
*
* @param location - a text description of the context of validation (for human consumers to help locate the problem - echoed into error messages)
* @param source - the bytes to validate
* @param cntType - the format of the content. one of XML, JSON, TURTLE
* @param options - a list of space separated options
* @return
* @throws Exception
*/
public byte[] validateResource(String location, byte[] source, String cntType, String options) throws Exception {
try {
IdStatus resourceIdRule = IdStatus.OPTIONAL;
boolean anyExtensionsAllowed = true;
BestPracticeWarningLevel bpWarnings = BestPracticeWarningLevel.Ignore;
CheckDisplayOption displayOption = CheckDisplayOption.Ignore;
for (String s : options.split(" ")) {
if ("id-optional".equalsIgnoreCase(s))
resourceIdRule = IdStatus.OPTIONAL;
else if ("id-required".equalsIgnoreCase(s))
resourceIdRule = IdStatus.REQUIRED;
else if ("id-prohibited".equalsIgnoreCase(s))
resourceIdRule = IdStatus.PROHIBITED;
else if ("any-extensions".equalsIgnoreCase(s))
// This is already the default
anyExtensionsAllowed = true;
else if ("strict-extensions".equalsIgnoreCase(s))
anyExtensionsAllowed = false;
else if ("bp-ignore".equalsIgnoreCase(s))
bpWarnings = BestPracticeWarningLevel.Ignore;
else if ("bp-hint".equalsIgnoreCase(s))
bpWarnings = BestPracticeWarningLevel.Hint;
else if ("bp-warning".equalsIgnoreCase(s))
bpWarnings = BestPracticeWarningLevel.Warning;
else if ("bp-error".equalsIgnoreCase(s))
bpWarnings = BestPracticeWarningLevel.Error;
else if ("display-ignore".equalsIgnoreCase(s))
displayOption = CheckDisplayOption.Ignore;
else if ("display-check".equalsIgnoreCase(s))
displayOption = CheckDisplayOption.Check;
else if ("display-case-space".equalsIgnoreCase(s))
displayOption = CheckDisplayOption.CheckCaseAndSpace;
else if ("display-case".equalsIgnoreCase(s))
displayOption = CheckDisplayOption.CheckCase;
else if ("display-space".equalsIgnoreCase(s))
displayOption = CheckDisplayOption.CheckSpace;
else if (!Utilities.noString(s))
throw new Exception("Unknown option " + s);
}
OperationOutcome oo = validator.validate(location, source, FhirFormat.valueOf(cntType), null, resourceIdRule, anyExtensionsAllowed, bpWarnings, displayOption);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
new XmlParser().compose(bs, oo);
validationCount++;
return bs.toByteArray();
} catch (Exception e) {
exceptionCount++;
synchronized (lock) {
lastException = e.getMessage();
}
throw e;
}
}
use of org.hl7.fhir.r4.utils.formats.Turtle 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.r4.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.
the class RdfParserBase method compose.
@Override
public void compose(OutputStream stream, Resource resource) throws IOException {
Turtle ttl = new Turtle();
// ttl.setFormat(FFormat);
ttl.prefix("fhir", "http://hl7.org/fhir/");
ttl.prefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
Section section = ttl.section("resource");
Subject subject;
if (url != null)
subject = section.triple("<" + url + ">", "a", "fhir:" + resource.getResourceType().toString());
else
subject = section.triple("[]", "a", "fhir:" + resource.getResourceType().toString());
composeResource(subject, resource);
try {
ttl.commit(stream, false);
} catch (Exception e) {
throw new IOException(e);
}
}
use of org.hl7.fhir.r4.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.
the class BinaryRenderer method ttl.
private void ttl(XhtmlNode x, Binary bin) {
String content = "\r\n" + getBinContentAsString(bin);
XhtmlNode pre = x.pre("rdf language-turtle");
pre.code(content);
}
Aggregations