Back to index

lightning-sunbird  0.9+nobinonly
nsParser.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  
00074 #ifndef NS_PARSER__
00075 #define NS_PARSER__
00076 
00077 #include "nsIParser.h"
00078 #include "nsDeque.h"
00079 #include "nsParserNode.h"
00080 #include "nsIURL.h"
00081 #include "CParserContext.h"
00082 #include "nsParserCIID.h"
00083 #include "nsITokenizer.h"
00084 #include "nsHTMLTags.h"
00085 #include "nsDTDUtils.h"
00086 #include "nsTimer.h"
00087 #include "nsIEventQueue.h"
00088 #include "nsIContentSink.h"
00089 #include "nsIParserFilter.h"
00090 #include "nsCOMArray.h"
00091 #include "nsIUnicharStreamListener.h"
00092 
00093 class nsIDTD;
00094 class nsScanner;
00095 class nsIProgressEventSink;
00096 
00097 #ifdef _MSC_VER
00098 #pragma warning( disable : 4275 )
00099 #endif
00100 
00101 
00102 class nsParser : public nsIParser,
00103                  public nsIStreamListener{
00104 
00105   
00106   public:
00107     friend class CTokenHandler;
00111     static nsresult Init();
00112 
00116     static void Shutdown();
00117 
00118     NS_DECL_ISUPPORTS
00119 
00120 
00125     nsParser();
00126 
00127 
00132     virtual ~nsParser();
00133 
00140     NS_IMETHOD_(void) SetContentSink(nsIContentSink* aSink);
00141 
00148     NS_IMETHOD_(nsIContentSink*) GetContentSink(void);
00149     
00159     NS_IMETHOD_(void) GetCommand(nsCString& aCommand);
00160     NS_IMETHOD_(void) SetCommand(const char* aCommand);
00161     NS_IMETHOD_(void) SetCommand(eParserCommands aParserCommand);
00162 
00172     NS_IMETHOD_(void) SetDocumentCharset(const nsACString& aCharset, PRInt32 aSource);
00173 
00174     NS_IMETHOD_(void) GetDocumentCharset(nsACString& aCharset, PRInt32& aSource)
00175     {
00176          aCharset = mCharset;
00177          aSource = mCharsetSource;
00178     }
00179 
00180 
00181     NS_IMETHOD_(void) SetParserFilter(nsIParserFilter* aFilter);
00182     
00183     NS_IMETHOD RegisterDTD(nsIDTD* aDTD);
00184 
00191     NS_IMETHOD_(nsDTDMode) GetParseMode(void);
00192 
00200     NS_IMETHOD Parse(nsIURI* aURL,
00201                      nsIRequestObserver* aListener = nsnull,
00202                      PRBool aEnableVerify = PR_FALSE,
00203                      void* aKey = 0,
00204                      nsDTDMode aMode = eDTDMode_autodetect);
00205 
00212     NS_IMETHOD Parse(nsIInputStream* aStream,
00213                      const nsACString& aMimeType,
00214                      PRBool aEnableVerify = PR_FALSE,
00215                      void* aKey = 0,
00216                      nsDTDMode aMode = eDTDMode_autodetect);
00217 
00224     NS_IMETHOD Parse(const nsAString& aSourceBuffer,
00225                      void* aKey,
00226                      const nsACString& aContentType,
00227                      PRBool aEnableVerify,
00228                      PRBool aLastCall,
00229                      nsDTDMode aMode = eDTDMode_autodetect);
00230 
00234     NS_IMETHOD ParseFragment(const nsAString& aSourceBuffer,
00235                              void* aKey,
00236                              nsVoidArray& aTagStack,
00237                              PRBool aXMLMode,
00238                              const nsACString& aContentType,
00239                              nsDTDMode aMode = eDTDMode_autodetect);
00240 
00241 
00248     NS_IMETHOD BuildModel(void);
00249 
00259     NS_IMETHOD        ContinueParsing();
00260     NS_IMETHOD        ContinueInterruptedParsing();
00261     NS_IMETHOD_(void) BlockParser();
00262     NS_IMETHOD_(void) UnblockParser();
00263     NS_IMETHOD        Terminate(void);
00264 
00271     NS_IMETHOD_(PRBool) IsParserEnabled();
00272 
00279     NS_IMETHOD_(PRBool) IsComplete();
00280 
00291     void SetUnusedInput(nsString& aBuffer);
00292 
00298     virtual nsresult ResumeParse(PRBool allowIteration = PR_TRUE, 
00299                                  PRBool aIsFinalChunk = PR_FALSE,
00300                                  PRBool aCanInterrupt = PR_TRUE);
00301 
00302      //*********************************************
00303       // These methods are callback methods used by
00304       // net lib to let us know about our inputstream.
00305       //*********************************************
00306     // nsIRequestObserver methods:
00307     NS_DECL_NSIREQUESTOBSERVER
00308 
00309     // nsIStreamListener methods:
00310     NS_DECL_NSISTREAMLISTENER
00311 
00312     void              PushContext(CParserContext& aContext);
00313     CParserContext*   PopContext();
00314     CParserContext*   PeekContext() {return mParserContext;}
00315 
00322     nsresult GetTokenizer(nsITokenizer*& aTokenizer);
00323 
00330     NS_IMETHOD GetChannel(nsIChannel** aChannel);
00331 
00338     NS_IMETHOD GetDTD(nsIDTD** aDTD);
00339   
00344     PRBool DetectMetaTag(const char* aBytes, 
00345                          PRInt32 aLen, 
00346                          nsCString& oCharset, 
00347                          PRInt32& oCharsetSource);
00348 
00349     void SetSinkCharset(nsACString& aCharset);
00350 
00356     NS_IMETHODIMP CancelParsingEvents();
00357 
00364     PRBool CanInterrupt(void);
00365 
00371     void SetCanInterrupt(PRBool aCanInterrupt);
00372 
00381     nsresult PostContinueEvent();
00382 
00387     void HandleParserContinueEvent(void);
00388 
00393     nsresult DataAdded(const nsSubstring& aData, nsIRequest *aRequest);
00394 
00395     static nsCOMArray<nsIUnicharStreamListener> *sParserDataListeners;
00396 
00397 protected:
00398 
00405     nsresult WillBuildModel(nsString& aFilename);
00406 
00413     nsresult DidBuildModel(nsresult anErrorCode);
00414     
00415 private:
00416 
00417     /*******************************************
00418       These are the tokenization methods...
00419      *******************************************/
00420 
00430     PRBool WillTokenize(PRBool aIsFinalChunk = PR_FALSE);
00431 
00432    
00441     nsresult Tokenize(PRBool aIsFinalChunk = PR_FALSE);
00442 
00452     PRBool DidTokenize(PRBool aIsFinalChunk = PR_FALSE);
00453 
00454   
00455 protected:
00456     //*********************************************
00457     // And now, some data members...
00458     //*********************************************
00459     
00460       
00461     nsCOMPtr<nsIEventQueue>      mEventQueue;
00462     CParserContext*              mParserContext;
00463     nsCOMPtr<nsIRequestObserver> mObserver;
00464     nsCOMPtr<nsIContentSink>     mSink;
00465    
00466     nsCOMPtr<nsIParserFilter> mParserFilter;
00467     nsTokenAllocator          mTokenAllocator;
00468     
00469     eParserCommands     mCommand;
00470     nsresult            mInternalState;
00471     PRInt32             mStreamStatus;
00472     PRInt32             mCharsetSource;
00473     
00474     PRUint16            mFlags;
00475 
00476     nsString            mUnusedInput;
00477     nsCString           mCharset;
00478     nsCString           mCommandStr;
00479 
00480     
00481    
00482 public:  
00483    
00484     MOZ_TIMER_DECLARE(mParseTime)
00485     MOZ_TIMER_DECLARE(mDTDTime)
00486     MOZ_TIMER_DECLARE(mTokenizeTime)
00487 };
00488 
00489 #endif 
00490