Coverage Report - net.sf.jmatchparser.util.charset.jchardet.JChardetCharsetProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
JChardetCharsetProvider
55%
5/9
66%
4/6
3
 
 1  
 /* ***** BEGIN LICENSE BLOCK *****
 2  
  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 3  
  *
 4  
  * The contents of this file are subject to the Mozilla Public License Version
 5  
  * 1.1 (the "License"); you may not use this file except in compliance with
 6  
  * the License. You may obtain a copy of the License at
 7  
  * http://www.mozilla.org/MPL/
 8  
  *
 9  
  * Software distributed under the License is distributed on an "AS IS" basis,
 10  
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11  
  * for the specific language governing rights and limitations under the
 12  
  * License.
 13  
  *
 14  
  * The Original Code is mozilla.org code.
 15  
  *
 16  
  * The Initial Developer of the Original Code is
 17  
  * Netscape Communications Corporation.
 18  
  * Portions created by the Initial Developer are Copyright (C) 1998
 19  
  * the Initial Developer. All Rights Reserved.
 20  
  *
 21  
  * Alternatively, the contents of this file may be used under the terms of
 22  
  * either of the GNU General Public License Version 2 or later (the "GPL"),
 23  
  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 24  
  * in which case the provisions of the GPL or the LGPL are applicable instead
 25  
  * of those above. If you wish to allow use of your version of this file only
 26  
  * under the terms of either the GPL or the LGPL, and not to allow others to
 27  
  * use your version of this file under the terms of the MPL, indicate your
 28  
  * decision by deleting the provisions above and replace them with the notice
 29  
  * and other provisions required by the GPL or the LGPL. If you do not delete
 30  
  * the provisions above, a recipient may use your version of this file under
 31  
  * the terms of any one of the MPL, the GPL or the LGPL.
 32  
  *
 33  
  * ***** END LICENSE BLOCK ***** */
 34  
 package net.sf.jmatchparser.util.charset.jchardet;
 35  
 
 36  
 import java.nio.charset.Charset;
 37  
 import java.nio.charset.spi.CharsetProvider;
 38  
 import java.util.Arrays;
 39  
 import java.util.Iterator;
 40  
 
 41  
 import net.sf.jmatchparser.util.charset.jchardet.JChardetCharset;
 42  
 
 43  
 /**
 44  
  * Charset provider that provides a convenience charset that uses <a
 45  
  * href="http://jchardet.sourceforge.net/">jChardet</a> to dynamically detect
 46  
  * the charset it uses for decoding.
 47  
  * 
 48  
  * <p>
 49  
  * The name of this charset is "<tt>jChardet</tt>".
 50  
  * 
 51  
  * <p>
 52  
  * jChardet also supports limiting the character sets detected. This is
 53  
  * available via additional charsets "<tt>jChardet-JAPANESE</tt>", "
 54  
  * <tt>jChardet-CHINESE</tt>", "<tt>jChardet-SIMPLIFIED-CHINESE</tt>", "
 55  
  * <tt>jChardet-TRADITIONAL-CHINESE</tt>", "<tt>jChardet-KOREAN</tt>".
 56  
  * 
 57  
  * <p>
 58  
  * This class is loaded automatically via SPI when it is in the class path.
 59  
  * 
 60  
  * <h2>Character sets that can be detected</h2>
 61  
  * 
 62  
  * <ul>
 63  
  * <li>UTF-8</li>
 64  
  * <li>Shift_JIS</li>
 65  
  * <li>EUC-JP</li>
 66  
  * <li>ISO-2022-JP</li>
 67  
  * <li>EUC-KR</li>
 68  
  * <li>ISO-2022-KR</li>
 69  
  * <li>Big5</li>
 70  
  * <li>x-euc-tw</li>
 71  
  * <li>GB2312</li>
 72  
  * <li>GB18030</li>
 73  
  * <li>ISO-2022-CN</li>
 74  
  * <li>HZ-GB-2312 <i>(not supported by Java by default)</i></li>
 75  
  * <li>windows-1252</li>
 76  
  * <li>UTF-16BE</li>
 77  
  * <li>UTF-16LE</li>
 78  
  * </ul>
 79  
  */
 80  95
 public class JChardetCharsetProvider extends CharsetProvider {
 81  
 
 82  
         @Override
 83  
         public Charset charsetForName(String charsetName) {
 84  497
                 for (int i = 0; i < JChardetCharset.NAMES.length; i++) {
 85  430
                         if (charsetName.equals(JChardetCharset.NAMES[i]))
 86  28
                                 return JChardetCharset.getInstance(i);
 87  
                 }
 88  67
                 return null;
 89  
         }
 90  
 
 91  
         @Override
 92  
         public Iterator<Charset> charsets() {
 93  0
                 Charset[] charsets = new Charset[JChardetCharset.NAMES.length];
 94  0
                 for (int i = 0; i < charsets.length; i++) {
 95  0
                         charsets[i] = JChardetCharset.getInstance(i);
 96  
                 }
 97  0
                 return Arrays.asList(charsets).iterator();
 98  
         }
 99  
 }