View Javadoc

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 Initial Developer of the Original Code is University Health Network. Copyright (C) 
10  2001.  All Rights Reserved. 
11  
12  Contributor(s): Jens Kristian Villadsen from Cetrea A/S
13  
14  Alternatively, the contents of this file may be used under the terms of the 
15  GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
16  applicable instead of those above.  If you wish to allow use of your version of this 
17  file only under the terms of the GPL and not to allow others to use your version 
18  of this file under the MPL, indicate your decision by deleting  the provisions above 
19  and replace  them with the notice and other provisions required by the GPL License.  
20  If you do not delete the provisions above, a recipient may use your version of 
21  this file under either the MPL or the GPL. 
22  
23  */
24  
25  
26  package ca.uhn.hl7v2.llp;
27  
28  import java.nio.charset.Charset;
29  
30  import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
31  
32  /**
33   * Charset utility class
34   * 
35   * @author Jens Kristian Villadsen from Cetrea A/S
36   */
37  public class CharSetUtil
38  {
39  	/**
40  	 * Accepts a charset name as defined by HL7 (ie. an MSH-18 value) into
41  	 * a Java charset corresponding to that name
42  	 * 
43  	 * @param value
44  	 *            The MSH-18 field
45  	 * @return The MSH-18 field represented as CharSet
46  	 * @throws EncodingNotSupportedException
47  	 */
48  	public static Charset convertHL7CharacterEncodingToCharSetvalue(String value) throws EncodingNotSupportedException
49  	{
50  		if(value == null)
51  		{
52  			return Charset.forName("US-ASCII");
53  		}
54  
55  		String trimmedString = value.trim().toUpperCase();
56  		{
57  			if("".equals(trimmedString))
58  				return Charset.forName("US-ASCII");
59  			if("ASCII".equals(trimmedString))
60  				return Charset.forName("US-ASCII");
61  			if("8859/1".equals(trimmedString))
62  				return Charset.forName("ISO-8859-1");
63  			if("8859/2".equals(trimmedString))
64  				return Charset.forName("ISO-8859-2");
65  			if("8859/3".equals(trimmedString))
66  				return Charset.forName("ISO-8859-3");
67  			if("8859/4".equals(trimmedString))
68  				return Charset.forName("ISO-8859-4");
69  			if("8859/5".equals(trimmedString))
70  				return Charset.forName("ISO-8859-5");
71  			if("8859/6".equals(trimmedString))
72  				return Charset.forName("ISO-8859-6");
73  			if("8859/7".equals(trimmedString))
74  				return Charset.forName("ISO-8859-7");
75  			if("8859/8".equals(trimmedString))
76  				return Charset.forName("ISO-8859-8");
77  			if("8859/9".equals(trimmedString))
78  				return Charset.forName("ISO-8859-9");
79  			if("8859/15".equals(trimmedString))
80  				return Charset.forName("ISO-8859-15");
81  			if("UNICODE UTF-8".equals(trimmedString))
82  				return Charset.forName("UTF-8");
83  			if("UNICODE UTF-16".equals(trimmedString))
84  				return Charset.forName("UTF-16");
85  			if("UNICODE".equals(trimmedString))
86  				return Charset.forName("UTF-8");
87  		}
88  		throw new EncodingNotSupportedException(value);
89  	}
90  }