Back to index

lightning-sunbird  0.9+nobinonly
txStylesheetCompiler.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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 TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Jonas Sicking.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Jonas Sicking <jonas@sicking.cc>
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 #ifndef TRANSFRMX_TXSTYLESHEETCOMPILER_H
00040 #define TRANSFRMX_TXSTYLESHEETCOMPILER_H
00041 
00042 #include "txStack.h"
00043 #include "txXSLTPatterns.h"
00044 #include "Expr.h"
00045 #include "txIXPathContext.h"
00046 #include "nsAutoPtr.h"
00047 #include "txStylesheet.h"
00048 
00049 class txHandlerTable;
00050 class txElementContext;
00051 class txInstructionContainer;
00052 class txInstruction;
00053 class txNamespaceMap;
00054 class txToplevelItem;
00055 class txPushNewContext;
00056 class txStylesheetCompiler;
00057 
00058 class txElementContext : public TxObject
00059 {
00060 public:
00061     txElementContext(const nsAString& aBaseURI);
00062     txElementContext(const txElementContext& aOther);
00063 
00064     PRBool mPreserveWhitespace;
00065     PRBool mForwardsCompatibleParsing;
00066     nsString mBaseURI;
00067     nsRefPtr<txNamespaceMap> mMappings;
00068     nsVoidArray mInstructionNamespaces;
00069     PRInt32 mDepth;
00070 };
00071 
00072 class txACompileObserver
00073 {
00074 public:
00075     virtual nsrefcnt AddRef() = 0;
00076     virtual nsrefcnt Release() = 0;
00077 
00078     virtual nsresult loadURI(const nsAString& aUri,
00079                              const nsAString& aReferrerUri,
00080                              txStylesheetCompiler* aCompiler) = 0;
00081     virtual void onDoneCompiling(txStylesheetCompiler* aCompiler,
00082                                  nsresult aResult,
00083                                  const PRUnichar *aErrorText = nsnull,
00084                                  const PRUnichar *aParam = nsnull) = 0;
00085 };
00086 
00087 #define TX_DECL_ACOMPILEOBSERVER \
00088   nsrefcnt AddRef(); \
00089   nsrefcnt Release(); \
00090   nsresult loadURI(const nsAString& aUri, const nsAString& aReferrerUri, \
00091                    txStylesheetCompiler* aCompiler); \
00092   void onDoneCompiling(txStylesheetCompiler* aCompiler, nsresult aResult, \
00093                        const PRUnichar *aErrorText = nsnull, \
00094                        const PRUnichar *aParam = nsnull)
00095 
00096 class txStylesheetCompilerState : public txIParseContext
00097 {
00098 public:
00099     txStylesheetCompilerState(txACompileObserver* aObserver);
00100     ~txStylesheetCompilerState();
00101     
00102     nsresult init(const nsAString& aStylesheetURI, txStylesheet* aStylesheet,
00103                   txListIterator* aInsertPosition);
00104 
00105     // Embedded stylesheets state
00106     PRBool handleEmbeddedSheet()
00107     {
00108         return mEmbedStatus == eInEmbed;
00109     }
00110     void doneEmbedding()
00111     {
00112         mEmbedStatus = eHasEmbed;
00113     }
00114 
00115     // Stack functions
00116     nsresult pushHandlerTable(txHandlerTable* aTable);
00117     void popHandlerTable();
00118     nsresult pushSorter(txPushNewContext* aSorter);
00119     void popSorter();
00120     nsresult pushChooseGotoList();
00121     void popChooseGotoList();
00122     nsresult pushObject(TxObject* aObject);
00123     TxObject* popObject();
00124     nsresult pushPtr(void* aPtr);
00125     void* popPtr();
00126 
00127     // stylesheet functions
00128     nsresult addToplevelItem(txToplevelItem* aItem);
00129     nsresult openInstructionContainer(txInstructionContainer* aContainer);
00130     void closeInstructionContainer();
00131     nsresult addInstruction(nsAutoPtr<txInstruction> aInstruction);
00132     nsresult loadIncludedStylesheet(const nsAString& aURI);
00133     nsresult loadImportedStylesheet(const nsAString& aURI,
00134                                     txStylesheet::ImportFrame* aFrame);
00135     
00136     // misc
00137     nsresult addGotoTarget(txInstruction** aTargetPointer);
00138     nsresult addVariable(const txExpandedName& aName);
00139 
00140     // txIParseContext
00141     nsresult resolveNamespacePrefix(nsIAtom* aPrefix, PRInt32& aID);
00142     nsresult resolveFunctionCall(nsIAtom* aName, PRInt32 aID,
00143                                  FunctionCall*& aFunction);
00144     PRBool caseInsensitiveNameTests();
00145 
00149     PRBool fcp()
00150     {
00151         return mElementContext->mForwardsCompatibleParsing;
00152     }
00153 
00154     void SetErrorOffset(PRUint32 aOffset);
00155 
00156 
00157     nsRefPtr<txStylesheet> mStylesheet;
00158     txHandlerTable* mHandlerTable;
00159     nsAutoPtr<txElementContext> mElementContext;
00160     txPushNewContext* mSorter;
00161     nsAutoPtr<txList> mChooseGotoList;
00162     PRPackedBool mDOE;
00163     PRPackedBool mSearchingForFallback;
00164 
00165 protected:
00166     nsRefPtr<txACompileObserver> mObserver;
00167     nsVoidArray mInScopeVariables;
00168     nsVoidArray mChildCompilerList;
00169     // embed info, target information is the ID
00170     nsString mTarget;
00171     enum 
00172     {
00173         eNoEmbed,
00174         eNeedEmbed,
00175         eInEmbed,
00176         eHasEmbed
00177     } mEmbedStatus;
00178     nsString mStylesheetURI;
00179     PRPackedBool mIsTopCompiler;
00180     PRPackedBool mDoneWithThisStylesheet;
00181     txStack mObjectStack;
00182     txStack mOtherStack;
00183 
00184 private:
00185     txInstruction** mNextInstrPtr;
00186     txListIterator mToplevelIterator;
00187     nsVoidArray mGotoTargetPointers;
00188 };
00189 
00190 struct txStylesheetAttr
00191 {
00192     PRInt32 mNamespaceID;
00193     nsCOMPtr<nsIAtom> mLocalName;
00194     nsCOMPtr<nsIAtom> mPrefix;
00195     nsString mValue;
00196 };
00197 
00198 class txStylesheetCompiler : private txStylesheetCompilerState,
00199                              public txACompileObserver
00200 {
00201 public:
00202     friend class txStylesheetCompilerState;
00203     txStylesheetCompiler(const nsAString& aStylesheetURI,
00204                          txACompileObserver* aObserver);
00205     txStylesheetCompiler(const nsAString& aStylesheetURI,
00206                          txStylesheet* aStylesheet,
00207                          txListIterator* aInsertPosition,
00208                          txACompileObserver* aObserver);
00209     virtual nsrefcnt AddRef();
00210     virtual nsrefcnt Release();
00211 
00212     void setBaseURI(const nsString& aBaseURI);
00213 
00214     nsresult startElement(PRInt32 aNamespaceID, nsIAtom* aLocalName,
00215                           nsIAtom* aPrefix, txStylesheetAttr* aAttributes,
00216                           PRInt32 aAttrCount);
00217     nsresult startElement(const PRUnichar *aName,
00218                           const PRUnichar **aAtts,
00219                           PRInt32 aAttrCount, PRInt32 aIDOffset);
00220     nsresult endElement();
00221     nsresult characters(const nsAString& aStr);
00222     nsresult doneLoading();
00223 
00224     void cancel(nsresult aError, const PRUnichar *aErrorText = nsnull,
00225                 const PRUnichar *aParam = nsnull);
00226 
00227     txStylesheet* getStylesheet();
00228 
00229     // txACompileObserver
00230     nsresult loadURI(const nsAString& aUri, const nsAString& aReferrerUri,
00231                      txStylesheetCompiler* aCompiler);
00232     void onDoneCompiling(txStylesheetCompiler* aCompiler, nsresult aResult,
00233                          const PRUnichar *aErrorText = nsnull,
00234                          const PRUnichar *aParam = nsnull);
00235 
00236 private:
00237     nsresult startElementInternal(PRInt32 aNamespaceID, nsIAtom* aLocalName,
00238                                   nsIAtom* aPrefix,
00239                                   txStylesheetAttr* aAttributes,
00240                                   PRInt32 aAttrCount,
00241                                   PRInt32 aIDOffset = -1);
00242 
00243     nsresult flushCharacters();
00244     nsresult ensureNewElementContext();
00245     nsresult maybeDoneCompiling();
00246 
00247     nsAutoRefCnt mRefCnt;
00248     nsString mCharacters;
00249     nsresult mStatus;
00250 };
00251 
00252 class txInScopeVariable {
00253 public:
00254     txInScopeVariable(const txExpandedName& aName) : mName(aName), mLevel(1)
00255     {
00256     }
00257     txExpandedName mName;
00258     PRInt32 mLevel;
00259 };
00260 
00261 #endif