1/* ==================================================================== 2 * Copyright (c) 2006 J.T. Beetstra 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining 5 * a copy of this software and associated documentation files (the 6 * "Software"), to deal in the Software without restriction, including 7 * without limitation the rights to use, copy, modify, merge, publish, 8 * distribute, sublicense, and/or sell copies of the Software, and to 9 * permit persons to whom the Software is furnished to do so, subject to 10 * the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 * ==================================================================== 23 */ 24 25package com.beetstra.jutf7; 26 27import java.io.UnsupportedEncodingException; 28import java.nio.charset.Charset; 29import java.util.Arrays; 30import java.util.Iterator; 31import java.util.List; 32 33/** 34 * <p> 35 * Charset service-provider class used for both variants of the UTF-7 charset 36 * and the modified-UTF-7 charset. 37 * </p> 38 * 39 * @author Jaap Beetstra 40 */ 41public class CharsetProvider extends java.nio.charset.spi.CharsetProvider { 42 private static final String UTF7_NAME = "UTF-7"; 43 private static final String UTF7_O_NAME = "X-UTF-7-OPTIONAL"; 44 private static final String UTF7_M_NAME = "X-MODIFIED-UTF-7"; 45 private static final String[] UTF7_ALIASES = new String[] { 46 "UNICODE-1-1-UTF-7", "CSUNICODE11UTF7", "X-RFC2152", "X-RFC-2152" 47 }; 48 private static final String[] UTF7_O_ALIASES = new String[] { 49 "X-RFC2152-OPTIONAL", "X-RFC-2152-OPTIONAL" 50 }; 51 private static final String[] UTF7_M_ALIASES = new String[] { 52 "X-IMAP-MODIFIED-UTF-7", "X-IMAP4-MODIFIED-UTF7", "X-IMAP4-MODIFIED-UTF-7", 53 "X-RFC3501", "X-RFC-3501" 54 }; 55 private Charset utf7charset = new UTF7Charset(UTF7_NAME, UTF7_ALIASES, false); 56 private Charset utf7oCharset = new UTF7Charset(UTF7_O_NAME, UTF7_O_ALIASES, true); 57 private Charset imap4charset = new ModifiedUTF7Charset(UTF7_M_NAME, UTF7_M_ALIASES); 58 private List charsets; 59 60 public CharsetProvider() { 61 charsets = Arrays.asList(new Object[] { 62 utf7charset, imap4charset, utf7oCharset 63 }); 64 } 65 66 /** 67 * {@inheritDoc} 68 */ 69 public Charset charsetForName(String charsetName) { 70 charsetName = charsetName.toUpperCase(); 71 for (Iterator iter = charsets.iterator(); iter.hasNext();) { 72 Charset charset = (Charset)iter.next(); 73 if (charset.name().equals(charsetName)) 74 return charset; 75 } 76 for (Iterator iter = charsets.iterator(); iter.hasNext();) { 77 Charset charset = (Charset)iter.next(); 78 if (charset.aliases().contains(charsetName)) 79 return charset; 80 } 81 return null; 82 } 83 84 /** 85 * {@inheritDoc} 86 */ 87 public Iterator charsets() { 88 return charsets.iterator(); 89 } 90} 91