clean up error handling when parsing structure maps

This commit is contained in:
Grahame Grieve 2023-01-20 04:24:42 +11:00
parent 7cc01ef7db
commit b16fc37b02
4 changed files with 50 additions and 34 deletions

View File

@ -50,20 +50,31 @@ import org.hl7.fhir.utilities.Utilities;
public class FHIRLexer {
public class FHIRLexerException extends FHIRException {
public FHIRLexerException() {
super();
}
private SourceLocation location;
public FHIRLexerException(String message, Throwable cause) {
super(message, cause);
}
// public FHIRLexerException() {
// super();
// }
//
// public FHIRLexerException(String message, Throwable cause) {
// super(message, cause);
// }
//
// public FHIRLexerException(String message) {
// super(message);
// }
//
// public FHIRLexerException(Throwable cause) {
// super(cause);
// }
public FHIRLexerException(String message) {
public FHIRLexerException(String message, SourceLocation location) {
super(message);
this.location = location;
}
public FHIRLexerException(Throwable cause) {
super(cause);
public SourceLocation getLocation() {
return location;
}
}
@ -145,11 +156,11 @@ public class FHIRLexer {
}
public FHIRLexerException error(String msg) {
return error(msg, currentLocation.toString());
return error(msg, currentLocation.toString(), currentLocation);
}
public FHIRLexerException error(String msg, String location) {
return new FHIRLexerException("Error @"+location+": "+msg);
public FHIRLexerException error(String msg, String location, SourceLocation loc) {
return new FHIRLexerException("Error @"+location+": "+msg, loc);
}
public void next() throws FHIRLexerException {
@ -450,7 +461,7 @@ public class FHIRLexer {
i = i + 4;
break;
default:
throw new FHIRLexerException("Unknown character escape \\"+s.charAt(i));
throw new FHIRLexerException("Unknown character escape \\"+s.charAt(i), currentLocation);
}
} else {
b.append(ch);
@ -499,7 +510,7 @@ public class FHIRLexer {
i = i + 4;
break;
default:
throw new FHIRLexerException("Unknown character escape \\"+s.charAt(i));
throw new FHIRLexerException("Unknown character escape \\"+s.charAt(i), currentLocation);
}
} else {
b.append(ch);

View File

@ -1305,14 +1305,14 @@ public class FHIRPathEngine {
private boolean checkParamCount(FHIRLexer lexer, SourceLocation location, ExpressionNode exp, int count) throws FHIRLexerException {
if (exp.getParameters().size() != count) {
throw lexer.error("The function \""+exp.getName()+"\" requires "+Integer.toString(count)+" parameters", location.toString());
throw lexer.error("The function \""+exp.getName()+"\" requires "+Integer.toString(count)+" parameters", location.toString(), location);
}
return true;
}
private boolean checkParamCount(FHIRLexer lexer, SourceLocation location, ExpressionNode exp, int countMin, int countMax) throws FHIRLexerException {
if (exp.getParameters().size() < countMin || exp.getParameters().size() > countMax) {
throw lexer.error("The function \""+exp.getName()+"\" requires between "+Integer.toString(countMin)+" and "+Integer.toString(countMax)+" parameters", location.toString());
throw lexer.error("The function \""+exp.getName()+"\" requires between "+Integer.toString(countMin)+" and "+Integer.toString(countMax)+" parameters", location.toString(), location);
}
return true;
}

View File

@ -72,6 +72,9 @@ import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.SourceLocation;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.validation.ValidationMessage;
import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType;
import org.hl7.fhir.utilities.validation.ValidationMessage.Source;
import org.hl7.fhir.utilities.validation.ValidationOptions;
import org.hl7.fhir.utilities.xhtml.NodeType;
import org.hl7.fhir.utilities.xhtml.XhtmlNode;
@ -640,26 +643,32 @@ public class StructureMapUtilities {
return result;
}
public Element parseEM(String text, String srcName) throws FHIRException {
public Element parseEM(String text, String srcName, List<ValidationMessage> list) throws FHIRException {
FHIRLexer lexer = new FHIRLexer(text, srcName);
if (lexer.done())
throw lexer.error("Map Input cannot be empty");
lexer.token("map");
Element result = Manager.build(worker, worker.fetchTypeDefinition("StructureMap"));
result.makeElement("url").markLocation(lexer.getCurrentLocation()).setValue(lexer.readConstant("url"));
lexer.token("=");
result.makeElement("name").markLocation(lexer.getCurrentLocation()).setValue(lexer.readConstant("name"));
result.makeElement("description").markLocation(lexer.getCurrentLocation()).setValue(lexer.getAllComments());
while (lexer.hasToken("conceptmap"))
parseConceptMapEM(result, lexer);
try {
result.makeElement("url").markLocation(lexer.getCurrentLocation()).setValue(lexer.readConstant("url"));
lexer.token("=");
result.makeElement("name").markLocation(lexer.getCurrentLocation()).setValue(lexer.readConstant("name"));
result.makeElement("description").markLocation(lexer.getCurrentLocation()).setValue(lexer.getAllComments());
while (lexer.hasToken("conceptmap"))
parseConceptMapEM(result, lexer);
while (lexer.hasToken("uses"))
parseUsesEM(result, lexer);
while (lexer.hasToken("imports"))
parseImportsEM(result, lexer);
while (lexer.hasToken("uses"))
parseUsesEM(result, lexer);
while (lexer.hasToken("imports"))
parseImportsEM(result, lexer);
while (!lexer.done()) {
parseGroupEM(result, lexer);
while (!lexer.done()) {
parseGroupEM(result, lexer);
}
} catch (FHIRLexerException e) {
list.add(new ValidationMessage(Source.InstanceValidator, IssueType.INVALID, e.getLocation().getLine(), e.getLocation().getColumn(), null, e.getMessage(), IssueSeverity.FATAL));
} catch (Exception e) {
list.add(new ValidationMessage(Source.InstanceValidator, IssueType.INVALID, null, e.getMessage(), IssueSeverity.FATAL));
}
return result;
@ -3076,8 +3085,4 @@ public class StructureMapUtilities {
this.terminologyServiceOptions = terminologyServiceOptions;
}
public Element parseForValidation(ByteArrayInputStream byteArrayInputStream, String version, List<ValidationMessage> errors) {
return null;
}
}

View File

@ -98,7 +98,7 @@ public class StructureMapUtilitiesTest implements ITransformerServices {
String fileMap = TestingUtilities.loadTestResource("r5", "structure-mapping", "syntax.map");
System.out.println(fileMap);
Element structureMap = scu.parseEM(fileMap, "Syntax");
Element structureMap = scu.parseEM(fileMap, "Syntax", null);
// assertSerializeDeserialize(structureMap);
//
// String renderedMap = StructureMapUtilities.render(structureMap);