File indexing completed on 2024-04-28 11:39:39

0001 /*
0002  * expression.h - 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 #ifndef EXPRESSION_H
0026 #define EXPRESSION_H
0027 
0028 #include "util.h"
0029 
0030 #include <QHash>
0031 #include <QList>
0032 
0033 #include <dom/dom_string.h>
0034 #include <xml/dom_stringimpl.h>
0035 #include <xml/dom_nodelistimpl.h>
0036 #include <misc/shared.h>
0037 
0038 namespace DOM
0039 {
0040 class NodeImpl;
0041 }
0042 
0043 namespace khtml
0044 {
0045 
0046 class XPathNSResolverImpl;
0047 namespace XPath
0048 {
0049 
0050 struct EvaluationContext {
0051     EvaluationContext()
0052     {
0053         reset(nullptr, nullptr);
0054     }
0055 
0056     void reset(DOM::NodeImpl *ctx, XPathNSResolverImpl *res)
0057     {
0058         node     = ctx;
0059         resolver = res;
0060         size     = 1;
0061         position = 1;
0062         exceptionCode = 0;
0063     }
0064 
0065     DOM::NodeImpl *node;
0066     unsigned long size;
0067     unsigned long position;
0068     QHash<DOM::DOMString, DOM::DOMString> variableBindings;
0069 
0070     // reports only first one.
0071     void reportException(int ec)
0072     {
0073         if (!exceptionCode) {
0074             exceptionCode = ec;
0075         }
0076     }
0077 
0078     int exceptionCode;
0079 
0080     /* The function library is globally accessible through
0081      * FunctionLibrary::self()
0082      */
0083     XPathNSResolverImpl *resolver;
0084 };
0085 
0086 class Value
0087 {
0088 public:
0089     enum Type {
0090         Nodeset, Boolean, Number, String
0091     };
0092 
0093     Value();
0094     explicit Value(DOM::NodeImpl *value);
0095     explicit Value(const DomNodeList &value);
0096     explicit Value(bool value);
0097     explicit Value(double value);
0098     explicit Value(const DOM::DOMString &value);
0099 
0100     Type type() const;
0101     bool isNodeset() const;
0102     bool isBoolean() const;
0103     bool isNumber() const;
0104     bool isString() const;
0105 
0106     DomNodeList &toNodeset(); // may return 0
0107     const DomNodeList &toNodeset() const;
0108     bool toBoolean() const;
0109     double toNumber() const;
0110     DOM::DOMString toString() const;
0111 
0112     QString dump() const;
0113 
0114 private:
0115     // Catch undesired conversions -- this manages to go to bool otherwise!
0116     explicit Value(const char *);
0117 
0118     Type m_type;
0119     DomNodeList m_nodeset;
0120     bool m_bool;
0121     double m_number;
0122     DOM::DOMString m_string;
0123 };
0124 
0125 class Expression
0126 {
0127 public:
0128     static EvaluationContext &evaluationContext();
0129 
0130     Expression();
0131     virtual ~Expression();
0132     virtual Value evaluate() const;
0133 
0134     void addSubExpression(Expression *expr);
0135     void optimize();
0136     virtual bool isConstant() const;
0137 
0138     virtual QString dump() const = 0;
0139 
0140     static void reportInvalidExpressionErr();
0141     static void reportNamespaceErr();
0142 protected:
0143     unsigned int subExprCount() const;
0144     Expression *subExpr(unsigned int i);
0145     const Expression *subExpr(unsigned int i) const;
0146 
0147 private:
0148     virtual Value doEvaluate() const = 0;
0149 
0150     QList<Expression *> m_subExpressions;
0151     Value *m_constantValue;
0152 };
0153 
0154 } // namespace XPath
0155 
0156 } // namespace khtml
0157 
0158 #endif // EXPRESSION_H
0159