View Javadoc

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  package com.beetstra.jutf7;
25  
26  import java.io.UnsupportedEncodingException;
27  import java.nio.charset.Charset;
28  import java.util.Arrays;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Locale;
32  
33  /**
34   * <p>Charset service-provider class used for both variants of the UTF-7 charset 
35   * and the modified-UTF-7 charset.</p>
36   * 
37   * @author Jaap Beetstra
38   */
39  public class CharsetProvider extends java.nio.charset.spi.CharsetProvider {
40  	private static final String UTF7_NAME = "UTF-7";
41  	private static final String UTF7_O_NAME = "X-UTF-7-OPTIONAL";
42  	private static final String UTF7_M_NAME = "X-MODIFIED-UTF-7";
43  	private static final String[] UTF7_ALIASES = new String[] { "UNICODE-1-1-UTF-7",
44  			"CSUNICODE11UTF7", "X-RFC2152", "X-RFC-2152" };
45  	private static final String[] UTF7_O_ALIASES = new String[] { "X-RFC2152-OPTIONAL",
46  			"X-RFC-2152-OPTIONAL" };
47  	private static final String[] UTF7_M_ALIASES = new String[] { "X-IMAP-MODIFIED-UTF-7",
48  			"X-IMAP4-MODIFIED-UTF7", "X-IMAP4-MODIFIED-UTF-7", "X-RFC3501", "X-RFC-3501" };
49  	private Charset utf7charset = new UTF7Charset(UTF7_NAME, UTF7_ALIASES, false);
50  	private Charset utf7oCharset = new UTF7Charset(UTF7_O_NAME, UTF7_O_ALIASES, true);
51  	private Charset imap4charset = new ModifiedUTF7Charset(UTF7_M_NAME, UTF7_M_ALIASES);
52  	private List charsets;
53  
54  	public CharsetProvider() {
55  		charsets = Arrays.asList(new Object[] { utf7charset, imap4charset, utf7oCharset });
56  	}
57  
58  	/**
59  	 * {@inheritDoc}
60  	 */
61  	public Charset charsetForName(String charsetName) {
62  		charsetName = charsetName.toUpperCase(Locale.US);
63  		for (Iterator iter = charsets.iterator(); iter.hasNext();) {
64  			Charset charset = (Charset) iter.next();
65  			if (charset.name().equals(charsetName))
66  				return charset;
67  		}
68  		for (Iterator iter = charsets.iterator(); iter.hasNext();) {
69  			Charset charset = (Charset) iter.next();
70  			if (charset.aliases().contains(charsetName))
71  				return charset;
72  		}
73  		return null;
74  	}
75  
76  	/**
77  	 * {@inheritDoc}
78  	 */
79  	public Iterator charsets() {
80  		return charsets.iterator();
81  	}
82  
83  	public static void main(String[] args) throws UnsupportedEncodingException {
84  		if (args.length < 2) {
85  			showUsage();
86  		} else if ("encode".equalsIgnoreCase(args[0])) {
87  			byte[] encoded = args[1].getBytes(UTF7_NAME);
88  			System.out.println(new String(encoded, "US-ASCII"));
89  		} else if ("decode".equalsIgnoreCase(args[0])) {
90  			byte[] bytes = args[1].getBytes("US-ASCII");
91  			System.out.println(new String(bytes, UTF7_NAME));
92  		} else
93  			showUsage();
94  	}
95  
96  	private static void showUsage() {
97  		System.out.println("Usage: java -jar jutf7.jar [encode|decode] <text>");
98  		System.out.println();
99  		System.out.println("Example: java -jar jutf7 encode caf�");
100 		System.out.println("Result: caf+AOk-");
101 	}
102 }