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
27 package ca.uhn.hl7v2.model;
28
29 import ca.uhn.hl7v2.HL7Exception;
30 import ca.uhn.hl7v2.Location;
31
32
33
34
35 public final class MessageVisitors {
36
37 private MessageVisitors() {}
38
39
40
41
42
43
44
45
46
47 public static <T extends MessageVisitor> T visit(Visitable visitable, T visitor) throws HL7Exception {
48 visitable.accept(visitor, new Location());
49 return visitor;
50 }
51
52
53
54
55
56
57 public static <T extends MessageVisitor> StructuresVisitor<T> visitStructures(T visitor) {
58 return new StructuresVisitor<>(visitor);
59 }
60
61
62
63
64
65
66
67
68
69 public static <T extends MessageVisitor> PopulatedVisitor<T> visitPopulatedElements(T visitor) {
70 return new PopulatedVisitor<>(visitor);
71 }
72
73
74 public final static class StructuresVisitor<T extends MessageVisitor> extends DelegatingMessageVisitor<T> {
75
76 StructuresVisitor(T visitor) {
77 super(visitor);
78 }
79
80 @Override
81 public boolean start(Segment segment, Location location) throws HL7Exception {
82 super.start(segment, location);
83 return false;
84 }
85
86 @Override
87 public final boolean start(Composite type, Location location) {
88 return false;
89 }
90
91 @Override
92 public boolean visit(Primitive type, Location location) {
93 return false;
94 }
95 }
96
97 public final static class PopulatedVisitor<T extends MessageVisitor> extends DelegatingMessageVisitor<T> {
98
99 PopulatedVisitor(T visitor) {
100 super(visitor);
101 }
102
103 @Override
104 public boolean start(Group group, Location location) throws HL7Exception {
105 return !group.isEmpty() && super.start(group, location);
106 }
107
108 @Override
109 public boolean end(Group group, Location location) throws HL7Exception {
110 return group.isEmpty() || super.end(group, location);
111 }
112
113 @Override
114 public boolean start(Segment segment, Location location) throws HL7Exception {
115 return !segment.isEmpty() && super.start(segment, location);
116 }
117
118 @Override
119 public boolean end(Segment segment, Location location) throws HL7Exception {
120 return segment.isEmpty() || super.end(segment, location);
121 }
122
123 @Override
124 public final boolean start(Composite type, Location location) throws HL7Exception {
125 return !type.isEmpty() && super.start(type, location);
126 }
127
128 @Override
129 public boolean end(Composite type, Location location) throws HL7Exception {
130 return type.isEmpty() || super.end(type, location);
131 }
132
133 @Override
134 public boolean visit(Primitive type, Location location) throws HL7Exception {
135 return type.isEmpty() || super.visit(type, location);
136 }
137 }
138 }