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 : * Axel Hecht.
19 : * Portions created by the Initial Developer are Copyright (C) 2001
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Axel Hecht <axel@pike.org>
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 TX_XSLT_PATTERNS_H
40 : #define TX_XSLT_PATTERNS_H
41 :
42 : #include "txExpr.h"
43 : #include "txXMLUtils.h"
44 :
45 : class ProcessorState;
46 :
47 : class txPattern
48 : {
49 : public:
50 0 : txPattern()
51 0 : {
52 0 : MOZ_COUNT_CTOR(txPattern);
53 0 : }
54 0 : virtual ~txPattern()
55 0 : {
56 0 : MOZ_COUNT_DTOR(txPattern);
57 0 : }
58 :
59 : /*
60 : * Determines whether this Pattern matches the given node.
61 : */
62 : virtual bool matches(const txXPathNode& aNode,
63 : txIMatchContext* aContext) = 0;
64 :
65 : /*
66 : * Returns the default priority of this Pattern.
67 : *
68 : * Simple Patterns return the values as specified in XPath 5.5.
69 : * Returns -Inf for union patterns, as it shouldn't be called on them.
70 : */
71 : virtual double getDefaultPriority() = 0;
72 :
73 : /**
74 : * Returns the type of this pattern.
75 : */
76 : enum Type {
77 : STEP_PATTERN,
78 : UNION_PATTERN,
79 : OTHER_PATTERN
80 : };
81 0 : virtual Type getType()
82 : {
83 0 : return OTHER_PATTERN;
84 : }
85 :
86 : /**
87 : * Returns sub-expression at given position
88 : */
89 : virtual Expr* getSubExprAt(PRUint32 aPos) = 0;
90 :
91 : /**
92 : * Replace sub-expression at given position. Does not delete the old
93 : * expression, that is the responsibility of the caller.
94 : */
95 : virtual void setSubExprAt(PRUint32 aPos, Expr* aExpr) = 0;
96 :
97 : /**
98 : * Returns sub-pattern at given position
99 : */
100 : virtual txPattern* getSubPatternAt(PRUint32 aPos) = 0;
101 :
102 : /**
103 : * Replace sub-pattern at given position. Does not delete the old
104 : * pattern, that is the responsibility of the caller.
105 : */
106 : virtual void setSubPatternAt(PRUint32 aPos, txPattern* aPattern) = 0;
107 :
108 : #ifdef TX_TO_STRING
109 : /*
110 : * Returns the String representation of this Pattern.
111 : * @param dest the String to use when creating the String
112 : * representation. The String representation will be appended to
113 : * any data in the destination String, to allow cascading calls to
114 : * other #toString() methods for Patterns.
115 : * @return the String representation of this Pattern.
116 : */
117 : virtual void toString(nsAString& aDest) = 0;
118 : #endif
119 : };
120 :
121 : #define TX_DECL_PATTERN_BASE \
122 : bool matches(const txXPathNode& aNode, txIMatchContext* aContext); \
123 : double getDefaultPriority(); \
124 : virtual Expr* getSubExprAt(PRUint32 aPos); \
125 : virtual void setSubExprAt(PRUint32 aPos, Expr* aExpr); \
126 : virtual txPattern* getSubPatternAt(PRUint32 aPos); \
127 : virtual void setSubPatternAt(PRUint32 aPos, txPattern* aPattern)
128 :
129 : #ifndef TX_TO_STRING
130 : #define TX_DECL_PATTERN TX_DECL_PATTERN_BASE
131 : #else
132 : #define TX_DECL_PATTERN \
133 : TX_DECL_PATTERN_BASE; \
134 : void toString(nsAString& aDest)
135 : #endif
136 :
137 : #define TX_IMPL_PATTERN_STUBS_NO_SUB_EXPR(_class) \
138 : Expr* \
139 : _class::getSubExprAt(PRUint32 aPos) \
140 : { \
141 : return nsnull; \
142 : } \
143 : void \
144 : _class::setSubExprAt(PRUint32 aPos, Expr* aExpr) \
145 : { \
146 : NS_NOTREACHED("setting bad subexpression index"); \
147 : }
148 :
149 : #define TX_IMPL_PATTERN_STUBS_NO_SUB_PATTERN(_class) \
150 : txPattern* \
151 : _class::getSubPatternAt(PRUint32 aPos) \
152 : { \
153 : return nsnull; \
154 : } \
155 : void \
156 : _class::setSubPatternAt(PRUint32 aPos, txPattern* aPattern) \
157 : { \
158 : NS_NOTREACHED("setting bad subexpression index"); \
159 : }
160 :
161 : class txUnionPattern : public txPattern
162 0 : {
163 : public:
164 0 : nsresult addPattern(txPattern* aPattern)
165 : {
166 0 : return mLocPathPatterns.AppendElement(aPattern) ?
167 0 : NS_OK : NS_ERROR_OUT_OF_MEMORY;
168 : }
169 :
170 : TX_DECL_PATTERN;
171 : Type getType();
172 :
173 : private:
174 : txOwningArray<txPattern> mLocPathPatterns;
175 : };
176 :
177 : class txLocPathPattern : public txPattern
178 0 : {
179 : public:
180 : nsresult addStep(txPattern* aPattern, bool isChild);
181 :
182 : TX_DECL_PATTERN;
183 :
184 : private:
185 0 : class Step {
186 : public:
187 : nsAutoPtr<txPattern> pattern;
188 : bool isChild;
189 : };
190 :
191 : nsTArray<Step> mSteps;
192 : };
193 :
194 : class txRootPattern : public txPattern
195 0 : {
196 : public:
197 : #ifdef TX_TO_STRING
198 0 : txRootPattern()
199 0 : : mSerialize(true)
200 : {
201 0 : }
202 : #endif
203 :
204 : TX_DECL_PATTERN;
205 :
206 : #ifdef TX_TO_STRING
207 : public:
208 0 : void setSerialize(bool aSerialize)
209 : {
210 0 : mSerialize = aSerialize;
211 0 : }
212 :
213 : private:
214 : // Don't serialize txRootPattern if it's used in a txLocPathPattern
215 : bool mSerialize;
216 : #endif
217 : };
218 :
219 : class txIdPattern : public txPattern
220 0 : {
221 : public:
222 : txIdPattern(const nsSubstring& aString);
223 :
224 : TX_DECL_PATTERN;
225 :
226 : private:
227 : nsCOMArray<nsIAtom> mIds;
228 : };
229 :
230 : class txKeyPattern : public txPattern
231 0 : {
232 : public:
233 0 : txKeyPattern(nsIAtom* aPrefix, nsIAtom* aLocalName,
234 : PRInt32 aNSID, const nsAString& aValue)
235 : : mName(aNSID, aLocalName),
236 : #ifdef TX_TO_STRING
237 : mPrefix(aPrefix),
238 : #endif
239 0 : mValue(aValue)
240 : {
241 0 : }
242 :
243 : TX_DECL_PATTERN;
244 :
245 : private:
246 : txExpandedName mName;
247 : #ifdef TX_TO_STRING
248 : nsCOMPtr<nsIAtom> mPrefix;
249 : #endif
250 : nsString mValue;
251 : };
252 :
253 : class txStepPattern : public txPattern,
254 : public PredicateList
255 0 : {
256 : public:
257 0 : txStepPattern(txNodeTest* aNodeTest, bool isAttr)
258 0 : : mNodeTest(aNodeTest), mIsAttr(isAttr)
259 : {
260 0 : }
261 :
262 : TX_DECL_PATTERN;
263 : Type getType();
264 :
265 0 : txNodeTest* getNodeTest()
266 : {
267 0 : return mNodeTest;
268 : }
269 0 : void setNodeTest(txNodeTest* aNodeTest)
270 : {
271 0 : mNodeTest.forget();
272 0 : mNodeTest = aNodeTest;
273 0 : }
274 :
275 : private:
276 : nsAutoPtr<txNodeTest> mNodeTest;
277 : bool mIsAttr;
278 : };
279 :
280 : #endif // TX_XSLT_PATTERNS_H
|