Search in sources :

Example 11 with TTLURL

use of org.hl7.fhir.utilities.turtle.Turtle.TTLURL in project org.hl7.fhir.core by hapifhir.

the class TurtleParser method parseResource.

private void parseResource(Turtle src, String npath, TTLComplex object, Element context, 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, "reference to " + url + " cannot be resolved", IssueSeverity.FATAL);
            return;
        }
    } else
        throw new FHIRFormatError("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, "Unknown resource type (missing rdfs:type)", 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, "Unexpected datatype for rdfs:type)", 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());
    context.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.r4.utils.formats.Turtle.TTLComplex) StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) SpecialElement(org.hl7.fhir.r4.elementmodel.Element.SpecialElement) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) TTLURL(org.hl7.fhir.r4.utils.formats.Turtle.TTLURL) TTLObject(org.hl7.fhir.r4.utils.formats.Turtle.TTLObject) TTLList(org.hl7.fhir.r4.utils.formats.Turtle.TTLList)

Example 12 with TTLURL

use of org.hl7.fhir.utilities.turtle.Turtle.TTLURL in project org.hl7.fhir.core by hapifhir.

the class TurtleParser method parse.

private Element parse(Turtle src, TTLComplex cmp) throws FHIRFormatError, DefinitionException {
    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, "Unknown resource type (missing rdfs:type)", 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, "Unexpected datatype for rdfs:type)", 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.dstu3.model.StructureDefinition) SpecialElement(org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement) TTLURL(org.hl7.fhir.dstu3.utils.formats.Turtle.TTLURL) TTLObject(org.hl7.fhir.dstu3.utils.formats.Turtle.TTLObject) TTLList(org.hl7.fhir.dstu3.utils.formats.Turtle.TTLList)

Example 13 with TTLURL

use of org.hl7.fhir.utilities.turtle.Turtle.TTLURL in project org.hl7.fhir.core by hapifhir.

the class Turtle method parse.

private void parse(Lexer lexer) throws FHIRFormatError {
    boolean doPrefixes = true;
    while (!lexer.done()) {
        if (doPrefixes && (lexer.peek(LexerTokenType.TOKEN, "@") || lexer.peek(LexerTokenType.WORD, "PREFIX") || lexer.peek(LexerTokenType.WORD, "BASE"))) {
            boolean sparqlStyle = false;
            boolean base = false;
            if (lexer.peek(LexerTokenType.TOKEN, "@")) {
                lexer.token("@");
                String p = lexer.word();
                if (p.equals("base"))
                    base = true;
                else if (!p.equals("prefix"))
                    throw new FHIRFormatError("Unexpected token " + p);
            } else {
                sparqlStyle = true;
                String p = lexer.word();
                if (p.equals("BASE"))
                    base = true;
                else if (!p.equals("PREFIX"))
                    throw new FHIRFormatError("Unexpected token " + p);
            }
            String prefix = null;
            if (!base) {
                prefix = lexer.peekType() == LexerTokenType.WORD ? lexer.next(LexerTokenType.WORD, false) : null;
                lexer.token(":");
            }
            String url = lexer.next(LexerTokenType.URI, false);
            if (!sparqlStyle)
                lexer.token(".");
            if (!base)
                prefix(prefix, url);
            else if (this.base == null)
                this.base = url;
            else
                throw new FHIRFormatError("Duplicate @base");
        } else if (lexer.peekType() == LexerTokenType.URI) {
            doPrefixes = false;
            TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol);
            uri.setUri(lexer.uri());
            TTLComplex complex = parseComplex(lexer);
            objects.put(uri, complex);
            lexer.token(".");
        } else if (lexer.peekType() == LexerTokenType.WORD) {
            doPrefixes = false;
            TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol);
            String pfx = lexer.word();
            if (!prefixes.containsKey(pfx))
                throw new FHIRFormatError("Unknown prefix " + pfx);
            lexer.token(":");
            uri.setUri(prefixes.get(pfx) + lexer.word());
            TTLComplex complex = parseComplex(lexer);
            objects.put(uri, complex);
            lexer.token(".");
        } else if (lexer.peek(LexerTokenType.TOKEN, ":")) {
            doPrefixes = false;
            TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol);
            lexer.token(":");
            if (!prefixes.containsKey(null))
                throw new FHIRFormatError("Unknown prefix ''");
            uri.setUri(prefixes.get(null) + lexer.word());
            TTLComplex complex = parseComplex(lexer);
            objects.put(uri, complex);
            lexer.token(".");
        } else if (lexer.peek(LexerTokenType.TOKEN, "[")) {
            doPrefixes = false;
            lexer.token("[");
            TTLComplex bnode = parseComplex(lexer);
            lexer.token("]");
            TTLComplex complex = null;
            if (!lexer.peek(LexerTokenType.TOKEN, ".")) {
                complex = parseComplex(lexer);
                // at this point, we collapse bnode and complex, and give bnode a fictional identity
                bnode.addPredicates(complex.predicates);
            }
            objects.put(anonymousId(), bnode);
            lexer.token(".");
        } else
            throw lexer.error("Unknown token " + lexer.token);
    }
}
Also used : FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError)

