/** * <p>Compares two HL7 messages to see if they are equivalent (in terms of their * HL7 meaning). Semantically irrelevant differences (e.g. spaces in an XML tag; * extra field delimiters at the end of a segment; XML vs. ER7 encoding; XML attributes) * are ignored. This check is performed without assuming the correctness of the HAPI parsers, * and can therefore be used to test them. This is done by parsing a message, encoding it * again, and comparing the result with this original. </p> * <p>If one message is in XML and the other in ER7, the former is converted to ER7 to * perform the comparison. This process relies on the HAPI parsers. However, the * parsed message is first encoded as XML and compared to the original, so that the * integrity of the parser can be verified. An exception is thrown if this comparison * is unsuccessful. </p> * @return true if given messages are semantically equivalent */ public static boolean equivalent(String message1, String message2) throws HL7Exception { Pair<String> messages = standardize(message1, message2); return messages.getValue1().equals(messages.getValue2()); }
/** * <p>Compares two HL7 messages to see if they are equivalent (in terms of their * HL7 meaning). Semantically irrelevant differences (e.g. spaces in an XML tag; * extra field delimiters at the end of a segment; XML vs. ER7 encoding; XML attributes) * are ignored. This check is performed without assuming the correctness of the HAPI parsers, * and can therefore be used to test them. This is done by parsing a message, encoding it * again, and comparing the result with this original. </p> * <p>If one message is in XML and the other in ER7, the former is converted to ER7 to * perform the comparison. This process relies on the HAPI parsers. However, the * parsed message is first encoded as XML and compared to the original, so that the * integrity of the parser can be verified. An exception is thrown if this comparison * is unsuccessful. </p> * @return true if given messages are semantically equivalent */ public static boolean equivalent(String message1, String message2) throws HL7Exception { Pair<String> messages = standardize(message1, message2); return messages.getValue1().equals(messages.getValue2()); }
static Pair<String> standardize(String message1, String message2) throws HL7Exception { String encoding1 = parser.getEncoding(message1); String encoding2 = parser.getEncoding(message2); if (!encoding1.equals(encoding2)) { if (encoding1.equals("XML")) { message1 = safeER7Conversion(message1); } else { message2 = safeER7Conversion(message2); } } String std1, std2; try { std1 = standardize(message1); std2 = standardize(message2); } catch (SAXException e) { throw new HL7Exception("Equivalence check failed due to SAXException: " + e.getMessage()); } return new Pair<String>(std1, std2); }
static Pair<String> standardize(String message1, String message2) throws HL7Exception { String encoding1 = parser.getEncoding(message1); String encoding2 = parser.getEncoding(message2); if (!encoding1.equals(encoding2)) { if (encoding1.equals("XML")) { message1 = safeER7Conversion(message1); } else { message2 = safeER7Conversion(message2); } } String std1, std2; try { std1 = standardize(message1); std2 = standardize(message2); } catch (SAXException e) { throw new HL7Exception("Equivalence check failed due to SAXException: " + e.getMessage()); } return new Pair<String>(std1, std2); }