1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package ca.uhn.hl7v2.validation.builder.support;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Set;
31
32 import ca.uhn.hl7v2.HL7Exception;
33 import ca.uhn.hl7v2.model.Message;
34 import ca.uhn.hl7v2.model.Structure;
35 import ca.uhn.hl7v2.model.SuperStructure;
36 import ca.uhn.hl7v2.validation.MessageRule;
37 import ca.uhn.hl7v2.validation.ValidationException;
38 import ca.uhn.hl7v2.validation.impl.AbstractMessageRule;
39
40
41
42
43
44
45
46 @SuppressWarnings("serial")
47 public class OnlyAllowableSegmentsInSuperstructureRule extends AbstractMessageRule {
48
49 public static final MessageRule ONLY_ALLOWABLE_SEGMENTS = new OnlyAllowableSegmentsInSuperstructureRule();
50
51 public ValidationException[] apply(Message msg) {
52 List<ValidationException> exceptions = new ArrayList<>();
53
54 if (msg instanceof SuperStructure) {
55 checkStructure((SuperStructure)msg, exceptions);
56 }
57
58 return exceptions.toArray(new ValidationException[0]);
59 }
60
61 private void checkStructure(SuperStructure theMsg, List<ValidationException> theExceptions) {
62 String messageStructure = theMsg.getMessage().getName();
63 Set<String> nonStandardNames = theMsg.getNonStandardNames();
64
65 FORNAME:
66 for (String name : theMsg.getNames()) {
67 if (nonStandardNames != null && nonStandardNames.contains(name)) {
68 continue;
69 }
70
71 try {
72 for (Structure rep : theMsg.getAll(name)) {
73
74 if (!rep.isEmpty()) {
75 Set<String> structuresWhichChildAppliesTo = theMsg.getStructuresWhichChildAppliesTo(name);
76 if (!structuresWhichChildAppliesTo.contains(messageStructure)) {
77 String msgSimpleName = theMsg.getMessage().getClass().getSimpleName();
78 theExceptions.add(new ValidationException("Message (superstructure " + msgSimpleName + ") of type " + messageStructure + " must not have content in " + name));
79 }
80 continue FORNAME;
81 }
82
83 }
84 } catch (HL7Exception e) {
85
86 throw new Error("Can't get rep of structure " + name + ". This is probably a HAPI bug");
87 }
88
89 }
90 }
91
92 }