Search in sources :

Example 1 with TTLComplex

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

the class TurtleParser method parseChildren.

private void parseChildren(Turtle src, String path, TTLComplex object, Element context, boolean primitive) throws FHIRFormatError, DefinitionException {
    List<Property> properties = context.getProperty().getChildProperties(context.getName(), null);
    Set<String> processed = new HashSet<String>();
    if (primitive)
        processed.add(FHIR_URI_BASE + "value");
    // first pass: process the properties
    for (Property property : properties) {
        if (property.isChoice()) {
            for (TypeRefComponent type : property.getDefinition().getType()) {
                String eName = property.getName().substring(0, property.getName().length() - 3) + Utilities.capitalize(type.getCode());
                parseChild(src, object, context, processed, property, path, getFormalName(property, eName));
            }
        } else {
            parseChild(src, object, context, processed, property, path, getFormalName(property));
        }
    }
    // second pass: check for things not processed
    if (policy != ValidationPolicy.NONE) {
        for (String u : object.getPredicates().keySet()) {
            if (!processed.contains(u)) {
                TTLObject n = object.getPredicates().get(u);
                logError(n.getLine(), n.getCol(), path, IssueType.STRUCTURE, "Unrecognised predicate '" + u + "'", IssueSeverity.ERROR);
            }
        }
    }
}
Also used : TypeRefComponent(org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent) TTLObject(org.hl7.fhir.dstu3.utils.formats.Turtle.TTLObject) HashSet(java.util.HashSet)

Example 2 with TTLComplex

use of org.hl7.fhir.utilities.turtle.Turtle.TTLComplex 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 FHIRFormatError, DefinitionException {
    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.dstu3.utils.formats.Turtle.TTLComplex) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) SpecialElement(org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) 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 3 with TTLComplex

use of org.hl7.fhir.utilities.turtle.Turtle.TTLComplex 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 4 with TTLComplex

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

the class Turtle method parseComplex.

private TTLComplex parseComplex(Lexer lexer) throws FHIRFormatError {
    TTLComplex result = new TTLComplex(lexer.startLine, lexer.startCol);
    boolean done = lexer.peek(LexerTokenType.TOKEN, "]");
    while (!done) {
        String uri = null;
        if (lexer.peekType() == LexerTokenType.URI)
            uri = lexer.uri();
        else {
            String t = lexer.peekType() == LexerTokenType.WORD ? lexer.word() : null;
            if (lexer.type == LexerTokenType.TOKEN && lexer.token.equals(":")) {
                lexer.token(":");
                if (!prefixes.containsKey(t))
                    throw new FHIRFormatError("unknown prefix " + t);
                uri = prefixes.get(t) + lexer.word();
            } else if (t.equals("a"))
                uri = prefixes.get("rdfs") + "type";
            else
                throw lexer.error("unexpected token");
        }
        boolean inlist = false;
        if (lexer.peek(LexerTokenType.TOKEN, "(")) {
            inlist = true;
            lexer.token("(");
        }
        boolean rpt = false;
        do {
            if (lexer.peek(LexerTokenType.TOKEN, "[")) {
                lexer.token("[");
                result.addPredicate(uri, parseComplex(lexer));
                lexer.token("]");
            } else if (lexer.peekType() == LexerTokenType.URI) {
                TTLURL u = new TTLURL(lexer.startLine, lexer.startCol);
                u.setUri(lexer.uri());
                result.addPredicate(uri, u);
            } else if (lexer.peekType() == LexerTokenType.LITERAL) {
                TTLLiteral u = new TTLLiteral(lexer.startLine, lexer.startCol);
                u.value = lexer.literal();
                if (lexer.peek(LexerTokenType.TOKEN, "^")) {
                    lexer.token("^");
                    lexer.token("^");
                    if (lexer.peekType() == LexerTokenType.URI) {
                        u.type = lexer.uri();
                    } else {
                        String l = lexer.word();
                        lexer.token(":");
                        u.type = prefixes.get(l) + lexer.word();
                    }
                }
                if (lexer.peek(LexerTokenType.TOKEN, "@")) {
                    // lang tag - skip it
                    lexer.token("@");
                    String lang = lexer.word();
                    if (!lang.matches(LANG_REGEX)) {
                        throw new FHIRFormatError("Invalid Language tag " + lang);
                    }
                }
                result.addPredicate(uri, u);
            } else if (lexer.peekType() == LexerTokenType.WORD || lexer.peek(LexerTokenType.TOKEN, ":")) {
                int sl = lexer.startLine;
                int sc = lexer.startCol;
                String pfx = lexer.peekType() == LexerTokenType.WORD ? lexer.word() : null;
                if (Utilities.isDecimal(pfx, true) && !lexer.peek(LexerTokenType.TOKEN, ":")) {
                    TTLLiteral u = new TTLLiteral(sl, sc);
                    u.value = pfx;
                    result.addPredicate(uri, u);
                } else if (("false".equals(pfx) || "true".equals(pfx)) && !lexer.peek(LexerTokenType.TOKEN, ":")) {
                    TTLLiteral u = new TTLLiteral(sl, sc);
                    u.value = pfx;
                    result.addPredicate(uri, u);
                } else {
                    if (!prefixes.containsKey(pfx))
                        throw new FHIRFormatError("Unknown prefix " + (pfx == null ? "''" : pfx));
                    TTLURL u = new TTLURL(sl, sc);
                    lexer.token(":");
                    u.setUri(prefixes.get(pfx) + lexer.word());
                    result.addPredicate(uri, u);
                }
            } else if (!lexer.peek(LexerTokenType.TOKEN, ";") && (!inlist || !lexer.peek(LexerTokenType.TOKEN, ")"))) {
                throw new FHIRFormatError("unexpected token " + lexer.token);
            }
            if (inlist)
                rpt = !lexer.peek(LexerTokenType.TOKEN, ")");
            else {
                rpt = lexer.peek(LexerTokenType.TOKEN, ",");
                if (rpt)
                    lexer.readNext(false);
            }
        } while (rpt);
        if (inlist)
            lexer.token(")");
        if (lexer.peek(LexerTokenType.TOKEN, ";")) {
            while ((lexer.peek(LexerTokenType.TOKEN, ";"))) lexer.token(";");
            done = lexer.peek(LexerTokenType.TOKEN, ".") || lexer.peek(LexerTokenType.TOKEN, "]");
        } else {
            done = true;
        }
    }
    return result;
}
Also used : FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError)

Example 5 with TTLComplex

use of org.hl7.fhir.utilities.turtle.Turtle.TTLComplex 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)

Aggregations

FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)10 TTLObject (org.hl7.fhir.utilities.turtle.Turtle.TTLObject)8 TTLList (org.hl7.fhir.utilities.turtle.Turtle.TTLList)6 HashSet (java.util.HashSet)4 TTLObject (org.hl7.fhir.dstu3.utils.formats.Turtle.TTLObject)4 TTLObject (org.hl7.fhir.r4.utils.formats.Turtle.TTLObject)4 TTLURL (org.hl7.fhir.utilities.turtle.Turtle.TTLURL)4 TTLList (org.hl7.fhir.dstu3.utils.formats.Turtle.TTLList)3 TTLList (org.hl7.fhir.r4.utils.formats.Turtle.TTLList)3 SpecialElement (org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement)2 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)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 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