Coverage Report - ca.uhn.hl7v2.hoh.encoder.EncodingStyle
 
Classes in this File Line Coverage Branch Coverage Complexity
EncodingStyle
86%
20/23
60%
6/10
3.25
 
 1  
 package ca.uhn.hl7v2.hoh.encoder;
 2  
 
 3  
 import java.util.HashMap;
 4  
 import java.util.Map;
 5  
 
 6  15
 public enum EncodingStyle {
 7  
 
 8  
         /**
 9  
          * <p>
 10  
          * ER7 (Pipe and Hat, or Vertical Bar encoding)
 11  
          * </p>
 12  
          * <p>
 13  
          * Content type: <code>application/hl7-v2</code>
 14  
          * </p>
 15  
          */
 16  5
         ER7("application/hl7-v2"),
 17  
 
 18  
         /**
 19  
          * <p>
 20  
          * XML encoding
 21  
          * </p>
 22  
          * <p>
 23  
          * Content type: <code>application/hl7-v2+xml</code>
 24  
          * </p>
 25  
          */
 26  5
         XML("application/hl7-v2+xml");
 27  
 
 28  5
         private static final Map<String, EncodingStyle> ourContentTypeToEncodingStyles = new HashMap<String, EncodingStyle>();
 29  
 
 30  
         static {
 31  15
                 for (EncodingStyle next : values()) {
 32  10
                         ourContentTypeToEncodingStyles.put(next.myContentType, next);
 33  
                 }
 34  5
         }
 35  
 
 36  
         private String myContentType;
 37  
 
 38  10
         EncodingStyle(String theContentType) {
 39  10
                 myContentType = theContentType;
 40  10
         }
 41  
 
 42  
         /**
 43  
          * Returns the encoding style (e.g. ER7) for a given content type (e.g.
 44  
          * application/hl7-v2), or <code>null</code> if content type does not match
 45  
          * an HL7 definition.
 46  
          * 
 47  
          * @param theContentType
 48  
          *            The content type (case insensitive)
 49  
          * @return Returns null if no matching
 50  
          * @throws NullPointerException
 51  
          *             If theContentType is null
 52  
          */
 53  
         public static EncodingStyle getEncodingStyleForContentType(String theContentType) {
 54  425
                 return ourContentTypeToEncodingStyles.get(theContentType.toLowerCase());
 55  
         }
 56  
 
 57  
         /**
 58  
          * Detect the encoding style of a given message
 59  
          * 
 60  
          * @throws NullPointerException If theMessage is null
 61  
          * @throws IllegalArgumentException If the message is not ER7 or XML
 62  
          */
 63  
         public static EncodingStyle detect(String theMessage) {
 64  660
                 if (theMessage == null) {
 65  0
                         throw new NullPointerException("Message can not be null");
 66  
                 }
 67  
 
 68  660
                 for (int i = 0; i < theMessage.length(); i++) {
 69  660
                         char nextChar = theMessage.charAt(i);
 70  660
                         if (Character.isLetter(nextChar)) {
 71  570
                                 return ER7;
 72  
                         }
 73  90
                         if (Character.isWhitespace(nextChar)) {
 74  0
                                 continue;
 75  
                         }
 76  90
                         if (nextChar == '<') {
 77  90
                                 return XML;
 78  
                         }
 79  
                 }
 80  
 
 81  0
                 throw new IllegalArgumentException("Message does not appear to be ER7 or XML");
 82  
 
 83  
         }
 84  
 
 85  
         /**
 86  
          * Returns the MIME type (content-type) associated with this encoding
 87  
          */
 88  
         public String getContentType() {
 89  585
                 return myContentType;
 90  
         }
 91  
 
 92  
 }