This commit is contained in:
ftang%netscape.com 1999-06-22 03:03:32 +00:00
Родитель 10664be5d3
Коммит 8b7556346e
14 изменённых файлов: 1137 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,31 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsCDETAdaptorCID_h__
#define nsCDETAdaptorCID_h__
#include "nscore.h"
#define NS_CHARSET_DETECTION_ADAPTOR_PROGID "component://netscape/intl/charsetdetectionadaptor"
// {12BB8F17-2389-11d3-B3BF-00805F8A6670}
#define NS_CHARSET_DETECTION_ADAPTOR_CID \
{ 0x12bb8f17, 0x2389, 0x11d3, { 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
#endif // nsCDETAdaptorCID_h__

Просмотреть файл

@ -0,0 +1,55 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsDetetctionConfident_h__
#define nsDetetctionConfident_h__
/*
This type is used to indidcate how confident the detection module about
the return result.
eNoAnswerYet is used to indidcate that the detector have not find out a
answer yet based on the data it received.
eBestAnswer is used to indidcate that the answer the detector returned
is the best one within the knowledge of the detector.
In other words, the test to all other candidcates fail.
For example, the (Shift_JIS/EUC-JP/ISO-2022-JP) detection
module may return this with answer "Shift_JIS "if it receive
bytes > 0x80 (which make ISO-2022-JP test failed) and byte
0x82 (which may EUC-JP test failed)
eSureAnswer is used to indidcate that the detector are 100% sure about the
answer.
Exmaple 1; the Shift_JIS/ISO-2022-JP/EUC-JP detector return
this w/ ISO-2022-JP when it hit one of the following ESC seq
ESC ( J
ESC $ @
ESC $ B
Example 2: the detector which can detect UCS2 return w/ UCS2
when the first 2 byte are BOM mark.
Example 3: the Korean detector return ISO-2022-KR when it
hit ESC $ ) C
*/
typedef enum {
eNoAnswerYet = 0,
eBestAnswer,
eSureAnswer
} nsDetectionConfident;
#endif /* nsDetetctionConfident_h__ */

Просмотреть файл

@ -0,0 +1,46 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsICDETAdaptor_h__
#define nsICDETAdaptor_h__
#include "nsISupports.h"
class nsICharsetDetector;
class nsIWebShellServices;
// {12BB8F13-2389-11d3-B3BF-00805F8A6670}
#define NS_ICHARSETDETECTIONADAPTOR_IID \
{ 0x12bb8f13, 0x2389, 0x11d3, { 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
/*
This interface is defined to be serverd as an Adaptor between
nsIWebShell, nsICharsetDetector and nsIParserFilter
It make the nsICharsetDetector implementation independent from the
nsIParserFilter and nsIWebShell
*/
class nsICharsetDetectionAdaptor : public nsISupports {
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_ICHARSETDETECTIONADAPTOR_IID)
/*
Initialize it by setup the nsICharsetDetector and the
nsIWebShell
*/
NS_IMETHOD Init(nsIWebShellServices* aWebShell, nsICharsetDetector *aDetector) = 0;
};
#endif /* nsICDETAdaptor_h__ */

Просмотреть файл

@ -0,0 +1,38 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsICDETObserver_h__
#define nsICDDETObserver_h__
#include "nsISupports.h"
#include "nsDetectionConfident.h"
// {12BB8F12-2389-11d3-B3BF-00805F8A6670}
#define NS_ICHARSETDETECTIONOBSERVER_IID \
{ 0x12bb8f12, 0x2389, 0x11d3, { 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
/*
Used to inform answer by nsICharsetDetector
*/
class nsICharsetDetectionObserver : public nsISupports {
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_ICHARSETDETECTIONOBSERVER_IID)
NS_IMETHOD Notify(const char* aCharset, nsDetectionConfident aConf) = 0;
};
#endif /* nsICDETObserver_h__ */

Просмотреть файл

@ -0,0 +1,64 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsICharsetDetector_h__
#define nsICharsetDetector_h__
#include "nsISupports.h"
class nsICharsetDetectionObserver;
// {12BB8F14-2389-11d3-B3BF-00805F8A6670}
#define NS_ICHARSETDETECTOR_IID \
{ 0x12bb8f14, 0x2389, 0x11d3, { 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
#define NS_CHARSET_DETECTOR_PROGID_BASE "component://netscape/intl/charsetdetect?type="
class nsICharsetDetector : public nsISupports {
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_ICHARSETDETECTOR_IID)
/*
Setup the observer so it know how to notify the answer
*/
NS_IMETHOD Init(nsICharsetDetectionObserver* observer) = 0;
/*
Return the charset the detector know about
*/
NS_IMETHOD GetCandidates(PRInt32 aNum, const char* aCharsetArray[]) = 0;
/*
Feed a block of bytes to the detector.
It will call the Notify function of the nsICharsetObserver if it find out
the answer.
aBytesArray - array of bytes
aLen - length of aBytesArray
oDontFeedMe - return PR_TRUE if the detector do not need the following block
PR_FALSE it need more bytes.
This is used to enhance performance
*/
NS_IMETHOD DoIt(const char* aBytesArray, PRUint32 aLen, PRBool* oDontFeedMe) = 0;
/*
It also tell the detector the last chance the make a decision
*/
NS_IMETHOD Done() = 0;
};
#endif /* nsICharsetDetector_h__ */

Просмотреть файл

@ -0,0 +1,54 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsIStringCharsetDetector_h__
#define nsIStringCharsetDetector_h__
#include "nsISupports.h"
#include "nsString.h"
#include "nsDetectionConfident.h"
// {12BB8F15-2389-11d3-B3BF-00805F8A6670}
#define NS_ISTRINGCHARSETDETECTOR_IID \
{ 0x12bb8f15, 0x2389, 0x11d3, { 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
/*
This interface is similar to nsICharsetDetector
The difference is it is for line base detection instead of block based
detectection.
*/
class nsIStringCharsetDetector : public nsISupports {
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_ISTRINGCHARSETDETECTOR_IID)
/*
Return the Charset the detector know about
*/
NS_IMETHOD GetCandidates(PRInt32 aNum, const char* aCharsetArray[]) = 0;
/*
Perform the charset detection
aBytesArray- the bytes
aLen- the length of the bytes
oCharset- the charset answer
oConfident - the confidence of the answer
*/
NS_IMETHOD DoIt(const char* aBytesArray, PRUint32 aLen,
const char** oCharset, nsDetectionConfident &oConfident) = 0;
};
#endif /* nsIStringCharsetDetector_h__ */

Просмотреть файл

@ -0,0 +1,36 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsIXMLEncodingService_h__
#define nsIXMLEncodingService_h__
#include "nsISupports.h"
// {12BB8F11-2389-11d3-B3BF-00805F8A6670}
#define NS_IXML_ENCODING_SERVICE_IID \
{ 0x12bb8f11, 0x2389, 0x11d3, { 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
class nsIXMLEncodingService : public nsISupports {
public:
NS_DEFINE_STATIC_IID_ACCESSOR( NS_IXML_ENCODING_SERVICE_IID )
NS_IMETHOD Start() = 0;
NS_IMETHOD End() = 0;
};
#endif // nsIXMLEncodingService_h__

Просмотреть файл

@ -0,0 +1,31 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsXMLEncodingCID_h__
#define nsXMLEncodingCID_h__
#include "nscore.h"
#define NS_XML_ENCODING_PROGID "component://netscape/intl/xmlencoding"
// {12BB8F16-2389-11d3-B3BF-00805F8A6670}
#define NS_XML_ENCODING_CID \
{ 0x12bb8f16, 0x2389, 0x11d3, { 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
#endif // nsXMLEncodingCID_h__

Просмотреть файл

@ -0,0 +1,244 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#define IMPL_NS_IPARSERFILTER
#include "nsICharsetDetectionObserver.h"
#include "nsICharsetDetectionAdaptor.h"
#include "nsDetectionAdaptor.h"
#include "nsICharsetDetector.h"
#include "nsIWebShellServices.h"
#include "pratom.h"
#include "nsCharDetDll.h"
#ifdef IMPL_NS_IPARSERFILTER
#include "nsIParserFilter.h"
static NS_DEFINE_IID(kIParserFilterIID, NS_IPARSERFILTER_IID);
#endif /* IMPL_NS_IPARSERFILTER */
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
class CToken;
class nsDetectionAdaptor : public nsICharsetDetectionObserver,
#ifdef IMPL_NS_IPARSERFILTER
public nsIParserFilter,
#endif /* IMPL_NS_IPARSERFILTER */
public nsICharsetDetectionAdaptor
{
public:
NS_DECL_ISUPPORTS
public:
nsDetectionAdaptor( void );
virtual ~nsDetectionAdaptor( void );
// Methods to support nsICharsetDetectionObserver
NS_IMETHOD Notify(const char* aCharset, nsDetectionConfident aConf);
// Methods to support nsICharsetDetectionAdaptor
NS_IMETHOD Init(nsIWebShellServices* aWebShellSvc, nsICharsetDetector *aDetector);
// Methode to suppor nsIParserFilter
NS_IMETHOD RawBuffer(char * buffer, PRUint32 * buffer_length) ;
NS_IMETHOD Finish();
// really don't care the following two, only because they are defined
// in nsIParserFilter.h
NS_IMETHOD WillAddToken(CToken & token) { return NS_OK; };
NS_IMETHOD ProcessTokens( void ) {return NS_OK;};
private:
nsICharsetDetector* mDetector;
nsIWebShellServices* mWebShellSvc;
PRBool mDontFeedToDetector;
};
//--------------------------------------------------------------
nsDetectionAdaptor::nsDetectionAdaptor( void )
{
NS_INIT_REFCNT();
PR_AtomicIncrement(& g_InstanceCount);
mDetector = nsnull;
mWebShellSvc = nsnull;
mDontFeedToDetector = PR_TRUE;
}
//--------------------------------------------------------------
nsDetectionAdaptor::~nsDetectionAdaptor()
{
NS_IF_RELEASE(mDetector);
NS_IF_RELEASE(mWebShellSvc);
PR_AtomicDecrement(& g_InstanceCount);
}
//--------------------------------------------------------------
NS_IMPL_ADDREF ( nsDetectionAdaptor );
NS_IMPL_RELEASE ( nsDetectionAdaptor );
//--------------------------------------------------------------
//----------------------------------------------------------------------
NS_IMETHODIMP nsDetectionAdaptor::QueryInterface(REFNSIID aIID, void**aInstancePtr)
{
if( NULL == aInstancePtr) {
return NS_ERROR_NULL_POINTER;
}
*aInstancePtr = NULL;
if( aIID.Equals ( nsICharsetDetectionObserver::GetIID() )) {
*aInstancePtr = (void*) ((nsICharsetDetectionObserver*) this);
NS_ADDREF_THIS();
return NS_OK;
}
if( aIID.Equals ( nsICharsetDetectionAdaptor::GetIID() )) {
*aInstancePtr = (void*) ((nsICharsetDetectionAdaptor*) this);
NS_ADDREF_THIS();
return NS_OK;
}
#ifdef IMPL_NS_IPARSERFILTER
if( aIID.Equals ( kIParserFilterIID)) {
*aInstancePtr = (void*) ((nsIParserFilter*) this);
NS_ADDREF_THIS();
return NS_OK;
}
#endif /* IMPL_NS_IPARSERFILTER */
if( aIID.Equals ( kISupportsIID )) {
*aInstancePtr = (void*) ( this );
NS_ADDREF_THIS();
return NS_OK;
}
return NS_NOINTERFACE;
}
//--------------------------------------------------------------
NS_IMETHODIMP nsDetectionAdaptor::Notify(
const char* aCharset, nsDetectionConfident aConf)
{
nsresult rv = NS_OK;
rv = mWebShellSvc->ReloadDocument( aCharset, kCharsetFromAutoDetection);
return NS_OK;
}
//--------------------------------------------------------------
NS_IMETHODIMP nsDetectionAdaptor::Init(
nsIWebShellServices* aWebShellSvc, nsICharsetDetector *aDetector)
{
if((nsnull != aWebShellSvc) && (nsnull != aDetector))
{
nsICharsetDetectionObserver* aObserver = nsnull;
nsresult rv = NS_OK;
rv = this->QueryInterface(nsICharsetDetectionObserver::GetIID(),
(void**) &aObserver);
if(NS_SUCCEEDED(rv)) {
aDetector->Init(aObserver);
NS_ADDREF(aWebShellSvc);
mWebShellSvc = aWebShellSvc;
NS_ADDREF(aDetector);
mDetector = aDetector;
mDontFeedToDetector = PR_FALSE;
NS_IF_RELEASE(aObserver);
}
}
return NS_ERROR_ILLEGAL_VALUE;
}
//--------------------------------------------------------------
NS_IMETHODIMP nsDetectionAdaptor::RawBuffer
(char * buffer, PRUint32 * buffer_length)
{
if((mDontFeedToDetector) || (nsnull == mWebShellSvc) || (nsnull == mDetector))
return NS_OK;
nsresult rv = NS_OK;
rv = mDetector->DoIt((const char*)buffer, *buffer_length, &mDontFeedToDetector);
return NS_OK;
}
//--------------------------------------------------------------
NS_IMETHODIMP nsDetectionAdaptor::Finish()
{
if((mDontFeedToDetector) || (nsnull == mWebShellSvc) || (nsnull == mDetector))
return NS_OK;
nsresult rv = NS_OK;
rv = mDetector->Done();
NS_IF_RELEASE(mWebShellSvc);
NS_IF_RELEASE(mDetector); // need this line so no cycle reference
return NS_OK;
}
//--------------------------------------------------------------
class nsDetectionAdaptorFactory : public nsIFactory {
NS_DECL_ISUPPORTS
public:
nsDetectionAdaptorFactory() {
NS_INIT_REFCNT();
PR_AtomicIncrement(&g_InstanceCount);
}
virtual ~nsDetectionAdaptorFactory() {
PR_AtomicDecrement(&g_InstanceCount);
}
NS_IMETHOD CreateInstance(nsISupports* aDelegate, const nsIID& aIID, void** aResult);
NS_IMETHOD LockFactory(PRBool aLock);
};
//--------------------------------------------------------------
NS_DEFINE_IID( kIFactoryIID, NS_IFACTORY_IID);
NS_IMPL_ISUPPORTS( nsDetectionAdaptorFactory , kIFactoryIID);
//--------------------------------------------------------------
NS_IMETHODIMP nsDetectionAdaptorFactory::CreateInstance(
nsISupports* aDelegate, const nsIID &aIID, void** aResult)
{
if(NULL == aResult)
return NS_ERROR_NULL_POINTER;
if(NULL != aDelegate)
return NS_ERROR_NO_AGGREGATION;
*aResult = NULL;
nsDetectionAdaptor *inst = new nsDetectionAdaptor();
if(NULL == inst) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult res =inst->QueryInterface(aIID, aResult);
if(NS_FAILED(res)) {
delete inst;
}
return res;
}
//--------------------------------------------------------------
NS_IMETHODIMP nsDetectionAdaptorFactory::LockFactory(PRBool aLock)
{
if(aLock)
PR_AtomicIncrement( &g_LockCount );
else
PR_AtomicDecrement( &g_LockCount );
return NS_OK;
}
//==============================================================
nsIFactory* NEW_DETECTION_ADAPTOR_FACTORY()
{
return new nsDetectionAdaptorFactory();
}

Просмотреть файл

@ -0,0 +1,26 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsDetectionAdaptor_h__
#define nsDetectionAdaptor_h__
#include "nsIFactory.h"
nsIFactory* NEW_DETECTION_ADAPTOR_FACTORY();
#endif /* nsDetectionAdaptor_h__ */

Просмотреть файл

@ -0,0 +1,77 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
//#define DONT_INFORM_WEBSHELL
#include "nsIServiceManager.h"
#include "nsIDocumentLoader.h"
#include "nsIWebShellServices.h"
#include "nsIContentViewerContainer.h"
#include "nsObserverBase.h"
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kDocLoaderServiceCID, NS_DOCUMENTLOADER_SERVICE_CID);
static NS_DEFINE_IID(kIDocumentLoaderIID, NS_IDOCUMENTLOADER_IID);
static NS_DEFINE_IID(kIWebShellServicesIID, NS_IWEB_SHELL_SERVICES_IID);
//-------------------------------------------------------------------------
NS_IMETHODIMP nsObserverBase::NotifyWebShell(
PRUint32 aDocumentID, const char* charset, nsCharsetSource source)
{
nsresult res = NS_OK;
nsresult rv = NS_OK;
// shoudl docLoader a memeber to increase performance ???
nsIDocumentLoader * docLoader = nsnull;
nsIContentViewerContainer * cvc = nsnull;
nsIWebShellServices* wss = nsnull;
if(NS_FAILED(rv =nsServiceManager::GetService(kDocLoaderServiceCID,
kIDocumentLoaderIID,
(nsISupports**)&docLoader)))
goto done;
if(NS_FAILED(rv =docLoader->GetContentViewerContainer(aDocumentID, &cvc)))
goto done;
if(NS_FAILED( rv = cvc->QueryInterface(kIWebShellServicesIID, (void**)&wss)))
goto done;
#ifndef DONT_INFORM_WEBSHELL
// ask the webshellservice to load the URL
if(NS_FAILED( rv = wss->SetRendering(PR_FALSE) ))
goto done;
// XXX nisheeth, uncomment the following two line to see the reent problem
// if(NS_FAILED(rv = wss->StopDocumentLoad()))
// goto done;
if(NS_FAILED(rv = wss->ReloadDocument(charset, source)))
goto done;
res = NS_ERROR_HTMLPARSER_STOPPARSING;
#endif
done:
if(docLoader) {
nsServiceManager::ReleaseService(kDocLoaderServiceCID,docLoader);
}
NS_IF_RELEASE(cvc);
NS_IF_RELEASE(wss);
return res;
}

Просмотреть файл

@ -0,0 +1,49 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsObserverBase_h__
#define nsObserverBase_h__
#include "nsIParser.h"
//==========================================================================
//
// Class declaration for the class
//
//==========================================================================
class nsObserverBase {
public:
nsObserverBase() {};
virtual ~nsObserverBase() {};
/*
* Subject call observer when the parser hit the tag
* @param aDocumentID- ID of the document
* @param aTag- the tag
* @param valueArray - array of value
*/
protected:
NS_IMETHOD NotifyWebShell(PRUint32 aDocumentID, const char* charset, nsCharsetSource source);
};
#endif /* nsObserverBase_h__ */

Просмотреть файл

@ -0,0 +1,362 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsICharsetAlias.h"
#include "nsXMLEncodingObserver.h"
#include "nsIXMLEncodingService.h"
#include "nsIElementObserver.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsISupports.h"
#include "nsCRT.h"
#include "nsIParser.h"
#include "pratom.h"
#include "nsCharDetDll.h"
#include "nsIServiceManager.h"
#include "nsObserverBase.h"
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
//==========================================================================
//
// Class declaration for the class
//
//==========================================================================
class nsXMLEncodingObserver: public nsIElementObserver,
public nsIObserver,
public nsObserverBase,
public nsIXMLEncodingService {
NS_DECL_ISUPPORTS
public:
nsXMLEncodingObserver();
virtual ~nsXMLEncodingObserver();
/* methode for nsIElementObserver */
/*
* This method return the tag which the observer care about
*/
NS_IMETHOD_(const char*)GetTagNameAt(PRUint32 aTagIndex);
/*
* Subject call observer when the parser hit the tag
* @param aDocumentID- ID of the document
* @param aTag- the tag
* @param numOfAttributes - number of attributes
* @param nameArray - array of name.
* @param valueArray - array of value
*/
NS_IMETHOD Notify(PRUint32 aDocumentID, eHTMLTags aTag, PRUint32 numOfAttributes,
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
NS_IMETHOD Notify(PRUint32 aDocumentID, const PRUnichar* aTag, PRUint32 numOfAttributes,
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
/* methode for nsIObserver */
NS_DECL_IOBSERVER
/* methode for nsIXMLEncodingService */
NS_IMETHOD Start();
NS_IMETHOD End();
private:
NS_IMETHOD Notify(PRUint32 aDocumentID, PRUint32 numOfAttributes,
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
};
//-------------------------------------------------------------------------
nsXMLEncodingObserver::nsXMLEncodingObserver()
{
NS_INIT_REFCNT();
PR_AtomicIncrement(& g_InstanceCount);
}
//-------------------------------------------------------------------------
nsXMLEncodingObserver::~nsXMLEncodingObserver()
{
PR_AtomicDecrement(& g_InstanceCount);
}
//-------------------------------------------------------------------------
NS_IMPL_ADDREF ( nsXMLEncodingObserver );
NS_IMPL_RELEASE ( nsXMLEncodingObserver );
//-------------------------------------------------------------------------
NS_IMETHODIMP nsXMLEncodingObserver::QueryInterface(REFNSIID aIID, void** aInstancePtr)
{
if( NULL == aInstancePtr) {
return NS_ERROR_NULL_POINTER;
}
*aInstancePtr = NULL;
if( aIID.Equals ( nsIElementObserver::GetIID() )) {
*aInstancePtr = (void*) ((nsIElementObserver*) this);
NS_ADDREF_THIS();
return NS_OK;
}
if( aIID.Equals ( nsIObserver::GetIID() )) {
*aInstancePtr = (void*) ((nsIObserver*) this);
NS_ADDREF_THIS();
return NS_OK;
}
if( aIID.Equals ( nsIXMLEncodingService::GetIID() )) {
*aInstancePtr = (void*) ((nsIXMLEncodingService*) this);
NS_ADDREF_THIS();
return NS_OK;
}
if( aIID.Equals ( kISupportsIID )) {
*aInstancePtr = (void*) ( this );
NS_ADDREF_THIS();
return NS_OK;
}
return NS_NOINTERFACE;
}
//-------------------------------------------------------------------------
NS_IMETHODIMP_(const char*) nsXMLEncodingObserver::GetTagNameAt(PRUint32 aTagIndex)
{
if (aTagIndex == 0) {
return "?XML";
}else {
return nsnull;
}
}
//-------------------------------------------------------------------------
NS_IMETHODIMP nsXMLEncodingObserver::Notify(
PRUint32 aDocumentID,
const PRUnichar* aTag,
PRUint32 numOfAttributes,
const PRUnichar* nameArray[],
const PRUnichar* valueArray[])
{
if(0 != nsCRT::strcasecmp(aTag, "?XML"))
return NS_ERROR_ILLEGAL_VALUE;
else
return Notify(aDocumentID, numOfAttributes, nameArray, valueArray);
}
//-------------------------------------------------------------------------
NS_IMETHODIMP nsXMLEncodingObserver::Notify(
PRUint32 aDocumentID,
eHTMLTags aTag,
PRUint32 numOfAttributes,
const PRUnichar* nameArray[],
const PRUnichar* valueArray[])
{
if(eHTMLTag_meta != aTag)
return NS_ERROR_ILLEGAL_VALUE;
else
return Notify(aDocumentID, numOfAttributes, nameArray, valueArray);
}
//-------------------------------------------------------------------------
NS_IMETHODIMP nsXMLEncodingObserver::Notify(
PRUint32 aDocumentID,
PRUint32 numOfAttributes,
const PRUnichar* nameArray[],
const PRUnichar* valueArray[])
{
nsresult res = NS_OK;
PRUint32 i;
if(numOfAttributes >= 3)
{
PRBool bGotCurrentCharset=PR_FALSE;
PRBool bGotCurrentCharsetSource = PR_FALSE;
PRBool bGotEncoding = PR_FALSE;
nsAutoString currentCharset("unknown");
nsAutoString charsetSourceStr("unknown");
nsAutoString encoding("unknown");
for(i=0; i < numOfAttributes; i++)
{
if(0==nsCRT::strcmp(nameArray[i], "charset"))
{
bGotCurrentCharset = PR_TRUE;
currentCharset = valueArray[i];
} else if(0==nsCRT::strcmp(nameArray[i], "charsetSource")) {
bGotCurrentCharsetSource = PR_TRUE;
charsetSourceStr = valueArray[i];
} else if(0==nsCRT::strcasecmp(nameArray[i], "encoding")) {
bGotEncoding = PR_TRUE;
encoding = valueArray[i];
}
}
// if we cannot find currentCharset or currentCharsetSource
// return error.
if( ! (bGotCurrentCharset && bGotCurrentCharsetSource))
{
return NS_ERROR_ILLEGAL_VALUE;
}
PRInt32 err;
PRInt32 charsetSourceInt = charsetSourceStr.ToInteger(&err);
// if we cannot convert the string into nsCharsetSource, return error
if(NS_FAILED(err))
return NS_ERROR_ILLEGAL_VALUE;
nsCharsetSource currentCharsetSource = (nsCharsetSource)charsetSourceInt;
if(kCharsetFromMetaTag > currentCharsetSource)
{
if(! encoding.Equals(currentCharset))
{
nsICharsetAlias* calias = nsnull;
res = nsServiceManager::GetService(
kCharsetAliasCID,
nsICharsetAlias::GetIID(),
(nsISupports**) &calias);
if(NS_SUCCEEDED(res) && (nsnull != calias) )
{
PRBool same = PR_FALSE;
res = calias->Equals( encoding, currentCharset, &same);
if(NS_SUCCEEDED(res) && (! same))
{
nsAutoString preferred;
res = calias->GetPreferred(encoding, preferred);
if(NS_SUCCEEDED(res))
{
const char* charsetInCStr = preferred.ToNewCString();
if(nsnull != charsetInCStr) {
res = NotifyWebShell(aDocumentID, charsetInCStr, kCharsetFromMetaTag );
delete [] (char*)charsetInCStr;
return res;
}
} // if check for GetPreferred
} // if check res for Equals
nsServiceManager::ReleaseService(kCharsetAliasCID, calias);
} // if check res for GetService
} // if Equals
} // if
} // if
return NS_OK;
}
//-------------------------------------------------------------------------
NS_IMETHODIMP nsXMLEncodingObserver::Observe(nsISupports*, const PRUnichar*, const PRUnichar*)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
//-------------------------------------------------------------------------
NS_IMETHODIMP nsXMLEncodingObserver::Start()
{
nsresult res = NS_OK;
nsAutoString xmlTopic("xmlparser");
nsIObserverService* anObserverService = nsnull;
res = nsServiceManager::GetService(NS_OBSERVERSERVICE_PROGID,
nsIObserverService::GetIID(),
(nsISupports**) &anObserverService);
if(NS_FAILED(res))
goto done;
res = anObserverService->AddObserver(this, xmlTopic.GetUnicode());
nsServiceManager::ReleaseService(NS_OBSERVERSERVICE_PROGID,
anObserverService);
done:
return res;
}
//-------------------------------------------------------------------------
NS_IMETHODIMP nsXMLEncodingObserver::End()
{
nsresult res = NS_OK;
nsAutoString xmlTopic("xmlparser");
nsIObserverService* anObserverService = nsnull;
res = nsServiceManager::GetService(NS_OBSERVERSERVICE_PROGID,
nsIObserverService::GetIID(),
(nsISupports**) &anObserverService);
if(NS_FAILED(res))
goto done;
res = anObserverService->RemoveObserver(this, xmlTopic.GetUnicode());
nsServiceManager::ReleaseService(NS_OBSERVERSERVICE_PROGID,
anObserverService);
done:
return res;
}
//==========================================================================
class nsXMLEncodingObserverFactory : public nsIFactory {
NS_DECL_ISUPPORTS
public:
nsXMLEncodingObserverFactory() {
NS_INIT_REFCNT();
PR_AtomicIncrement(&g_InstanceCount);
}
virtual ~nsXMLEncodingObserverFactory() {
PR_AtomicDecrement(&g_InstanceCount);
}
NS_IMETHOD CreateInstance(nsISupports* aDelegate, const nsIID& aIID, void** aResult);
NS_IMETHOD LockFactory(PRBool aLock);
};
//--------------------------------------------------------------
NS_DEFINE_IID( kIFactoryIID, NS_IFACTORY_IID);
NS_IMPL_ISUPPORTS( nsXMLEncodingObserverFactory , kIFactoryIID);
NS_IMETHODIMP nsXMLEncodingObserverFactory::CreateInstance(
nsISupports* aDelegate, const nsIID &aIID, void** aResult)
{
if(NULL == aResult)
return NS_ERROR_NULL_POINTER;
if(NULL != aDelegate)
return NS_ERROR_NO_AGGREGATION;
*aResult = NULL;
nsXMLEncodingObserver *inst = new nsXMLEncodingObserver();
if(NULL == inst) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult res =inst->QueryInterface(aIID, aResult);
if(NS_FAILED(res)) {
delete inst;
}
return res;
}
//--------------------------------------------------------------
NS_IMETHODIMP nsXMLEncodingObserverFactory::LockFactory(PRBool aLock)
{
if(aLock)
PR_AtomicIncrement( &g_LockCount );
else
PR_AtomicDecrement( &g_LockCount );
return NS_OK;
}
//==============================================================
nsIFactory* NEW_XML_ENCODING_OBSERVER_FACTORY()
{
return new nsXMLEncodingObserverFactory();
}

Просмотреть файл

@ -0,0 +1,24 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsXMLEncodingObserverFactory_h__
#define nsXMLEncodingObserverFactory_h__
#include "nsIFactory.h"
nsIFactory* NEW_XML_ENCODING_OBSERVER_FACTORY();
#endif // nsXMLEncodingObserverFactory_h__