1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Universal charset detector code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2001
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Shy Shalom <shooshX@gmail.com>
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 : #include "nscore.h"
40 :
41 : #include "nsUniversalDetector.h"
42 : #include "nsUdetXPCOMWrapper.h"
43 : #include "nsCharSetProber.h" // for DumpStatus
44 :
45 : #include "nsUniversalCharDetDll.h"
46 : //---- for XPCOM
47 : #include "nsIFactory.h"
48 : #include "nsISupports.h"
49 : #include "pratom.h"
50 : #include "prmem.h"
51 : #include "nsCOMPtr.h"
52 :
53 : static NS_DEFINE_CID(kUniversalDetectorCID, NS_UNIVERSAL_DETECTOR_CID);
54 : static NS_DEFINE_CID(kUniversalStringDetectorCID, NS_UNIVERSAL_STRING_DETECTOR_CID);
55 :
56 : //---------------------------------------------------------------------
57 0 : nsXPCOMDetector:: nsXPCOMDetector(PRUint32 aLanguageFilter)
58 0 : : nsUniversalDetector(aLanguageFilter)
59 : {
60 0 : }
61 : //---------------------------------------------------------------------
62 0 : nsXPCOMDetector::~nsXPCOMDetector()
63 : {
64 0 : }
65 : //---------------------------------------------------------------------
66 :
67 0 : NS_IMPL_ISUPPORTS1(nsXPCOMDetector, nsICharsetDetector)
68 :
69 : //---------------------------------------------------------------------
70 0 : NS_IMETHODIMP nsXPCOMDetector::Init(
71 : nsICharsetDetectionObserver* aObserver)
72 : {
73 0 : NS_ASSERTION(mObserver == nsnull , "Init twice");
74 0 : if(nsnull == aObserver)
75 0 : return NS_ERROR_ILLEGAL_VALUE;
76 :
77 0 : mObserver = aObserver;
78 0 : return NS_OK;
79 : }
80 : //----------------------------------------------------------
81 0 : NS_IMETHODIMP nsXPCOMDetector::DoIt(const char* aBuf,
82 : PRUint32 aLen, bool* oDontFeedMe)
83 : {
84 0 : NS_ASSERTION(mObserver != nsnull , "have not init yet");
85 :
86 0 : if((nsnull == aBuf) || (nsnull == oDontFeedMe))
87 0 : return NS_ERROR_ILLEGAL_VALUE;
88 :
89 0 : this->Reset();
90 0 : nsresult rv = this->HandleData(aBuf, aLen);
91 0 : if (NS_FAILED(rv))
92 0 : return rv;
93 :
94 0 : if (mDone)
95 : {
96 0 : if (mDetectedCharset)
97 0 : Report(mDetectedCharset);
98 :
99 0 : *oDontFeedMe = true;
100 : }
101 0 : *oDontFeedMe = false;
102 0 : return NS_OK;
103 : }
104 : //----------------------------------------------------------
105 0 : NS_IMETHODIMP nsXPCOMDetector::Done()
106 : {
107 0 : NS_ASSERTION(mObserver != nsnull , "have not init yet");
108 : #ifdef DEBUG_chardet
109 : for (PRInt32 i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
110 : {
111 : // If no data was received the array might stay filled with nulls
112 : // the way it was initialized in the constructor.
113 : if (mCharSetProbers[i])
114 : mCharSetProbers[i]->DumpStatus();
115 : }
116 : #endif
117 :
118 0 : this->DataEnd();
119 0 : return NS_OK;
120 : }
121 : //----------------------------------------------------------
122 0 : void nsXPCOMDetector::Report(const char* aCharset)
123 : {
124 0 : NS_ASSERTION(mObserver != nsnull , "have not init yet");
125 : #ifdef DEBUG_chardet
126 : printf("Universal Charset Detector report charset %s . \r\n", aCharset);
127 : #endif
128 0 : mObserver->Notify(aCharset, eBestAnswer);
129 0 : }
130 :
131 :
132 : //---------------------------------------------------------------------
133 0 : nsXPCOMStringDetector:: nsXPCOMStringDetector(PRUint32 aLanguageFilter)
134 0 : : nsUniversalDetector(aLanguageFilter)
135 : {
136 0 : }
137 : //---------------------------------------------------------------------
138 0 : nsXPCOMStringDetector::~nsXPCOMStringDetector()
139 : {
140 0 : }
141 : //---------------------------------------------------------------------
142 0 : NS_IMPL_ISUPPORTS1(nsXPCOMStringDetector, nsIStringCharsetDetector)
143 : //---------------------------------------------------------------------
144 0 : void nsXPCOMStringDetector::Report(const char *aCharset)
145 : {
146 0 : mResult = aCharset;
147 : #ifdef DEBUG_chardet
148 : printf("New Charset Prober report charset %s . \r\n", aCharset);
149 : #endif
150 0 : }
151 : //---------------------------------------------------------------------
152 0 : NS_IMETHODIMP nsXPCOMStringDetector::DoIt(const char* aBuf,
153 : PRUint32 aLen, const char** oCharset,
154 : nsDetectionConfident &oConf)
155 : {
156 0 : mResult = nsnull;
157 0 : this->Reset();
158 0 : nsresult rv = this->HandleData(aBuf, aLen);
159 0 : if (NS_FAILED(rv))
160 0 : return rv;
161 0 : this->DataEnd();
162 0 : if (mResult)
163 : {
164 0 : *oCharset=mResult;
165 0 : oConf = eBestAnswer;
166 : }
167 0 : return NS_OK;
168 : }
|