1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
package ca.uhn.hl7v2.parser; |
25 | |
|
26 | |
import java.io.File; |
27 | |
import java.io.FileReader; |
28 | |
import java.util.ArrayList; |
29 | |
import java.util.HashSet; |
30 | |
import java.util.List; |
31 | |
import java.util.Set; |
32 | |
|
33 | |
import ca.uhn.hl7v2.DefaultHapiContext; |
34 | |
import org.slf4j.Logger; |
35 | |
import org.slf4j.LoggerFactory; |
36 | |
import org.w3c.dom.DOMException; |
37 | |
import org.w3c.dom.Document; |
38 | |
import org.w3c.dom.Element; |
39 | |
import org.w3c.dom.Node; |
40 | |
import org.w3c.dom.NodeList; |
41 | |
|
42 | |
import ca.uhn.hl7v2.HL7Exception; |
43 | |
import ca.uhn.hl7v2.HapiContext; |
44 | |
import ca.uhn.hl7v2.model.Group; |
45 | |
import ca.uhn.hl7v2.model.Message; |
46 | |
import ca.uhn.hl7v2.model.Segment; |
47 | |
import ca.uhn.hl7v2.model.Structure; |
48 | |
import ca.uhn.hl7v2.util.XMLUtils; |
49 | |
import ca.uhn.hl7v2.validation.impl.NoValidation; |
50 | |
import ca.uhn.hl7v2.validation.impl.ValidationContextFactory; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public class DefaultXMLParser extends XMLParser { |
71 | |
|
72 | 5 | private static final Logger log = LoggerFactory.getLogger(DefaultXMLParser.class); |
73 | |
|
74 | |
private static final Set<String> ourForceGroupNames; |
75 | |
|
76 | |
static { |
77 | 5 | ourForceGroupNames = new HashSet<String>(); |
78 | 5 | ourForceGroupNames.add("DIET"); |
79 | 5 | } |
80 | |
|
81 | |
public DefaultXMLParser() { |
82 | 50 | super(); |
83 | 50 | } |
84 | |
|
85 | |
public DefaultXMLParser(HapiContext context) { |
86 | 435 | super(context); |
87 | 435 | } |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public DefaultXMLParser(ModelClassFactory theFactory) { |
95 | 0 | super(theFactory); |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public Document encodeDocument(Message source) throws HL7Exception { |
107 | 125 | String messageClassName = source.getClass().getName(); |
108 | 125 | String messageName = messageClassName.substring(messageClassName.lastIndexOf('.') + 1); |
109 | |
try { |
110 | 125 | Document doc = XMLUtils.emptyDocument(messageName); |
111 | |
|
112 | |
|
113 | 125 | encode(source, doc.getDocumentElement()); |
114 | 125 | return doc; |
115 | 0 | } catch (Exception e) { |
116 | 0 | throw new HL7Exception( |
117 | 0 | "Can't create XML document - " + e.getClass().getName(), e); |
118 | |
} |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
private void encode(Group groupObject, Element groupElement) throws HL7Exception { |
126 | 265 | String[] childNames = groupObject.getNames(); |
127 | 265 | String messageName = groupObject.getMessage().getName(); |
128 | |
|
129 | |
try { |
130 | 2725 | for (String name : childNames) { |
131 | 2460 | Structure[] reps = groupObject.getAll(name); |
132 | 2995 | for (Structure rep : reps) { |
133 | 535 | String elementName = makeGroupElementName(messageName, name); |
134 | |
Element childElement; |
135 | |
try { |
136 | 535 | childElement = groupElement.getOwnerDocument().createElement(elementName); |
137 | 0 | } catch (DOMException e) { |
138 | 0 | throw new HL7Exception( |
139 | 0 | "Can't encode element " + elementName + " in group " + groupObject.getClass().getName(), e); |
140 | 535 | } |
141 | 535 | groupElement.appendChild(childElement); |
142 | 535 | if (rep instanceof Group) { |
143 | 140 | encode((Group) rep, childElement); |
144 | |
} |
145 | 395 | else if (rep instanceof Segment) { |
146 | 395 | encode((Segment) rep, childElement); |
147 | |
} |
148 | |
} |
149 | |
} |
150 | 0 | } catch (DOMException e) { |
151 | 0 | throw new HL7Exception( |
152 | 0 | "Can't encode group " + groupObject.getClass().getName(), e); |
153 | 265 | } |
154 | 265 | } |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public Message parseDocument(Document xmlMessage, String version) throws HL7Exception { |
173 | |
|
174 | 55 | assertNamespaceURI(xmlMessage.getDocumentElement().getNamespaceURI()); |
175 | |
|
176 | 55 | Message message = instantiateMessage(xmlMessage.getDocumentElement().getLocalName(), version, true); |
177 | |
|
178 | |
|
179 | |
|
180 | 55 | parse(message, xmlMessage.getDocumentElement()); |
181 | 55 | return message; |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
private void parse(Group groupObject, Element groupElement) throws HL7Exception { |
189 | 150 | String[] childNames = groupObject.getNames(); |
190 | 150 | String messageName = groupObject.getMessage().getName(); |
191 | |
|
192 | 150 | NodeList allChildNodes = groupElement.getChildNodes(); |
193 | 150 | List<String> unparsedElementList = new ArrayList<String>(); |
194 | 920 | for (int i = 0; i < allChildNodes.getLength(); i++) { |
195 | 770 | Node node = allChildNodes.item(i); |
196 | 770 | String name = node.getLocalName(); |
197 | 770 | if (node.getNodeType() == Node.ELEMENT_NODE && !unparsedElementList.contains(name)) { |
198 | 280 | assertNamespaceURI(node.getNamespaceURI()); |
199 | 280 | unparsedElementList.add(name); |
200 | |
} |
201 | |
} |
202 | |
|
203 | |
|
204 | 1290 | for (String nextChildName : childNames) { |
205 | 1140 | String childName = nextChildName; |
206 | 1140 | if(groupObject.isGroup(nextChildName)) { |
207 | 205 | childName = makeGroupElementName(groupObject.getMessage().getName(), nextChildName); |
208 | |
} |
209 | 1140 | unparsedElementList.remove(childName); |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | 1140 | if (nextChildName.length() == 4 && Character.isDigit(nextChildName.charAt(3))) { |
215 | 115 | log.trace("Skipping rep segment: {}", nextChildName); |
216 | |
} else { |
217 | 1025 | parseReps(groupElement, groupObject, messageName, nextChildName, nextChildName); |
218 | |
} |
219 | |
} |
220 | |
|
221 | 150 | for (String segName : unparsedElementList) { |
222 | 5 | String segIndexName = groupObject.addNonstandardSegment(segName); |
223 | 5 | parseReps(groupElement, groupObject, messageName, segName, segIndexName); |
224 | 5 | } |
225 | 150 | } |
226 | |
|
227 | |
|
228 | |
private void parseReps(Element groupElement, Group groupObject, |
229 | |
String messageName, String childName, String childIndexName) throws HL7Exception { |
230 | |
|
231 | 1030 | String groupName = makeGroupElementName(messageName, childName); |
232 | 1030 | List<Element> reps = getChildElementsByTagName(groupElement, groupName); |
233 | 1030 | log.trace("# of elements matching {}: {}", groupName, reps.size()); |
234 | |
|
235 | 1030 | if (groupObject.isRepeating(childIndexName)) { |
236 | 590 | for (int i = 0; i < reps.size(); i++) { |
237 | 95 | parseRep(reps.get(i), groupObject.get(childIndexName, i)); |
238 | |
} |
239 | |
} else { |
240 | 535 | if (reps.size() > 0) { |
241 | 205 | parseRep(reps.get(0), groupObject.get(childIndexName, 0)); |
242 | |
} |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | 535 | if (reps.size() > 1) { |
251 | |
String newIndexName; |
252 | 10 | int i=1; |
253 | |
try { |
254 | 20 | for (i = 1; i < reps.size(); i++) { |
255 | 10 | newIndexName = childName+(i+1); |
256 | 10 | Structure st = groupObject.get(newIndexName); |
257 | 10 | parseRep(reps.get(i), st); |
258 | |
} |
259 | 0 | } catch(Throwable t) { |
260 | 0 | log.info("Issue Parsing: " + t); |
261 | 0 | newIndexName = groupObject.addNonstandardSegment(childName); |
262 | 0 | for (int j = i; j < reps.size(); j++) { |
263 | 0 | parseRep(reps.get(j), groupObject.get(newIndexName, j-i)); |
264 | |
} |
265 | 10 | } |
266 | |
} |
267 | |
|
268 | |
} |
269 | 1030 | } |
270 | |
|
271 | |
private void parseRep(Element theElem, Structure theObj) throws HL7Exception { |
272 | 310 | if (theObj instanceof Group) { |
273 | 80 | parse((Group) theObj, theElem); |
274 | |
} |
275 | 230 | else if (theObj instanceof Segment) { |
276 | 230 | parse((Segment) theObj, theElem); |
277 | |
} |
278 | 310 | log.trace("Parsed element: {}", theElem.getNodeName()); |
279 | 310 | } |
280 | |
|
281 | |
|
282 | |
private List<Element> getChildElementsByTagName(Element theElement, String theName) throws HL7Exception { |
283 | 1030 | List<Element> result = new ArrayList<Element>(10); |
284 | 1030 | NodeList children = theElement.getChildNodes(); |
285 | |
|
286 | 8390 | for (int i = 0; i < children.getLength(); i++) { |
287 | 7360 | Node child = children.item(i); |
288 | 7360 | if (child.getNodeType() == Node.ELEMENT_NODE && child.getLocalName().equals(theName)) { |
289 | 310 | assertNamespaceURI(child.getNamespaceURI()); |
290 | 310 | result.add((Element)child); |
291 | |
} |
292 | |
} |
293 | |
|
294 | 1030 | return result; |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
protected static String makeGroupElementName(String messageName, String className) { |
316 | |
String ret; |
317 | |
|
318 | 1770 | if (className.length() > 4 || ourForceGroupNames.contains(className)) { |
319 | 550 | StringBuilder elementName = new StringBuilder(); |
320 | 550 | elementName.append(messageName); |
321 | 550 | elementName.append('.'); |
322 | 550 | elementName.append(className); |
323 | 550 | ret = elementName.toString(); |
324 | 550 | } else if (className.length() == 4) { |
325 | |
|
326 | |
|
327 | |
|
328 | 5 | ret = className.substring(0,3); |
329 | |
} else { |
330 | 1215 | ret = className; |
331 | |
} |
332 | |
|
333 | 1770 | return ret; |
334 | |
} |
335 | |
|
336 | |
|
337 | |
public static void main(String args[]) { |
338 | 0 | if (args.length != 1) { |
339 | 0 | System.out.println("Usage: DefaultXMLParser pipe_encoded_file"); |
340 | 0 | System.exit(1); |
341 | |
} |
342 | |
|
343 | |
|
344 | |
try { |
345 | 0 | File messageFile = new File(args[0]); |
346 | 0 | long fileLength = messageFile.length(); |
347 | 0 | FileReader r = new FileReader(messageFile); |
348 | 0 | char[] cbuf = new char[(int) fileLength]; |
349 | 0 | System.out.println("Reading message file ... " + r.read(cbuf) + " of " + fileLength + " chars"); |
350 | 0 | r.close(); |
351 | 0 | String messString = String.valueOf(cbuf); |
352 | |
|
353 | 0 | Parser inParser = null; |
354 | 0 | Parser outParser = null; |
355 | 0 | PipeParser pp = new PipeParser(); |
356 | 0 | ca.uhn.hl7v2.parser.XMLParser xp = new DefaultXMLParser(); |
357 | 0 | System.out.println("Encoding: " + pp.getEncoding(messString)); |
358 | 0 | if (pp.getEncoding(messString) != null) { |
359 | 0 | inParser = pp; |
360 | 0 | outParser = xp; |
361 | |
} |
362 | 0 | else if (xp.getEncoding(messString) != null) { |
363 | 0 | inParser = xp; |
364 | 0 | outParser = pp; |
365 | |
} |
366 | |
|
367 | 0 | Message mess = inParser.parse(messString); |
368 | 0 | System.out.println("Got message of type " + mess.getClass().getName()); |
369 | |
|
370 | 0 | String otherEncoding = outParser.encode(mess); |
371 | 0 | System.out.println(otherEncoding); |
372 | |
} |
373 | 0 | catch (Exception e) { |
374 | 0 | e.printStackTrace(); |
375 | 0 | } |
376 | 0 | } |
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
@Override |
382 | |
public void parse(Message theMessage, String theString) throws HL7Exception { |
383 | 15 | Document doc = parseStringIntoDocument(theString); |
384 | 15 | parse(theMessage, doc.getDocumentElement()); |
385 | |
|
386 | 15 | applySuperStructureName(theMessage); |
387 | 15 | } |
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
public static XMLParser getInstanceWithNoValidation() { |
394 | 15 | HapiContext context = new DefaultHapiContext(ValidationContextFactory.noValidation()); |
395 | 15 | XMLParser retVal = context.getXMLParser(); |
396 | 15 | return retVal; |
397 | |
} |
398 | |
|
399 | |
|
400 | |
} |