196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project/* ====================================================================
296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * Copyright (c) 2006 J.T. Beetstra
396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project *
496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * Permission is hereby granted, free of charge, to any person obtaining
596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * a copy of this software and associated documentation files (the
696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * "Software"), to deal in the Software without restriction, including
796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * without limitation the rights to use, copy, modify, merge, publish,
896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * distribute, sublicense, and/or sell copies of the Software, and to
996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * permit persons to whom the Software is furnished to do so, subject to
1096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * the following conditions:
1196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project *
1296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * The above copyright notice and this permission notice shall be
1396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * included in all copies or substantial portions of the Software.
1496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project *
1596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
1896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
1996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * ====================================================================
2396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project */
2496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
2596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectpackage com.beetstra.jutf7;
2696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
2796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport java.io.UnsupportedEncodingException;
2896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport java.nio.charset.Charset;
2996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport java.util.Arrays;
3096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport java.util.Iterator;
3196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport java.util.List;
3296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
3396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project/**
3496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * <p>
3596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * Charset service-provider class used for both variants of the UTF-7 charset
3696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * and the modified-UTF-7 charset.
3796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * </p>
3896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project *
3996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * @author Jaap Beetstra
4096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project */
4196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectpublic class CharsetProvider extends java.nio.charset.spi.CharsetProvider {
4296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    private static final String UTF7_NAME = "UTF-7";
4396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    private static final String UTF7_O_NAME = "X-UTF-7-OPTIONAL";
4496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    private static final String UTF7_M_NAME = "X-MODIFIED-UTF-7";
4596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    private static final String[] UTF7_ALIASES = new String[] {
4696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            "UNICODE-1-1-UTF-7", "CSUNICODE11UTF7", "X-RFC2152", "X-RFC-2152"
4796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    };
4896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    private static final String[] UTF7_O_ALIASES = new String[] {
4996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            "X-RFC2152-OPTIONAL", "X-RFC-2152-OPTIONAL"
5096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    };
5196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    private static final String[] UTF7_M_ALIASES = new String[] {
5296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            "X-IMAP-MODIFIED-UTF-7", "X-IMAP4-MODIFIED-UTF7", "X-IMAP4-MODIFIED-UTF-7",
5396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            "X-RFC3501", "X-RFC-3501"
5496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    };
5596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    private Charset utf7charset = new UTF7Charset(UTF7_NAME, UTF7_ALIASES, false);
5696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    private Charset utf7oCharset = new UTF7Charset(UTF7_O_NAME, UTF7_O_ALIASES, true);
5796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    private Charset imap4charset = new ModifiedUTF7Charset(UTF7_M_NAME, UTF7_M_ALIASES);
5896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    private List charsets;
5996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
6096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public CharsetProvider() {
6196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        charsets = Arrays.asList(new Object[] {
6296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project                utf7charset, imap4charset, utf7oCharset
6396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        });
6496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
6596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
6696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    /**
6796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project     * {@inheritDoc}
6896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project     */
6996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public Charset charsetForName(String charsetName) {
7096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        charsetName = charsetName.toUpperCase();
7196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        for (Iterator iter = charsets.iterator(); iter.hasNext();) {
7296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            Charset charset = (Charset)iter.next();
7396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            if (charset.name().equals(charsetName))
7496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project                return charset;
7596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        }
7696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        for (Iterator iter = charsets.iterator(); iter.hasNext();) {
7796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            Charset charset = (Charset)iter.next();
7896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            if (charset.aliases().contains(charsetName))
7996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project                return charset;
8096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        }
8196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        return null;
8296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
8396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
8496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    /**
8596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project     * {@inheritDoc}
8696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project     */
8796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public Iterator charsets() {
8896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        return charsets.iterator();
8996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
9096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project}
91