2 * Copyright (C) 2005 Frerich Raabe <raabe@kde.org>
3 * Copyright (C) 2006, 2009 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "XPathExpression.h"
33 #include "ExceptionCode.h"
34 #include "PlatformString.h"
35 #include "XPathException.h"
36 #include "XPathExpressionNode.h"
37 #include "XPathNSResolver.h"
38 #include "XPathParser.h"
39 #include "XPathResult.h"
40 #include "XPathUtil.h"
44 using namespace XPath;
46 PassRefPtr<XPathExpression> XPathExpression::createExpression(const String& expression, XPathNSResolver* resolver, ExceptionCode& ec)
48 RefPtr<XPathExpression> expr = XPathExpression::create();
51 expr->m_topExpression = parser.parseStatement(expression, resolver, ec);
52 if (!expr->m_topExpression)
55 return expr.release();
58 XPathExpression::~XPathExpression()
60 delete m_topExpression;
63 PassRefPtr<XPathResult> XPathExpression::evaluate(Node* contextNode, unsigned short type, XPathResult*, ExceptionCode& ec)
65 if (!isValidContextNode(contextNode)) {
66 ec = NOT_SUPPORTED_ERR;
70 EvaluationContext& evaluationContext = Expression::evaluationContext();
71 evaluationContext.node = contextNode;
72 evaluationContext.size = 1;
73 evaluationContext.position = 1;
74 evaluationContext.hadTypeConversionError = false;
75 RefPtr<XPathResult> result = XPathResult::create(contextNode->document(), m_topExpression->evaluate());
76 evaluationContext.node = 0; // Do not hold a reference to the context node, as this may prevent the whole document from being destroyed in time.
78 if (evaluationContext.hadTypeConversionError) {
79 // It is not specified what to do if type conversion fails while evaluating an expression, and INVALID_EXPRESSION_ERR is not exactly right
80 // when the failure happens in an otherwise valid expression because of a variable. But XPathEvaluator does not support variables, so it's close enough.
81 ec = XPathException::INVALID_EXPRESSION_ERR;
85 if (type != XPathResult::ANY_TYPE) {
87 result->convertTo(type, ec);
97 #endif // ENABLE(XPATH)