File indexing completed on 2024-04-21 11:31:22

0001 /*
0002  * parsedstatement.cc - Copyright 2005 Frerich Raabe <raabe@kde.org>
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  *
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  *
0014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0016  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0017  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0018  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0019  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0023  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 #include "parsedstatement.h"
0026 #include "path.h"
0027 
0028 #include "xml/dom_nodeimpl.h"
0029 #include "xml/dom_nodelistimpl.h"
0030 #include "xml/dom3_xpathimpl.h"
0031 
0032 using namespace DOM;
0033 
0034 namespace khtml
0035 {
0036 namespace XPath
0037 {
0038 
0039 Expression *khtmlParseXPathStatement(const DOMString &statement, int &ec);
0040 
0041 ParsedStatement::ParsedStatement(const DOMString &statement, khtml::XPathNSResolverImpl *res)
0042     : m_res(res), m_expr(nullptr), m_ec(0)
0043 {
0044     parse(statement);
0045 }
0046 
0047 ParsedStatement::~ParsedStatement()
0048 {
0049     delete m_expr;
0050 }
0051 
0052 void ParsedStatement::parse(const DOMString &statement)
0053 {
0054     // qCDebug(KHTML_LOG) << "parsing:" << statement.string();
0055     m_ec = 0;
0056     delete m_expr;
0057     Expression::evaluationContext().reset(nullptr, m_res.get());
0058 
0059     m_expr = khtmlParseXPathStatement(statement, m_ec);
0060 
0061     // qCDebug(KHTML_LOG) << "AST:" << (m_expr ? m_expr->dump() : QString::fromLatin1("*** parse error ***"));
0062 }
0063 
0064 void ParsedStatement::optimize()
0065 {
0066     if (!m_expr) {
0067         return;
0068     }
0069     m_expr->optimize();
0070 }
0071 
0072 Value ParsedStatement::evaluate(NodeImpl *context, int &ec) const
0073 {
0074     Expression::evaluationContext().reset(context, m_res.get());
0075     Value res = m_expr->evaluate();
0076     ec = Expression::evaluationContext().exceptionCode;
0077 
0078     // If the result is a nodeset, we need to put it in document order
0079     // and remove duplicates.
0080     if (res.isNodeset()) {
0081         res.toNodeset()->normalizeUpto(StaticNodeListImpl::DocumentOrder);
0082     }
0083     return res;
0084 }
0085 
0086 QString ParsedStatement::dump() const
0087 {
0088     return m_expr->dump();
0089 }
0090 
0091 } // namespace XPath
0092 } // namespace khtml
0093