1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** 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.
210 /* Note that this function should be equal to reset(). */
212 /* This is the area which atomicValue uses. Becauase we want as()
213 * to return null on null-constructed objects, we initialize it. */
216 /* This signals that we're not an atomic value. */
220 inline Item(const QXmlNodeModelIndex &n) : node(n.m_storage)
224 inline Item(const Item &other) : node(other.node)
226 Q_ASSERT_X(sizeof(QXmlNodeModelIndex) >= sizeof(AtomicValue), Q_FUNC_INFO,
227 "Since we're only copying the node member, it must be the largest.");
229 atomicValue->ref.ref();
232 inline Item(const AtomicValue::Ptr &a)
236 atomicValue = a.data();
237 atomicValue->ref.ref();
239 /* Signal that we're housing an atomic value. */
240 node.model = reinterpret_cast<const QAbstractXmlNodeModel *>(~0);
243 node.model = 0; /* Like the default constructor. */
246 inline Item(const AtomicValue *const a)
248 /* Note, the implementation is a copy of the constructor above. */
253 atomicValue->ref.ref();
255 /* Signal that we're housing an atomic value. */
256 node.model = reinterpret_cast<const QAbstractXmlNodeModel *>(~0);
259 node.model = 0; /* Like the default constructor. */
264 if(isAtomicValue() && !atomicValue->ref.deref())
268 inline Item &operator=(const Item &other)
270 Q_ASSERT_X(sizeof(QXmlNodeModelIndex) >= sizeof(AtomicValue *), Q_FUNC_INFO,
271 "If this doesn't hold, we won't copy all data.");
273 if(other.isAtomicValue())
274 other.atomicValue->ref.ref();
278 if(!atomicValue->ref.deref())
287 template<typename TCastTarget>
288 inline TCastTarget *as() const
290 #if defined(Patternist_DEBUG) && !defined(Q_CC_XLC)
291 /* At least on aix-xlc-64, the compiler cries when it sees dynamic_cast. */
292 Q_ASSERT_X(atomicValue == 0 || dynamic_cast<const TCastTarget *>(atomicValue),
294 "The cast is invalid. This class does not inherit the cast target.");
296 return const_cast<TCastTarget *>(static_cast<const TCastTarget *>(atomicValue));
300 * @short Returns the string value of this Item.
302 * In the case of a node, it is the node value corresponding to
303 * the particular node type. For atomic values, it is equivalent
304 * to the value cast as <tt>xs:string</tt>.
306 * Conceptually, this functions corresponds to the <tt>dm:string-value</tt> accessor.
308 * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dm-string-value">XQuery 1.0 and
309 * XPath 2.0 Data Model, 5.13 string-value Accessor</a>
310 * @returns the string value.
312 inline QString stringValue() const
315 return atomicValue->stringValue();
317 return asNode().stringValue();
321 * @short Returns the typed value of this item.
323 * Conceptually, this functions corresponds to the <tt>dm:typed-value</tt> accessor. Here are
324 * examples of what the typed value of an Item is:
326 * - The typed value of an atomic value is always the atomic value itself.
327 * - A comment node has always a typed value of type @c xs:string
328 * - For attribute and element nodes, the typed value can be arbitrary. For example, an
329 * element can have a sequence of @c xs:dateTime instances.
331 * @returns the typed value of this item
332 * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dm-typed-value">XQuery 1.0 and
333 * XPath 2.0 Data Model, 5.15 typed-value Accessor</a>
335 Item::Iterator::Ptr sequencedTypedValue() const;
338 * @short Determines whether this item is an atomic value, or a node.
340 * If this Item is @c null, @c false is returned.
343 * @returns @c true if it is an atomic value, otherwise @c false.
345 inline bool isAtomicValue() const
347 /* Setting node.model to ~0, signals that it's an atomic value. */
348 return node.model == reinterpret_cast<QAbstractXmlNodeModel *>(~0);
352 * @short Determines whether this item is an atomic value, or a node.
354 * If this Item is @c null, false is returned.
356 * @see isAtomicValue()
357 * @returns @c true if this item is a node, otherwise @c false.
359 inline bool isNode() const
361 //return !isAtomicValue();
362 return node.model && node.model != reinterpret_cast<QAbstractXmlNodeModel *>(~0);
366 * @short Returns the ItemType this Item is of.
368 * For example, if this Item is an XML node, more specifically a text node,
369 * <tt>text()</tt> is returned. That is, BuiltinTypes::text. However, if this
370 * Item is an atomic value of type <tt>xs:long</tt> that is what's returned,
371 * BuiltinTypes::xsLong.
373 * @returns the type of this Item.
375 inline QExplicitlySharedDataPointer<ItemType> type() const
378 return atomicValue->type();
380 return asNode().type();
383 inline const AtomicValue *asAtomicValue() const
385 Q_ASSERT(isAtomicValue());
389 inline const QXmlNodeModelIndex &asNode() const
391 Q_ASSERT_X(isNode() || isNull(), Q_FUNC_INFO,
392 "This item isn't a valid QXmlNodeModelIndex.");
393 Q_ASSERT_X(sizeof(QXmlNodeModelIndex) == sizeof(QPatternist::NodeIndexStorage), Q_FUNC_INFO,
394 "If this doesn't hold, something is wrong.");
396 return reinterpret_cast<const QXmlNodeModelIndex &>(node);
399 inline operator bool() const
404 inline bool isNull() const
411 /* Note that this function should be equal to the default
417 static inline Item fromPublic(const QXmlItem &i)
419 const Item it(i.m_node);
420 if(it.isAtomicValue())
421 it.asAtomicValue()->ref.ref();
426 static inline QXmlItem toPublic(const Item &i)
434 NodeIndexStorage node;
435 const AtomicValue *atomicValue;
440 inline Item toItem(const QExplicitlySharedDataPointer<T> atomicValue)
442 return Item(atomicValue.data());
446 * This is an overload, provided for convenience.
447 * @relates QXmlNodeModelIndex
449 static inline QString formatData(const QXmlNodeModelIndex node)
451 return node.stringValue(); // This can be improved a lot.
455 inline QXmlName QXmlNodeModelIndex::name() const
457 return m_storage.model->name(*this);
460 inline QXmlNodeModelIndex QXmlNodeModelIndex::root() const
462 return m_storage.model->root(*this);
465 inline QXmlNodeModelIndex::Iterator::Ptr QXmlNodeModelIndex::iterate(const QXmlNodeModelIndex::Axis axis) const
467 return m_storage.model->iterate(*this, axis);
470 inline QUrl QXmlNodeModelIndex::documentUri() const
472 return m_storage.model->documentUri(*this);
475 inline QUrl QXmlNodeModelIndex::baseUri() const
477 return m_storage.model->baseUri(*this);
480 inline QXmlNodeModelIndex::NodeKind QXmlNodeModelIndex::kind() const
482 return m_storage.model->kind(*this);
485 inline bool QXmlNodeModelIndex::isDeepEqual(const QXmlNodeModelIndex &other) const
487 return m_storage.model->isDeepEqual(*this, other);
490 inline QXmlNodeModelIndex::DocumentOrder QXmlNodeModelIndex::compareOrder(const QXmlNodeModelIndex &other) const
492 Q_ASSERT_X(model() == other.model(), Q_FUNC_INFO, "The API docs guarantees the two nodes are from the same model");
493 return m_storage.model->compareOrder(*this, other);
496 inline bool QXmlNodeModelIndex::is(const QXmlNodeModelIndex &other) const
498 return m_storage.model == other.m_storage.model &&
499 m_storage.data == other.m_storage.data &&
500 m_storage.additionalData == other.m_storage.additionalData;
503 inline void QXmlNodeModelIndex::sendNamespaces(QAbstractXmlReceiver *const receiver) const
505 m_storage.model->sendNamespaces(*this, receiver);
508 inline QVector<QXmlName> QXmlNodeModelIndex::namespaceBindings() const
510 return m_storage.model->namespaceBindings(*this);
513 inline QXmlName::NamespaceCode QXmlNodeModelIndex::namespaceForPrefix(const QXmlName::PrefixCode prefix) const
515 return m_storage.model->namespaceForPrefix(*this, prefix);
518 inline QString QXmlNodeModelIndex::stringValue() const
520 return m_storage.model->stringValue(*this);
523 inline QPatternist::ItemType::Ptr QXmlNodeModelIndex::type() const
525 return m_storage.model->type(*this);
528 inline QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<QPatternist::Item> > QXmlNodeModelIndex::sequencedTypedValue() const
530 return m_storage.model->sequencedTypedValue(*this);
533 inline QXmlItem::QXmlItem(const QPatternist::Item &i) : m_node(i.node)
536 m_atomicValue->ref.ref();
539 Q_DECLARE_TYPEINFO(QPatternist::Item::Iterator::Ptr, Q_MOVABLE_TYPE);
540 Q_DECLARE_TYPEINFO(QPatternist::AtomicValue, Q_MOVABLE_TYPE);