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.parser;
28
29 import ca.uhn.hl7v2.DefaultHapiContext;
30 import ca.uhn.hl7v2.HL7Exception;
31 import ca.uhn.hl7v2.HapiContext;
32 import ca.uhn.hl7v2.model.Message;
33 import ca.uhn.hl7v2.model.Segment;
34 import ca.uhn.hl7v2.model.Type;
35 import ca.uhn.hl7v2.validation.ValidationContext;
36 import ca.uhn.hl7v2.validation.impl.NoValidation;
37 import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
38
39
40
41
42
43
44
45
46
47 public class GenericParser extends Parser {
48
49 private Parser primaryParser;
50 private Parser secondaryParser;
51 private final PipeParser pipeParser;
52 private final XMLParser xmlParser;
53
54
55 public GenericParser() {
56 this(new DefaultHapiContext());
57 }
58
59
60
61
62 public GenericParser(HapiContext context) {
63 super(context);
64 pipeParser = new PipeParser(context);
65 xmlParser = new DefaultXMLParser(context);
66 setPipeParserAsPrimary();
67 }
68
69
70
71
72
73
74 public GenericParser(ModelClassFactory theFactory) {
75 super(theFactory);
76
77 pipeParser = new PipeParser(theFactory);
78 xmlParser = new DefaultXMLParser(theFactory);
79 setPipeParserAsPrimary();
80 }
81
82
83
84
85
86
87 public void setXMLParserAsPrimary() {
88 primaryParser = xmlParser;
89 secondaryParser = pipeParser;
90 }
91
92
93
94
95
96
97 public void setPipeParserAsPrimary() {
98 primaryParser = pipeParser;
99 secondaryParser = xmlParser;
100 }
101
102
103
104
105
106
107 public boolean isPipeParserPrimary() {
108 return primaryParser == pipeParser;
109 }
110
111
112
113
114
115
116
117 public void setValidationContext(ValidationContext theContext) {
118 super.setValidationContext(theContext);
119
120
121 if (xmlParser != null) {
122 pipeParser.setValidationContext(theContext);
123 xmlParser.setValidationContext(theContext);
124 }
125 }
126
127
128
129
130
131
132 private Parser getAppropriateParser(String message) throws HL7Exception {
133 String encoding = getEncoding(message);
134 if ("VB".equalsIgnoreCase(encoding)) return pipeParser;
135 if ("XML".equalsIgnoreCase(encoding)) return xmlParser;
136 throw new HL7Exception("Can't find appropriate parser - encoding not recognized");
137 }
138
139
140
141
142
143
144
145
146
147 protected String doEncode(Message source, String encoding) throws HL7Exception,
148 EncodingNotSupportedException {
149 String ret;
150 if (encoding == null)
151 encoding = "";
152 if (encoding.equalsIgnoreCase("VB")) {
153 ret = pipeParser.doEncode(source);
154 } else if (encoding.equalsIgnoreCase("XML")) {
155 ret = xmlParser.doEncode(source);
156 } else {
157 throw new EncodingNotSupportedException("The encoding " + encoding
158 + " is not supported by " + this.getClass().getName());
159 }
160 return ret;
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 public Segment getCriticalResponseData(String message) throws HL7Exception {
180 return getAppropriateParser(message).getCriticalResponseData(message);
181 }
182
183
184
185
186
187
188
189
190 public String getVersion(String message) throws HL7Exception {
191 return getAppropriateParser(message).getVersion(message);
192 }
193
194
195
196
197
198
199
200
201
202 public String getEncoding(String message) {
203 String encoding = primaryParser.getEncoding(message);
204 return (encoding == null ? secondaryParser.getEncoding(message) : encoding);
205 }
206
207
208
209
210
211
212
213
214
215
216 public String getAckID(String message) {
217 try {
218 return getAppropriateParser(message).getAckID(message);
219 } catch (HL7Exception e) {
220 return null;
221 }
222 }
223
224
225
226
227 public boolean supportsEncoding(String encoding) {
228 return (primaryParser.supportsEncoding(encoding) || secondaryParser.supportsEncoding(encoding));
229 }
230
231
232
233
234 public String getDefaultEncoding() {
235 return primaryParser.getDefaultEncoding();
236 }
237
238
239
240
241
242
243
244 protected Message doParse(String message, String version) throws HL7Exception {
245 return getAppropriateParser(message).doParse(message, version);
246 }
247
248
249
250
251 @Override
252 public Message parse(String theMessage) throws HL7Exception {
253 Message retVal = super.parse(theMessage);
254 Parser parser = getAppropriateParser(theMessage);
255 retVal.setParser(parser);
256 return retVal;
257 }
258
259
260
261
262
263
264
265 protected String doEncode(Message source) throws HL7Exception {
266 return primaryParser.doEncode(source);
267 }
268
269
270
271
272 @Override
273 public String doEncode(Segment structure, EncodingCharacters encodingCharacters)
274 throws HL7Exception {
275 return primaryParser.doEncode(structure, encodingCharacters);
276 }
277
278
279
280
281 @Override
282 public String doEncode(Type type, EncodingCharacters encodingCharacters) throws HL7Exception {
283 return primaryParser.doEncode(type, encodingCharacters);
284 }
285
286
287
288
289 @Override
290 public void parse(Type type, String string, EncodingCharacters encodingCharacters)
291 throws HL7Exception {
292 primaryParser.parse(type, string, encodingCharacters);
293 }
294
295
296
297
298 @Override
299 public void parse(Segment segment, String string, EncodingCharacters encodingCharacters)
300 throws HL7Exception {
301 primaryParser.parse(segment, string, encodingCharacters);
302 }
303
304 @Override
305 public void parse(Message message, String string) throws HL7Exception {
306 primaryParser.parse(message, string);
307 }
308
309
310
311
312
313
314
315 public static GenericParser getInstanceWithNoValidation() {
316 return new GenericParser(
317 new DefaultHapiContext(ValidationContextFactory.noValidation()));
318 }
319
320 @Override
321 protected Message doParseForSpecificPackage(String theMessage, String theVersion,
322 String thePackageName) throws HL7Exception {
323 return primaryParser.doParseForSpecificPackage(theMessage, theVersion, thePackageName);
324 }
325
326 public static void main(String[] args) throws HL7Exception {
327
328 String msgString = "MSH|^~\\&|RAMSOFT|SENDING FACILITY|RAMSOFT|RECEIVING FACILITY|20101223202939-0400||ADT^A08|101|P|2.3.1||||||||\r"
329 + "EVN|A08|20101223202939-0400||||\r"
330 + "PID||P12345^^^ISSUER|P12345^^^ISSUER||PATIENT^TEST^M^^^^||19741018|M|||10808 FOOTHILL BLVD^^RANCHO CUCAMONGA^CA^91730^US||(909)481-5872^^^sales@ramsoft.com|(909)481-5800x1||M||12345|286-50-9510|||\r"
331 + "PV1||O||||||||||||||||||||||||||||||||||||||||||||||||||\r"
332 + "AL1|1||^PORK^|\r"
333 + "AL1|2||^PENICILLIN^|";
334
335 GenericParser parser = new GenericParser();
336 parser.setValidationContext(ValidationContextFactory.noValidation());
337 Message msg = parser.parse(msgString);
338 System.out.println(msg.getClass().getName());
339
340 }
341
342
343
344 }