| 1 |  |  package ca.uhn.hl7v2.util; | 
  | 2 |  |   | 
  | 3 |  |  import java.util.NoSuchElementException; | 
  | 4 |  |   | 
  | 5 |  |  import org.slf4j.Logger; | 
  | 6 |  |  import org.slf4j.LoggerFactory; | 
  | 7 |  |   | 
  | 8 |  |  import ca.uhn.hl7v2.HL7Exception; | 
  | 9 |  |  import ca.uhn.hl7v2.model.Group; | 
  | 10 |  |  import ca.uhn.hl7v2.model.Message; | 
  | 11 |  |  import ca.uhn.hl7v2.model.Segment; | 
  | 12 |  |  import ca.uhn.hl7v2.model.Structure; | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |   | 
  | 17 |  |   | 
  | 18 |  |   | 
  | 19 |  |   | 
  | 20 |  |   | 
  | 21 |  |   | 
  | 22 |  |   | 
  | 23 |  |   | 
  | 24 |  |   | 
  | 25 |  |   | 
  | 26 |  |   | 
  | 27 |  |   | 
  | 28 |  |   | 
  | 29 | 8683 |  public class MessageIterator implements java.util.Iterator<Structure> { | 
  | 30 |  |   | 
  | 31 |  |      private Structure currentStructure;  | 
  | 32 |  |      private String direction; | 
  | 33 |  |      private Position next; | 
  | 34 |  |      private boolean handleUnexpectedSegments; | 
  | 35 |  |       | 
  | 36 | 5 |      private static final Logger log = LoggerFactory.getLogger(MessageIterator.class); | 
  | 37 |  |       | 
  | 38 |  |       | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |   | 
  | 43 |  |   | 
  | 44 |  |   | 
  | 45 |  |   | 
  | 46 |  |        | 
  | 47 |  |       | 
  | 48 | 8588 |      public MessageIterator(Structure start, String direction, boolean handleUnexpectedSegments) { | 
  | 49 | 8588 |          this.currentStructure = start; | 
  | 50 | 8588 |          this.direction = direction; | 
  | 51 | 8588 |          this.handleUnexpectedSegments = handleUnexpectedSegments; | 
  | 52 | 8588 |      } | 
  | 53 |  |       | 
  | 54 |  |       | 
  | 55 |  |   | 
  | 56 |  |   | 
  | 57 |  |   | 
  | 58 |  |   | 
  | 59 |  |   | 
  | 60 |  |   | 
  | 61 |  |   | 
  | 62 |  |   | 
  | 63 |  |   | 
  | 64 |  |   | 
  | 65 |  |   | 
  | 66 |  |   | 
  | 67 |  |   | 
  | 68 |  |   | 
  | 69 |  |   | 
  | 70 |  |   | 
  | 71 |  |   | 
  | 72 |  |   | 
  | 73 |  |   | 
  | 74 |  |   | 
  | 75 |  |   | 
  | 76 |  |   | 
  | 77 |  |   | 
  | 78 |  |   | 
  | 79 |  |   | 
  | 80 |  |        | 
  | 81 |  |       | 
  | 82 |  |       | 
  | 83 |  |   | 
  | 84 |  |   | 
  | 85 |  |      public boolean hasNext() { | 
  | 86 | 17426 |          boolean has = true; | 
  | 87 | 17426 |          if (next == null) { | 
  | 88 | 8748 |              if (Group.class.isAssignableFrom(currentStructure.getClass())) { | 
  | 89 | 3543 |                  groupNext((Group) currentStructure); | 
  | 90 |  |              } else { | 
  | 91 | 5205 |                  Group parent = currentStructure.getParent(); | 
  | 92 | 5205 |                  Index i = getIndex(parent, currentStructure); | 
  | 93 | 5205 |                  Position currentPosition = new Position(parent, i); | 
  | 94 |  |                   | 
  | 95 |  |                  try {                     | 
  | 96 | 5205 |                      if (parent.isRepeating(i.name) && currentStructure.getName().equals(direction)) { | 
  | 97 | 5 |                          nextRep(currentPosition); | 
  | 98 |  |                      } else { | 
  | 99 | 5200 |                          has = nextPosition(currentPosition, this.direction, this.handleUnexpectedSegments); | 
  | 100 |  |                      } | 
  | 101 | 0 |                  } catch (HL7Exception e) { | 
  | 102 | 0 |                      throw new Error("HL7Exception arising from bad index: " + e.getMessage()); | 
  | 103 | 5205 |                  } | 
  | 104 |  |              } | 
  | 105 |  |          } | 
  | 106 | 17426 |          log.debug("MessageIterator.hasNext() in direction {}? {}", direction, has); | 
  | 107 | 17426 |          return has; | 
  | 108 |  |      } | 
  | 109 |  |       | 
  | 110 |  |       | 
  | 111 |  |   | 
  | 112 |  |   | 
  | 113 |  |   | 
  | 114 |  |      private void groupNext(Group current) { | 
  | 115 | 3543 |          next = new Position(current, current.getNames()[0], 0); | 
  | 116 | 3543 |      } | 
  | 117 |  |       | 
  | 118 |  |       | 
  | 119 |  |   | 
  | 120 |  |    | 
  | 121 |  |      private void nextRep(Position current) {         | 
  | 122 | 10 |          next = new Position(current.parent, current.index.name, current.index.rep + 1); | 
  | 123 | 10 |      } | 
  | 124 |  |       | 
  | 125 |  |       | 
  | 126 |  |   | 
  | 127 |  |   | 
  | 128 |  |   | 
  | 129 |  |   | 
  | 130 |  |      private boolean nextPosition(Position currPos, String direction, boolean makeNewSegmentIfNeeded) throws HL7Exception { | 
  | 131 | 6195 |          boolean nextExists = true; | 
  | 132 | 6195 |          if (isLast(currPos)) { | 
  | 133 | 1020 |              nextExists = nextFromGroupEnd(currPos, direction, makeNewSegmentIfNeeded); | 
  | 134 |  |          } else { | 
  | 135 | 5175 |              nextSibling(currPos); | 
  | 136 |  |          } | 
  | 137 | 6195 |          return nextExists; | 
  | 138 |  |      } | 
  | 139 |  |       | 
  | 140 |  |       | 
  | 141 |  |      private boolean nextFromGroupEnd(Position currPos, String direction, boolean makeNewSegmentIfNeeded) throws HL7Exception { | 
  | 142 | 1020 |          assert isLast(currPos); | 
  | 143 | 1020 |          boolean nextExists = true; | 
  | 144 |  |           | 
  | 145 |  |           | 
  | 146 |  |           | 
  | 147 |  |           | 
  | 148 | 1020 |          if (!makeNewSegmentIfNeeded && Message.class.isAssignableFrom(currPos.parent.getClass())) { | 
  | 149 | 10 |              nextExists = false; | 
  | 150 | 1010 |          } else if (!makeNewSegmentIfNeeded || matchExistsAfterPosition(currPos, direction, false, true)) {      | 
  | 151 | 1000 |              Group grandparent = currPos.parent.getParent(); | 
  | 152 | 1000 |              Index parentIndex = getIndex(grandparent, currPos.parent); | 
  | 153 | 1000 |              Position parentPos = new Position(grandparent, parentIndex); | 
  | 154 |  |               | 
  | 155 |  |              try { | 
  | 156 | 1000 |                  boolean parentRepeats = parentPos.parent.isRepeating(parentPos.index.name);                 | 
  | 157 | 1000 |                  if (parentRepeats && contains(parentPos.parent.get(parentPos.index.name, 0), direction, false, true)) { | 
  | 158 | 5 |                      nextRep(parentPos); | 
  | 159 |  |                  } else { | 
  | 160 | 995 |                      nextExists = nextPosition(parentPos, direction, makeNewSegmentIfNeeded); | 
  | 161 |  |                  } | 
  | 162 | 0 |              } catch (HL7Exception e) { | 
  | 163 | 0 |                  throw new Error("HL7Exception arising from bad index: " + e.getMessage()); | 
  | 164 | 1000 |              } | 
  | 165 | 1000 |          } else { | 
  | 166 | 10 |              newSegment(currPos.parent, direction); | 
  | 167 |  |          } | 
  | 168 | 1020 |          return nextExists; | 
  | 169 |  |      } | 
  | 170 |  |       | 
  | 171 |  |       | 
  | 172 |  |   | 
  | 173 |  |   | 
  | 174 |  |   | 
  | 175 |  |   | 
  | 176 |  |   | 
  | 177 |  |   | 
  | 178 |  |   | 
  | 179 |  |   | 
  | 180 |  |   | 
  | 181 |  |   | 
  | 182 |  |   | 
  | 183 |  |   | 
  | 184 |  |      public static boolean matchExistsAfterPosition(Position pos, String name, boolean firstDescendentsOnly, boolean upToFirstRequired) throws HL7Exception { | 
  | 185 | 140 |          boolean matchExists = false; | 
  | 186 |  |           | 
  | 187 |  |           | 
  | 188 | 140 |          if (pos.parent.isRepeating(pos.index.name)) {             | 
  | 189 | 55 |              Structure s = pos.parent.get(pos.index.name, pos.index.rep); | 
  | 190 | 55 |              matchExists = contains(s, name, firstDescendentsOnly, upToFirstRequired); | 
  | 191 |  |          }  | 
  | 192 |  |           | 
  | 193 |  |           | 
  | 194 | 140 |          if (!matchExists) { | 
  | 195 | 130 |              String[] siblings = pos.parent.getNames(); | 
  | 196 | 130 |              boolean after = false; | 
  | 197 | 1135 |              for (int i = 0; i < siblings.length && !matchExists; i++) { | 
  | 198 | 1030 |                  if (after) { | 
  | 199 | 200 |                      matchExists = contains(pos.parent.get(siblings[i]), name, firstDescendentsOnly, upToFirstRequired); | 
  | 200 | 200 |                      if (upToFirstRequired && pos.parent.isRequired(siblings[i])) break;  | 
  | 201 |  |                  } | 
  | 202 | 1005 |                  if (pos.index.name.equals(siblings[i])) after = true;                 | 
  | 203 |  |              }  | 
  | 204 |  |          } | 
  | 205 |  |           | 
  | 206 |  |           | 
  | 207 | 140 |          if (!matchExists && !Message.class.isAssignableFrom(pos.parent.getClass())) { | 
  | 208 | 75 |              Group grandparent = pos.parent.getParent(); | 
  | 209 | 75 |              Position parentPos = new Position(grandparent, getIndex(grandparent, pos.parent)); | 
  | 210 | 75 |              matchExists = matchExistsAfterPosition(parentPos, name, firstDescendentsOnly, upToFirstRequired); | 
  | 211 |  |          } | 
  | 212 | 140 |          log.debug("Match exists after position {} for {}? {}", new Object[] {pos, name, matchExists}); | 
  | 213 | 140 |          return matchExists; | 
  | 214 |  |      } | 
  | 215 |  |       | 
  | 216 |  |       | 
  | 217 |  |   | 
  | 218 |  |   | 
  | 219 |  |   | 
  | 220 |  |      private void newSegment(Group parent, String name) throws HL7Exception { | 
  | 221 | 10 |          log.info("MessageIterator creating new segment: {}", name); | 
  | 222 | 10 |          parent.addNonstandardSegment(name); | 
  | 223 | 10 |          next = new Position(parent, parent.getNames()[parent.getNames().length-1], 0); | 
  | 224 | 10 |      } | 
  | 225 |  |       | 
  | 226 |  |       | 
  | 227 |  |   | 
  | 228 |  |   | 
  | 229 |  |   | 
  | 230 |  |   | 
  | 231 |  |   | 
  | 232 |  |   | 
  | 233 |  |   | 
  | 234 |  |   | 
  | 235 |  |   | 
  | 236 |  |   | 
  | 237 |  |   | 
  | 238 |  |   | 
  | 239 |  |   | 
  | 240 |  |      public static boolean contains(Structure s, String name, boolean firstDescendentsOnly, boolean upToFirstRequired) { | 
  | 241 | 840 |          boolean contains = false; | 
  | 242 | 840 |          if (Segment.class.isAssignableFrom(s.getClass())) { | 
  | 243 | 660 |              if (s.getName().equals(name)) contains = true;             | 
  | 244 |  |          } else { | 
  | 245 | 180 |              Group g = (Group) s; | 
  | 246 | 180 |              String[] names = g.getNames(); | 
  | 247 | 550 |              for (int i = 0; i < names.length && !contains; i++) { | 
  | 248 |  |                  try { | 
  | 249 | 495 |                      contains = contains(g.get(names[i], 0), name, firstDescendentsOnly, upToFirstRequired);                 | 
  | 250 | 495 |                      if (firstDescendentsOnly) break; | 
  | 251 | 470 |                      if (upToFirstRequired && g.isRequired(names[i])) break;  | 
  | 252 | 0 |                  } catch (HL7Exception e) { | 
  | 253 | 0 |                      throw new Error("HL7Exception due to bad index: " + e.getMessage()); | 
  | 254 | 370 |                  } | 
  | 255 |  |              } | 
  | 256 |  |          } | 
  | 257 | 840 |          return contains; | 
  | 258 |  |      } | 
  | 259 |  |       | 
  | 260 |  |       | 
  | 261 |  |   | 
  | 262 |  |   | 
  | 263 |  |   | 
  | 264 |  |      public static boolean isLast(Position p) { | 
  | 265 | 7225 |          String[] names = p.parent.getNames(); | 
  | 266 | 7225 |          return names[names.length-1].equals(p.index.name); | 
  | 267 |  |      } | 
  | 268 |  |       | 
  | 269 |  |       | 
  | 270 |  |   | 
  | 271 |  |   | 
  | 272 |  |   | 
  | 273 |  |      private void nextSibling(Position pos) { | 
  | 274 | 5175 |          String[] names = pos.parent.getNames(); | 
  | 275 | 5175 |          int i = 0; | 
  | 276 | 11920 |          for (; i < names.length && !names[i].equals(pos.index.name); i++) {} | 
  | 277 | 5175 |          String nextName = names[i+1]; | 
  | 278 |  |           | 
  | 279 | 5175 |          this.next = new Position(pos.parent, nextName, 0); | 
  | 280 | 5175 |      } | 
  | 281 |  |       | 
  | 282 |  |       | 
  | 283 |  |   | 
  | 284 |  |   | 
  | 285 |  |   | 
  | 286 |  |   | 
  | 287 |  |   | 
  | 288 |  |   | 
  | 289 |  |   | 
  | 290 |  |   | 
  | 291 |  |   | 
  | 292 |  |   | 
  | 293 |  |   | 
  | 294 |  |   | 
  | 295 |  |   | 
  | 296 |  |   | 
  | 297 |  |   | 
  | 298 |  |   | 
  | 299 |  |   | 
  | 300 |  |   | 
  | 301 |  |   | 
  | 302 |  |   | 
  | 303 |  |   | 
  | 304 |  |   | 
  | 305 |  |   | 
  | 306 |  |      public Structure next() { | 
  | 307 | 8738 |          if (!hasNext()) { | 
  | 308 | 5 |              throw new NoSuchElementException("No more nodes in message"); | 
  | 309 |  |          } | 
  | 310 |  |          try { | 
  | 311 | 8733 |              this.currentStructure = next.parent.get(next.index.name, next.index.rep); | 
  | 312 | 0 |          } catch (HL7Exception e) { | 
  | 313 | 0 |              throw new NoSuchElementException("HL7Exception: " + e.getMessage()); | 
  | 314 | 8733 |          } | 
  | 315 | 8733 |          clearNext(); | 
  | 316 | 8733 |          return this.currentStructure; | 
  | 317 |  |      } | 
  | 318 |  |       | 
  | 319 |  |       | 
  | 320 |  |      public void remove() { | 
  | 321 | 0 |          throw new UnsupportedOperationException("Can't remove a node from a message"); | 
  | 322 |  |      } | 
  | 323 |  |       | 
  | 324 |  |      public String getDirection() { | 
  | 325 | 0 |          return this.direction; | 
  | 326 |  |      } | 
  | 327 |  |       | 
  | 328 |  |      public void setDirection(String direction) { | 
  | 329 | 45 |          clearNext(); | 
  | 330 | 45 |          this.direction = direction; | 
  | 331 | 45 |      } | 
  | 332 |  |       | 
  | 333 |  |      private void clearNext() { | 
  | 334 | 8778 |          next = null; | 
  | 335 | 8778 |      } | 
  | 336 |  |       | 
  | 337 |  |       | 
  | 338 |  |   | 
  | 339 |  |   | 
  | 340 |  |   | 
  | 341 |  |      public static Index getIndex(Group parent, Structure child) { | 
  | 342 | 26638 |          Index index = null; | 
  | 343 | 26638 |          String[] names = parent.getNames(); | 
  | 344 | 61148 |          findChild : for (int i = 0; i < names.length; i++) { | 
  | 345 | 61143 |              if (names[i].startsWith(child.getName())) { | 
  | 346 |  |                  try { | 
  | 347 | 26668 |                      Structure[] reps = parent.getAll(names[i]); | 
  | 348 | 26698 |                      for (int j = 0; j < reps.length; j++) { | 
  | 349 | 26663 |                          if (child.equals(reps[j])) { | 
  | 350 | 26633 |                              index = new Index(names[i], j); | 
  | 351 | 26633 |                              break findChild;  | 
  | 352 |  |                          } | 
  | 353 |  |                      } | 
  | 354 | 0 |                  } catch (HL7Exception e) { | 
  | 355 | 0 |                      log.error(e.getMessage(), e); | 
  | 356 | 0 |                      throw new Error("Internal HL7Exception finding structure index: " + e.getMessage()); | 
  | 357 | 35 |                  } | 
  | 358 |  |              } | 
  | 359 |  |          } | 
  | 360 | 26638 |          return index; | 
  | 361 |  |      } | 
  | 362 |  |       | 
  | 363 |  |       | 
  | 364 |  |   | 
  | 365 |  |   | 
  | 366 |  |   | 
  | 367 |  |      public static class Index { | 
  | 368 |  |          public String name; | 
  | 369 |  |          public int rep; | 
  | 370 | 35391 |          public Index(String name, int rep) { | 
  | 371 | 35391 |              this.name = name; | 
  | 372 | 35391 |              this.rep = rep; | 
  | 373 | 35391 |          } | 
  | 374 |  |           | 
  | 375 |  |           | 
  | 376 |  |          public boolean equals(Object o) { | 
  | 377 | 5 |              boolean equals = false; | 
  | 378 | 5 |              if (o != null && o instanceof Index) { | 
  | 379 | 5 |                  Index i = (Index) o; | 
  | 380 | 5 |                  if (i.rep == rep && i.name.equals(name)) equals = true; | 
  | 381 |  |              } | 
  | 382 | 5 |              return equals; | 
  | 383 |  |          } | 
  | 384 |  |           | 
  | 385 |  |           | 
  | 386 |  |          public int hashCode() { | 
  | 387 | 0 |              return name.hashCode() + 700 * rep; | 
  | 388 |  |          } | 
  | 389 |  |           | 
  | 390 |  |                   | 
  | 391 |  |          public String toString() { | 
  | 392 | 0 |              return this.name + ":" + this.rep; | 
  | 393 |  |          } | 
  | 394 |  |      } | 
  | 395 |  |       | 
  | 396 |  |       | 
  | 397 |  |   | 
  | 398 |  |   | 
  | 399 |  |      public static class Position { | 
  | 400 |  |          public Group parent; | 
  | 401 |  |          public Index index; | 
  | 402 | 8753 |          public Position(Group parent, String name, int rep) { | 
  | 403 | 8753 |              this.parent = parent; | 
  | 404 | 8753 |              this.index = new Index(name, rep); | 
  | 405 | 8753 |          } | 
  | 406 | 6280 |          public Position(Group parent, Index i) { | 
  | 407 | 6280 |              this.parent = parent; | 
  | 408 | 6280 |              this.index = i; | 
  | 409 | 6280 |          } | 
  | 410 |  |   | 
  | 411 |  |           | 
  | 412 |  |          public boolean equals(Object o) { | 
  | 413 | 0 |              boolean equals = false; | 
  | 414 | 0 |              if (o != null && o instanceof Position) { | 
  | 415 | 0 |                  Position p = (Position) o; | 
  | 416 | 0 |                  if (p.parent.equals(parent) && p.index.equals(index)) equals = true; | 
  | 417 |  |              } | 
  | 418 | 0 |              return equals; | 
  | 419 |  |          } | 
  | 420 |  |           | 
  | 421 |  |           | 
  | 422 |  |          public int hashCode() { | 
  | 423 | 0 |              return parent.hashCode() + index.hashCode(); | 
  | 424 |  |          } | 
  | 425 |  |           | 
  | 426 |  |          public String toString() { | 
  | 427 | 0 |              StringBuffer ret = new StringBuffer(parent.getName()); | 
  | 428 | 0 |              ret.append(":"); | 
  | 429 | 0 |              ret.append(index.name); | 
  | 430 | 0 |              ret.append("("); | 
  | 431 | 0 |              ret.append(index.rep); | 
  | 432 | 0 |              ret.append(")"); | 
  | 433 | 0 |              return ret.toString();            | 
  | 434 |  |          } | 
  | 435 |  |      } | 
  | 436 |  |  } |