KHTML
expression.h
Go to the documentation of this file.
00001 /* 00002 * expression.h - Copyright 2005 Frerich Raabe <raabe@kde.org> 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00015 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00016 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00017 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00018 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00019 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00020 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00021 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00022 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00023 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00024 */ 00025 #ifndef EXPRESSION_H 00026 #define EXPRESSION_H 00027 00028 #include "util.h" 00029 00030 #include <QHash> 00031 #include <QList> 00032 00033 #include <dom/dom_string.h> 00034 #include <xml/dom_stringimpl.h> 00035 #include <xml/dom_nodelistimpl.h> 00036 #include <misc/shared.h> 00037 00038 namespace DOM { 00039 class NodeImpl; 00040 } 00041 00042 namespace khtml { 00043 00044 class XPathNSResolverImpl; 00045 namespace XPath { 00046 00047 struct EvaluationContext 00048 { 00049 EvaluationContext() { reset(0, 0); } 00050 00051 void reset(DOM::NodeImpl* ctx, XPathNSResolverImpl* res) { 00052 node = ctx; 00053 resolver = res; 00054 size = 1; 00055 position = 1; 00056 exceptionCode = 0; 00057 } 00058 00059 DOM::NodeImpl *node; 00060 unsigned long size; 00061 unsigned long position; 00062 QHash<DOM::DOMString, DOM::DOMString> variableBindings; 00063 00064 // reports only first one. 00065 void reportException(int ec) { if (!exceptionCode) exceptionCode = ec; } 00066 00067 int exceptionCode; 00068 00069 /* The function library is globally accessible through 00070 * FunctionLibrary::self() 00071 */ 00072 XPathNSResolverImpl *resolver; 00073 }; 00074 00075 class Value 00076 { 00077 public: 00078 enum Type { 00079 Nodeset, Boolean, Number, String 00080 }; 00081 00082 Value(); 00083 explicit Value( DOM::NodeImpl *value ); 00084 explicit Value( const DomNodeList &value ); 00085 explicit Value( bool value ); 00086 explicit Value( double value ); 00087 explicit Value( const DOM::DOMString &value ); 00088 00089 Type type() const; 00090 bool isNodeset() const; 00091 bool isBoolean() const; 00092 bool isNumber() const; 00093 bool isString() const; 00094 00095 DomNodeList &toNodeset(); // may return 0 00096 const DomNodeList &toNodeset() const; 00097 bool toBoolean() const; 00098 double toNumber() const; 00099 DOM::DOMString toString() const; 00100 00101 QString dump() const; 00102 00103 private: 00104 // Catch undesired conversions -- this manages to go to bool otherwise! 00105 explicit Value( const char* ); 00106 00107 Type m_type; 00108 DomNodeList m_nodeset; 00109 bool m_bool; 00110 double m_number; 00111 DOM::DOMString m_string; 00112 }; 00113 00114 class Expression 00115 { 00116 public: 00117 static EvaluationContext &evaluationContext(); 00118 00119 Expression(); 00120 virtual ~Expression(); 00121 virtual Value evaluate() const; 00122 00123 void addSubExpression( Expression *expr ); 00124 void optimize(); 00125 virtual bool isConstant() const; 00126 00127 virtual QString dump() const = 0; 00128 00129 static void reportInvalidExpressionErr(); 00130 static void reportNamespaceErr(); 00131 protected: 00132 unsigned int subExprCount() const; 00133 Expression *subExpr( unsigned int i ); 00134 const Expression *subExpr( unsigned int i ) const; 00135 00136 private: 00137 virtual Value doEvaluate() const = 0; 00138 00139 QList<Expression *> m_subExpressions; 00140 Value *m_constantValue; 00141 }; 00142 00143 } // namespace XPath 00144 00145 } // namespace khtml 00146 00147 #endif // EXPRESSION_H 00148 00149 // kate: indent-width 4; replace-tabs off; tab-width 4; space-indent off;
KDE 4.6 API Reference