1 package ca.uhn.hl7v2.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.io.PushbackReader;
7 import java.io.Reader;
8 import java.util.Iterator;
9
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class Hl7InputStreamMessageStringIterator implements Iterator<String> {
36
37 @SuppressWarnings("unused")
38 private static final Logger ourLog = LoggerFactory.getLogger(Hl7InputStreamMessageStringIterator.class);
39
40 private final StringBuilder myBuffer = new StringBuilder();
41 private boolean myFoundMessageInBuffer = false;
42 private Boolean myHasNext;
43 private boolean myIgnoreComments;
44 private String myNext;
45 private final Reader myReader;
46
47
48
49
50
51
52
53 public Hl7InputStreamMessageStringIterator(InputStream theInputStream) {
54 this(new InputStreamReader(theInputStream));
55 }
56
57
58
59
60
61
62
63 public Hl7InputStreamMessageStringIterator(Reader theReader) {
64 myReader = new PushbackReader(theReader);
65 }
66
67
68
69
70 public boolean hasNext() {
71 if (myHasNext == null) {
72
73 int next;
74 int prev = -1;
75 int endOfBuffer = -1;
76 boolean inComment = false;
77
78 while (true) {
79 try {
80 next = myReader.read();
81 } catch (IOException e) {
82 throw new ParseFailureError("IOException reading from input", e);
83 }
84
85 if (next == -1) {
86 break;
87 }
88
89 char nextChar = (char) next;
90 if (nextChar == '#' && myIgnoreComments && (prev == -1 || prev == '\n' || prev == '\r')) {
91 inComment = true;
92 continue;
93 }
94
95
96 if (nextChar == 10) {
97 if (myBuffer.length() > 0) {
98 if (myBuffer.charAt(myBuffer.length() - 1) == 13) {
99
100 } else {
101 myBuffer.append((char) 13);
102 }
103 }
104 } else if (inComment) {
105 if (nextChar == 13) {
106 inComment = false;
107 }
108 } else {
109 myBuffer.append(nextChar);
110 }
111
112 prev = next;
113
114 int bLength = myBuffer.length();
115 if (nextChar == 'H' && bLength >= 3) {
116 if (myBuffer.charAt(bLength - 2) == 'S') {
117 if (myBuffer.charAt(bLength - 3) == 'M') {
118 if (myFoundMessageInBuffer) {
119 if (myBuffer.charAt(bLength - 4) < 32) {
120 endOfBuffer = bLength - 3;
121 break;
122 }
123 } else {
124
125
126 myBuffer.delete(0, bLength - 3);
127 myFoundMessageInBuffer = true;
128 }
129 }
130 }
131 }
132
133 }
134
135 if (!myFoundMessageInBuffer) {
136 myHasNext = false;
137 return false;
138 }
139
140 String msgString;
141 if (endOfBuffer > -1) {
142 msgString = myBuffer.substring(0, endOfBuffer);
143 myBuffer.delete(0, endOfBuffer);
144 } else {
145 msgString = myBuffer.toString();
146 myBuffer.setLength(0);
147 }
148
149 if (!msgString.startsWith("MSH")) {
150 myHasNext = Boolean.FALSE;
151 return false;
152 }
153
154 myNext = msgString;
155 myHasNext = Boolean.TRUE;
156
157 }
158 return myHasNext;
159 }
160
161
162
163
164 public String next() {
165 if (!hasNext()) {
166 throw new IllegalStateException();
167 }
168 String retVal = myNext;
169 myNext = null;
170 myHasNext = null;
171 return retVal;
172 }
173
174
175
176
177
178
179
180 public void remove() {
181 throw new UnsupportedOperationException();
182 }
183
184
185
186
187
188 public void setIgnoreComments(boolean theIgnoreComments) {
189 myIgnoreComments = theIgnoreComments;
190 }
191
192 public static class ParseFailureError extends RuntimeException {
193
194 private static final long serialVersionUID = 1L;
195
196 public ParseFailureError(String theMessage, Exception theCause) {
197 super(theMessage, theCause);
198 }
199
200 }
201
202 }