1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14
* The Original Code is mozilla.org code.
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.
22
* Kohei TAKETA <k-tak@void.in> (Java port)
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
package org.mozilla.universalchardet.prober;
41
import org.mozilla.universalchardet.prober.distributionanalysis.EUCTWDistributionAnalysis;
42
import org.mozilla.universalchardet.prober.statemachine.CodingStateMachine;
43
import org.mozilla.universalchardet.prober.statemachine.EUCTWSMModel;
44
import org.mozilla.universalchardet.prober.statemachine.SMModel;
45
import org.mozilla.universalchardet.Constants;
48
public class EUCTWProber extends CharsetProber
50
////////////////////////////////////////////////////////////////
52
////////////////////////////////////////////////////////////////
53
private CodingStateMachine codingSM;
54
private ProbingState state;
56
private EUCTWDistributionAnalysis distributionAnalyzer;
58
private byte[] lastChar;
60
private static final SMModel smModel = new EUCTWSMModel();
63
////////////////////////////////////////////////////////////////
65
////////////////////////////////////////////////////////////////
69
this.codingSM = new CodingStateMachine(smModel);
70
this.distributionAnalyzer = new EUCTWDistributionAnalysis();
71
this.lastChar = new byte[2];
76
public String getCharSetName()
78
return Constants.CHARSET_EUC_TW;
82
public float getConfidence()
84
float distribCf = this.distributionAnalyzer.getConfidence();
90
public ProbingState getState()
96
public ProbingState handleData(byte[] buf, int offset, int length)
100
int maxPos = offset + length;
101
for (int i=offset; i<maxPos; ++i) {
102
codingState = this.codingSM.nextState(buf[i]);
103
if (codingState == SMModel.ERROR) {
104
this.state = ProbingState.NOT_ME;
107
if (codingState == SMModel.ITSME) {
108
this.state = ProbingState.FOUND_IT;
111
if (codingState == SMModel.START) {
112
int charLen = this.codingSM.getCurrentCharLen();
114
this.lastChar[1] = buf[offset];
115
this.distributionAnalyzer.handleOneChar(this.lastChar, 0, charLen);
117
this.distributionAnalyzer.handleOneChar(buf, i-1, charLen);
122
this.lastChar[0] = buf[maxPos-1];
124
if (this.state == ProbingState.DETECTING) {
125
if (this.distributionAnalyzer.gotEnoughData() && getConfidence() > SHORTCUT_THRESHOLD) {
126
this.state = ProbingState.FOUND_IT;
136
this.codingSM.reset();
137
this.state = ProbingState.DETECTING;
138
this.distributionAnalyzer.reset();
139
java.util.Arrays.fill(this.lastChar, (byte)0);
143
public void setOption()