KHtml

expression.h
1 /*
2  * expression.h - Copyright 2005 Frerich Raabe <[email protected]>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef EXPRESSION_H
26 #define EXPRESSION_H
27 
28 #include "util.h"
29 
30 #include <QHash>
31 #include <QList>
32 
33 #include <dom/dom_string.h>
34 #include <xml/dom_stringimpl.h>
35 #include <xml/dom_nodelistimpl.h>
36 #include <misc/shared.h>
37 
38 namespace DOM
39 {
40 class NodeImpl;
41 }
42 
43 namespace khtml
44 {
45 
46 class XPathNSResolverImpl;
47 namespace XPath
48 {
49 
50 struct EvaluationContext {
51  EvaluationContext()
52  {
53  reset(nullptr, nullptr);
54  }
55 
56  void reset(DOM::NodeImpl *ctx, XPathNSResolverImpl *res)
57  {
58  node = ctx;
59  resolver = res;
60  size = 1;
61  position = 1;
62  exceptionCode = 0;
63  }
64 
65  DOM::NodeImpl *node;
66  unsigned long size;
67  unsigned long position;
69 
70  // reports only first one.
71  void reportException(int ec)
72  {
73  if (!exceptionCode) {
74  exceptionCode = ec;
75  }
76  }
77 
78  int exceptionCode;
79 
80  /* The function library is globally accessible through
81  * FunctionLibrary::self()
82  */
83  XPathNSResolverImpl *resolver;
84 };
85 
86 class Value
87 {
88 public:
89  enum Type {
90  Nodeset, Boolean, Number, String
91  };
92 
93  Value();
94  explicit Value(DOM::NodeImpl *value);
95  explicit Value(const DomNodeList &value);
96  explicit Value(bool value);
97  explicit Value(double value);
98  explicit Value(const DOM::DOMString &value);
99 
100  Type type() const;
101  bool isNodeset() const;
102  bool isBoolean() const;
103  bool isNumber() const;
104  bool isString() const;
105 
106  DomNodeList &toNodeset(); // may return 0
107  const DomNodeList &toNodeset() const;
108  bool toBoolean() const;
109  double toNumber() const;
110  DOM::DOMString toString() const;
111 
112  QString dump() const;
113 
114 private:
115  // Catch undesired conversions -- this manages to go to bool otherwise!
116  explicit Value(const char *);
117 
118  Type m_type;
119  DomNodeList m_nodeset;
120  bool m_bool;
121  double m_number;
122  DOM::DOMString m_string;
123 };
124 
125 class Expression
126 {
127 public:
128  static EvaluationContext &evaluationContext();
129 
130  Expression();
131  virtual ~Expression();
132  virtual Value evaluate() const;
133 
134  void addSubExpression(Expression *expr);
135  void optimize();
136  virtual bool isConstant() const;
137 
138  virtual QString dump() const = 0;
139 
140  static void reportInvalidExpressionErr();
141  static void reportNamespaceErr();
142 protected:
143  unsigned int subExprCount() const;
144  Expression *subExpr(unsigned int i);
145  const Expression *subExpr(unsigned int i) const;
146 
147 private:
148  virtual Value doEvaluate() const = 0;
149 
150  QList<Expression *> m_subExpressions;
151  Value *m_constantValue;
152 };
153 
154 } // namespace XPath
155 
156 } // namespace khtml
157 
158 #endif // EXPRESSION_H
159 
This file is part of the HTML rendering engine for KDE.
Type type(const QSqlDatabase &db)
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
This library provides a full-featured HTML parser and widget.
char * toString(const T &value)
KGuiItem reset()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.