1 /*
2 The contents of this file are subject to the Mozilla Public License Version 1.1
3 (the "License"); you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at http://www.mozilla.org/MPL/
5 Software distributed under the License is distributed on an "AS IS" basis,
6 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7 specific language governing rights and limitations under the License.
8
9 The Original Code is "MessageVisitors.java ". Description:
10 "Static methods for working with MessageVisitor"
11
12 The Initial Developer of the Original Code is University Health Network. Copyright (C)
13 2013. All Rights Reserved.
14
15 Contributor(s): ______________________________________.
16
17 Alternatively, the contents of this file may be used under the terms of the
18 GNU General Public License (the "GPL"), in which case the provisions of the GPL are
19 applicable instead of those above. If you wish to allow use of your version of this
20 file only under the terms of the GPL and not to allow others to use your version
21 of this file under the MPL, indicate your decision by deleting the provisions above
22 and replace them with the notice and other provisions required by the GPL License.
23 If you do not delete the provisions above, a recipient may use your version of
24 this file under either the MPL or the GPL.
25 */
26
27 package ca.uhn.hl7v2.model;
28
29 import ca.uhn.hl7v2.HL7Exception;
30 import ca.uhn.hl7v2.Location;
31
32 /**
33 * Static methods for working with MessageVisitor
34 */
35 public final class MessageVisitors {
36
37 private MessageVisitors() {}
38
39 /**
40 * Lets a visitor visit a message of parts thereof.
41 *
42 * @param visitor MessageVisitor
43 * @param visitable message element to be visited
44 * @return the MessageVisitor
45 * @throws HL7Exception if an error occurs while visiting
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 * Returns a MessageVisitor that only visits structures
54 * @param visitor delegate MessageVisitor instance
55 * @return MessageVisitor that only visits structures
56 */
57 public static <T extends MessageVisitor> StructuresVisitor<T> visitStructures(T visitor) {
58 return new StructuresVisitor<>(visitor);
59 }
60
61 /**
62 * Returns a MessageVisitor that only visits populated message elements.
63 * You can further nest delegate visitors, e.g.
64 * <code>structures(populated(new MyVisitor()))</code>
65 *
66 * @param visitor delegate MessageVisitor instance
67 * @return MessageVisitor that only visits populated message elements
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; // don't descend into fields
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 }