use of org.hl7.fhir.dstu3.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.
the class TurtleTests method test_organization_example_f003_burgers_ENT.
@Test
public void test_organization_example_f003_burgers_ENT() throws FileNotFoundException, IOException, Exception {
System.out.println("organization-example-f003-burgers-ENT.ttl");
new Turtle().parse(TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\organization-example-f003-burgers-ENT.ttl"));
}
use of org.hl7.fhir.dstu3.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.
the class TurtleParser method parse.
private Element parse(Turtle src, TTLComplex cmp) throws FHIRException {
TTLObject type = cmp.getPredicates().get("http://www.w3.org/2000/01/rdf-schema#type");
if (type == null) {
logError(cmp.getLine(), cmp.getCol(), "(document)", IssueType.INVALID, context.formatMessage(I18nConstants.UNKNOWN_RESOURCE_TYPE_MISSING_RDFSTYPE), IssueSeverity.FATAL);
return null;
}
if (type instanceof TTLList) {
// this is actually broken - really we have to look through the structure definitions at this point
for (TTLObject obj : ((TTLList) type).getList()) {
if (obj instanceof TTLURL && ((TTLURL) obj).getUri().startsWith(FHIR_URI_BASE)) {
type = obj;
break;
}
}
}
if (!(type instanceof TTLURL)) {
logError(cmp.getLine(), cmp.getCol(), "(document)", IssueType.INVALID, context.formatMessage(I18nConstants.UNEXPECTED_DATATYPE_FOR_RDFSTYPE), IssueSeverity.FATAL);
return null;
}
String name = ((TTLURL) type).getUri();
String ns = name.substring(0, name.lastIndexOf("/"));
name = name.substring(name.lastIndexOf("/") + 1);
String path = "/" + name;
StructureDefinition sd = getDefinition(cmp.getLine(), cmp.getCol(), ns, name);
if (sd == null)
return null;
Element result = new Element(name, new Property(context, sd.getSnapshot().getElement().get(0), sd));
result.markLocation(cmp.getLine(), cmp.getCol());
result.setType(name);
parseChildren(src, path, cmp, result, false);
result.numberChildren();
return result;
}
use of org.hl7.fhir.dstu3.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.
the class TurtleParser method parse.
@Override
public List<NamedElement> parse(InputStream input) throws IOException, FHIRException {
List<NamedElement> res = new ArrayList<>();
Turtle src = new Turtle();
if (policy == ValidationPolicy.EVERYTHING) {
try {
src.parse(TextFile.streamToString(input));
} catch (Exception e) {
logError(-1, -1, "(document)", IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_TURTLE_, e.getMessage()), IssueSeverity.FATAL);
return null;
}
Element e = parse(src);
if (e != null) {
res.add(new NamedElement(null, e));
}
} else {
src.parse(TextFile.streamToString(input));
Element e = parse(src);
if (e != null) {
res.add(new NamedElement(null, e));
}
}
return res;
}
use of org.hl7.fhir.dstu3.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.
the class TurtleParser method parseResource.
private void parseResource(Turtle src, String npath, TTLComplex object, Element element, Property property, String name, TTLObject e) throws FHIRException {
TTLComplex obj;
if (e instanceof TTLComplex)
obj = (TTLComplex) e;
else if (e instanceof TTLURL) {
String url = ((TTLURL) e).getUri();
obj = src.getObject(url);
if (obj == null) {
logError(e.getLine(), e.getCol(), npath, IssueType.INVALID, context.formatMessage(I18nConstants.REFERENCE_TO__CANNOT_BE_RESOLVED, url), IssueSeverity.FATAL);
return;
}
} else
throw new FHIRFormatError(context.formatMessage(I18nConstants.WRONG_TYPE_FOR_RESOURCE));
TTLObject type = obj.getPredicates().get("http://www.w3.org/2000/01/rdf-schema#type");
if (type == null) {
logError(object.getLine(), object.getCol(), npath, IssueType.INVALID, context.formatMessage(I18nConstants.UNKNOWN_RESOURCE_TYPE_MISSING_RDFSTYPE), IssueSeverity.FATAL);
return;
}
if (type instanceof TTLList) {
// this is actually broken - really we have to look through the structure definitions at this point
for (TTLObject tobj : ((TTLList) type).getList()) {
if (tobj instanceof TTLURL && ((TTLURL) tobj).getUri().startsWith(FHIR_URI_BASE)) {
type = tobj;
break;
}
}
}
if (!(type instanceof TTLURL)) {
logError(object.getLine(), object.getCol(), npath, IssueType.INVALID, context.formatMessage(I18nConstants.UNEXPECTED_DATATYPE_FOR_RDFSTYPE), IssueSeverity.FATAL);
return;
}
String rt = ((TTLURL) type).getUri();
String ns = rt.substring(0, rt.lastIndexOf("/"));
rt = rt.substring(rt.lastIndexOf("/") + 1);
StructureDefinition sd = getDefinition(object.getLine(), object.getCol(), ns, rt);
if (sd == null)
return;
Element n = new Element(tail(name), property).markLocation(object.getLine(), object.getCol());
element.getChildren().add(n);
n.updateProperty(new Property(this.context, sd.getSnapshot().getElement().get(0), sd), SpecialElement.fromProperty(n.getProperty()), property);
n.setType(rt);
parseChildren(src, npath, obj, n, false);
}
use of org.hl7.fhir.dstu3.utils.formats.Turtle in project org.hl7.fhir.core by hapifhir.
the class NativeHostServices method convertResource.
/**
* Convert a resource to R4 from 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 of the content. one of r2, r3
* @return - the converted resource (or an exception if can't be converted)
* @throws FHIRException
* @throws IOException
*/
public byte[] convertResource(byte[] r, String fmt, String version) throws FHIRException, IOException {
try {
if (VersionUtilities.isR3Ver(version)) {
org.hl7.fhir.dstu3.formats.ParserBase p3 = org.hl7.fhir.dstu3.formats.FormatUtilities.makeParser(fmt);
org.hl7.fhir.dstu3.model.Resource res3 = p3.parse(r);
Resource res4 = VersionConvertorFactory_30_50.convertResource(res3);
org.hl7.fhir.r5.formats.ParserBase p4 = org.hl7.fhir.r5.formats.FormatUtilities.makeParser(fmt);
convertCount++;
return p4.composeBytes(res4);
} else if (VersionUtilities.isR2Ver(version)) {
org.hl7.fhir.dstu2.formats.ParserBase p2 = org.hl7.fhir.dstu2.formats.FormatUtilities.makeParser(fmt);
org.hl7.fhir.dstu2.model.Resource res2 = p2.parse(r);
Resource res4 = VersionConvertorFactory_10_50.convertResource(res2, conv_10_50_advisor);
org.hl7.fhir.r5.formats.ParserBase p4 = org.hl7.fhir.r5.formats.FormatUtilities.makeParser(fmt);
convertCount++;
return p4.composeBytes(res4);
} else if (VersionUtilities.isR2BVer(version)) {
org.hl7.fhir.dstu2016may.formats.ParserBase p2 = org.hl7.fhir.dstu2016may.formats.FormatUtilities.makeParser(fmt);
org.hl7.fhir.dstu2016may.model.Resource res2 = p2.parse(r);
Resource res4 = VersionConvertorFactory_14_50.convertResource(res2);
org.hl7.fhir.r5.formats.ParserBase p4 = org.hl7.fhir.r5.formats.FormatUtilities.makeParser(fmt);
convertCount++;
return p4.composeBytes(res4);
} else if (VersionUtilities.isR4Ver(version)) {
org.hl7.fhir.r4.formats.ParserBase p2 = org.hl7.fhir.r4.formats.FormatUtilities.makeParser(fmt);
org.hl7.fhir.r4.model.Resource res2 = p2.parse(r);
Resource res4 = VersionConvertorFactory_40_50.convertResource(res2);
org.hl7.fhir.r5.formats.ParserBase p4 = org.hl7.fhir.r5.formats.FormatUtilities.makeParser(fmt);
convertCount++;
return p4.composeBytes(res4);
} else
throw new FHIRException("Unsupported version " + version);
} catch (Exception e) {
exceptionCount++;
synchronized (lock) {
lastException = e.getMessage();
}
throw e;
}
}
Aggregations