1
2
3
4 package ca.uhn.hl7v2.examples;
5
6 import ca.uhn.hl7v2.DefaultHapiContext;
7 import ca.uhn.hl7v2.HL7Exception;
8 import ca.uhn.hl7v2.HapiContext;
9 import ca.uhn.hl7v2.Version;
10 import ca.uhn.hl7v2.conf.store.ClasspathProfileStore;
11 import ca.uhn.hl7v2.model.Message;
12 import ca.uhn.hl7v2.validation.Validator;
13 import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
14 import ca.uhn.hl7v2.validation.builder.support.DefaultValidationBuilder;
15 import ca.uhn.hl7v2.validation.impl.SimpleValidationExceptionHandler;
16
17
18
19
20
21
22
23
24 public class MessageValidationUsingConformanceProfile {
25
26
27
28
29
30 public static void main(String[] args) throws HL7Exception {
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 ValidationRuleBuilder builder = new DefaultValidationBuilder() {
46
47 @Override
48 protected void configure() {
49 super.configure();
50 forVersion(Version.V24)
51 .message("ADT", "*")
52 .conformance("ADT_A31");
53 }
54
55 };
56
57
58
59 HapiContext context = new DefaultHapiContext();
60 context.setValidationRuleBuilder(builder);
61 context.getParserConfiguration().setValidating(false);
62 context.setProfileStore(new ClasspathProfileStore("/ca/uhn/hl7v2/examples/profiles"));
63
64 String validMessageString = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4\r\n"
65 + "EVN|A31|200903230934\r\n"
66 + "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||";
67 Message validMessage = context.getPipeParser().parse(validMessageString);
68
69
70 SimpleValidationExceptionHandlerandler.html#SimpleValidationExceptionHandler">SimpleValidationExceptionHandler handler = new SimpleValidationExceptionHandler(context);
71
72 Validator<Boolean> validator = context.getMessageValidator();
73
74 if (!validator.validate(validMessage, handler)) {
75 System.out.println("Found " + handler.getExceptions().size() + " problems");
76 for (Exception e : handler.getExceptions()) {
77 System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage());
78 }
79 }
80
81 }
82
83 }