public static void main(String args[]) { if (args.length != 2) { System.out.println("Usage: DefaultValidator message_file profile_file"); System.exit(1); } DefaultValidator val = new DefaultValidator(); try { String msgString = loadFile(args[0]); Parser parser = new GenericParser(); Message message = parser.parse(msgString); String profileString = loadFile(args[1]); ProfileParser profParser = new ProfileParser(true); RuntimeProfile profile = profParser.parse(profileString); HL7Exception[] exceptions = val.validate(message, profile.getMessage()); System.out.println("Exceptions: "); for (int i = 0; i < exceptions.length; i++) { System.out.println((i + 1) + ". " + exceptions[i].getMessage()); } } catch (Exception e) { e.printStackTrace(); } }
/** * @see Validator#validate */ public HL7Exception[] validate(Message message, StaticDef profile) throws ProfileException, HL7Exception { List<HL7Exception> exList = new ArrayList<HL7Exception>(); Terser t = new Terser(message); checkMessageType(t.get("/MSH-9-1"), profile, exList); checkEventType(t.get("/MSH-9-2"), profile, exList); checkMessageStructure(t.get("/MSH-9-3"), profile, exList); exList.addAll(doTestGroup(message, profile, profile.getIdentifier(), validateChildren)); return exList.toArray(new HL7Exception[exList.size()]); }
/** * Tests a Type against the corresponding section of a profile. * * @param encoded optional encoded form of type (if you want to specify this -- if null, default * pipe-encoded form is used to check length and constant val) */ public List<HL7Exception> testType(Type type, AbstractComponent<?> profile, String encoded, String profileID) { List<HL7Exception> exList = new ArrayList<HL7Exception>(); if (encoded == null) encoded = PipeParser.encode(type, this.enc); testUsage(encoded, profile.getUsage(), profile.getName(), exList); if (!profile.getUsage().equals("X")) { checkDataType(profile.getDatatype(), type, exList); checkLength(profile.getLength(), profile.getName(), encoded, exList); checkConstantValue(profile.getConstantValue(), encoded, exList); testTypeAgainstTable(type, profile, profileID, exList); } return exList; }
encoded = ((Primitive) type).getValue(); exList.addAll(testType(type, profile, encoded, profileID)); try { Type child = comp.getComponent(i - 1); exList.addAll(doTestComponent(child, childProfile, profileID, validateChildren)); } catch (DataTypeException de) { exList.add(new ProfileNotHL7CompliantException( checkExtraComponents(comp, profile.getComponents(), exList); } else { exList.add(new ProfileNotHL7CompliantException("A field has type primitive "
testCardinality(instancesWithContent.size(), struct.getMin(), struct.getMax(), struct.getUsage(), struct.getName(), exList); exList.addAll(testStructure(s, struct, profileID)); checkForExtraStructures(group, allowedStructures, exList);
HL7Exception ce = testCardinality(instancesWithContent.size(), field.getMin(), field.getMax(), field.getUsage(), field.getName(), exList); if (ce != null) { List<HL7Exception> childExceptions = doTestField(s, field, escape, profileID, validateChildren); for (HL7Exception ex : childExceptions) { checkForExtraFields(segment, allowedFields, exList);
DefaultValidator validator = new DefaultValidator(); HL7Exception[] exceptions = validator.validate(validMessage, profile.getMessage());
protected List<HL7Exception> doTestComponent(Type type, Component profile, String profileID, boolean theValidateChildren) throws ProfileException, HL7Exception { List<HL7Exception> exList = new ArrayList<HL7Exception>(); exList.addAll(testType(type, profile, null, profileID)); // test children if (profile.getSubComponents() > 0 && !profile.getUsage().equals("X") && (!type.isEmpty())) { if (Composite.class.isAssignableFrom(type.getClass())) { Composite comp = (Composite) type; if (theValidateChildren) { for (int i = 1; i <= profile.getSubComponents(); i++) { SubComponent childProfile = profile.getSubComponent(i); try { Type child = comp.getComponent(i - 1); exList.addAll(testType(child, childProfile, null, profileID)); } catch (DataTypeException de) { exList.add(new ProfileNotHL7CompliantException( "More subcomponents in profile than allowed in message: " + de.getMessage())); } } } checkExtraComponents(comp, profile.getSubComponents(), exList); } else { exList.add(new ProfileNotFollowedException("A component has primitive type " + type.getClass().getName() + " but the profile defines subcomponents")); } } return exList; }
public ca.uhn.hl7v2.conf.check.Validator getConformanceValidator() { return new ca.uhn.hl7v2.conf.check.DefaultValidator(this); }
/** * Tests table values for ID, IS, and CE types. An empty list is returned for all other types or * if the table name or number is missing. */ protected void testTypeAgainstTable(Type type, AbstractComponent<?> profile, String profileID, List<HL7Exception> exList) { if (profile.getTable() != null && (type.getName().equals("IS") || type.getName().equals("ID"))) { String codeSystem = String.format("HL7%1$4s", profile.getTable()).replace(" ", "0"); String value = ((Primitive) type).getValue(); addTableTestResult(profileID, codeSystem, value, exList); } else if (type.getName().equals("CE")) { String value = Terser.getPrimitive(type, 1, 1).getValue(); String codeSystem = Terser.getPrimitive(type, 3, 1).getValue(); addTableTestResult(profileID, codeSystem, value, exList); value = Terser.getPrimitive(type, 4, 1).getValue(); codeSystem = Terser.getPrimitive(type, 6, 1).getValue(); addTableTestResult(profileID, codeSystem, value, exList); } }
/** * Tests a Type against the corresponding section of a profile. * * @param encoded optional encoded form of type (if you want to specify this -- if null, default * pipe-encoded form is used to check length and constant val) */ public List<HL7Exception> testType(Type type, AbstractComponent<?> profile, String encoded, String profileID) { List<HL7Exception> exList = new ArrayList<HL7Exception>(); if (encoded == null) encoded = PipeParser.encode(type, this.enc); testUsage(encoded, profile.getUsage(), profile.getName(), exList); if (!profile.getUsage().equals("X")) { checkDataType(profile.getDatatype(), type, exList); checkLength(profile.getLength(), profile.getName(), encoded, exList); checkConstantValue(profile.getConstantValue(), encoded, exList); testTypeAgainstTable(type, profile, profileID, exList); } return exList; }
encoded = ((Primitive) type).getValue(); exList.addAll(testType(type, profile, encoded, profileID)); try { Type child = comp.getComponent(i - 1); exList.addAll(doTestComponent(child, childProfile, profileID, validateChildren)); } catch (DataTypeException de) { exList.add(new ProfileNotHL7CompliantException( checkExtraComponents(comp, profile.getComponents(), exList); } else { exList.add(new ProfileNotHL7CompliantException("A field has type primitive "
testCardinality(instancesWithContent.size(), struct.getMin(), struct.getMax(), struct.getUsage(), struct.getName(), exList); exList.addAll(testStructure(s, struct, profileID)); checkForExtraStructures(group, allowedStructures, exList);
HL7Exception ce = testCardinality(instancesWithContent.size(), field.getMin(), field.getMax(), field.getUsage(), field.getName(), exList); if (ce != null) { List<HL7Exception> childExceptions = doTestField(s, field, escape, profileID, validateChildren); for (HL7Exception ex : childExceptions) { checkForExtraFields(segment, allowedFields, exList);
protected List<HL7Exception> doTestComponent(Type type, Component profile, String profileID, boolean theValidateChildren) throws ProfileException, HL7Exception { List<HL7Exception> exList = new ArrayList<HL7Exception>(); exList.addAll(testType(type, profile, null, profileID)); // test children if (profile.getSubComponents() > 0 && !profile.getUsage().equals("X") && (!type.isEmpty())) { if (Composite.class.isAssignableFrom(type.getClass())) { Composite comp = (Composite) type; if (theValidateChildren) { for (int i = 1; i <= profile.getSubComponents(); i++) { SubComponent childProfile = profile.getSubComponent(i); try { Type child = comp.getComponent(i - 1); exList.addAll(testType(child, childProfile, null, profileID)); } catch (DataTypeException de) { exList.add(new ProfileNotHL7CompliantException( "More subcomponents in profile than allowed in message: " + de.getMessage())); } } } checkExtraComponents(comp, profile.getSubComponents(), exList); } else { exList.add(new ProfileNotFollowedException("A component has primitive type " + type.getClass().getName() + " but the profile defines subcomponents")); } } return exList; }
public ca.uhn.hl7v2.conf.check.Validator getConformanceValidator() { return new ca.uhn.hl7v2.conf.check.DefaultValidator(this); }
/** * Tests table values for ID, IS, and CE types. An empty list is returned for all other types or * if the table name or number is missing. */ protected void testTypeAgainstTable(Type type, AbstractComponent<?> profile, String profileID, List<HL7Exception> exList) { if (profile.getTable() != null && (type.getName().equals("IS") || type.getName().equals("ID"))) { String codeSystem = String.format("HL7%1$4s", profile.getTable()).replace(" ", "0"); String value = ((Primitive) type).getValue(); addTableTestResult(profileID, codeSystem, value, exList); } else if (type.getName().equals("CE")) { String value = Terser.getPrimitive(type, 1, 1).getValue(); String codeSystem = Terser.getPrimitive(type, 3, 1).getValue(); addTableTestResult(profileID, codeSystem, value, exList); value = Terser.getPrimitive(type, 4, 1).getValue(); codeSystem = Terser.getPrimitive(type, 6, 1).getValue(); addTableTestResult(profileID, codeSystem, value, exList); } }
/** * @see Validator#validate */ public HL7Exception[] validate(Message message, StaticDef profile) throws ProfileException, HL7Exception { List<HL7Exception> exList = new ArrayList<HL7Exception>(); Terser t = new Terser(message); checkMessageType(t.get("/MSH-9-1"), profile, exList); checkEventType(t.get("/MSH-9-2"), profile, exList); checkMessageStructure(t.get("/MSH-9-3"), profile, exList); exList.addAll(doTestGroup(message, profile, profile.getIdentifier(), validateChildren)); return exList.toArray(new HL7Exception[exList.size()]); }
public static void main(String args[]) { if (args.length != 2) { System.out.println("Usage: DefaultValidator message_file profile_file"); System.exit(1); } DefaultValidator val = new DefaultValidator(); try { String msgString = loadFile(args[0]); Parser parser = new GenericParser(); Message message = parser.parse(msgString); String profileString = loadFile(args[1]); ProfileParser profParser = new ProfileParser(true); RuntimeProfile profile = profParser.parse(profileString); HL7Exception[] exceptions = val.validate(message, profile.getMessage()); System.out.println("Exceptions: "); for (int i = 0; i < exceptions.length; i++) { System.out.println((i + 1) + ". " + exceptions[i].getMessage()); } } catch (Exception e) { e.printStackTrace(); } }