Coverage Report - ca.uhn.hl7v2.protocol.impl.ParseChecker
 
Classes in this File Line Coverage Branch Coverage Complexity
ParseChecker
0%
0/47
0%
0/24
7
 
 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  0
 public class ParseChecker {
 27  
 
 28  0
     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  0
         log.info("ParseChecker is checking parse integrity (turn this off if you are not testing)");
 37  0
         String newMessageText = parser.encode(parsedMessage);
 38  
         
 39  0
         log.info("******************* Comparing Messages ****************\r\n");
 40  0
         log.info("Original:           {}", originalMessageText);
 41  0
         log.info("Parsed and Encoded: {}", newMessageText);
 42  
         
 43  0
         if (!originalMessageText.equals(newMessageText)) {
 44  
             //check each segment
 45  0
             StringTokenizer tok = new StringTokenizer(originalMessageText, "\r");
 46  0
             List<String> one = new ArrayList<String>();
 47  0
             while (tok.hasMoreTokens()) {
 48  0
                 String seg = tok.nextToken();
 49  0
                 if (seg.length() > 4)
 50  0
                     one.add(seg);
 51  0
             }
 52  0
             tok = new StringTokenizer(newMessageText, "\r");
 53  0
             List<String> two = new ArrayList<String>();
 54  0
             while (tok.hasMoreTokens()) {
 55  0
                 String seg = tok.nextToken();
 56  0
                 if (seg.length() > 4)
 57  0
                     two.add(stripExtraDelimiters(seg, seg.charAt(3)));
 58  0
             }
 59  
             
 60  0
             if (one.size() != two.size()) {
 61  0
                 log.info("Warning: inbound and parsed messages have different numbers of segments: \r\n");
 62  0
                 log.info("Original: {}", originalMessageText);
 63  0
                 log.info("Parsed:   {}", newMessageText);
 64  
             }
 65  
             else {
 66  
                 //check each segment
 67  0
                 for (int i = 0; i < one.size(); i++) {
 68  0
                     String origSeg = one.get(i);
 69  0
                     String newSeg = two.get(i);
 70  0
                     if (!origSeg.equals(newSeg)) {
 71  0
                         log.info("Warning: inbound and parsed message segment differs: \r\n");
 72  0
                         log.info("Original: {}", origSeg);
 73  0
                         log.info("Parsed:   {}", newSeg);
 74  
                     }
 75  
                 }
 76  
             }
 77  0
         }
 78  
         else {
 79  0
             log.info("No differences found");
 80  
         }
 81  
         
 82  0
         log.info("********************  End Comparison  ******************\r\n");
 83  
         
 84  0
     }
 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  0
         char[] chars = in.toCharArray();
 93  
         
 94  
         //search from back end for first occurance of non-delimiter ...
 95  0
         int c = chars.length - 1;
 96  0
         boolean found = false;
 97  0
         while (c >= 0 && !found) {
 98  0
             if (chars[c--] != delim)
 99  0
                 found = true;
 100  
         }
 101  
         
 102  0
         String ret = "";
 103  0
         if (found)
 104  0
             ret = String.valueOf(chars, 0, c + 2);
 105  0
         return ret;
 106  
     }
 107  
     
 108  
     
 109  
 }