1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
46 // This file is not part of the Qt API. It exists purely as an
47 // implementation detail. This header file may change from version to
48 // version without notice, or even be removed.
52 #ifndef Patternist_Item_H
53 #define Patternist_Item_H
55 #include <QtXmlPatterns/private/qcppcastinghelper_p.h>
56 #include <QtXmlPatterns/private/qitemtype_p.h>
57 #include <QtXmlPatterns/private/qsingletoniterator_p.h>
58 #include <QtXmlPatterns/QAbstractXmlNodeModel>
65 * @short Due to strong interdependencies, this file contains the definitions for
66 * the classes Item, QXmlNodeModelIndex, QAbstractXmlNodeModel and AtomicValue. The implementations are
67 * in their respective source files.
72 * @short Qt's base class for reference counting.
79 template<typename T> class QList;
80 template<typename T> class QVector;
81 template<typename T> class QAbstractXmlForwardIterator;
83 class QSourceLocation;
84 class QAbstractXmlReceiver;
91 class QObjectNodeModel;
92 template<typename T> class EmptyIterator;
93 template<typename T, typename ListType> class ListIterator;
96 * @short Base class for all classes representing atomic values.
98 * Instantiating AtomicValues sub classes from a value of somekind,
99 * for a certain type is done in three different ways:
101 * - The static factory fromLexical which available in most classes. This
102 * function attempts to create a value from a QString that is considered
103 * a lexical representation of the value. Thus, this function performs validation, takes
104 * care of whitespace facets, and everything else related to instantiating a value from
105 * a lexical representation.
106 * - The static factory function fromValue. This function exists for
107 * values where a C++ type exists which corresponds to the type's value space.
108 * - By using instances available in CommonValues. This is the preferred method
109 * since it uses existing singleton instances and thus saves memory. CommonValues
110 * should be used whenever possible, it can be thought as a collection of constant values.
112 * For types that does not distinguish the value space and lexical space, such as <tt>xs:string</tt>,
113 * only the fromValue() function exist, and fromLexical() is omitted.
115 * @ingroup Patternist_xdm
116 * @author Frans Englich <frans.englich@nokia.com>
118 class AtomicValue : public QSharedData
119 , public CppCastingHelper<AtomicValue>
122 virtual ~AtomicValue();
125 * A smart pointer wrapping AtomicValue instances.
127 typedef QExplicitlySharedDataPointer<AtomicValue> Ptr;
130 * A list if smart pointers wrapping AtomicValue instances.
132 typedef QList<AtomicValue::Ptr> List;
135 * Determines whether this atomic value has an error. This is used
136 * for implementing casting.
138 * @returns always @c false
140 virtual bool hasError() const;
143 * Always fails by issuing the type error ReportContext::FORG0006. Sub-classes
144 * whose represented type do allow EBV to be extracted from, must thus
145 * re-implement this function.
147 virtual bool evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &context) const;
149 virtual QString stringValue() const = 0;
150 virtual ItemType::Ptr type() const = 0;
153 * Converts @p value to a QVariant.
155 static QVariant toQt(const AtomicValue *const value);
157 static inline QVariant toQt(const AtomicValue::Ptr &value)
159 return toQt(value.data());
162 static Item toXDM(const QVariant &value);
164 static ItemType::Ptr qtToXDMType(const QXmlItem &item);
172 * @short Represents an item in the XPath 2.0 Data Model.
174 * There exists two types of items: nodes and atomic values.
176 * The XQuery 1.0 and XPath 2.0 Data Model and XML Path Language (XPath) 2.0 specification
177 * makes a very strong distinction between a sequence of items and an atomized sequence.
179 * @ingroup Patternist_xdm
180 * @author Frans Englich <frans.englich@nokia.com>
184 friend class QT_PREPEND_NAMESPACE(QXmlItem);
188 * A smart pointer wrapping an Item instance.
190 typedef QAbstractXmlForwardIterator<Item> Iterator;
193 * A list of Item instances, each wrapped in a smart pointer.
195 typedef QList<Item> List;
198 * A vector of Item instances, each wrapped in a smart pointer.
200 typedef QVector<Item> Vector;
202 typedef QPatternist::SingletonIterator<Item> SingletonIterator;
203 typedef QPatternist::EmptyIterator<Item> EmptyIterator;
206 * Default constructor.
213 inline Item(const QXmlNodeModelIndex &n) : node(n.m_storage)
217 inline Item(const Item &other) : node(other.node)
219 Q_ASSERT_X(sizeof(QXmlNodeModelIndex) >= sizeof(AtomicValue), Q_FUNC_INFO,
220 "Since we're only copying the node member, it must be the largest.");
222 atomicValue->ref.ref();
225 inline Item(const AtomicValue::Ptr &a)
230 atomicValue = a.data();
231 atomicValue->ref.ref();
233 /* Signal that we're housing an atomic value. */
234 node.model = reinterpret_cast<const QAbstractXmlNodeModel *>(~0);
238 inline Item(const AtomicValue *const a)
240 /* Note, the implementation is a copy of the constructor above. */
245 atomicValue->ref.ref();
247 /* Signal that we're housing an atomic value. */
248 node.model = reinterpret_cast<const QAbstractXmlNodeModel *>(~0);
254 if(isAtomicValue() && !atomicValue->ref.deref())
258 inline Item &operator=(const Item &other)
260 Q_ASSERT_X(sizeof(QXmlNodeModelIndex) >= sizeof(AtomicValue *), Q_FUNC_INFO,
261 "If this doesn't hold, we won't copy all data.");
263 if(other.isAtomicValue())
264 other.atomicValue->ref.ref();
268 if(!atomicValue->ref.deref())
277 template<typename TCastTarget>
278 inline TCastTarget *as() const
280 #if defined(Patternist_DEBUG) && !defined(Q_CC_XLC)
281 /* At least on aix-xlc-64, the compiler cries when it sees dynamic_cast. */
282 Q_ASSERT_X(atomicValue == 0 || dynamic_cast<const TCastTarget *>(atomicValue),
284 "The cast is invalid. This class does not inherit the cast target.");
286 return const_cast<TCastTarget *>(static_cast<const TCastTarget *>(atomicValue));
290 * @short Returns the string value of this Item.
292 * In the case of a node, it is the node value corresponding to
293 * the particular node type. For atomic values, it is equivalent
294 * to the value cast as <tt>xs:string</tt>.
296 * Conceptually, this functions corresponds to the <tt>dm:string-value</tt> accessor.
298 * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dm-string-value">XQuery 1.0 and
299 * XPath 2.0 Data Model, 5.13 string-value Accessor</a>
300 * @returns the string value.
302 inline QString stringValue() const
305 return atomicValue->stringValue();
307 return asNode().stringValue();
311 * @short Returns the typed value of this item.
313 * Conceptually, this functions corresponds to the <tt>dm:typed-value</tt> accessor. Here are
314 * examples of what the typed value of an Item is:
316 * - The typed value of an atomic value is always the atomic value itself.
317 * - A comment node has always a typed value of type @c xs:string
318 * - For attribute and element nodes, the typed value can be arbitrary. For example, an
319 * element can have a sequence of @c xs:dateTime instances.
321 * @returns the typed value of this item
322 * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dm-typed-value">XQuery 1.0 and
323 * XPath 2.0 Data Model, 5.15 typed-value Accessor</a>
325 Item::Iterator::Ptr sequencedTypedValue() const;
328 * @short Determines whether this item is an atomic value, or a node.
330 * If this Item is @c null, @c false is returned.
333 * @returns @c true if it is an atomic value, otherwise @c false.
335 inline bool isAtomicValue() const
337 /* Setting node.model to ~0, signals that it's an atomic value. */
338 return node.model == reinterpret_cast<QAbstractXmlNodeModel *>(~0);
342 * @short Determines whether this item is an atomic value, or a node.
344 * If this Item is @c null, false is returned.
346 * @see isAtomicValue()
347 * @returns @c true if this item is a node, otherwise @c false.
349 inline bool isNode() const
351 //return !isAtomicValue();
352 return node.model && node.model != reinterpret_cast<QAbstractXmlNodeModel *>(~0);
356 * @short Returns the ItemType this Item is of.
358 * For example, if this Item is an XML node, more specifically a text node,
359 * <tt>text()</tt> is returned. That is, BuiltinTypes::text. However, if this
360 * Item is an atomic value of type <tt>xs:long</tt> that is what's returned,
361 * BuiltinTypes::xsLong.
363 * @returns the type of this Item.
365 inline QExplicitlySharedDataPointer<ItemType> type() const
368 return atomicValue->type();
370 return asNode().type();
373 inline const AtomicValue *asAtomicValue() const
375 Q_ASSERT(isAtomicValue());
379 inline const QXmlNodeModelIndex &asNode() const
381 Q_ASSERT_X(isNode() || isNull(), Q_FUNC_INFO,
382 "This item isn't a valid QXmlNodeModelIndex.");
383 Q_ASSERT_X(sizeof(QXmlNodeModelIndex) == sizeof(QPatternist::NodeIndexStorage), Q_FUNC_INFO,
384 "If this doesn't hold, something is wrong.");
386 return reinterpret_cast<const QXmlNodeModelIndex &>(node);
389 inline operator bool() const
394 inline bool isNull() const
404 static inline Item fromPublic(const QXmlItem &i)
406 const Item it(i.m_node);
407 if(it.isAtomicValue())
408 it.asAtomicValue()->ref.ref();
413 static inline QXmlItem toPublic(const Item &i)
421 NodeIndexStorage node;
422 const AtomicValue *atomicValue;
427 inline Item toItem(const QExplicitlySharedDataPointer<T> atomicValue)
429 return Item(atomicValue.data());
433 * This is an overload, provided for convenience.
434 * @relates QXmlNodeModelIndex
436 static inline QString formatData(const QXmlNodeModelIndex node)
438 return node.stringValue(); // This can be improved a lot.
442 inline QXmlName QXmlNodeModelIndex::name() const
444 return m_storage.model->name(*this);
447 inline QXmlNodeModelIndex QXmlNodeModelIndex::root() const
449 return m_storage.model->root(*this);
452 inline QXmlNodeModelIndex::Iterator::Ptr QXmlNodeModelIndex::iterate(const QXmlNodeModelIndex::Axis axis) const
454 return m_storage.model->iterate(*this, axis);
457 inline QUrl QXmlNodeModelIndex::documentUri() const
459 return m_storage.model->documentUri(*this);
462 inline QUrl QXmlNodeModelIndex::baseUri() const
464 return m_storage.model->baseUri(*this);
467 inline QXmlNodeModelIndex::NodeKind QXmlNodeModelIndex::kind() const
469 return m_storage.model->kind(*this);
472 inline bool QXmlNodeModelIndex::isDeepEqual(const QXmlNodeModelIndex &other) const
474 return m_storage.model->isDeepEqual(*this, other);
477 inline QXmlNodeModelIndex::DocumentOrder QXmlNodeModelIndex::compareOrder(const QXmlNodeModelIndex &other) const
479 Q_ASSERT_X(model() == other.model(), Q_FUNC_INFO, "The API docs guarantees the two nodes are from the same model");
480 return m_storage.model->compareOrder(*this, other);
483 inline bool QXmlNodeModelIndex::is(const QXmlNodeModelIndex &other) const
485 return m_storage.model == other.m_storage.model &&
486 m_storage.data == other.m_storage.data &&
487 m_storage.additionalData == other.m_storage.additionalData;
490 inline void QXmlNodeModelIndex::sendNamespaces(QAbstractXmlReceiver *const receiver) const
492 m_storage.model->sendNamespaces(*this, receiver);
495 inline QVector<QXmlName> QXmlNodeModelIndex::namespaceBindings() const
497 return m_storage.model->namespaceBindings(*this);
500 inline QXmlName::NamespaceCode QXmlNodeModelIndex::namespaceForPrefix(const QXmlName::PrefixCode prefix) const
502 return m_storage.model->namespaceForPrefix(*this, prefix);
505 inline QString QXmlNodeModelIndex::stringValue() const
507 return m_storage.model->stringValue(*this);
510 inline QPatternist::ItemType::Ptr QXmlNodeModelIndex::type() const
512 return m_storage.model->type(*this);
515 inline QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<QPatternist::Item> > QXmlNodeModelIndex::sequencedTypedValue() const
517 return m_storage.model->sequencedTypedValue(*this);
520 inline QXmlItem::QXmlItem(const QPatternist::Item &i) : m_node(i.node)
523 m_atomicValue->ref.ref();
526 Q_DECLARE_TYPEINFO(QPatternist::Item::Iterator::Ptr, Q_MOVABLE_TYPE);
527 Q_DECLARE_TYPEINFO(QPatternist::AtomicValue, Q_MOVABLE_TYPE);