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.nio.charset.Charset;
28import java.util.Arrays;
29import java.util.Iterator;
30import java.util.List;
31
32/**
33 * <p>
34 * Charset service-provider class used for both variants of the UTF-7 charset
35 * and the modified-UTF-7 charset.
36 * </p>
37 *
38 * @author Jaap Beetstra
39 */
40public class CharsetProvider extends java.nio.charset.spi.CharsetProvider {
41    private static final String UTF7_NAME = "UTF-7";
42    private static final String UTF7_O_NAME = "X-UTF-7-OPTIONAL";
43    private static final String UTF7_M_NAME = "X-MODIFIED-UTF-7";
44    private static final String[] UTF7_ALIASES = new String[] {
45            "UNICODE-1-1-UTF-7", "CSUNICODE11UTF7", "X-RFC2152", "X-RFC-2152"
46    };
47    private static final String[] UTF7_O_ALIASES = new String[] {
48            "X-RFC2152-OPTIONAL", "X-RFC-2152-OPTIONAL"
49    };
50    private static final String[] UTF7_M_ALIASES = new String[] {
51            "X-IMAP-MODIFIED-UTF-7", "X-IMAP4-MODIFIED-UTF7", "X-IMAP4-MODIFIED-UTF-7",
52            "X-RFC3501", "X-RFC-3501"
53    };
54    private Charset utf7charset = new UTF7Charset(UTF7_NAME, UTF7_ALIASES, false);
55    private Charset utf7oCharset = new UTF7Charset(UTF7_O_NAME, UTF7_O_ALIASES, true);
56    private Charset imap4charset = new ModifiedUTF7Charset(UTF7_M_NAME, UTF7_M_ALIASES);
57    private List<Charset> charsets;
58
59    public CharsetProvider() {
60        charsets = Arrays.asList(new Charset[] {
61                utf7charset, imap4charset, utf7oCharset
62        });
63    }
64
65    /**
66     * {@inheritDoc}
67     */
68    @Override
69    public Charset charsetForName(String charsetName) {
70        charsetName = charsetName.toUpperCase();
71        for (Iterator<Charset> iter = charsets.iterator(); iter.hasNext();) {
72            Charset charset = iter.next();
73            if (charset.name().equals(charsetName))
74                return charset;
75        }
76        for (Iterator<Charset> iter = charsets.iterator(); iter.hasNext();) {
77            Charset charset = iter.next();
78            if (charset.aliases().contains(charsetName))
79                return charset;
80        }
81        return null;
82    }
83
84    /**
85     * {@inheritDoc}
86     */
87    @Override
88    public Iterator<Charset> charsets() {
89        return charsets.iterator();
90    }
91}
92