Back to index

lightning-sunbird  0.9+nobinonly
CParserContext.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.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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 
00039 #include "nsIAtom.h"
00040 #include "CParserContext.h"
00041 #include "nsToken.h"
00042 #include "prenv.h"  
00043 #include "nsIHTMLContentSink.h"
00044 #include "nsHTMLTokenizer.h"
00045 #include "nsExpatDriver.h"
00046 
00047 MOZ_DECL_CTOR_COUNTER(CParserContext)
00048 
00049 
00057 CParserContext::CParserContext(nsScanner* aScanner, 
00058                                void *aKey, 
00059                                eParserCommands aCommand,
00060                                nsIRequestObserver* aListener, 
00061                                nsIDTD *aDTD, 
00062                                eAutoDetectResult aStatus, 
00063                                PRBool aCopyUnused)
00064 { 
00065   MOZ_COUNT_CTOR(CParserContext); 
00066 
00067   mScanner=aScanner; 
00068   mKey=aKey; 
00069   mPrevContext=0; 
00070   mListener=aListener; 
00071   NS_IF_ADDREF(mListener); 
00072   mDTDMode=eDTDMode_unknown; 
00073   mAutoDetectStatus=aStatus; 
00074   mTransferBuffer=0; 
00075   mDTD=aDTD; 
00076   NS_IF_ADDREF(mDTD);
00077   mTokenizer = 0;
00078   mTransferBufferSize=eTransferBufferSize; 
00079   mStreamListenerState=eNone; 
00080   mMultipart=PR_TRUE; 
00081   mContextType=eCTNone; 
00082   mCopyUnused=aCopyUnused; 
00083   mParserCommand=aCommand;
00084   mRequest=0;
00085 } 
00086 
00095 CParserContext::CParserContext(const CParserContext &aContext) : mMimeType() {
00096   MOZ_COUNT_CTOR(CParserContext);
00097 
00098   mScanner=aContext.mScanner;
00099   mKey=aContext.mKey;
00100   mPrevContext=0;
00101   mListener=aContext.mListener;
00102   NS_IF_ADDREF(mListener);
00103 
00104   mDTDMode=aContext.mDTDMode;
00105   mAutoDetectStatus=aContext.mAutoDetectStatus;
00106   mTransferBuffer=aContext.mTransferBuffer;
00107   mDTD=aContext.mDTD;
00108   NS_IF_ADDREF(mDTD);
00109 
00110   mTokenizer = aContext.mTokenizer;
00111   NS_IF_ADDREF(mTokenizer);
00112 
00113   mTransferBufferSize=eTransferBufferSize;
00114   mStreamListenerState=aContext.mStreamListenerState;
00115   mMultipart=aContext.mMultipart;
00116   mContextType=aContext.mContextType;
00117   mRequest=aContext.mRequest;
00118   mParserCommand=aContext.mParserCommand;
00119   SetMimeType(aContext.mMimeType);
00120 }
00121 
00122 
00128 CParserContext::~CParserContext(){
00129 
00130   MOZ_COUNT_DTOR(CParserContext);
00131 
00132   if(mScanner) {
00133     delete mScanner;
00134     mScanner=nsnull;
00135   }
00136 
00137   if(mTransferBuffer)
00138     delete [] mTransferBuffer;
00139 
00140   NS_IF_RELEASE(mDTD);
00141   NS_IF_RELEASE(mListener);
00142   NS_IF_RELEASE(mTokenizer);
00143 
00144   //Remember that it's ok to simply ingore the PrevContext.
00145 
00146 }
00147 
00148 
00153 void CParserContext::SetMimeType(const nsACString& aMimeType){
00154   mMimeType.Assign(aMimeType);
00155 
00156   mDocType=ePlainText;
00157 
00158   if(mMimeType.EqualsLiteral(kHTMLTextContentType))
00159     mDocType=eHTML_Strict;
00160   else if (mMimeType.EqualsLiteral(kXMLTextContentType)          ||
00161            mMimeType.EqualsLiteral(kXMLApplicationContentType)   ||
00162            mMimeType.EqualsLiteral(kXHTMLApplicationContentType) ||
00163            mMimeType.EqualsLiteral(kXULTextContentType)          ||
00164 #ifdef MOZ_SVG
00165            mMimeType.EqualsLiteral(kSVGTextContentType)          ||
00166 #endif
00167            mMimeType.EqualsLiteral(kRDFApplicationContentType)   ||
00168            mMimeType.EqualsLiteral(kRDFTextContentType))
00169     mDocType=eXML;
00170 }
00171 
00172 nsresult
00173 CParserContext::GetTokenizer(PRInt32 aType,
00174                              nsIContentSink* aSink,
00175                              nsITokenizer*& aTokenizer) {
00176   nsresult result = NS_OK;
00177   
00178   if(!mTokenizer) {
00179     if (aType == NS_IPARSER_FLAG_HTML || mParserCommand == eViewSource) {
00180       nsCOMPtr<nsIHTMLContentSink> theSink = do_QueryInterface(aSink);
00181       PRUint16 theFlags = 0;
00182 
00183       if (theSink) {
00184         // XXX This code is repeated both here and in CNavDTD. Can the two
00185         // callsites be combined?
00186         PRBool enabled;
00187         theSink->IsEnabled(eHTMLTag_frameset, &enabled);
00188         if(enabled) {
00189           theFlags |= NS_IPARSER_FLAG_FRAMES_ENABLED;
00190         }
00191         
00192         theSink->IsEnabled(eHTMLTag_script, &enabled);
00193         if(enabled) {
00194           theFlags |= NS_IPARSER_FLAG_SCRIPT_ENABLED;
00195         }
00196       }
00197 
00198       result = NS_NewHTMLTokenizer(&mTokenizer,mDTDMode,mDocType,
00199                                    mParserCommand,theFlags);
00200       // Make sure the new tokenizer has all of the necessary information.
00201       // XXX this might not be necessary.
00202       if (mTokenizer && mPrevContext) {
00203         mTokenizer->CopyState(mPrevContext->mTokenizer);
00204       }
00205     }
00206     else if (aType == NS_IPARSER_FLAG_XML)
00207     {
00208       result = CallQueryInterface(mDTD, &mTokenizer);
00209     }
00210   }
00211   
00212   aTokenizer = mTokenizer;
00213   return result;
00214 }