Search in sources :

Example 21 with FHIRLexerException

use of org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException in project org.hl7.fhir.core by hapifhir.

the class FHIRLexer method takeDottedToken.

public String takeDottedToken() throws FHIRLexerException {
    StringBuilder b = new StringBuilder();
    b.append(take());
    while (!done() && getCurrent().equals(".")) {
        b.append(take());
        b.append(take());
    }
    return b.toString();
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder)

Example 22 with FHIRLexerException

use of org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException in project org.hl7.fhir.core by hapifhir.

the class InstanceValidator method checkInvariant.

public void checkInvariant(ValidatorHostContext hostContext, List<ValidationMessage> errors, String path, StructureDefinition profile, Element resource, Element element, ElementDefinitionConstraintComponent inv) throws FHIRException {
    // if (debug) {
    // System.out.println("inv "+inv.getKey()+" on "+path+" in "+resource.fhirType()+" {{ "+inv.getExpression()+" }}");
    // }
    ExpressionNode n = (ExpressionNode) inv.getUserData("validator.expression.cache");
    if (n == null) {
        long t = System.nanoTime();
        try {
            n = fpe.parse(fixExpr(inv.getExpression(), inv.getKey()));
        } catch (FHIRLexerException e) {
            rule(errors, IssueType.INVARIANT, element.line(), element.col(), path, false, I18nConstants.PROBLEM_PROCESSING_EXPRESSION__IN_PROFILE__PATH__, inv.getExpression(), profile.getUrl(), path, e.getMessage());
            return;
        }
        timeTracker.fpe(t);
        inv.setUserData("validator.expression.cache", n);
    }
    String msg;
    boolean ok;
    try {
        long t = System.nanoTime();
        ok = fpe.evaluateToBoolean(hostContext, resource, hostContext.getRootResource(), element, n);
        timeTracker.fpe(t);
        msg = fpe.forLog();
    } catch (Exception ex) {
        ok = false;
        msg = ex.getMessage();
    }
    if (!ok) {
        if (!Utilities.noString(msg)) {
            msg = "'" + inv.getHuman() + "' (" + msg + ")";
        } else if (wantInvariantInMessage) {
            msg = "'" + inv.getHuman() + "'  [" + n.toString() + "]";
        } else {
            msg = context.formatMessage(I18nConstants.INV_FAILED, "'" + inv.getHuman() + "'");
        }
        if (inv.hasExtension("http://hl7.org/fhir/StructureDefinition/elementdefinition-bestpractice") && ToolingExtensions.readBooleanExtension(inv, "http://hl7.org/fhir/StructureDefinition/elementdefinition-bestpractice")) {
            if (bpWarnings == BestPracticeWarningLevel.Hint)
                hint(errors, IssueType.INVARIANT, element.line(), element.col(), path, ok, inv.getKey() + ": " + msg);
            else if (bpWarnings == BestPracticeWarningLevel.Warning)
                warning(errors, IssueType.INVARIANT, element.line(), element.col(), path, ok, inv.getKey() + ": '" + inv.getHuman() + "' " + msg);
            else if (bpWarnings == BestPracticeWarningLevel.Error)
                rule(errors, IssueType.INVARIANT, element.line(), element.col(), path, ok, inv.getKey() + ": '" + inv.getHuman() + "' " + msg);
        } else if (inv.getSeverity() == ConstraintSeverity.ERROR) {
            rule(errors, IssueType.INVARIANT, element.line(), element.col(), path, ok, inv.getKey() + ": '" + inv.getHuman() + "' " + msg);
        } else if (inv.getSeverity() == ConstraintSeverity.WARNING) {
            warning(errors, IssueType.INVARIANT, element.line(), element.col(), path, ok, inv.getKey() + ": '" + inv.getHuman() + "' " + msg);
        }
    }
}
Also used : FHIRLexerException(org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException) ExpressionNode(org.hl7.fhir.r5.model.ExpressionNode) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) FHIRLexerException(org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)

Example 23 with FHIRLexerException

use of org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method check.

