From 6af30d5f7fc2ea0d51f3748ef336e304f30ac07e Mon Sep 17 00:00:00 2001 From: markiantorno Date: Fri, 21 Feb 2020 14:58:02 -0500 Subject: [PATCH] refactoring InstanceValidator code for readability. --- .../r5/validation/EnableWhenEvaluator.java | 3 +- .../fhir/r5/validation/ValidationEngine.java | 1 + .../InstanceValidator.java | 85 ++++++------------- .../InstanceValidatorFactory.java | 2 +- .../utils/ResolvedReference.java | 61 +++++++++++++ .../validation/tests/ValidationTestSuite.java | 2 +- 6 files changed, 92 insertions(+), 62 deletions(-) rename org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/{ => instancevalidator}/InstanceValidator.java (99%) rename org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/{ => instancevalidator}/InstanceValidatorFactory.java (95%) create mode 100644 org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/instancevalidator/utils/ResolvedReference.java diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/EnableWhenEvaluator.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/EnableWhenEvaluator.java index 20d21d7d7..039938219 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/EnableWhenEvaluator.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/EnableWhenEvaluator.java @@ -21,7 +21,6 @@ package org.hl7.fhir.r5.validation; */ import java.util.*; -import java.util.logging.Level; import java.util.stream.*; import org.hl7.fhir.exceptions.FHIRException; @@ -29,7 +28,7 @@ import org.hl7.fhir.r5.elementmodel.Element; import org.hl7.fhir.r5.model.*; import org.hl7.fhir.r5.model.Questionnaire.*; import org.hl7.fhir.r5.utils.FHIRPathEngine; -import org.hl7.fhir.r5.validation.InstanceValidator.ValidatorHostContext; +import org.hl7.fhir.r5.validation.instancevalidator.InstanceValidator.ValidatorHostContext; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/ValidationEngine.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/ValidationEngine.java index 7f727fe38..411244049 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/ValidationEngine.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/ValidationEngine.java @@ -22,6 +22,7 @@ import org.hl7.fhir.r5.terminologies.ConceptMapEngine; import org.hl7.fhir.r5.utils.*; import org.hl7.fhir.r5.utils.IResourceValidator.*; import org.hl7.fhir.r5.utils.StructureMapUtilities.ITransformerServices; +import org.hl7.fhir.r5.validation.instancevalidator.InstanceValidator; import org.hl7.fhir.utilities.IniFile; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/InstanceValidator.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/instancevalidator/InstanceValidator.java similarity index 99% rename from org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/InstanceValidator.java rename to org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/instancevalidator/InstanceValidator.java index 401d9ce5b..35735917d 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/InstanceValidator.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/instancevalidator/InstanceValidator.java @@ -1,4 +1,4 @@ -package org.hl7.fhir.r5.validation; +package org.hl7.fhir.r5.validation.instancevalidator; /*- * #%L @@ -35,9 +35,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; -import java.util.stream.Collectors; -import ca.uhn.fhir.context.FhirContext; import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.r5.model.Reference; @@ -94,8 +92,6 @@ import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.FhirPublication; import org.hl7.fhir.r5.model.HumanName; import org.hl7.fhir.r5.model.Identifier; -import org.hl7.fhir.r5.model.ImplementationGuide; -import org.hl7.fhir.r5.model.ImplementationGuide.ImplementationGuideGlobalComponent; import org.hl7.fhir.r5.model.InstantType; import org.hl7.fhir.r5.model.IntegerType; import org.hl7.fhir.r5.model.Period; @@ -130,11 +126,12 @@ import org.hl7.fhir.r5.utils.FHIRPathEngine; import org.hl7.fhir.r5.utils.FHIRPathEngine.IEvaluationContext; import org.hl7.fhir.r5.utils.IResourceValidator; import org.hl7.fhir.r5.utils.ToolingExtensions; +import org.hl7.fhir.r5.validation.BaseValidator; +import org.hl7.fhir.r5.validation.EnableWhenEvaluator; import org.hl7.fhir.r5.validation.EnableWhenEvaluator.QStack; -import org.hl7.fhir.r5.validation.InstanceValidator.EntrySummary; -import org.hl7.fhir.r5.validation.InstanceValidator.ResolvedReference; +import org.hl7.fhir.r5.validation.XVerExtensionManager; +import org.hl7.fhir.r5.validation.instancevalidator.utils.ResolvedReference; import org.hl7.fhir.utilities.CommaSeparatedStringBuilder; -import org.hl7.fhir.utilities.TerminologyServiceOptions; import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.Utilities.DecimalStatus; import org.hl7.fhir.utilities.VersionUtilities; @@ -172,39 +169,7 @@ import ca.uhn.fhir.util.ObjectUtil; public class InstanceValidator extends BaseValidator implements IResourceValidator { - public class ResolvedReference { - - private Element resource; - private Element focus; - private boolean external; - private NodeStack stack; - - public String getType() { - return focus.fhirType(); - } - - public ValidatorHostContext hostContext(ValidatorHostContext hostContext, StructureDefinition profile) { - if (external) { - return hostContext.forRemoteReference(profile, resource); - } else { - return hostContext.forLocalReference(profile, resource); - } - } - - public Element getResource() { - return resource; - } - - public Element getFocus() { - return focus; - } - - public NodeStack getStack() { - return stack; - } - } - - public class ValidatorHostContext { + public class ValidatorHostContext { private Object appContext; private Element container; // bundle, or parameters private Element resource; @@ -2996,10 +2961,10 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat IndexedElement res = getContainedById(stack.getElement(), ref.substring(1)); if (res != null) { ResolvedReference rr = new ResolvedReference(); - rr.resource = stack.getElement(); - rr.focus = res.match; - rr.external = false; - rr.stack = stack.push(res.match, res.index, res.match.getProperty().getDefinition(), res.match.getProperty().getDefinition()); + rr.setResource(stack.getElement()); + rr.setFocus(res.match); + rr.setExternal(false); + rr.setStack(stack.push(res.match, res.index, res.match.getProperty().getDefinition(), res.match.getProperty().getDefinition())); return rr; } } @@ -3028,10 +2993,12 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat return null; } else { ResolvedReference rr = new ResolvedReference(); - rr.resource = res.match; - rr.focus = res.match; - rr.external = false; - rr.stack = stack.push(res.entry, res.index, res.entry.getProperty().getDefinition(), res.entry.getProperty().getDefinition()).push(res.match, -1, res.match.getProperty().getDefinition(), res.match.getProperty().getDefinition()); + rr.setResource(res.match); + rr.setFocus(res.match); + rr.setExternal(false); + rr.setStack(stack.push(res.entry, res.index, res.entry.getProperty().getDefinition(), + res.entry.getProperty().getDefinition()).push(res.match, -1, + res.match.getProperty().getDefinition(), res.match.getProperty().getDefinition())); return rr; } } @@ -3047,10 +3014,12 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat return null; } else { ResolvedReference rr = new ResolvedReference(); - rr.resource = res.match; - rr.focus = res.match; - rr.external = false; - rr.stack = new NodeStack(hostContext).push(res.entry, res.index, res.entry.getProperty().getDefinition(), res.entry.getProperty().getDefinition()).push(res.match, -1, res.match.getProperty().getDefinition(), res.match.getProperty().getDefinition()); + rr.setResource(res.match); + rr.setFocus(res.match); + rr.setExternal(false); + rr.setStack(new NodeStack(hostContext).push(res.entry, res.index, res.entry.getProperty().getDefinition(), + res.entry.getProperty().getDefinition()).push(res.match, -1, + res.match.getProperty().getDefinition(), res.match.getProperty().getDefinition())); return rr; } } @@ -3071,16 +3040,16 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat private ResolvedReference makeExternalRef(Element external, String path) { ResolvedReference res = new ResolvedReference(); - res.resource = external; - res.focus = external; - res.external = true; - res.stack = new NodeStack(external, path); + res.setResource(external); + res.setFocus(external); + res.setExternal(true); + res.setStack(new NodeStack(external, path)); return res; } private Element resolve(Object appContext, String ref, NodeStack stack, List errors, String path) throws IOException, FHIRException { - Element local = localResolve(ref, stack, errors, path, null, null).focus; + Element local = localResolve(ref, stack, errors, path, null, null).getFocus(); if (local!=null) return local; if (fetcher == null) diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/InstanceValidatorFactory.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/instancevalidator/InstanceValidatorFactory.java similarity index 95% rename from org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/InstanceValidatorFactory.java rename to org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/instancevalidator/InstanceValidatorFactory.java index 539822357..90ef501a3 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/InstanceValidatorFactory.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/instancevalidator/InstanceValidatorFactory.java @@ -1,4 +1,4 @@ -package org.hl7.fhir.r5.validation; +package org.hl7.fhir.r5.validation.instancevalidator; import org.hl7.fhir.exceptions.FHIRException; diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/instancevalidator/utils/ResolvedReference.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/instancevalidator/utils/ResolvedReference.java new file mode 100644 index 000000000..0d77dc288 --- /dev/null +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/instancevalidator/utils/ResolvedReference.java @@ -0,0 +1,61 @@ +package org.hl7.fhir.r5.validation.instancevalidator.utils; + +import org.hl7.fhir.r5.elementmodel.Element; +import org.hl7.fhir.r5.model.StructureDefinition; +import org.hl7.fhir.r5.validation.instancevalidator.InstanceValidator; + +public class ResolvedReference { + + private Element resource; + private Element focus; + private boolean external; + private InstanceValidator.NodeStack stack; + + public ResolvedReference setResource(Element resource) { + this.resource = resource; + return this; + } + + public Element getResource() { + return resource; + } + + public ResolvedReference setFocus(Element focus) { + this.focus = focus; + return this; + } + + public boolean isExternal() { + return external; + } + + public ResolvedReference setExternal(boolean external) { + this.external = external; + return this; + } + + public ResolvedReference setStack(InstanceValidator.NodeStack stack) { + this.stack = stack; + return this; + } + + public InstanceValidator.NodeStack getStack() { + return stack; + } + + public String getType() { + return focus.fhirType(); + } + + public Element getFocus() { + return focus; + } + + public InstanceValidator.ValidatorHostContext hostContext(InstanceValidator.ValidatorHostContext hostContext, StructureDefinition profile) { + if (external) { + return hostContext.forRemoteReference(profile, resource); + } else { + return hostContext.forLocalReference(profile, resource); + } + } +} diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ValidationTestSuite.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ValidationTestSuite.java index 66b5dd003..7b694f755 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ValidationTestSuite.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ValidationTestSuite.java @@ -26,7 +26,7 @@ import org.hl7.fhir.r5.utils.FHIRPathEngine.IEvaluationContext; import org.hl7.fhir.r5.utils.IResourceValidator; import org.hl7.fhir.r5.utils.IResourceValidator.IValidatorResourceFetcher; import org.hl7.fhir.r5.utils.IResourceValidator.ReferenceValidationPolicy; -import org.hl7.fhir.r5.validation.InstanceValidator; +import org.hl7.fhir.r5.validation.instancevalidator.InstanceValidator; import org.hl7.fhir.r5.validation.ValidationEngine; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities;