~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/juniversalchardet/src/org/mozilla/universalchardet/prober/EUCTWProber.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * Contributor(s):
 
22
 *   Kohei TAKETA <k-tak@void.in> (Java port)
 
23
 *   Lersh99
 
24
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
package org.mozilla.universalchardet.prober;
 
40
 
 
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;
 
46
 
 
47
 
 
48
public class EUCTWProber extends CharsetProber
 
49
{
 
50
    ////////////////////////////////////////////////////////////////
 
51
    // fields
 
52
    ////////////////////////////////////////////////////////////////
 
53
    private CodingStateMachine          codingSM;
 
54
    private ProbingState                state;
 
55
    
 
56
    private EUCTWDistributionAnalysis   distributionAnalyzer;
 
57
    
 
58
    private byte[]                      lastChar;
 
59
 
 
60
    private static final SMModel smModel = new EUCTWSMModel();
 
61
 
 
62
    
 
63
    ////////////////////////////////////////////////////////////////
 
64
    // methods
 
65
    ////////////////////////////////////////////////////////////////
 
66
    public EUCTWProber()
 
67
    {
 
68
        super();
 
69
        this.codingSM = new CodingStateMachine(smModel);
 
70
        this.distributionAnalyzer = new EUCTWDistributionAnalysis();
 
71
        this.lastChar = new byte[2];
 
72
        reset();
 
73
    }
 
74
    
 
75
    @Override
 
76
    public String getCharSetName()
 
77
    {
 
78
        return Constants.CHARSET_EUC_TW;
 
79
    }
 
80
 
 
81
    @Override
 
82
    public float getConfidence()
 
83
    {
 
84
        float distribCf = this.distributionAnalyzer.getConfidence();
 
85
        
 
86
        return distribCf;
 
87
    }
 
88
 
 
89
    @Override
 
90
    public ProbingState getState()
 
91
    {
 
92
        return this.state;
 
93
    }
 
94
 
 
95
    @Override
 
96
    public ProbingState handleData(byte[] buf, int offset, int length)
 
97
    {
 
98
        int codingState;
 
99
        
 
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;
 
105
                break;
 
106
            }
 
107
            if (codingState == SMModel.ITSME) {
 
108
                this.state = ProbingState.FOUND_IT;
 
109
                break;
 
110
            }
 
111
            if (codingState == SMModel.START) {
 
112
                int charLen = this.codingSM.getCurrentCharLen();
 
113
                if (i == offset) {
 
114
                    this.lastChar[1] = buf[offset];
 
115
                    this.distributionAnalyzer.handleOneChar(this.lastChar, 0, charLen);
 
116
                } else {
 
117
                    this.distributionAnalyzer.handleOneChar(buf, i-1, charLen);
 
118
                }
 
119
            }
 
120
        }
 
121
        
 
122
        this.lastChar[0] = buf[maxPos-1];
 
123
        
 
124
        if (this.state == ProbingState.DETECTING) {
 
125
            if (this.distributionAnalyzer.gotEnoughData() && getConfidence() > SHORTCUT_THRESHOLD) {
 
126
                this.state = ProbingState.FOUND_IT;
 
127
            }
 
128
        }
 
129
        
 
130
        return this.state;
 
131
    }
 
132
 
 
133
    @Override
 
134
    public void reset()
 
135
    {
 
136
        this.codingSM.reset();
 
137
        this.state = ProbingState.DETECTING;
 
138
        this.distributionAnalyzer.reset();
 
139
        java.util.Arrays.fill(this.lastChar, (byte)0);
 
140
    }
 
141
 
 
142
    @Override
 
143
    public void setOption()
 
144
    {}
 
145
}