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
28 package ca.uhn.hl7v2.util;
29
30 import java.util.*;
31 import ca.uhn.hl7v2.model.*;
32 import ca.uhn.hl7v2.HL7Exception;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class MessageNavigator {
50
51 private final Group root;
52 private Stack<GroupContext> ancestors;
53 private int currentChild;
54 private Group currentGroup;
55 private String[] childNames;
56
57
58
59
60
61
62
63 public MessageNavigator(Group root) {
64 this.root = root;
65 reset();
66 }
67
68 public Group getRoot() {
69 return this.root;
70 }
71
72
73
74
75
76
77
78 public void drillDown(int childNumber, int rep) throws HL7Exception {
79 if (childNumber != -1) {
80 Structure s = currentGroup.get(childNames[childNumber], rep);
81 if (!(s instanceof Group)) {
82 throw new HL7Exception("Can't drill into segment");
83 }
84 Group="../../../../ca/uhn/hl7v2/model/Group.html#Group">Group group = (Group) s;
85
86
87 GroupContext gc = new GroupContext(this.currentGroup, this.currentChild);
88 this.ancestors.push(gc);
89
90 this.currentGroup = group;
91 }
92
93 this.currentChild = 0;
94 this.childNames = this.currentGroup.getNames();
95 }
96
97
98
99
100 public void drillDown(int rep) throws HL7Exception {
101 drillDown(this.currentChild, rep);
102 }
103
104
105
106
107
108
109 public boolean drillUp() {
110
111 if (!this.ancestors.empty()) {
112 GroupContext gc = this.ancestors.pop();
113 this.currentGroup = gc.group;
114 this.currentChild = gc.child;
115 this.childNames = this.currentGroup.getNames();
116 return true;
117 } else {
118 if (this.currentChild == -1) {
119 return false;
120 } else {
121 this.currentChild = -1;
122 return true;
123 }
124 }
125 }
126
127
128
129
130 public boolean hasNextChild() {
131 return (this.childNames.length > this.currentChild + 1);
132 }
133
134
135
136
137 public void nextChild() throws HL7Exception {
138 toChild(this.currentChild + 1);
139 }
140
141
142
143
144
145 public String toChild(int child) throws HL7Exception {
146 if (child >= 0 && child < this.childNames.length) {
147 this.currentChild = child;
148 return this.childNames[child];
149 } else {
150 throw new HL7Exception("Can't advance to child " + child + " -- only " + this.childNames.length + " children");
151 }
152 }
153
154
155 public void reset() {
156 this.ancestors = new Stack<>();
157 this.currentGroup = root;
158 this.currentChild = -1;
159 this.childNames = currentGroup.getNames();
160 }
161
162
163
164
165
166 public Structure getCurrentStructure(int rep) throws HL7Exception {
167 if (this.currentChild != -1) {
168 String childName = this.childNames[this.currentChild];
169 return this.currentGroup.get(childName, rep);
170 }
171 return this.currentGroup;
172 }
173
174
175
176
177
178 public Group getCurrentGroup() {
179 return this.currentGroup;
180 }
181
182
183
184
185
186 public Structure[] getCurrentChildReps() throws HL7Exception {
187 if (this.currentGroup == this.root && this.currentChild == -1)
188 throw new HL7Exception("Pointer is at root of navigator: there is no current child");
189
190 String childName = this.childNames[this.currentChild];
191 return this.currentGroup.getAll(childName);
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205 public String iterate(boolean segmentsOnly, boolean loop) throws HL7Exception {
206 Structure start;
207
208 if (this.currentChild == -1) {
209 start = this.currentGroup;
210 } else {
211 start = (this.currentGroup.get(this.childNames[this.currentChild]));
212 }
213
214
215
216 Iterator<Structure> it = new MessageIterator(start, "doesn't exist", false);
217 if (segmentsOnly) {
218 it = new FilterIterator<>(it, new StructurePredicate(Segment.class));
219 }
220
221 if (it.hasNext()) {
222 Structure next = it.next();
223 return drillHere(next);
224 } else if (loop) {
225 this.reset();
226 return "";
227 } else {
228 throw new HL7Exception("End of message reached while iterating without loop");
229 }
230
231 }
232
233
234
235
236
237 private String drillHere(Structure destination) throws HL7Exception {
238 Structure pathElem = destination;
239 Stack<Structure> pathStack = new Stack<>();
240 Stack<MessageIterator.Index> indexStack = new Stack<>();
241 do {
242 MessageIterator.Index index = MessageIterator.getIndex(pathElem.getParent(), pathElem);
243 indexStack.push(index);
244 pathElem = pathElem.getParent();
245 pathStack.push(pathElem);
246 } while (!root.equals(pathElem) && !Message.class.isAssignableFrom(pathElem.getClass()));
247
248 if (!root.equals(pathElem)) {
249 throw new HL7Exception("The destination provided is not under the root of this navigator");
250 }
251
252 this.reset();
253 String retVal = null;
254 while (!pathStack.isEmpty()) {
255 Group"../../../../ca/uhn/hl7v2/model/Group.html#Group">Group parent = (Group) pathStack.pop();
256 MessageIterator.Index index = indexStack.pop();
257 int child = search(parent.getNames(), index.name);
258 if (!pathStack.isEmpty()) {
259 this.drillDown(child, 0);
260 } else {
261 retVal= this.toChild(child);
262 }
263 }
264
265 return retVal;
266 }
267
268
269
270 private int search(Object[] list, Object item) {
271 int found = -1;
272 for (int i = 0; i < list.length && found == -1; i++) {
273 if (list[i].equals(item)) found = i;
274 }
275 return found;
276 }
277
278
279
280
281
282
283 private static class GroupContext {
284 public final Group group;
285 public final int child;
286
287 public GroupContext(Group g, int c) {
288 group = g;
289 child = c;
290 }
291 }
292
293 }