| 1 | |
|
| 2 | |
|
| 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 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | 0 | public class ParseChecker { |
| 27 | |
|
| 28 | 0 | private static final Logger log = LoggerFactory.getLogger(ParseChecker.class); |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
private static String stripExtraDelimiters(String in, char delim) { |
| 92 | 0 | char[] chars = in.toCharArray(); |
| 93 | |
|
| 94 | |
|
| 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 | |
} |