Search in sources :

Example 6 with Turtle

use of org.hl7.fhir.r4.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"));
}
Also used : Turtle(org.hl7.fhir.dstu3.utils.formats.Turtle) Test(org.junit.jupiter.api.Test)

Example 7 with Turtle

use of org.hl7.fhir.r4.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;
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) TTLURL(org.hl7.fhir.utilities.turtle.Turtle.TTLURL) TTLObject(org.hl7.fhir.utilities.turtle.Turtle.TTLObject) TTLList(org.hl7.fhir.utilities.turtle.Turtle.TTLList)

Example 8 with Turtle

use of org.hl7.fhir.r4.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;
}
Also used : Turtle(org.hl7.fhir.utilities.turtle.Turtle) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) ArrayList(java.util.ArrayList) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 9 with Turtle

use of org.hl7.fhir.r4.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);
}
Also used : TTLComplex(org.hl7.fhir.utilities.turtle.Turtle.TTLComplex) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) TTLURL(org.hl7.fhir.utilities.turtle.Turtle.TTLURL) TTLObject(org.hl7.fhir.utilities.turtle.Turtle.TTLObject) TTLList(org.hl7.fhir.utilities.turtle.Turtle.TTLList)

Example 10 with Turtle

use of org.hl7.fhir.r4.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;
    }
}
Also used : Resource(org.hl7.fhir.r5.model.Resource) FHIRException(org.hl7.fhir.exceptions.FHIRException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

Turtle (org.hl7.fhir.dstu3.utils.formats.Turtle)337 Test (org.junit.jupiter.api.Test)334 IOException (java.io.IOException)13 Turtle (org.hl7.fhir.utilities.turtle.Turtle)8 TTLObject (org.hl7.fhir.utilities.turtle.Turtle.TTLObject)8 FHIRException (org.hl7.fhir.exceptions.FHIRException)6 TTLList (org.hl7.fhir.utilities.turtle.Turtle.TTLList)6 HashSet (java.util.HashSet)4 TTLObject (org.hl7.fhir.dstu3.utils.formats.Turtle.TTLObject)4 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)4 TTLObject (org.hl7.fhir.r4.utils.formats.Turtle.TTLObject)4 SpecialElement (org.hl7.fhir.r4b.elementmodel.Element.SpecialElement)4 NamedElement (org.hl7.fhir.r4b.elementmodel.ParserBase.NamedElement)4 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)4 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)4 Section (org.hl7.fhir.utilities.turtle.Turtle.Section)4 Subject (org.hl7.fhir.utilities.turtle.Turtle.Subject)4 TTLURL (org.hl7.fhir.utilities.turtle.Turtle.TTLURL)4 FileNotFoundException (java.io.FileNotFoundException)3 SpecialElement (org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement)3