Coverage Report - ca.uhn.hl7v2.parser.EncodingDetector
 
Classes in this File Line Coverage Branch Coverage Complexity
EncodingDetector
93%
31/33
90%
20/22
5.2
 
 1  
 /**
 2  
 The contents of this file are subject to the Mozilla Public License Version 1.1
 3  
 (the "License"); you may not use this file except in compliance with the License.
 4  
 You may obtain a copy of the License at http://www.mozilla.org/MPL/
 5  
 Software distributed under the License is distributed on an "AS IS" basis,
 6  
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
 7  
 specific language governing rights and limitations under the License.
 8  
 
 9  
 The Original Code is "GroupPointer.java".  Description:
 10  
 "A GroupPointer is used when parsing traditionally encoded HL7 messages"
 11  
 
 12  
 The Initial Developer of the Original Code is University Health Network. Copyright (C)
 13  
 2001.  All Rights Reserved.
 14  
 
 15  
 Contributor(s): ______________________________________.
 16  
 
 17  
 Alternatively, the contents of this file may be used under the terms of the
 18  
 GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
 19  
 applicable instead of those above.  If you wish to allow use of your version of this
 20  
 file only under the terms of the GPL and not to allow others to use your version
 21  
 of this file under the MPL, indicate your decision by deleting  the provisions above
 22  
 and replace  them with the notice and other provisions required by the GPL License.
 23  
 If you do not delete the provisions above, a recipient may use your version of
 24  
 this file under either the MPL or the GPL.
 25  
 
 26  
 */
 27  
 
 28  
 package ca.uhn.hl7v2.parser;
 29  
 
 30  
 import java.util.StringTokenizer;
 31  
 
 32  
 /**
 33  
  * Detects message encoding (ER7 / XML) without relying on any
 34  
  * external dependencies
 35  
  */
 36  
 public final class EncodingDetector {
 37  
 
 38  
         /**
 39  
          * Non instantiable
 40  
          */
 41  0
         private EncodingDetector() {
 42  
                 // nothing
 43  0
         }
 44  
 
 45  
     /**
 46  
      * Throws an exception if the message is not ER7 encoded
 47  
      *
 48  
      * @param theMessage message to be examined
 49  
      * @throws RuntimeException if message is not ER7-encoded
 50  
      */
 51  
         public static void assertEr7Encoded(String theMessage) {
 52  
         // quit if the string is too short
 53  1229
         if (theMessage.length() < 4)
 54  7
             throw new RuntimeException("The message is less than 4 characters long");
 55  
 
 56  
         // string should start with "MSH"
 57  1222
         if (!theMessage.startsWith("MSH"))
 58  32
             throw new RuntimeException("The message does not start with MSH");
 59  
 
 60  
         // 4th character of each segment should be field delimiter
 61  1190
         char fourthChar = theMessage.charAt(3);
 62  1190
         StringTokenizer st = new StringTokenizer(theMessage, String.valueOf(PipeParser.SEGMENT_DELIMITER), false);
 63  5059
         while (st.hasMoreTokens()) {
 64  3873
             String x = st.nextToken();
 65  3873
             if (x.length() > 0) {
 66  3873
                 if (Character.isWhitespace(x.charAt(0)))
 67  153
                     x = PipeParser.stripLeadingWhitespace(x);
 68  3873
                 if (x.length() >= 4 && x.charAt(3) != fourthChar)
 69  4
                     throw new RuntimeException(String.format(
 70  
                             "The 4th character should have been a %c, but it was a %c", x.charAt(3), fourthChar));
 71  
             }
 72  3869
         }
 73  
 
 74  
         // should be at least 11 field delimiters (because MSH-12 is required)
 75  1186
         int nextFieldDelimLoc = 0;
 76  14152
         for (int i = 0; i < 11; i++) {
 77  12974
             nextFieldDelimLoc = theMessage.indexOf(fourthChar, nextFieldDelimLoc + 1);
 78  12974
             if (nextFieldDelimLoc < 0)
 79  8
                 throw new RuntimeException("Expected to find required field MSH-12");
 80  
         }
 81  
             
 82  1178
         }
 83  
         
 84  
         /**
 85  
          * Returns true if the message is ER7 (pipe-and-hat) encoded
 86  
      *
 87  
      * @param theMessage message to be examined
 88  
      * @return true if message is ER7-encoded
 89  
      */
 90  
     public static boolean isEr7Encoded(String theMessage) {
 91  
         try {
 92  1229
             assertEr7Encoded(theMessage);
 93  1178
             return true;
 94  51
         } catch (Exception e) {
 95  51
             return false;
 96  
         }
 97  
         }
 98  
 
 99  
     /**
 100  
      * Throws an exception if the message is not XML encoded
 101  
      *
 102  
      * @param theMessage message to be examined
 103  
      * @throws RuntimeException if message is not XML-encoded
 104  
      */
 105  
     public static void assertXmlEncoded(String theMessage) {
 106  50
         if (!theMessage.contains("MSH.1>")) throw new RuntimeException("Expected to find MSH.1");
 107  38
         if (!theMessage.contains("MSH.2>")) throw new RuntimeException("Expected to find MSH.2");
 108  38
     }
 109  
 
 110  
     /**
 111  
      * Returns true if the message is XML encoded. Note that this 
 112  
      * message does not perform a very robust check, and does not
 113  
      * validate for well-formedness. It is only intended to perform
 114  
      * a simple check for XML vs. ER7 messages.
 115  
      *
 116  
      * @param theMessage message to be examined
 117  
      * @return true if message is XML-encoded
 118  
      */
 119  
         public static boolean isXmlEncoded(String theMessage) {
 120  
         try {
 121  50
             assertXmlEncoded(theMessage);
 122  38
             return true;
 123  12
         } catch (Exception e) {
 124  12
             return false;
 125  
         }
 126  
         }
 127  
 
 128  
         
 129  
 }