001    /**
002    The contents of this file are subject to the Mozilla Public License Version 1.1 
003    (the "License"); you may not use this file except in compliance with the License. 
004    You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005    Software distributed under the License is distributed on an "AS IS" basis, 
006    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007    specific language governing rights and limitations under the License. 
008    
009    The Initial Developer of the Original Code is University Health Network. Copyright (C) 
010    2001.  All Rights Reserved. 
011    
012    Contributor(s): Jens Kristian Villadsen from Cetrea A/S
013    
014    Alternatively, the contents of this file may be used under the terms of the 
015    GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
016    applicable instead of those above.  If you wish to allow use of your version of this 
017    file only under the terms of the GPL and not to allow others to use your version 
018    of this file under the MPL, indicate your decision by deleting  the provisions above 
019    and replace  them with the notice and other provisions required by the GPL License.  
020    If you do not delete the provisions above, a recipient may use your version of 
021    this file under either the MPL or the GPL. 
022    
023    */
024    
025    
026    package ca.uhn.hl7v2.llp;
027    
028    import java.nio.charset.Charset;
029    
030    import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
031    
032    /**
033     * Charset utility class
034     * 
035     * @author Jens Kristian Villadsen from Cetrea A/S
036     */
037    public class CharSetUtil
038    {
039            /**
040             * Accepts a charset name as defined by HL7 (ie. an MSH-18 value) into
041             * a Java charset corresponding to that name
042             * 
043             * @param value
044             *            The MSH-18 field
045             * @return The MSH-18 field represented as CharSet
046             * @throws EncodingNotSupportedException
047             */
048            public static Charset convertHL7CharacterEncodingToCharSetvalue(String value) throws EncodingNotSupportedException
049            {
050                    if(value == null)
051                    {
052                            return Charset.forName("US-ASCII");
053                    }
054    
055                    String trimmedString = value.trim().toUpperCase();
056                    {
057                            if("".equals(trimmedString))
058                                    return Charset.forName("US-ASCII");
059                            if("ASCII".equals(trimmedString))
060                                    return Charset.forName("US-ASCII");
061                            if("8859/1".equals(trimmedString))
062                                    return Charset.forName("ISO-8859-1");
063                            if("8859/2".equals(trimmedString))
064                                    return Charset.forName("ISO-8859-2");
065                            if("8859/3".equals(trimmedString))
066                                    return Charset.forName("ISO-8859-3");
067                            if("8859/4".equals(trimmedString))
068                                    return Charset.forName("ISO-8859-4");
069                            if("8859/5".equals(trimmedString))
070                                    return Charset.forName("ISO-8859-5");
071                            if("8859/6".equals(trimmedString))
072                                    return Charset.forName("ISO-8859-6");
073                            if("8859/7".equals(trimmedString))
074                                    return Charset.forName("ISO-8859-7");
075                            if("8859/8".equals(trimmedString))
076                                    return Charset.forName("ISO-8859-8");
077                            if("8859/9".equals(trimmedString))
078                                    return Charset.forName("ISO-8859-9");
079                            if("8859/15".equals(trimmedString))
080                                    return Charset.forName("ISO-8859-15");
081                            if("UNICODE UTF-8".equals(trimmedString))
082                                    return Charset.forName("UTF-8");
083                            if("UNICODE UTF-16".equals(trimmedString))
084                                    return Charset.forName("UTF-16");
085                            if("UNICODE".equals(trimmedString))
086                                    return Charset.forName("UTF-8");
087                    }
088                    throw new EncodingNotSupportedException(value);
089            }
090    }