Qt 4.8
qvariableloader.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 <QVariant>
43 #include <QStringList>
44 
45 #include "qanyuri_p.h"
46 #include "qatomicstring_p.h"
47 #include "qbuiltintypes_p.h"
48 #include "qcommonsequencetypes_p.h"
49 #include "qgenericsequencetype_p.h"
50 #include "qinteger_p.h"
51 #include "qitem_p.h"
52 #include "qsequencetype_p.h"
53 #include "qvariableloader_p.h"
54 #include "qxmlquery_p.h"
55 
57 
58 namespace QPatternist
59 {
60 
61  class VariantListIterator : public ListIteratorPlatform<QVariant, Item, VariantListIterator>
62  {
63  public:
65  {
66  }
67 
68  private:
70 
71  inline Item inputToOutputItem(const QVariant &inputType) const
72  {
73  return AtomicValue::toXDM(inputType);
74  }
75  };
76 
77  class StringListIterator : public ListIteratorPlatform<QString, Item, StringListIterator>
78  {
79  public:
81  {
82  }
83 
84  private:
86 
87  static inline Item inputToOutputItem(const QString &inputType)
88  {
89  return AtomicString::fromValue(inputType);
90  }
91  };
92 
100  {
101  public:
103  const DynamicContext::Ptr &modelStorage) : DelegatingDynamicContext(other)
104  , m_modelStorage(modelStorage)
105  {
106  Q_ASSERT(m_modelStorage);
107  }
108 
109  virtual void addNodeModel(const QAbstractXmlNodeModel::Ptr &nodeModel)
110  {
111  m_modelStorage->addNodeModel(nodeModel);
112  }
113 
114  private:
116  };
117 }
118 
119 using namespace QPatternist;
120 
122  const SequenceType::Ptr &declaredType)
123 {
124  Q_UNUSED(declaredType);
125  const QVariant &variant = m_bindingHash.value(name);
126 
127 
128  if(variant.isNull())
129  return SequenceType::Ptr();
130  else if(variant.userType() == qMetaTypeId<QIODevice *>())
132  else if(variant.userType() == qMetaTypeId<QXmlQuery>())
133  {
134  const QXmlQuery variableQuery(qvariant_cast<QXmlQuery>(variant));
135  return variableQuery.d->expression()->staticType();
136  }
137  else
138  {
139  return makeGenericSequenceType(AtomicValue::qtToXDMType(qvariant_cast<QXmlItem>(variant)),
141  }
142 }
143 
145  const DynamicContext::Ptr &context)
146 {
147 
148  const QVariant &variant = m_bindingHash.value(name);
149  Q_ASSERT_X(!variant.isNull(), Q_FUNC_INFO,
150  "We assume that we have a binding.");
151 
152  /* Same code as in the default clause below. */
153  if(variant.userType() == qMetaTypeId<QIODevice *>())
154  return makeSingletonIterator(itemForName(name));
155  else if(variant.userType() == qMetaTypeId<QXmlQuery>())
156  {
157  const QXmlQuery variableQuery(qvariant_cast<QXmlQuery>(variant));
158 
159  return variableQuery.d->expression()->evaluateSequence(DynamicContext::Ptr(new TemporaryTreesRedirectingContext(variableQuery.d->dynamicContext(), context)));
160  }
161 
162  const QVariant v(qvariant_cast<QXmlItem>(variant).toAtomicValue());
163 
164  switch(v.type())
165  {
168  case QVariant::List:
170  default:
171  return makeSingletonIterator(itemForName(name));
172  }
173 }
174 
176 {
177  const QVariant &variant = m_bindingHash.value(name);
178 
179  if(variant.userType() == qMetaTypeId<QIODevice *>())
180  return Item(AnyURI::fromValue(QLatin1String("tag:trolltech.com,2007:QtXmlPatterns:QIODeviceVariable:") + m_namePool->stringForLocalName(name.localName())));
181 
182  const QXmlItem item(qvariant_cast<QXmlItem>(variant));
183 
184  if(item.isNode())
185  return Item::fromPublic(item);
186  else
187  {
188  const QVariant atomicValue(item.toAtomicValue());
189  /* If the atomicValue is null it means it doesn't exist in m_bindingHash, and therefore it must
190  * be a QIODevice, since Patternist guarantees to only ask for variables that announceExternalVariable()
191  * has accepted. */
192  if(atomicValue.isNull())
193  return Item(AnyURI::fromValue(QLatin1String("tag:trolltech.com,2007:QtXmlPatterns:QIODeviceVariable:") + m_namePool->stringForLocalName(name.localName())));
194  else
195  return AtomicValue::toXDM(atomicValue);
196  }
197 }
198 
200  const DynamicContext::Ptr &)
201 {
202  return itemForName(name);
203 }
204 
206  const QVariant &v2) const
207 {
208  /* Are both of type QIODevice *? */
209  if(v1.userType() == qMetaTypeId<QIODevice *>() && v1.userType() == v2.userType())
210  return true;
211 
212  /* Ok, we have two QXmlItems. */
213  const QXmlItem i1(qvariant_cast<QXmlItem>(v1));
214  const QXmlItem i2(qvariant_cast<QXmlItem>(v2));
215 
216  if(i1.isNode())
217  {
218  Q_ASSERT(false);
219  return false;
220  }
221  else if(i2.isAtomicValue())
222  return i1.toAtomicValue().type() == i2.toAtomicValue().type();
223  else
224  {
225  /* One is an atomic, the other is a node or they are null. */
226  return false;
227  }
228 }
229 
231 {
232  m_bindingHash.remove(name);
233 }
234 
236 {
237  return m_bindingHash.contains(name)
238  || (m_previousLoader && m_previousLoader->hasBinding(name));
239 }
240 
242 {
243  if(m_bindingHash.contains(name))
244  return m_bindingHash.value(name);
245  else if(m_previousLoader)
246  return m_previousLoader->valueFor(name);
247  else
248  return QVariant();
249 }
250 
252  const QVariant &value)
253 {
254  m_bindingHash.insert(name, value);
255 }
256 
258  const QVariant &variant) const
259 {
260  return hasBinding(name) && !isSameType(valueFor(name), variant);
261 }
262 
264 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
VariantListIterator(const QVariantList &list)
static Item inputToOutputItem(const QString &inputType)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
TemporaryTreesRedirectingContext(const DynamicContext::Ptr &other, const DynamicContext::Ptr &modelStorage)
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
Due to strong interdependencies, this file contains the definitions for the classes Item...
The QXmlItem class contains either an XML node or an atomic value.
QList< QVariant > toList() const
Returns the variant as a QVariantList if the variant has type() List or StringList ; otherwise return...
Definition: qvariant.cpp:2751
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static AtomicString::Ptr fromValue(const QString &value)
QExplicitlySharedDataPointer< QAbstractXmlForwardIterator< Item > > Ptr
A smart pointer wrapping an instance of a QAbstractXmlForwardIterator subclass.
virtual QPatternist::Item::Iterator::Ptr evaluateSequence(const QXmlName name, const QPatternist::DynamicContext::Ptr &)
virtual QPatternist::Item evaluateSingleton(const QXmlName name, const QPatternist::DynamicContext::Ptr &)
QStringList toStringList() const
Returns the variant as a QStringList if the variant has type() StringList, String ...
Definition: qvariant.cpp:2259
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isNode() const
Returns true if this item is a Node.
static const SequenceType::Ptr ExactlyOneAnyURI
const char * name
void removeBinding(const QXmlName &name)
Base class for dynamic contexts that are created from an existing one.
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The namespace for the internal API of QtXmlPatterns.
QVariant valueFor(const QXmlName &name) const
bool invalidationRequired(const QXmlName &name, const QVariant &variant) const
void addBinding(const QXmlName &name, const QVariant &value)
QVariant toAtomicValue() const
If this QXmlItem represents an atomic value, it is converted to an appropriate QVariant and returned...
bool hasBinding(const QXmlName &name) const
Helper class for ListIterator, and should only be instantiated through sub-classing.
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
QString localName(const QXmlNamePool &query) const
Returns the local name.
Definition: qxmlname.cpp:387
virtual SequenceType::Ptr staticType() const =0
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
The QXmlName class represents the name of an XML node, in an efficient, namespace-aware way...
Definition: qxmlname.h:58
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
virtual void addNodeModel(const QAbstractXmlNodeModel::Ptr &nodeModel)
Item inputToOutputItem(const QVariant &inputType) const
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
bool isAtomicValue() const
Returns true if this item is an atomic value.
StringListIterator(const QStringList &list)
virtual QPatternist::SequenceType::Ptr announceExternalVariable(const QXmlName name, const QPatternist::SequenceType::Ptr &declaredType)
QPatternist::Item itemForName(const QXmlName &name) const
static Item fromPublic(const QXmlItem &i)
Definition: qitem_p.h:408
static Cardinality exactlyOne()
QPatternist::Expression::Ptr expression(QIODevice *const queryDevice=0)
Definition: qxmlquery_p.h:235
const char * variant
bool isSameType(const QVariant &v1, const QVariant &v2) const
T value() const
Returns the stored value converted to the template type T.
Definition: qvariant.h:332
static ItemType::Ptr qtToXDMType(const QXmlItem &item)
static Item toXDM(const QVariant &value)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QPatternist::DynamicContext::Ptr dynamicContext(QAbstractXmlReceiver *const callback=0)
Definition: qxmlquery_p.h:175
static AnyURI::Ptr fromValue(const QString &value)
Definition: qanyuri.cpp:56
The QXmlQuery class performs XQueries on XML data, or on non-XML data modeled to look like XML...
Definition: qxmlquery.h:79
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
#define Q_FUNC_INFO
Definition: qglobal.h:1871