Coverage Report - ca.uhn.hl7v2.llp.HL7Charsets
 
Classes in this File Line Coverage Branch Coverage Complexity
HL7Charsets
78%
33/42
50%
6/12
4
 
 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 "Table0211.java".  Description:
 10  
  "Content of HL7 table 0211 mapped to Java charsets"
 11  
 
 12  
  The Initial Developer of the Original Code is University Health Network. Copyright (C)
 13  
  2013.  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  
 package ca.uhn.hl7v2.llp;
 28  
 
 29  
 import java.nio.charset.Charset;
 30  
 
 31  
 import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
 32  
 
 33  
 /**
 34  
  * HL7 Charsets from Table 0211 mapped to Java charsets
 35  
  *
 36  
  * @author Christian Ohr
 37  
  */
 38  70
 enum HL7Charsets {
 39  5
     ASCII       ("ASCII",           "US-ASCII"),        // ASCII
 40  5
     ISO_8859_1  ("8859/1",          "ISO-8859-1"),      // Western European Latin 1
 41  5
     ISO_8859_2  ("8859/2",          "ISO-8859-2"),      // Middle European
 42  5
     ISO_8859_3  ("8859/3",          "ISO-8859-3"),      // Southern European
 43  5
     ISO_8859_4  ("8859/4",          "ISO-8859-4"),      // Northern European
 44  5
     ISO_8859_5  ("8859/5",          "ISO-8859-5"),      // Cyrillic
 45  5
     ISO_8859_6  ("8859/6",          "ISO-8859-6"),      // Arab
 46  5
     ISO_8859_7  ("8859/7",          "ISO-8859-7"),      // Greek
 47  5
     ISO_8859_8  ("8859/8",          "ISO-8859-8"),      // Hebrew
 48  5
     ISO_8859_9  ("8859/9",          "ISO-8859-9"),      // Turk
 49  5
     ISO_8859_15 ("8859/15",         "ISO-8859-15"),     // Western European Latin 9
 50  5
     ISO_IR_6    ("ISO IR6",         "US-ASCII"),
 51  5
     ISO_IR_14   ("ISO IR14",        "JIS_X0201"),       // Japanese
 52  5
     ISO_IR_87   ("ISO IR87",        "x-JIS0208"),       // Japanese Kanji
 53  5
     ISO_IR_159  ("ISO IR159",       "JIS_X0212-1990"),  // Japanese Kanji
 54  5
     GB_18030    ("GB 18030-2000",   "GB18030"),         // Chinese
 55  5
     KS_X_1001   ("KS X 1001",       "EUC-KR"),          // Korean
 56  5
     CNS_11643   ("CNS 11643-1992",  "x-EUC-TW"),        // Chinese
 57  5
     BIG_5       ("BIG-5",           "Big5"),            // Chinese/Taiwan
 58  5
     UNICODE     ("UNICODE",         "UTF-8"),
 59  5
     UTF_8       ("UNICODE UTF-8",   "UTF-8"),
 60  5
     UTF_16      ("UNICODE UTF-16",  "UTF-16"),
 61  5
     UTF_32      ("UNICODE UTF-32",  "UTF-32");
 62  
 
 63  
 
 64  
     private String hl7EncodingName;
 65  
     private Charset charset;
 66  
 
 67  115
     private HL7Charsets(String hl7EncodingName, String charset) {
 68  115
         this.hl7EncodingName = hl7EncodingName;
 69  115
         this.charset = Charset.forName(charset);
 70  115
     }
 71  
 
 72  
     /**
 73  
      * Returns the Java charset for the HL7 charset name
 74  
      * @param hl7EncodingName
 75  
      * @return Java charset
 76  
      * @throws EncodingNotSupportedException if encoding name is unknown
 77  
      */
 78  
     public static Charset getCharsetForHL7Encoding(String hl7EncodingName) throws EncodingNotSupportedException {
 79  65
         if (hl7EncodingName == null || hl7EncodingName.length() == 0)
 80  0
             return HL7Charsets.ASCII.charset;
 81  885
         for (HL7Charsets encoding : HL7Charsets.values()) {
 82  880
             if (hl7EncodingName.equals(encoding.hl7EncodingName))
 83  60
                 return encoding.charset;
 84  
         }
 85  
 
 86  5
         throw new EncodingNotSupportedException(hl7EncodingName);
 87  
     }
 88  
 
 89  
     public static void main(String[] params) {
 90  0
         for (HL7Charsets entry : HL7Charsets.values()) {
 91  0
             Charset cs = entry.charset;
 92  0
             System.out.print(cs.displayName());
 93  0
             for (String alias : cs.aliases()) {
 94  0
                 System.out.print(" " + alias);
 95  0
             }
 96  0
             System.out.println();
 97  
         }
 98  0
     }
 99  
 }