Qt 4.8
qgenericpredicate.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qabstractfloat_p.h"
43 #include "qboolean_p.h"
44 #include "qbuiltintypes_p.h"
45 #include "qcommonsequencetypes_p.h"
46 #include "qemptysequence_p.h"
47 #include "qfirstitempredicate_p.h"
48 #include "qgenericsequencetype_p.h"
49 #include "qitemmappingiterator_p.h"
50 #include "qliteral_p.h"
51 #include "qpatternistlocale_p.h"
52 #include "qtruthpredicate_p.h"
53 
54 #include "qgenericpredicate_p.h"
55 
57 
58 using namespace QPatternist;
59 
61  const Expression::Ptr &predicate) : PairContainer(sourceExpression,
62  predicate)
63 {
64 }
65 
67  const Expression::Ptr &predicateExpression,
68  const StaticContext::Ptr &context,
70 {
71  Q_ASSERT(sourceExpression);
72  Q_ASSERT(predicateExpression);
73  Q_ASSERT(context);
74  const ItemType::Ptr type(predicateExpression->staticType()->itemType());
75 
76  if(predicateExpression->is(IDIntegerValue) &&
77  predicateExpression->as<Literal>()->item().as<Numeric>()->toInteger() == 1)
78  { /* Handle [1] */
79  return createFirstItem(sourceExpression);
80  }
81  else if(BuiltinTypes::numeric->xdtTypeMatches(type))
82  { /* A numeric predicate, other than [1]. */
83  /* TODO at somepoint we'll return a specialized expr here, NumericPredicate or so.
84  * Dependency analysis is a bit tricky, since the contained expression can depend on
85  * some loop component. */
86  return Expression::Ptr(new GenericPredicate(sourceExpression, predicateExpression));
87  }
88  else if(*CommonSequenceTypes::Empty == *type)
89  {
90  return EmptySequence::create(predicateExpression.data(), context);
91  }
92  else if(*BuiltinTypes::item == *type ||
94  {
95  /* The type couldn't be narrowed at compile time, so we use
96  * a generic predicate. This check is before the CommonSequenceTypes::EBV check,
97  * because the latter matches these types as well. */
98  return Expression::Ptr(new GenericPredicate(sourceExpression, predicateExpression));
99  }
100  else if(CommonSequenceTypes::EBV->itemType()->xdtTypeMatches(type))
101  {
102  return Expression::Ptr(new TruthPredicate(sourceExpression, predicateExpression));
103  }
104  else
105  {
106  context->error(QtXmlPatterns::tr("A value of type %1 cannot be a "
107  "predicate. A predicate must have "
108  "either a numeric type or an "
109  "Effective Boolean Value type.")
110  .arg(formatType(context->namePool(),
111  sourceExpression->staticType())),
113  return Expression::Ptr(); /* Silence compiler warning. */
114  }
115 }
116 
118 
119 {
120  return Expression::Ptr(new FirstItemPredicate(sourceExpression));
121 }
122 
124  const DynamicContext::Ptr &context) const
125 {
127  const Item pcateItem(it->next());
128 
129  if(!pcateItem)
130  return Item(); /* The predicate evaluated to the empty sequence */
131  else if(pcateItem.isNode())
132  return item;
133  /* Ok, now it must be an AtomicValue */
134  else if(BuiltinTypes::numeric->xdtTypeMatches(pcateItem.type()))
135  { /* It's a positional predicate. */
136  if(it->next())
137  {
138  context->error(QtXmlPatterns::tr("A positional predicate must "
139  "evaluate to a single numeric "
140  "value."),
142  return Item();
143  }
144 
145  if(Double::isEqual(static_cast<xsDouble>(context->contextPosition()),
146  pcateItem.as<Numeric>()->toDouble()))
147  {
148  return item;
149  }
150  else
151  return Item();
152  }
153  else if(Boolean::evaluateEBV(pcateItem, it, context)) /* It's a truth predicate. */
154  return item;
155  else
156  return Item();
157 }
158 
160 {
161  const Item::Iterator::Ptr focus(m_operand1->evaluateSequence(context));
162  const DynamicContext::Ptr newContext(context->createFocus());
163  newContext->setFocusIterator(focus);
164 
165  return makeItemMappingIterator<Item>(ConstPtr(this),
166  focus,
167  newContext);
168 }
169 
171 {
172  const Item::Iterator::Ptr focus(m_operand1->evaluateSequence(context));
173  const DynamicContext::Ptr newContext(context->createFocus());
174  newContext->setFocusIterator(focus);
175  return mapToItem(focus->next(), newContext);
176 }
177 
179 {
180  SequenceType::List result;
183  return result;
184 }
185 
187 {
189  return makeGenericSequenceType(type->itemType(),
190  type->cardinality() | Cardinality::zeroOrOne());
191 }
192 
194 {
195  return visitor->visit(this);
196 }
197 
199 {
200  return m_operand1->staticType()->itemType();
201 }
202 
204 {
205  return CreatesFocusForLast;
206 }
207 
209 {
210  return QLatin1String("predicate");
211 }
212 
214 {
215  return IDGenericPredicate;
216 }
217 
virtual ItemType::Ptr newFocusType() const
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
GenericPredicate(const Expression::Ptr &sourceExpression, const Expression::Ptr &predicate)
virtual SequenceType::Ptr staticType() const
static const SequenceType::Ptr ZeroOrMoreItems
#define it(className, varName)
virtual QString description() const
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
virtual NamePool::Ptr namePool() const =0
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static bool evaluateEBV(const Item::Iterator::Ptr &e, const QExplicitlySharedDataPointer< DynamicContext > &)
Definition: qboolean.cpp:55
The QString class provides a Unicode character string.
Definition: qstring.h:83
static Expression::Ptr create(const Expression *const replacementFor, const StaticContext::Ptr &context)
Creates an EmptySequence that is a replacement for replacementFor.
virtual xsInteger contextPosition() const =0
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
DynamicContext::Ptr createFocus()
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
const TCastTarget * as() const
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
static const ItemType::Ptr item
The namespace for the internal API of QtXmlPatterns.
virtual void setFocusIterator(const Item::Iterator::Ptr &it)=0
The QSourceLocation class identifies a location in a resource by URI, line, and column.
virtual SequenceType::List expectedOperandTypes() const
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
static Expression::Ptr create(const Expression::Ptr &sourceExpression, const Expression::Ptr &predicateExpression, const StaticContext::Ptr &context, const QSourceLocation &location)
Houses an AtomicValue, making it available as an Expression.
Definition: qliteral_p.h:74
QExplicitlySharedDataPointer< Expression > Ptr
A smart pointer wrapping mutable Expression instances.
virtual SequenceType::Ptr staticType() const =0
static const EmptySequenceType::Ptr Empty
static Cardinality zeroOrOne()
Contains functions used for formatting arguments, such as keywords and paths, in translated strings...
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
static bool isEqual(const xsDouble a, const xsDouble b)
Base class for all numeric values.
static QTestResult::TestLocation location
Definition: qtestresult.cpp:63
virtual xsDouble toDouble() const =0
static const AtomicType::Ptr numeric
virtual ExpressionVisitorResult::Ptr accept(const ExpressionVisitor::Ptr &visitor) const
static const SequenceType::Ptr EBV
A predicate which is optimized for filter expressions that are of type xs:boolean.
QExplicitlySharedDataPointer< const GenericPredicate > ConstPtr
static const AtomicType::Ptr xsAnyAtomicType
The QFlags class provides a type-safe way of storing OR-combinations of enum values.
Definition: qglobal.h:2313
virtual ItemType::Ptr itemType() const =0
Item mapToItem(const Item &subject, const DynamicContext::Ptr &) const
virtual Properties properties() const
bool is(const ID id) const
A predicate that always selects the first item from its sequence.
static Expression::Ptr createFirstItem(const Expression::Ptr &sourceExpression)
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
Base class for expressions that has exactly two operands.