More work on validator

This commit is contained in:
jamesagnew 2015-08-21 09:03:48 -04:00
parent 34d6ae1fc5
commit 07d654c97b
1 changed files with 148 additions and 143 deletions

View File

@ -37,12 +37,10 @@ import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
public class FhirInstanceValidator extends BaseValidatorBridge implements IValidatorModule {
private static FhirContext ourHl7OrgCtx;
static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirInstanceValidator.class);
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirInstanceValidator.class);
private BestPracticeWarningLevel myBestPracticeWarningLevel;
private DocumentBuilderFactory myDocBuilderFactory;
private BestPracticeWarningLevel myBestPracticeWarningLevel;
public FhirInstanceValidator() {
myDocBuilderFactory = DocumentBuilderFactory.newInstance();
myDocBuilderFactory.setNamespaceAware(true);
@ -62,6 +60,15 @@ private BestPracticeWarningLevel myBestPracticeWarningLevel;
return root.getLocalName();
}
/**
* Returns the "best practice" warning level (default is {@link BestPracticeWarningLevel#Hint})
*
* @see #setBestPracticeWarningLevel(BestPracticeWarningLevel) for more information on this value
*/
public BestPracticeWarningLevel getBestPracticeWarningLevel() {
return myBestPracticeWarningLevel;
}
private FhirContext getHl7OrgDstu2Ctx(FhirContext theCtx) {
if (theCtx.getVersion().getVersion() == FhirVersionEnum.DSTU2_HL7ORG) {
return theCtx;
@ -74,12 +81,10 @@ private BestPracticeWarningLevel myBestPracticeWarningLevel;
return retVal;
}
private StructureDefinition loadProfileOrReturnNull(List<ValidationMessage> theMessages, FhirContext theCtx,
String theResourceName) {
private StructureDefinition loadProfileOrReturnNull(List<ValidationMessage> theMessages, FhirContext theCtx, String theResourceName) {
if (isBlank(theResourceName)) {
if (theMessages != null) {
theMessages.add(new ValidationMessage().setLevel(IssueSeverity.FATAL)
.setMessage("Could not determine resource type from request. Content appears invalid."));
theMessages.add(new ValidationMessage().setLevel(IssueSeverity.FATAL).setMessage("Could not determine resource type from request. Content appears invalid."));
}
return null;
}
@ -90,8 +95,7 @@ private BestPracticeWarningLevel myBestPracticeWarningLevel;
InputStream inputStream = FhirInstanceValidator.class.getResourceAsStream(profileCpName);
if (inputStream == null) {
if (theMessages != null) {
theMessages.add(new ValidationMessage().setLevel(IssueSeverity.FATAL)
.setMessage("No profile found for resource type " + theResourceName));
theMessages.add(new ValidationMessage().setLevel(IssueSeverity.FATAL).setMessage("No profile found for resource type " + theResourceName));
} else {
return null;
}
@ -99,31 +103,30 @@ private BestPracticeWarningLevel myBestPracticeWarningLevel;
profileText = IOUtils.toString(inputStream, "UTF-8");
} catch (IOException e1) {
if (theMessages != null) {
theMessages.add(new ValidationMessage().setLevel(IssueSeverity.FATAL)
.setMessage("No profile found for resource type " + theResourceName));
theMessages.add(new ValidationMessage().setLevel(IssueSeverity.FATAL).setMessage("No profile found for resource type " + theResourceName));
}
return null;
}
StructureDefinition profile = getHl7OrgDstu2Ctx(theCtx).newXmlParser().parseResource(StructureDefinition.class,
profileText);
StructureDefinition profile = getHl7OrgDstu2Ctx(theCtx).newXmlParser().parseResource(StructureDefinition.class, profileText);
return profile;
}
/**
* Sets the "best practice warning level". When validating, any deviations from best practices will be reported at
* this level. {@link BestPracticeWarningLevel#Ignore} means that best practice deviations will not be reported,
* {@link BestPracticeWarningLevel#Warning} means that best practice deviations will be reported as warnings, etc.
* Default is {@link BestPracticeWarningLevel#Hint}
*
* @param theBestPracticeWarningLevel
* The level, must not be <code>null</code>
*/
public void setBestPracticeWarningLevel(BestPracticeWarningLevel theBestPracticeWarningLevel) {
Validate.notNull(theBestPracticeWarningLevel);
myBestPracticeWarningLevel = theBestPracticeWarningLevel;
}
protected List<ValidationMessage> validate(final FhirContext theCtx, String theInput, EncodingEnum theEncoding) {
WorkerContext workerContext = new WorkerContext() {
@Override
public StructureDefinition getProfile(String theId) {
StructureDefinition retVal = super.getProfile(theId);
if (retVal == null) {
if (theId.startsWith("http://hl7.org/fhir/StructureDefinition/")) {
retVal = loadProfileOrReturnNull(null, getHl7OrgDstu2Ctx(theCtx), theId.substring("http://hl7.org/fhir/StructureDefinition/".length()));
if (retVal != null) {
seeProfile(theId, retVal);
}
}
}
return retVal;
}};
WorkerContext workerContext = new HapiWorkerContext(theCtx);
org.hl7.fhir.instance.validation.InstanceValidator v;
try {
@ -178,30 +181,32 @@ private BestPracticeWarningLevel myBestPracticeWarningLevel;
return messages;
}
/**
* Returns the "best practice" warning level (default is {@link BestPracticeWarningLevel#Hint})
*
* @see #setBestPracticeWarningLevel(BestPracticeWarningLevel) for more information on this value
*/
public BestPracticeWarningLevel getBestPracticeWarningLevel() {
return myBestPracticeWarningLevel;
}
/**
* Sets the "best practice warning level". When validating, any deviations from best practices
* will be reported at this level. {@link BestPracticeWarningLevel#Ignore} means that best practice
* deviations will not be reported, {@link BestPracticeWarningLevel#Warning} means that best
* practice deviations will be reported as warnings, etc. Default is {@link BestPracticeWarningLevel#Hint}
* @param theBestPracticeWarningLevel The level, must not be <code>null</code>
*/
public void setBestPracticeWarningLevel(BestPracticeWarningLevel theBestPracticeWarningLevel) {
Validate.notNull(theBestPracticeWarningLevel);
myBestPracticeWarningLevel = theBestPracticeWarningLevel;
}
@Override
protected List<ValidationMessage> validate(IValidationContext<?> theCtx) {
return validate(theCtx.getFhirContext(), theCtx.getResourceAsString(), theCtx.getResourceAsStringEncoding());
}
private final class HapiWorkerContext extends WorkerContext {
private final FhirContext myCtx;
private HapiWorkerContext(FhirContext theCtx) {
myCtx = theCtx;
}
@Override
public StructureDefinition getProfile(String theId) {
StructureDefinition retVal = super.getProfile(theId);
if (retVal == null) {
if (theId.startsWith("http://hl7.org/fhir/StructureDefinition/")) {
retVal = loadProfileOrReturnNull(null, getHl7OrgDstu2Ctx(myCtx), theId.substring("http://hl7.org/fhir/StructureDefinition/".length()));
if (retVal != null) {
seeProfile(theId, retVal);
}
}
}
return retVal;
}
}
}