• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • sources
  • kde-4.12
  • kdelibs
  • khtml
  • xpath
path.cpp
Go to the documentation of this file.
1 /*
2  * path.cc - Copyright 2005 Frerich Raabe <raabe@kde.org>
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 #include "path.h"
26 
27 #include "xml/dom_docimpl.h"
28 #include "xml/dom_nodeimpl.h"
29 
30 using namespace DOM;
31 using namespace khtml;
32 using namespace khtml::XPath;
33 
34 Filter::Filter( Expression *expr, const QList<Predicate *> &predicates )
35  : m_expr( expr ),
36  m_predicates( predicates )
37 {
38 }
39 
40 Filter::~Filter()
41 {
42  delete m_expr;
43  qDeleteAll( m_predicates );
44 }
45 
46 QString Filter::dump() const
47 {
48  QString s = "<filter>";
49  s += m_expr->dump();
50  foreach( Predicate *predicate, m_predicates ) {
51  s += predicate->dump();
52  }
53  s += "</filter>";
54  return s;
55 }
56 
57 Value Filter::doEvaluate() const
58 {
59  Value v = m_expr->evaluate();
60  if ( !v.isNodeset() ) {
61  if ( !m_predicates.empty() ) {
62  kDebug(6011) << "Ignoring predicates for filter since expression does not evaluate to a nodeset!";
63  }
64  return v;
65  }
66 
67  DomNodeList inNodes = v.toNodeset(), outNodes;
68 
69  // Filter seems to work in document order, not axis order
70  inNodes->normalizeUpto(StaticNodeListImpl::DocumentOrder);
71 
72  foreach( Predicate *predicate, m_predicates ) {
73  outNodes = new StaticNodeListImpl();
74  Expression::evaluationContext().size = int(inNodes->length());
75 
76  for ( unsigned long n = 0; n < inNodes->length(); ++n ) {
77  NodeImpl *node = inNodes->item(n);
78  Expression::evaluationContext().node = node;
79  Expression::evaluationContext().position = n + 1;
80 
81  if ( predicate->evaluate() ) {
82  outNodes->append( node );
83  }
84  }
85 
86  inNodes = outNodes;
87  outNodes->setKnownNormalization(StaticNodeListImpl::DocumentOrder);
88 
89 #ifdef XPATH_VERBOSE
90  kDebug(6011) << "Predicate within filter trims to:" << outNodes->length();
91 #endif
92  }
93 
94  return Value( outNodes );
95 }
96 
97 LocationPath::LocationPath()
98  : m_absolute( false )
99 {
100 }
101 
102 LocationPath::~LocationPath()
103 {
104  qDeleteAll( m_steps );
105 }
106 
107 void LocationPath::optimize()
108 {
109  foreach( Step *step, m_steps ) {
110  step->optimize();
111  }
112 }
113 
114 Value LocationPath::doEvaluate() const
115 {
116 #ifdef XPATH_VERBOSE
117  if ( m_absolute ) {
118  kDebug(6011) << "Evaluating absolute path expression, steps:" << m_steps.count();
119  } else {
120  kDebug(6011) << "Evaluating relative path expression, steps:" << m_steps.count();
121  }
122 #endif
123 
124  DomNodeList inDomNodes = new StaticNodeListImpl,
125  outDomNodes;
126 
127  /* For absolute location paths, the context node is ignored - the
128  * document's root node is used instead.
129  */
130  NodeImpl *context = Expression::evaluationContext().node;
131  if ( m_absolute ) {
132  if ( context->nodeType() != Node::DOCUMENT_NODE ) {
133  context = context->ownerDocument();
134  }
135  }
136 
137  inDomNodes->append( context );
138 
139  if ( m_steps.isEmpty() )
140  return Value( inDomNodes );
141 
142  int s = 0;
143  foreach( Step *step, m_steps ) {
144 #ifdef XPATH_VERBOSE
145  kDebug(6011) << "-------------------------------------";
146  kDebug(6011) << "Step " << s << "insize " << inDomNodes->length();
147 #endif
148 
149  outDomNodes = new StaticNodeListImpl;
150  for ( unsigned long i = 0; i < inDomNodes->length(); ++i ) {
151  DomNodeList matches = step->evaluate( inDomNodes->item( i ) );
152  for ( unsigned long j = 0; j < matches->length(); ++j )
153  outDomNodes->append( matches->item( j ) );
154  }
155  inDomNodes = outDomNodes;
156 
157  ++s;
158  }
159 
160 #ifdef XPATH_VERBOSE
161  kDebug(6011) << "-------------------------------------";
162  kDebug(6011) << "output:" <<outDomNodes->length();
163  kDebug(6011) << "=====================================";
164 #endif
165 
166  return Value( outDomNodes );
167 }
168 
169 QString LocationPath::dump() const
170 {
171  QString s = "<locationpath absolute=\"";
172  s += m_absolute ? "true" : "false";
173  s += "\">";
174  foreach( Step *step, m_steps ) {
175  s += step->dump();
176  }
177  s += "</locationpath>";
178  return s;
179 }
180 
181 Path::Path( Filter *filter, LocationPath *path )
182  : m_filter( filter ),
183  m_path( path )
184 {
185 }
186 
187 Path::~Path()
188 {
189  delete m_filter;
190  delete m_path;
191 }
192 
193 QString Path::dump() const
194 {
195  if ( !m_filter && !m_path ) {
196  return "<path/>";
197  }
198 
199  QString s = "<path>";
200  if ( m_filter ) {
201  s += m_filter->dump();
202  }
203  if ( m_path ) {
204  s += m_path->dump();
205  }
206  s += "</path>";
207 
208  return s;
209 }
210 
211 Value Path::doEvaluate() const
212 {
213  NodeImpl* saveCtx = Expression::evaluationContext().node;
214 
215  Value initial = m_filter->evaluate();
216  if ( initial.isNodeset() ) {
217  // Pass in every output from the filter to the path, and union the results
218  DomNodeList out = new StaticNodeListImpl();
219  DomNodeList in = initial.toNodeset();
220 
221  for (unsigned long i = 0; i < in->length(); ++i) {
222  Expression::evaluationContext().node = in->item(i);
223 
224  DomNodeList singleSet = m_path->evaluate().toNodeset();
225  for (unsigned long j = 0; j < singleSet->length(); ++ j)
226  out->append(singleSet->item(j));
227  }
228 
229  Expression::evaluationContext().node = saveCtx;
230  return Value(out);
231  } else {
232  // ### what should happen in this case?
233  Expression::reportInvalidExpressionErr();
234  return Value();
235  }
236 }
237 
238 // kate: indent-width 4; replace-tabs off; tab-width 4; space-indent off;
khtml::XPath::Filter::~Filter
virtual ~Filter()
Definition: path.cpp:40
khtml::XPath::Path::~Path
virtual ~Path()
khtml::XPath::Value::isNodeset
bool isNodeset() const
Definition: expression.cpp:86
khtml::XPath::Step::dump
QString dump() const
Definition: step.cpp:476
khtml::XPath::Filter::dump
virtual QString dump() const
Definition: path.cpp:46
khtml::XPath::LocationPath::optimize
void optimize()
Definition: path.cpp:107
khtml::XPath::LocationPath
Definition: path.h:54
QString
khtml::XPath::DomNodeList
SharedPtr< DOM::StaticNodeListImpl > DomNodeList
Definition: util.h:41
khtml::XPath::Predicate::dump
QString dump() const
Definition: predicate.cpp:435
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
khtml::XPath::Filter
Definition: path.h:38
khtml::XPath::Value
Definition: expression.h:75
khtml::XPath::Step::optimize
void optimize()
Definition: step.cpp:469
khtml::XPath::Expression::dump
virtual QString dump() const =0
khtml::XPath::Predicate
Definition: predicate.h:148
khtml::XPath::Path::dump
virtual QString dump() const
Definition: path.cpp:193
khtml::XPath::LocationPath::dump
virtual QString dump() const
Definition: path.cpp:169
khtml::XPath::Expression::evaluationContext
static EvaluationContext & evaluationContext()
Definition: expression.cpp:212
khtml::XPath::EvaluationContext::node
DOM::NodeImpl * node
Definition: expression.h:59
khtml::XPath::Step::evaluate
DomNodeList evaluate(DOM::NodeImpl *context) const
Definition: step.cpp:129
khtml::XPath::EvaluationContext::position
unsigned long position
Definition: expression.h:61
khtml::XPath::Predicate::evaluate
bool evaluate() const
Definition: predicate.cpp:416
khtml::XPath::Expression::reportInvalidExpressionErr
static void reportInvalidExpressionErr()
Definition: expression.cpp:288
khtml::XPath::Path::Path
Path(Filter *filter, LocationPath *path)
Definition: path.cpp:181
khtml::XPath::Step
Definition: step.h:42
khtml::XPath::EvaluationContext::size
unsigned long size
Definition: expression.h:60
path.h
khtml::XPath::Expression::evaluate
virtual Value evaluate() const
Definition: expression.cpp:229
khtml::XPath::LocationPath::LocationPath
LocationPath()
Definition: path.cpp:97
khtml::XPath::LocationPath::~LocationPath
virtual ~LocationPath()
Definition: path.cpp:102
khtml::XPath::Expression
Definition: expression.h:114
khtml::XPath::Value::toNodeset
DomNodeList & toNodeset()
Definition: expression.cpp:106
QList
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:51:22 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal