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();
}
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);
}
}
}
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);
}
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);
}
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);
}
Aggregations