/**
 * check that paths referred to in the ExpressionNode are valid
 *
 * xPathStartsWithValueRef is a hack work around for the fact that FHIR Path sometimes needs a different starting point than the xpath
 *
 * returns a list of the possible types that might be returned by executing the ExpressionNode against a particular context
 *
 * @param context - the logical type against which this path is applied
 * @throws DefinitionException
 * @throws PathEngineException
 * @if the path is not valid
 */
public TypeDetails check(Object appContext, String resourceType, String context, ExpressionNode expr) throws FHIRLexerException, PathEngineException, DefinitionException {
    // if context is a path that refers to a type, do that conversion now
    TypeDetails types;
    if (!context.contains("."))
        types = new TypeDetails(CollectionStatus.SINGLETON, context);
    else {
        StructureDefinition sd = worker.fetchTypeDefinition(context.substring(0, context.indexOf('.')));
        if (sd == null)
            throw new PathEngineException("Unknown context " + context);
        ElementDefinitionMatch ed = getElementDefinition(sd, context, true);
        if (ed == null)
            throw new PathEngineException("Unknown context element " + context);
        if (ed.fixedType != null)
            types = new TypeDetails(CollectionStatus.SINGLETON, ed.fixedType);
        else if (ed.getDefinition().getType().isEmpty() || (isAbstractType(ed.getDefinition().getType())))
            types = new TypeDetails(CollectionStatus.SINGLETON, context);
        else {
            types = new TypeDetails(CollectionStatus.SINGLETON);
            for (TypeRefComponent t : ed.getDefinition().getType()) types.addType(t.getCode());
        }
    }
    return executeType(new ExecutionTypeContext(appContext, resourceType, context, types), types, expr, true);
}
Also used : TypeDetails(org.hl7.fhir.dstu2.model.ExpressionNode.TypeDetails) StructureDefinition(org.hl7.fhir.dstu2.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.dstu2.model.ElementDefinition.TypeRefComponent) PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Example 24 with FHIRLexerException

use of org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method evaluate.

/**
 * evaluate a path and return the matching elements
 *
 * @param base - the object against which the path is being evaluated
 * @param path - the FHIR Path statement to use
 * @return
 * @throws FHIRLexerException
 * @throws PathEngineException
 * @
 * @
 */
public List<Base> evaluate(Base base, String path) throws FHIRLexerException, PathEngineException {
    ExpressionNode exp = parse(path);
    List<Base> list = new ArrayList<Base>();
    if (base != null)
        list.add(base);
    log = new StringBuilder();
    return execute(new ExecutionContext(null, null, base, base), list, exp, true);
}
Also used : ExpressionNode(org.hl7.fhir.dstu2.model.ExpressionNode) ArrayList(java.util.ArrayList) Base(org.hl7.fhir.dstu2.model.Base)

Example 25 with FHIRLexerException

use of org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method parsePartial.

/**
 * Parse a path for later use using execute
 *
 * @param path
 * @return
 * @throws PathEngineException
 * @throws Exception
 */
public ExpressionNodeWithOffset parsePartial(String path, int i) throws FHIRLexerException {
    FHIRLexer lexer = new FHIRLexer(path, i);
    if (lexer.done())
        throw lexer.error("Path cannot be empty");
    ExpressionNode result = parseExpression(lexer, true);
    result.check();
    return new ExpressionNodeWithOffset(lexer.getCurrentStart(), result);
}
Also used : ExpressionNode(org.hl7.fhir.r4.model.ExpressionNode)

Aggregations

CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)9 ExpressionNode (org.hl7.fhir.r5.model.ExpressionNode)6 ExpressionNode (org.hl7.fhir.dstu2.model.ExpressionNode)5 ExpressionNode (org.hl7.fhir.r4.model.ExpressionNode)4 ExpressionNode (org.hl7.fhir.r4b.model.ExpressionNode)4 BigDecimal (java.math.BigDecimal)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ExpressionNode (org.hl7.fhir.dstu2016may.model.ExpressionNode)3 ExpressionNode (org.hl7.fhir.dstu3.model.ExpressionNode)3 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)3 SourceLocation (org.hl7.fhir.utilities.SourceLocation)3 Base (org.hl7.fhir.dstu2.model.Base)2 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 List (java.util.List)1 NotImplementedException (org.apache.commons.lang3.NotImplementedException)1 TypeRefComponent (org.hl7.fhir.dstu2.model.ElementDefinition.TypeRefComponent)1