Example 14 with TTLURL

use of org.hl7.fhir.utilities.turtle.Turtle.TTLURL in project org.hl7.fhir.core by hapifhir.

the class Turtle method parse.

private void parse(Lexer lexer) throws FHIRFormatError {
    boolean doPrefixes = true;
    while (!lexer.done()) {
        if (doPrefixes && (lexer.peek(LexerTokenType.TOKEN, "@") || lexer.peek(LexerTokenType.WORD, "PREFIX") || lexer.peek(LexerTokenType.WORD, "BASE"))) {
            boolean sparqlStyle = false;
            boolean base = false;
            if (lexer.peek(LexerTokenType.TOKEN, "@")) {
                lexer.token("@");
                String p = lexer.word();
                if (p.equals("base"))
                    base = true;
                else if (!p.equals("prefix"))
                    throw new FHIRFormatError("Unexpected token " + p);
            } else {
                sparqlStyle = true;
                String p = lexer.word();
                if (p.equals("BASE"))
                    base = true;
                else if (!p.equals("PREFIX"))
                    throw new FHIRFormatError("Unexpected token " + p);
            }
            String prefix = null;
            if (!base) {
                prefix = lexer.peekType() == LexerTokenType.WORD ? lexer.next(LexerTokenType.WORD, false) : null;
                lexer.token(":");
            }
            String url = lexer.next(LexerTokenType.URI, false);
            if (!sparqlStyle)
                lexer.token(".");
            if (!base)
                prefix(prefix, url);
            else if (this.base == null)
                this.base = url;
            else
                throw new FHIRFormatError("Duplicate @base");
        } else if (lexer.peekType() == LexerTokenType.URI) {
            doPrefixes = false;
            TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol);
            uri.setUri(lexer.uri());
            TTLComplex complex = parseComplex(lexer);
            objects.put(uri, complex);
            lexer.token(".");
        } else if (lexer.peekType() == LexerTokenType.WORD) {
            doPrefixes = false;
            TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol);
            String pfx = lexer.word();
            if (!prefixes.containsKey(pfx))
                throw new FHIRFormatError("Unknown prefix " + pfx);
            lexer.token(":");
            uri.setUri(prefixes.get(pfx) + lexer.word());
            TTLComplex complex = parseComplex(lexer);
            objects.put(uri, complex);
            lexer.token(".");
        } else if (lexer.peek(LexerTokenType.TOKEN, ":")) {
            doPrefixes = false;
            TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol);
            lexer.token(":");
            if (!prefixes.containsKey(null))
                throw new FHIRFormatError("Unknown prefix ''");
            uri.setUri(prefixes.get(null) + lexer.word());
            TTLComplex complex = parseComplex(lexer);
            objects.put(uri, complex);
            lexer.token(".");
        } else if (lexer.peek(LexerTokenType.TOKEN, "[")) {
            doPrefixes = false;
            lexer.token("[");
            TTLComplex bnode = parseComplex(lexer);
            lexer.token("]");
            TTLComplex complex = null;
            if (!lexer.peek(LexerTokenType.TOKEN, ".")) {
                complex = parseComplex(lexer);
                // at this point, we collapse bnode and complex, and give bnode a fictional identity
                bnode.addPredicates(complex.predicates);
            }
            objects.put(anonymousId(), bnode);
            lexer.token(".");
        } else
            throw lexer.error("Unknown token " + lexer.token);
    }
}
Also used : FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError)

Aggregations

FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)10 TTLList (org.hl7.fhir.utilities.turtle.Turtle.TTLList)4 TTLObject (org.hl7.fhir.utilities.turtle.Turtle.TTLObject)4 TTLURL (org.hl7.fhir.utilities.turtle.Turtle.TTLURL)4 SpecialElement (org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement)2 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)2 TTLList (org.hl7.fhir.dstu3.utils.formats.Turtle.TTLList)2 TTLObject (org.hl7.fhir.dstu3.utils.formats.Turtle.TTLObject)2 TTLURL (org.hl7.fhir.dstu3.utils.formats.Turtle.TTLURL)2 SpecialElement (org.hl7.fhir.r4.elementmodel.Element.SpecialElement)2 StructureDefinition (org.hl7.fhir.r4.model.StructureDefinition)2 TTLList (org.hl7.fhir.r4.utils.formats.Turtle.TTLList)2 TTLObject (org.hl7.fhir.r4.utils.formats.Turtle.TTLObject)2 TTLURL (org.hl7.fhir.r4.utils.formats.Turtle.TTLURL)2 SpecialElement (org.hl7.fhir.r4b.elementmodel.Element.SpecialElement)2 NamedElement (org.hl7.fhir.r4b.elementmodel.ParserBase.NamedElement)2 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)2 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)2 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)2 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)2