View Javadoc

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