1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is TransforMiiX XSLT processor code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Jonas Sicking.
19 : * Portions created by the Initial Developer are Copyright (C) 2002
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Jonas Sicking <jonas@sicking.cc>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef TRANSFRMX_TXSTYLESHEETCOMPILER_H
40 : #define TRANSFRMX_TXSTYLESHEETCOMPILER_H
41 :
42 : #include "txStack.h"
43 : #include "txXSLTPatterns.h"
44 : #include "txExpr.h"
45 : #include "txIXPathContext.h"
46 : #include "nsAutoPtr.h"
47 : #include "txStylesheet.h"
48 : #include "nsTArray.h"
49 :
50 : extern bool
51 : TX_XSLTFunctionAvailable(nsIAtom* aName, PRInt32 aNameSpaceID);
52 :
53 : class txHandlerTable;
54 : class txElementContext;
55 : class txInstructionContainer;
56 : class txInstruction;
57 : class txNamespaceMap;
58 : class txToplevelItem;
59 : class txPushNewContext;
60 : class txStylesheetCompiler;
61 : class txInScopeVariable;
62 :
63 : class txElementContext : public txObject
64 0 : {
65 : public:
66 : txElementContext(const nsAString& aBaseURI);
67 : txElementContext(const txElementContext& aOther);
68 :
69 : bool mPreserveWhitespace;
70 : bool mForwardsCompatibleParsing;
71 : nsString mBaseURI;
72 : nsRefPtr<txNamespaceMap> mMappings;
73 : nsTArray<PRInt32> mInstructionNamespaces;
74 : PRInt32 mDepth;
75 : };
76 :
77 : class txACompileObserver
78 0 : {
79 : public:
80 : NS_IMETHOD_(nsrefcnt) AddRef() = 0;
81 : NS_IMETHOD_(nsrefcnt) Release() = 0;
82 :
83 : virtual nsresult loadURI(const nsAString& aUri,
84 : const nsAString& aReferrerUri,
85 : txStylesheetCompiler* aCompiler) = 0;
86 : virtual void onDoneCompiling(txStylesheetCompiler* aCompiler,
87 : nsresult aResult,
88 : const PRUnichar *aErrorText = nsnull,
89 : const PRUnichar *aParam = nsnull) = 0;
90 : };
91 :
92 : #define TX_DECL_ACOMPILEOBSERVER \
93 : nsresult loadURI(const nsAString& aUri, const nsAString& aReferrerUri, \
94 : txStylesheetCompiler* aCompiler); \
95 : void onDoneCompiling(txStylesheetCompiler* aCompiler, nsresult aResult, \
96 : const PRUnichar *aErrorText = nsnull, \
97 : const PRUnichar *aParam = nsnull);
98 :
99 : class txStylesheetCompilerState : public txIParseContext
100 : {
101 : public:
102 : txStylesheetCompilerState(txACompileObserver* aObserver);
103 : ~txStylesheetCompilerState();
104 :
105 : nsresult init(const nsAString& aStylesheetURI, txStylesheet* aStylesheet,
106 : txListIterator* aInsertPosition);
107 :
108 : // Embedded stylesheets state
109 0 : bool handleEmbeddedSheet()
110 : {
111 0 : return mEmbedStatus == eInEmbed;
112 : }
113 0 : void doneEmbedding()
114 : {
115 0 : mEmbedStatus = eHasEmbed;
116 0 : }
117 :
118 : // Stack functions
119 : enum enumStackType
120 : {
121 : eElementHandler,
122 : eHandlerTable,
123 : eVariableItem,
124 : eCopy,
125 : eInstruction,
126 : ePushNewContext,
127 : eConditionalGoto,
128 : eCheckParam,
129 : ePushNullTemplateRule
130 : };
131 : nsresult pushHandlerTable(txHandlerTable* aTable);
132 : void popHandlerTable();
133 : nsresult pushSorter(txPushNewContext* aSorter);
134 : void popSorter();
135 : nsresult pushChooseGotoList();
136 : void popChooseGotoList();
137 : nsresult pushObject(txObject* aObject);
138 : txObject* popObject();
139 : nsresult pushPtr(void* aPtr, enumStackType aType);
140 : void* popPtr(enumStackType aType);
141 :
142 : // stylesheet functions
143 : nsresult addToplevelItem(txToplevelItem* aItem);
144 : nsresult openInstructionContainer(txInstructionContainer* aContainer);
145 : void closeInstructionContainer();
146 : nsresult addInstruction(nsAutoPtr<txInstruction> aInstruction);
147 : nsresult loadIncludedStylesheet(const nsAString& aURI);
148 : nsresult loadImportedStylesheet(const nsAString& aURI,
149 : txStylesheet::ImportFrame* aFrame);
150 :
151 : // misc
152 : nsresult addGotoTarget(txInstruction** aTargetPointer);
153 : nsresult addVariable(const txExpandedName& aName);
154 :
155 : // txIParseContext
156 : nsresult resolveNamespacePrefix(nsIAtom* aPrefix, PRInt32& aID);
157 : nsresult resolveFunctionCall(nsIAtom* aName, PRInt32 aID,
158 : FunctionCall** aFunction);
159 : bool caseInsensitiveNameTests();
160 :
161 : /**
162 : * Should the stylesheet be parsed in forwards compatible parsing mode.
163 : */
164 0 : bool fcp()
165 : {
166 0 : return mElementContext->mForwardsCompatibleParsing;
167 : }
168 :
169 : void SetErrorOffset(PRUint32 aOffset);
170 :
171 : static void shutdown();
172 :
173 :
174 : nsRefPtr<txStylesheet> mStylesheet;
175 : txHandlerTable* mHandlerTable;
176 : nsAutoPtr<txElementContext> mElementContext;
177 : txPushNewContext* mSorter;
178 : nsAutoPtr<txList> mChooseGotoList;
179 : bool mDOE;
180 : bool mSearchingForFallback;
181 :
182 : protected:
183 : nsRefPtr<txACompileObserver> mObserver;
184 : nsTArray<txInScopeVariable*> mInScopeVariables;
185 : nsTArray<txStylesheetCompiler*> mChildCompilerList;
186 : // embed info, target information is the ID
187 : nsString mTarget;
188 : enum
189 : {
190 : eNoEmbed,
191 : eNeedEmbed,
192 : eInEmbed,
193 : eHasEmbed
194 : } mEmbedStatus;
195 : nsString mStylesheetURI;
196 : bool mIsTopCompiler;
197 : bool mDoneWithThisStylesheet;
198 : txStack mObjectStack;
199 : txStack mOtherStack;
200 : nsTArray<enumStackType> mTypeStack;
201 :
202 : private:
203 : txInstruction** mNextInstrPtr;
204 : txListIterator mToplevelIterator;
205 : nsTArray<txInstruction**> mGotoTargetPointers;
206 : };
207 :
208 : struct txStylesheetAttr
209 0 : {
210 : PRInt32 mNamespaceID;
211 : nsCOMPtr<nsIAtom> mLocalName;
212 : nsCOMPtr<nsIAtom> mPrefix;
213 : nsString mValue;
214 : };
215 :
216 : class txStylesheetCompiler : private txStylesheetCompilerState,
217 : public txACompileObserver
218 0 : {
219 : public:
220 : friend class txStylesheetCompilerState;
221 : friend bool TX_XSLTFunctionAvailable(nsIAtom* aName,
222 : PRInt32 aNameSpaceID);
223 : txStylesheetCompiler(const nsAString& aStylesheetURI,
224 : txACompileObserver* aObserver);
225 : txStylesheetCompiler(const nsAString& aStylesheetURI,
226 : txStylesheet* aStylesheet,
227 : txListIterator* aInsertPosition,
228 : txACompileObserver* aObserver);
229 :
230 : void setBaseURI(const nsString& aBaseURI);
231 :
232 : nsresult startElement(PRInt32 aNamespaceID, nsIAtom* aLocalName,
233 : nsIAtom* aPrefix, txStylesheetAttr* aAttributes,
234 : PRInt32 aAttrCount);
235 : nsresult startElement(const PRUnichar *aName,
236 : const PRUnichar **aAtts,
237 : PRInt32 aAttrCount, PRInt32 aIDOffset);
238 : nsresult endElement();
239 : nsresult characters(const nsAString& aStr);
240 : nsresult doneLoading();
241 :
242 : void cancel(nsresult aError, const PRUnichar *aErrorText = nsnull,
243 : const PRUnichar *aParam = nsnull);
244 :
245 : txStylesheet* getStylesheet();
246 :
247 : TX_DECL_ACOMPILEOBSERVER
248 0 : NS_INLINE_DECL_REFCOUNTING(txStylesheetCompiler)
249 :
250 : private:
251 : nsresult startElementInternal(PRInt32 aNamespaceID, nsIAtom* aLocalName,
252 : nsIAtom* aPrefix,
253 : txStylesheetAttr* aAttributes,
254 : PRInt32 aAttrCount,
255 : PRInt32 aIDOffset = -1);
256 :
257 : nsresult flushCharacters();
258 : nsresult ensureNewElementContext();
259 : nsresult maybeDoneCompiling();
260 :
261 : nsString mCharacters;
262 : nsresult mStatus;
263 : };
264 :
265 0 : class txInScopeVariable {
266 : public:
267 0 : txInScopeVariable(const txExpandedName& aName) : mName(aName), mLevel(1)
268 : {
269 0 : }
270 : txExpandedName mName;
271 : PRInt32 mLevel;
272 : };
273 :
274 : #endif
|