Back to index

lightning-sunbird  0.9+nobinonly
nsUdetXPCOMWrapper.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Universal charset detector code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *          Shy Shalom <shooshX@gmail.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nscore.h"
00040 
00041 #include "nsUniversalDetector.h"
00042 #include "nsUdetXPCOMWrapper.h"
00043 #include "nsCharSetProber.h" // for DumpStatus
00044 
00045 #include "nsUniversalCharDetDll.h"
00046 //---- for XPCOM
00047 #include "nsIFactory.h"
00048 #include "nsISupports.h"
00049 #include "pratom.h"
00050 #include "prmem.h"
00051 #include "nsCOMPtr.h"
00052 
00053 static NS_DEFINE_CID(kUniversalDetectorCID, NS_UNIVERSAL_DETECTOR_CID);
00054 static NS_DEFINE_CID(kUniversalStringDetectorCID, NS_UNIVERSAL_STRING_DETECTOR_CID);
00055 
00056 //---------------------------------------------------------------------
00057 nsUniversalXPCOMDetector:: nsUniversalXPCOMDetector() : nsUniversalDetector()
00058 {
00059 }
00060 //---------------------------------------------------------------------
00061 nsUniversalXPCOMDetector::~nsUniversalXPCOMDetector() 
00062 {
00063 }
00064 //---------------------------------------------------------------------
00065 
00066 NS_IMPL_ISUPPORTS1(nsUniversalXPCOMDetector, nsICharsetDetector)
00067 
00068 //---------------------------------------------------------------------
00069 NS_IMETHODIMP nsUniversalXPCOMDetector::Init(
00070               nsICharsetDetectionObserver* aObserver)
00071 {
00072   NS_ASSERTION(mObserver == nsnull , "Init twice");
00073   if(nsnull == aObserver)
00074     return NS_ERROR_ILLEGAL_VALUE;
00075 
00076   mObserver = aObserver;
00077   return NS_OK;
00078 }
00079 //----------------------------------------------------------
00080 NS_IMETHODIMP nsUniversalXPCOMDetector::DoIt(const char* aBuf,
00081               PRUint32 aLen, PRBool* oDontFeedMe)
00082 {
00083   NS_ASSERTION(mObserver != nsnull , "have not init yet");
00084 
00085   if((nsnull == aBuf) || (nsnull == oDontFeedMe))
00086     return NS_ERROR_ILLEGAL_VALUE;
00087 
00088   nsresult rv = this->HandleData(aBuf, aLen);
00089   if (NS_FAILED(rv))
00090     return rv;
00091 
00092   if (mDone)
00093   {
00094     if (mDetectedCharset)
00095       Report(mDetectedCharset);
00096 
00097     *oDontFeedMe = PR_TRUE;
00098   }
00099   *oDontFeedMe = PR_FALSE;
00100   return NS_OK;
00101 }
00102 //----------------------------------------------------------
00103 NS_IMETHODIMP nsUniversalXPCOMDetector::Done()
00104 {
00105   NS_ASSERTION(mObserver != nsnull , "have not init yet");
00106 #ifdef DEBUG_chardet
00107   for (PRInt32 i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
00108   {
00109     // If no data was received the array might stay filled with nulls
00110     // the way it was initialized in the constructor.
00111     if (mCharSetProbers[i])
00112       mCharSetProbers[i]->DumpStatus();
00113   }
00114 #endif
00115 
00116   this->DataEnd();
00117   return NS_OK;
00118 }
00119 //----------------------------------------------------------
00120 void nsUniversalXPCOMDetector::Report(const char* aCharset)
00121 {
00122   NS_ASSERTION(mObserver != nsnull , "have not init yet");
00123 #ifdef DEBUG_chardet
00124   printf("Universal Charset Detector report charset %s . \r\n", aCharset);
00125 #endif
00126   mObserver->Notify(aCharset, eBestAnswer);
00127 }
00128 
00129 
00130 //---------------------------------------------------------------------
00131 nsUniversalXPCOMStringDetector:: nsUniversalXPCOMStringDetector()
00132   : nsUniversalDetector()
00133 {
00134 }
00135 //---------------------------------------------------------------------
00136 nsUniversalXPCOMStringDetector::~nsUniversalXPCOMStringDetector() 
00137 {
00138 }
00139 //---------------------------------------------------------------------
00140 NS_IMPL_ISUPPORTS1(nsUniversalXPCOMStringDetector, nsIStringCharsetDetector)
00141 //---------------------------------------------------------------------
00142 void nsUniversalXPCOMStringDetector::Report(const char *aCharset) 
00143 {
00144   mResult = aCharset;
00145 #ifdef DEBUG_chardet
00146   printf("New Charset Prober report charset %s . \r\n", aCharset);
00147 #endif
00148 }
00149 //---------------------------------------------------------------------
00150 NS_IMETHODIMP nsUniversalXPCOMStringDetector::DoIt(const char* aBuf,
00151                      PRUint32 aLen, const char** oCharset,
00152                      nsDetectionConfident &oConf)
00153 {
00154   mResult = nsnull;
00155   this->Reset();
00156   nsresult rv = this->HandleData(aBuf, aLen); 
00157   if (NS_FAILED(rv))
00158     return rv;
00159   this->DataEnd();
00160   if (mResult)
00161   {
00162     *oCharset=mResult;
00163     oConf = eBestAnswer;
00164   }
00165   return NS_OK;
00166 }