Back to index

lightning-sunbird  0.9+nobinonly
nsXMLEncodingObserver.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; 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.org 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) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or 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 #include "nsICharsetAlias.h"
00039 #include "nsXMLEncodingObserver.h"
00040 #include "nsIXMLEncodingService.h"
00041 #include "nsIElementObserver.h"
00042 #include "nsIObserver.h"
00043 #include "nsIObserverService.h"
00044 #include "nsISupports.h"
00045 #include "nsCRT.h"
00046 #include "nsIParser.h"
00047 #include "pratom.h"
00048 #include "nsCharDetDll.h"
00049 #include "nsIServiceManager.h"
00050 #include "nsObserverBase.h"
00051 #include "nsWeakReference.h"
00052 #include "nsReadableUtils.h"
00053 #include "nsUnicharUtils.h"
00054 
00055 static NS_DEFINE_CID(kCharsetAliasCID, NS_CHARSETALIAS_CID);
00056 
00057 static const eHTMLTags gTags[] = 
00058 { eHTMLTag_instruction,
00059   eHTMLTag_unknown
00060 };
00061 
00062 //-------------------------------------------------------------------------
00063 nsXMLEncodingObserver::nsXMLEncodingObserver()
00064 {
00065   bXMLEncodingObserverStarted = PR_FALSE;
00066 }
00067 //-------------------------------------------------------------------------
00068 nsXMLEncodingObserver::~nsXMLEncodingObserver()
00069 {
00070   // call to end the ObserverService
00071   if (bXMLEncodingObserverStarted == PR_TRUE) {
00072     End();
00073   }
00074 }
00075 
00076 //-------------------------------------------------------------------------
00077 NS_IMPL_ADDREF ( nsXMLEncodingObserver )
00078 NS_IMPL_RELEASE ( nsXMLEncodingObserver )
00079 
00080 // Use the new scheme
00081 NS_IMPL_QUERY_INTERFACE4(nsXMLEncodingObserver, 
00082                          nsIElementObserver, 
00083                          nsIObserver, 
00084                          nsIXMLEncodingService, 
00085                          nsISupportsWeakReference)
00086 
00087 //-------------------------------------------------------------------------
00088 NS_IMETHODIMP nsXMLEncodingObserver::Notify(
00089                      PRUint32 aDocumentID, 
00090                      const PRUnichar* aTag, 
00091                      PRUint32 numOfAttributes, 
00092                      const PRUnichar* nameArray[], 
00093                      const PRUnichar* valueArray[])
00094 {
00095     if(!nsDependentString(aTag).LowerCaseEqualsLiteral("?xml")) 
00096         return NS_ERROR_ILLEGAL_VALUE;
00097     else
00098         return Notify(aDocumentID, numOfAttributes, nameArray, valueArray);
00099 }
00100 //-------------------------------------------------------------------------
00101 NS_IMETHODIMP nsXMLEncodingObserver::Notify(
00102                      PRUint32 aDocumentID, 
00103                      eHTMLTags aTag, 
00104                      PRUint32 numOfAttributes, 
00105                      const PRUnichar* nameArray[], 
00106                      const PRUnichar* valueArray[])
00107 {
00108     if(eHTMLTag_meta != aTag) 
00109         return NS_ERROR_ILLEGAL_VALUE;
00110     else 
00111         return Notify(aDocumentID, numOfAttributes, nameArray, valueArray);
00112 }
00113 //-------------------------------------------------------------------------
00114 NS_IMETHODIMP nsXMLEncodingObserver::Notify(
00115                      PRUint32 aDocumentID, 
00116                      PRUint32 numOfAttributes, 
00117                      const PRUnichar* nameArray[], 
00118                      const PRUnichar* valueArray[])
00119 {
00120 
00121     nsresult res = NS_OK;
00122     PRUint32 i;
00123 
00124     if(numOfAttributes >= 3)
00125     {
00126       PRBool bGotCurrentCharset=PR_FALSE;
00127       PRBool bGotCurrentCharsetSource = PR_FALSE;
00128       PRBool bGotEncoding = PR_FALSE;
00129 
00130       nsCAutoString currentCharset(NS_LITERAL_CSTRING("unknown"));
00131       nsAutoString charsetSourceStr(NS_LITERAL_STRING("unknown"));
00132       nsCAutoString encoding(NS_LITERAL_CSTRING("unknown"));
00133 
00134       for(i=0; i < numOfAttributes; i++) 
00135       {
00136          if(0==nsCRT::strcmp(nameArray[i], NS_LITERAL_STRING("charset").get())) 
00137          {
00138            bGotCurrentCharset = PR_TRUE;
00139            CopyUCS2toASCII(nsDependentString(valueArray[i]), currentCharset);
00140          } else if(0==nsCRT::strcmp(nameArray[i], NS_LITERAL_STRING("charsetSource").get())) {
00141            bGotCurrentCharsetSource = PR_TRUE;
00142            charsetSourceStr = valueArray[i];
00143          } else if(nsDependentString(nameArray[i]).LowerCaseEqualsLiteral("encoding")) { 
00144            bGotEncoding = PR_TRUE;
00145            CopyUCS2toASCII(nsDependentString(valueArray[i]), encoding);
00146          }
00147       }
00148 
00149       // if we cannot find currentCharset or currentCharsetSource
00150       // return error.
00151       if( ! (bGotCurrentCharset && bGotCurrentCharsetSource))
00152       {
00153          return NS_ERROR_ILLEGAL_VALUE;
00154       }
00155 
00156       PRInt32 err;
00157       PRInt32 charsetSourceInt = charsetSourceStr.ToInteger(&err);
00158 
00159       // if we cannot convert the string into PRInt32, return error
00160       if(NS_FAILED(err))
00161          return NS_ERROR_ILLEGAL_VALUE;
00162 
00163       PRInt32 currentCharsetSource = charsetSourceInt;
00164 
00165       if(kCharsetFromMetaTag > currentCharsetSource)
00166       {
00167            if(! encoding.Equals(currentCharset)) 
00168            {
00169               nsCOMPtr<nsICharsetAlias> calias = do_GetService(kCharsetAliasCID, &res);
00170                if(NS_SUCCEEDED(res) && (nsnull != calias) ) 
00171                {
00172                     PRBool same = PR_FALSE;
00173                     res = calias->Equals( encoding, currentCharset, &same);
00174                     if(NS_SUCCEEDED(res) && (! same))
00175                     {
00176                           nsCAutoString preferred;
00177                           res = calias->GetPreferred(encoding,
00178                                                      preferred);
00179                           if(NS_SUCCEEDED(res))
00180                           {
00181                             res = NotifyWebShell(0,0, preferred.get(), kCharsetFromMetaTag );
00182                             return res;
00183                           } // if check for GetPreferred
00184                     } // if check res for Equals
00185                 } // if check res for GetService
00186             } // if Equals
00187        } // if 
00188     } // if 
00189 
00190     return NS_OK;
00191 }
00192 
00193 //-------------------------------------------------------------------------
00194 NS_IMETHODIMP nsXMLEncodingObserver::Observe(nsISupports*, const char*, const PRUnichar*) 
00195 {
00196     return NS_ERROR_NOT_IMPLEMENTED;
00197 }
00198 //-------------------------------------------------------------------------
00199 NS_IMETHODIMP nsXMLEncodingObserver::Start() 
00200 {
00201     nsresult res = NS_OK;
00202 
00203     if (bXMLEncodingObserverStarted == PR_TRUE) 
00204       return res;
00205 
00206     nsCOMPtr<nsIObserverService> anObserverService = do_GetService("@mozilla.org/observer-service;1", &res);
00207 
00208     if (NS_SUCCEEDED(res)) {
00209       res = anObserverService->AddObserver(this, "xmlparser", PR_TRUE);
00210 
00211       bXMLEncodingObserverStarted = PR_TRUE;
00212     }
00213 
00214     return res;
00215 }
00216 //-------------------------------------------------------------------------
00217 NS_IMETHODIMP nsXMLEncodingObserver::End() 
00218 {
00219     nsresult res = NS_OK;
00220     
00221     if (bXMLEncodingObserverStarted == PR_FALSE) 
00222       return res;
00223 
00224     nsCOMPtr<nsIObserverService> anObserverService = do_GetService("@mozilla.org/observer-service;1", &res);
00225     if (NS_SUCCEEDED(res)) {
00226       res = anObserverService->RemoveObserver(this, "xmlparser");
00227 
00228       bXMLEncodingObserverStarted = PR_FALSE;
00229     }
00230 
00231     return res;
00232 }
00233