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) 2002
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 : #include "txPatternParser.h"
40 : #include "txExprLexer.h"
41 : #include "nsGkAtoms.h"
42 : #include "txError.h"
43 : #include "txStringUtils.h"
44 : #include "txXSLTPatterns.h"
45 : #include "txIXPathContext.h"
46 : #include "txPatternOptimizer.h"
47 :
48 :
49 0 : txPattern* txPatternParser::createPattern(const nsAFlatString& aPattern,
50 : txIParseContext* aContext)
51 : {
52 0 : txExprLexer lexer;
53 0 : nsresult rv = lexer.parse(aPattern);
54 0 : if (NS_FAILED(rv)) {
55 : // XXX error report parsing error
56 0 : return 0;
57 : }
58 0 : nsAutoPtr<txPattern> pattern;
59 0 : rv = createUnionPattern(lexer, aContext, *getter_Transfers(pattern));
60 0 : if (NS_FAILED(rv)) {
61 : // XXX error report parsing error
62 0 : return 0;
63 : }
64 :
65 : txPatternOptimizer optimizer;
66 0 : txPattern* newPattern = nsnull;
67 0 : rv = optimizer.optimize(pattern, &newPattern);
68 0 : NS_ENSURE_SUCCESS(rv, nsnull);
69 :
70 0 : return newPattern ? newPattern : pattern.forget();
71 : }
72 :
73 0 : nsresult txPatternParser::createUnionPattern(txExprLexer& aLexer,
74 : txIParseContext* aContext,
75 : txPattern*& aPattern)
76 : {
77 0 : nsresult rv = NS_OK;
78 0 : txPattern* locPath = 0;
79 :
80 0 : rv = createLocPathPattern(aLexer, aContext, locPath);
81 0 : if (NS_FAILED(rv))
82 0 : return rv;
83 :
84 0 : Token::Type type = aLexer.peek()->mType;
85 0 : if (type == Token::END) {
86 0 : aPattern = locPath;
87 0 : return NS_OK;
88 : }
89 :
90 0 : if (type != Token::UNION_OP) {
91 0 : delete locPath;
92 0 : return NS_ERROR_XPATH_PARSE_FAILURE;
93 : }
94 :
95 0 : txUnionPattern* unionPattern = new txUnionPattern();
96 0 : if (!unionPattern) {
97 0 : delete locPath;
98 0 : return NS_ERROR_OUT_OF_MEMORY;
99 : }
100 0 : rv = unionPattern->addPattern(locPath);
101 : #if 0 // XXX addPattern can't fail yet, it doesn't check for mem
102 : if (NS_FAILED(rv)) {
103 : delete unionPattern;
104 : delete locPath;
105 : return rv;
106 : }
107 : #endif
108 :
109 0 : aLexer.nextToken();
110 0 : do {
111 0 : rv = createLocPathPattern(aLexer, aContext, locPath);
112 0 : if (NS_FAILED(rv)) {
113 0 : delete unionPattern;
114 0 : return rv;
115 : }
116 0 : rv = unionPattern->addPattern(locPath);
117 : #if 0 // XXX addPattern can't fail yet, it doesn't check for mem
118 : if (NS_FAILED(rv)) {
119 : delete unionPattern;
120 : delete locPath;
121 : return rv;
122 : }
123 : #endif
124 0 : type = aLexer.nextToken()->mType;
125 : } while (type == Token::UNION_OP);
126 :
127 0 : if (type != Token::END) {
128 0 : delete unionPattern;
129 0 : return NS_ERROR_XPATH_PARSE_FAILURE;
130 : }
131 :
132 0 : aPattern = unionPattern;
133 0 : return NS_OK;
134 : }
135 :
136 0 : nsresult txPatternParser::createLocPathPattern(txExprLexer& aLexer,
137 : txIParseContext* aContext,
138 : txPattern*& aPattern)
139 : {
140 0 : nsresult rv = NS_OK;
141 :
142 0 : bool isChild = true;
143 0 : bool isAbsolute = false;
144 0 : txPattern* stepPattern = 0;
145 0 : txLocPathPattern* pathPattern = 0;
146 :
147 0 : Token::Type type = aLexer.peek()->mType;
148 0 : switch (type) {
149 : case Token::ANCESTOR_OP:
150 0 : isChild = false;
151 0 : isAbsolute = true;
152 0 : aLexer.nextToken();
153 0 : break;
154 : case Token::PARENT_OP:
155 0 : aLexer.nextToken();
156 0 : isAbsolute = true;
157 0 : if (aLexer.peek()->mType == Token::END ||
158 0 : aLexer.peek()->mType == Token::UNION_OP) {
159 0 : aPattern = new txRootPattern();
160 :
161 0 : return aPattern ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
162 : }
163 0 : break;
164 : case Token::FUNCTION_NAME_AND_PAREN:
165 : // id(Literal) or key(Literal, Literal)
166 : {
167 : nsCOMPtr<nsIAtom> nameAtom =
168 0 : do_GetAtom(aLexer.nextToken()->Value());
169 0 : if (nameAtom == nsGkAtoms::id) {
170 0 : rv = createIdPattern(aLexer, stepPattern);
171 : }
172 0 : else if (nameAtom == nsGkAtoms::key) {
173 0 : rv = createKeyPattern(aLexer, aContext, stepPattern);
174 : }
175 0 : if (NS_FAILED(rv))
176 0 : return rv;
177 : }
178 0 : break;
179 : default:
180 0 : break;
181 : }
182 0 : if (!stepPattern) {
183 0 : rv = createStepPattern(aLexer, aContext, stepPattern);
184 0 : if (NS_FAILED(rv))
185 0 : return rv;
186 : }
187 :
188 0 : type = aLexer.peek()->mType;
189 0 : if (!isAbsolute && type != Token::PARENT_OP
190 : && type != Token::ANCESTOR_OP) {
191 0 : aPattern = stepPattern;
192 0 : return NS_OK;
193 : }
194 :
195 0 : pathPattern = new txLocPathPattern();
196 0 : if (!pathPattern) {
197 0 : delete stepPattern;
198 0 : return NS_ERROR_OUT_OF_MEMORY;
199 : }
200 :
201 0 : if (isAbsolute) {
202 0 : txRootPattern* root = new txRootPattern();
203 0 : if (!root) {
204 0 : delete stepPattern;
205 0 : delete pathPattern;
206 0 : return NS_ERROR_OUT_OF_MEMORY;
207 : }
208 :
209 : #ifdef TX_TO_STRING
210 0 : root->setSerialize(false);
211 : #endif
212 :
213 0 : rv = pathPattern->addStep(root, isChild);
214 0 : if (NS_FAILED(rv)) {
215 0 : delete stepPattern;
216 0 : delete pathPattern;
217 0 : delete root;
218 0 : return NS_ERROR_OUT_OF_MEMORY;
219 : }
220 : }
221 :
222 0 : rv = pathPattern->addStep(stepPattern, isChild);
223 0 : if (NS_FAILED(rv)) {
224 0 : delete stepPattern;
225 0 : delete pathPattern;
226 0 : return NS_ERROR_OUT_OF_MEMORY;
227 : }
228 0 : stepPattern = 0; // stepPattern is part of pathPattern now
229 :
230 0 : while (type == Token::PARENT_OP || type == Token::ANCESTOR_OP) {
231 0 : isChild = type == Token::PARENT_OP;
232 0 : aLexer.nextToken();
233 0 : rv = createStepPattern(aLexer, aContext, stepPattern);
234 0 : if (NS_FAILED(rv)) {
235 0 : delete pathPattern;
236 0 : return rv;
237 : }
238 0 : rv = pathPattern->addStep(stepPattern, isChild);
239 0 : if (NS_FAILED(rv)) {
240 0 : delete stepPattern;
241 0 : delete pathPattern;
242 0 : return NS_ERROR_OUT_OF_MEMORY;
243 : }
244 0 : stepPattern = 0; // stepPattern is part of pathPattern now
245 0 : type = aLexer.peek()->mType;
246 : }
247 0 : aPattern = pathPattern;
248 0 : return rv;
249 : }
250 :
251 0 : nsresult txPatternParser::createIdPattern(txExprLexer& aLexer,
252 : txPattern*& aPattern)
253 : {
254 : // check for '(' Literal ')'
255 0 : if (aLexer.peek()->mType != Token::LITERAL)
256 0 : return NS_ERROR_XPATH_PARSE_FAILURE;
257 : const nsDependentSubstring& value =
258 0 : aLexer.nextToken()->Value();
259 0 : if (aLexer.nextToken()->mType != Token::R_PAREN)
260 0 : return NS_ERROR_XPATH_PARSE_FAILURE;
261 0 : aPattern = new txIdPattern(value);
262 0 : return aPattern ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
263 : }
264 :
265 0 : nsresult txPatternParser::createKeyPattern(txExprLexer& aLexer,
266 : txIParseContext* aContext,
267 : txPattern*& aPattern)
268 : {
269 : // check for '(' Literal, Literal ')'
270 0 : if (aLexer.peek()->mType != Token::LITERAL)
271 0 : return NS_ERROR_XPATH_PARSE_FAILURE;
272 : const nsDependentSubstring& key =
273 0 : aLexer.nextToken()->Value();
274 0 : if (aLexer.nextToken()->mType != Token::COMMA &&
275 0 : aLexer.peek()->mType != Token::LITERAL)
276 0 : return NS_ERROR_XPATH_PARSE_FAILURE;
277 : const nsDependentSubstring& value =
278 0 : aLexer.nextToken()->Value();
279 0 : if (aLexer.nextToken()->mType != Token::R_PAREN)
280 0 : return NS_ERROR_XPATH_PARSE_FAILURE;
281 :
282 : const PRUnichar* colon;
283 0 : if (!XMLUtils::isValidQName(PromiseFlatString(key), &colon))
284 0 : return NS_ERROR_XPATH_PARSE_FAILURE;
285 0 : nsCOMPtr<nsIAtom> prefix, localName;
286 : PRInt32 namespaceID;
287 0 : nsresult rv = resolveQName(key, getter_AddRefs(prefix), aContext,
288 0 : getter_AddRefs(localName), namespaceID);
289 0 : if (NS_FAILED(rv))
290 0 : return rv;
291 :
292 0 : aPattern = new txKeyPattern(prefix, localName, namespaceID, value);
293 :
294 0 : return aPattern ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
295 : }
296 :
297 0 : nsresult txPatternParser::createStepPattern(txExprLexer& aLexer,
298 : txIParseContext* aContext,
299 : txPattern*& aPattern)
300 : {
301 0 : nsresult rv = NS_OK;
302 0 : bool isAttr = false;
303 0 : Token* tok = aLexer.peek();
304 0 : if (tok->mType == Token::AXIS_IDENTIFIER) {
305 0 : if (TX_StringEqualsAtom(tok->Value(), nsGkAtoms::attribute)) {
306 0 : isAttr = true;
307 : }
308 0 : else if (!TX_StringEqualsAtom(tok->Value(), nsGkAtoms::child)) {
309 : // all done already for CHILD_AXIS, for all others
310 : // XXX report unexpected axis error
311 0 : return NS_ERROR_XPATH_PARSE_FAILURE;
312 : }
313 0 : aLexer.nextToken();
314 : }
315 0 : else if (tok->mType == Token::AT_SIGN) {
316 0 : aLexer.nextToken();
317 0 : isAttr = true;
318 : }
319 0 : tok = aLexer.nextToken();
320 :
321 : txNodeTest* nodeTest;
322 0 : if (tok->mType == Token::CNAME) {
323 : // resolve QName
324 0 : nsCOMPtr<nsIAtom> prefix, lName;
325 : PRInt32 nspace;
326 0 : rv = resolveQName(tok->Value(), getter_AddRefs(prefix), aContext,
327 0 : getter_AddRefs(lName), nspace, true);
328 0 : if (NS_FAILED(rv)) {
329 : // XXX error report namespace resolve failed
330 0 : return rv;
331 : }
332 :
333 : PRUint16 nodeType = isAttr ?
334 : (PRUint16)txXPathNodeType::ATTRIBUTE_NODE :
335 0 : (PRUint16)txXPathNodeType::ELEMENT_NODE;
336 0 : nodeTest = new txNameTest(prefix, lName, nspace, nodeType);
337 0 : if (!nodeTest) {
338 0 : return NS_ERROR_OUT_OF_MEMORY;
339 : }
340 : }
341 : else {
342 0 : aLexer.pushBack();
343 0 : rv = createNodeTypeTest(aLexer, &nodeTest);
344 0 : NS_ENSURE_SUCCESS(rv, rv);
345 : }
346 :
347 0 : nsAutoPtr<txStepPattern> step(new txStepPattern(nodeTest, isAttr));
348 0 : if (!step) {
349 0 : delete nodeTest;
350 0 : return NS_ERROR_OUT_OF_MEMORY;
351 : }
352 :
353 0 : rv = parsePredicates(step, aLexer, aContext);
354 0 : NS_ENSURE_SUCCESS(rv, rv);
355 :
356 0 : aPattern = step.forget();
357 :
358 0 : return NS_OK;
359 : }
|