View Javadoc
1   /*
2    * Created on 21-Apr-2004
3    */
4   package ca.uhn.hl7v2.protocol.impl;
5   
6   import java.util.ArrayList;
7   import java.util.List;
8   import java.util.StringTokenizer;
9   
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import ca.uhn.hl7v2.HL7Exception;
14  import ca.uhn.hl7v2.model.Message;
15  import ca.uhn.hl7v2.parser.Parser;
16  
17  /**
18   * A debugging utility that logs raw messages and parsed/encoded versions, and warnings about 
19   * apparent discrepancies between them.  This information is all logged to HapiLog under 
20   * the name of this class, at the "info" level.  
21   * 
22   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
23   * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
24   */
25  public class ParseChecker {
26  
27      private static final Logger log = LoggerFactory.getLogger(ParseChecker.class);
28  
29      /**
30       * Encodes the given message and compares it to the given string.  Any differences
31       * are noted in the file [hapi.home]/parse_check.txt.  Ignores extra field delimiters.
32       */
33      public static void checkParse(String originalMessageText, Message parsedMessage, Parser parser)
34              throws HL7Exception {
35          log.info("ParseChecker is checking parse integrity (turn this off if you are not testing)");
36          String newMessageText = parser.encode(parsedMessage);
37          
38          log.info("******************* Comparing Messages ****************\r\n");
39          log.info("Original:           {}", originalMessageText);
40          log.info("Parsed and Encoded: {}", newMessageText);
41          
42          if (!originalMessageText.equals(newMessageText)) {
43              //check each segment
44              StringTokenizer tok = new StringTokenizer(originalMessageText, "\r");
45              List<String> one = new ArrayList<>();
46              while (tok.hasMoreTokens()) {
47                  String seg = tok.nextToken();
48                  if (seg.length() > 4)
49                      one.add(seg);
50              }
51              tok = new StringTokenizer(newMessageText, "\r");
52              List<String> two = new ArrayList<>();
53              while (tok.hasMoreTokens()) {
54                  String seg = tok.nextToken();
55                  if (seg.length() > 4)
56                      two.add(stripExtraDelimiters(seg, seg.charAt(3)));
57              }
58              
59              if (one.size() != two.size()) {
60                  log.info("Warning: inbound and parsed messages have different numbers of segments: \r\n");
61                  log.info("Original: {}", originalMessageText);
62                  log.info("Parsed:   {}", newMessageText);
63              }
64              else {
65                  //check each segment
66                  for (int i = 0; i < one.size(); i++) {
67                      String origSeg = one.get(i);
68                      String newSeg = two.get(i);
69                      if (!origSeg.equals(newSeg)) {
70                          log.info("Warning: inbound and parsed message segment differs: \r\n");
71                          log.info("Original: {}", origSeg);
72                          log.info("Parsed:   {}", newSeg);
73                      }
74                  }
75              }
76          }
77          else {
78              log.info("No differences found");
79          }
80          
81          log.info("********************  End Comparison  ******************\r\n");
82          
83      }
84      
85      /**
86       * Removes unecessary delimiters from the end of a field or segment.
87       * This is cut-and-pasted from PipeParser (just making it public in
88       * PipeParser would kind of cloud the purpose of PipeParser).
89       */
90      private static String stripExtraDelimiters(String in, char delim) {
91          char[] chars = in.toCharArray();
92          
93          //search from back end for first occurance of non-delimiter ...
94          int c = chars.length - 1;
95          boolean found = false;
96          while (c >= 0 && !found) {
97              if (chars[c--] != delim)
98                  found = true;
99          }
100         
101         String ret = "";
102         if (found)
103             ret = String.valueOf(chars, 0, c + 2);
104         return ret;
105     }
106     
107     
108 }