| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | // |
| 41 | // W A R N I N G |
| 42 | // ------------- |
| 43 | // |
| 44 | // This file is not part of the Qt API. It exists purely as an |
| 45 | // implementation detail. This header file may change from version to |
| 46 | // version without notice, or even be removed. |
| 47 | // |
| 48 | // We mean it. |
| 49 | |
| 50 | #ifndef Patternist_Item_H |
| 51 | #define Patternist_Item_H |
| 52 | |
| 53 | #include <QtXmlPatterns/private/qcppcastinghelper_p.h> |
| 54 | #include <QtXmlPatterns/private/qitemtype_p.h> |
| 55 | #include <QtXmlPatterns/private/qsingletoniterator_p.h> |
| 56 | #include <QtXmlPatterns/QAbstractXmlNodeModel> |
| 57 | |
| 58 | #include <QUrl> |
| 59 | #include <QVariant> |
| 60 | |
| 61 | /** |
| 62 | * @file |
| 63 | * @short Due to strong interdependencies, this file contains the definitions for |
| 64 | * the classes Item, QXmlNodeModelIndex, QAbstractXmlNodeModel and AtomicValue. The implementations are |
| 65 | * in their respective source files. |
| 66 | */ |
| 67 | |
| 68 | /** |
| 69 | * @class QSharedData |
| 70 | * @short Qt's base class for reference counting. |
| 71 | */ |
| 72 | |
| 73 | QT_BEGIN_NAMESPACE |
| 74 | |
| 75 | template<typename T> class QList; |
| 76 | template<typename T> class QVector; |
| 77 | template<typename T> class QAbstractXmlForwardIterator; |
| 78 | |
| 79 | class QSourceLocation; |
| 80 | class QAbstractXmlReceiver; |
| 81 | |
| 82 | namespace QPatternist |
| 83 | { |
| 84 | class DynamicContext; |
| 85 | class Item; |
| 86 | class ItemType; |
| 87 | class QObjectNodeModel; |
| 88 | template<typename T> class EmptyIterator; |
| 89 | template<typename T, typename ListType> class ListIterator; |
| 90 | |
| 91 | /** |
| 92 | * @short Base class for all classes representing atomic values. |
| 93 | * |
| 94 | * Instantiating AtomicValues sub classes from a value of somekind, |
| 95 | * for a certain type is done in three different ways: |
| 96 | * |
| 97 | * - The static factory fromLexical which available in most classes. This |
| 98 | * function attempts to create a value from a QString that is considered |
| 99 | * a lexical representation of the value. Thus, this function performs validation, takes |
| 100 | * care of whitespace facets, and everything else related to instantiating a value from |
| 101 | * a lexical representation. |
| 102 | * - The static factory function fromValue. This function exists for |
| 103 | * values where a C++ type exists which corresponds to the type's value space. |
| 104 | * - By using instances available in CommonValues. This is the preferred method |
| 105 | * since it uses existing singleton instances and thus saves memory. CommonValues |
| 106 | * should be used whenever possible, it can be thought as a collection of constant values. |
| 107 | * |
| 108 | * For types that does not distinguish the value space and lexical space, such as <tt>xs:string</tt>, |
| 109 | * only the fromValue() function exist, and fromLexical() is omitted. |
| 110 | * |
| 111 | * @ingroup Patternist_xdm |
| 112 | * @author Frans Englich <frans.englich@nokia.com> |
| 113 | */ |
| 114 | class Q_AUTOTEST_EXPORT AtomicValue : public QSharedData, public CppCastingHelper<AtomicValue> |
| 115 | { |
| 116 | public: |
| 117 | virtual ~AtomicValue(); |
| 118 | |
| 119 | /** |
| 120 | * A smart pointer wrapping AtomicValue instances. |
| 121 | */ |
| 122 | typedef QExplicitlySharedDataPointer<AtomicValue> Ptr; |
| 123 | |
| 124 | /** |
| 125 | * A list if smart pointers wrapping AtomicValue instances. |
| 126 | */ |
| 127 | typedef QList<AtomicValue::Ptr> List; |
| 128 | |
| 129 | /** |
| 130 | * Determines whether this atomic value has an error. This is used |
| 131 | * for implementing casting. |
| 132 | * |
| 133 | * @returns always @c false |
| 134 | */ |
| 135 | virtual bool hasError() const; |
| 136 | |
| 137 | /** |
| 138 | * Always fails by issuing the type error ReportContext::FORG0006. Sub-classes |
| 139 | * whose represented type do allow EBV to be extracted from, must thus |
| 140 | * re-implement this function. |
| 141 | */ |
| 142 | virtual bool evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &context) const; |
| 143 | |
| 144 | virtual QString stringValue() const = 0; |
| 145 | virtual ItemType::Ptr type() const = 0; |
| 146 | |
| 147 | /** |
| 148 | * Converts @p value to a QVariant. |
| 149 | */ |
| 150 | static QVariant toQt(const AtomicValue *const value); |
| 151 | |
| 152 | static inline QVariant toQt(const AtomicValue::Ptr &value) |
| 153 | { |
| 154 | return toQt(value: value.data()); |
| 155 | } |
| 156 | |
| 157 | static Item toXDM(const QVariant &value); |
| 158 | |
| 159 | static ItemType::Ptr qtToXDMType(const QXmlItem &item); |
| 160 | protected: |
| 161 | inline AtomicValue() |
| 162 | { |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | /** |
| 167 | * @short Represents an item in the XPath 2.0 Data Model. |
| 168 | * |
| 169 | * There exists two types of items: nodes and atomic values. |
| 170 | * |
| 171 | * The XQuery 1.0 and XPath 2.0 Data Model and XML Path Language (XPath) 2.0 specification |
| 172 | * makes a very strong distinction between a sequence of items and an atomized sequence. |
| 173 | * |
| 174 | * @ingroup Patternist_xdm |
| 175 | * @author Frans Englich <frans.englich@nokia.com> |
| 176 | */ |
| 177 | class Item |
| 178 | { |
| 179 | friend class QT_PREPEND_NAMESPACE(QXmlItem); |
| 180 | |
| 181 | public: |
| 182 | /** |
| 183 | * A smart pointer wrapping an Item instance. |
| 184 | */ |
| 185 | typedef QAbstractXmlForwardIterator<Item> Iterator; |
| 186 | |
| 187 | /** |
| 188 | * A list of Item instances, each wrapped in a smart pointer. |
| 189 | */ |
| 190 | typedef QList<Item> List; |
| 191 | |
| 192 | /** |
| 193 | * A vector of Item instances, each wrapped in a smart pointer. |
| 194 | */ |
| 195 | typedef QVector<Item> Vector; |
| 196 | |
| 197 | typedef QPatternist::SingletonIterator<Item> SingletonIterator; |
| 198 | typedef QPatternist::EmptyIterator<Item> EmptyIterator; |
| 199 | |
| 200 | /** |
| 201 | * Default constructor. |
| 202 | */ |
| 203 | inline Item() |
| 204 | { |
| 205 | reset(); |
| 206 | } |
| 207 | |
| 208 | inline Item(const QXmlNodeModelIndex &n) : node(n.m_storage) |
| 209 | { |
| 210 | } |
| 211 | |
| 212 | inline Item(const Item &other) : node(other.node) |
| 213 | { |
| 214 | Q_STATIC_ASSERT_X(sizeof(QXmlNodeModelIndex) >= sizeof(AtomicValue), |
| 215 | "Since we're only copying the node member, it must be the largest." ); |
| 216 | if(isAtomicValue()) |
| 217 | atomicValue->ref.ref(); |
| 218 | } |
| 219 | |
| 220 | inline Item(const AtomicValue::Ptr &a) |
| 221 | { |
| 222 | reset(); |
| 223 | if(a) |
| 224 | { |
| 225 | atomicValue = a.data(); |
| 226 | atomicValue->ref.ref(); |
| 227 | |
| 228 | /* Signal that we're housing an atomic value. */ |
| 229 | node.model = reinterpret_cast<const QAbstractXmlNodeModel *>(~0); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | inline Item(const AtomicValue *const a) |
| 234 | { |
| 235 | /* Note, the implementation is a copy of the constructor above. */ |
| 236 | reset(); |
| 237 | if(a) |
| 238 | { |
| 239 | atomicValue = a; |
| 240 | atomicValue->ref.ref(); |
| 241 | |
| 242 | /* Signal that we're housing an atomic value. */ |
| 243 | node.model = reinterpret_cast<const QAbstractXmlNodeModel *>(~0); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | inline ~Item() |
| 248 | { |
| 249 | if(isAtomicValue() && !atomicValue->ref.deref()) |
| 250 | delete atomicValue; |
| 251 | } |
| 252 | |
| 253 | inline Item &operator=(const Item &other) |
| 254 | { |
| 255 | Q_ASSERT_X(sizeof(QXmlNodeModelIndex) >= sizeof(AtomicValue *), Q_FUNC_INFO, |
| 256 | "If this doesn't hold, we won't copy all data." ); |
| 257 | |
| 258 | if(other.isAtomicValue()) |
| 259 | other.atomicValue->ref.ref(); |
| 260 | |
| 261 | if(isAtomicValue()) |
| 262 | { |
| 263 | if(!atomicValue->ref.deref()) |
| 264 | delete atomicValue; |
| 265 | } |
| 266 | |
| 267 | node = other.node; |
| 268 | |
| 269 | return *this; |
| 270 | } |
| 271 | |
| 272 | template<typename TCastTarget> |
| 273 | inline TCastTarget *as() const |
| 274 | { |
| 275 | #if defined(Patternist_DEBUG) && !defined(Q_CC_XLC) |
| 276 | /* At least on aix-xlc-64, the compiler cries when it sees dynamic_cast. */ |
| 277 | Q_ASSERT_X(atomicValue == 0 || dynamic_cast<const TCastTarget *>(atomicValue), |
| 278 | Q_FUNC_INFO, |
| 279 | "The cast is invalid. This class does not inherit the cast target." ); |
| 280 | #endif |
| 281 | return const_cast<TCastTarget *>(static_cast<const TCastTarget *>(atomicValue)); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * @short Returns the string value of this Item. |
| 286 | * |
| 287 | * In the case of a node, it is the node value corresponding to |
| 288 | * the particular node type. For atomic values, it is equivalent |
| 289 | * to the value cast as <tt>xs:string</tt>. |
| 290 | * |
| 291 | * Conceptually, this functions corresponds to the <tt>dm:string-value</tt> accessor. |
| 292 | * |
| 293 | * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dm-string-value">XQuery 1.0 and |
| 294 | * XPath 2.0 Data Model, 5.13 string-value Accessor</a> |
| 295 | * @returns the string value. |
| 296 | */ |
| 297 | inline QString stringValue() const |
| 298 | { |
| 299 | if(isAtomicValue()) |
| 300 | return atomicValue->stringValue(); |
| 301 | else |
| 302 | return asNode().stringValue(); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * @short Returns the typed value of this item. |
| 307 | * |
| 308 | * Conceptually, this functions corresponds to the <tt>dm:typed-value</tt> accessor. Here are |
| 309 | * examples of what the typed value of an Item is: |
| 310 | * |
| 311 | * - The typed value of an atomic value is always the atomic value itself. |
| 312 | * - A comment node has always a typed value of type @c xs:string |
| 313 | * - For attribute and element nodes, the typed value can be arbitrary. For example, an |
| 314 | * element can have a sequence of @c xs:dateTime instances. |
| 315 | * |
| 316 | * @returns the typed value of this item |
| 317 | * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dm-typed-value">XQuery 1.0 and |
| 318 | * XPath 2.0 Data Model, 5.15 typed-value Accessor</a> |
| 319 | */ |
| 320 | Item::Iterator::Ptr sequencedTypedValue() const; |
| 321 | |
| 322 | /** |
| 323 | * @short Determines whether this item is an atomic value, or a node. |
| 324 | * |
| 325 | * If this Item is @c null, @c false is returned. |
| 326 | * |
| 327 | * @see isNode() |
| 328 | * @returns @c true if it is an atomic value, otherwise @c false. |
| 329 | */ |
| 330 | inline bool isAtomicValue() const |
| 331 | { |
| 332 | /* Setting node.model to ~0, signals that it's an atomic value. */ |
| 333 | return node.model == reinterpret_cast<QAbstractXmlNodeModel *>(~0); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * @short Determines whether this item is an atomic value, or a node. |
| 338 | * |
| 339 | * If this Item is @c null, false is returned. |
| 340 | * |
| 341 | * @see isAtomicValue() |
| 342 | * @returns @c true if this item is a node, otherwise @c false. |
| 343 | */ |
| 344 | inline bool isNode() const |
| 345 | { |
| 346 | //return !isAtomicValue(); |
| 347 | return node.model && node.model != reinterpret_cast<QAbstractXmlNodeModel *>(~0); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * @short Returns the ItemType this Item is of. |
| 352 | * |
| 353 | * For example, if this Item is an XML node, more specifically a text node, |
| 354 | * <tt>text()</tt> is returned. That is, BuiltinTypes::text. However, if this |
| 355 | * Item is an atomic value of type <tt>xs:long</tt> that is what's returned, |
| 356 | * BuiltinTypes::xsLong. |
| 357 | * |
| 358 | * @returns the type of this Item. |
| 359 | */ |
| 360 | inline QExplicitlySharedDataPointer<ItemType> type() const |
| 361 | { |
| 362 | if(isAtomicValue()) |
| 363 | return atomicValue->type(); |
| 364 | else |
| 365 | return asNode().type(); |
| 366 | } |
| 367 | |
| 368 | inline const AtomicValue *asAtomicValue() const |
| 369 | { |
| 370 | Q_ASSERT(isAtomicValue()); |
| 371 | return atomicValue; |
| 372 | } |
| 373 | |
| 374 | inline const QXmlNodeModelIndex &asNode() const |
| 375 | { |
| 376 | Q_ASSERT_X(isNode() || isNull(), Q_FUNC_INFO, |
| 377 | "This item isn't a valid QXmlNodeModelIndex." ); |
| 378 | Q_ASSERT_X(sizeof(QXmlNodeModelIndex) == sizeof(QPatternist::NodeIndexStorage), Q_FUNC_INFO, |
| 379 | "If this doesn't hold, something is wrong." ); |
| 380 | |
| 381 | return reinterpret_cast<const QXmlNodeModelIndex &>(node); |
| 382 | } |
| 383 | |
| 384 | inline operator bool() const |
| 385 | { |
| 386 | return node.model; |
| 387 | } |
| 388 | |
| 389 | inline bool isNull() const |
| 390 | { |
| 391 | return !node.model; |
| 392 | } |
| 393 | |
| 394 | inline void reset() |
| 395 | { |
| 396 | node.reset(); |
| 397 | } |
| 398 | |
| 399 | static inline Item fromPublic(const QXmlItem &i) |
| 400 | { |
| 401 | const Item it(i.m_node); |
| 402 | if(it.isAtomicValue()) |
| 403 | it.asAtomicValue()->ref.ref(); |
| 404 | |
| 405 | return it; |
| 406 | } |
| 407 | |
| 408 | static inline QXmlItem toPublic(const Item &i) |
| 409 | { |
| 410 | return QXmlItem(i); |
| 411 | } |
| 412 | |
| 413 | private: |
| 414 | union |
| 415 | { |
| 416 | NodeIndexStorage node; |
| 417 | const AtomicValue *atomicValue; |
| 418 | }; |
| 419 | }; |
| 420 | |
| 421 | template<typename T> |
| 422 | inline Item toItem(const QExplicitlySharedDataPointer<T> atomicValue) |
| 423 | { |
| 424 | return Item(atomicValue.data()); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * This is an overload, provided for convenience. |
| 429 | * @relates QXmlNodeModelIndex |
| 430 | */ |
| 431 | static inline QString formatData(const QXmlNodeModelIndex node) |
| 432 | { |
| 433 | return node.stringValue(); // This can be improved a lot. |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | inline QXmlName QXmlNodeModelIndex::name() const |
| 438 | { |
| 439 | return m_storage.model->name(ni: *this); |
| 440 | } |
| 441 | |
| 442 | inline QXmlNodeModelIndex QXmlNodeModelIndex::root() const |
| 443 | { |
| 444 | return m_storage.model->root(n: *this); |
| 445 | } |
| 446 | |
| 447 | inline QXmlNodeModelIndex::Iterator::Ptr QXmlNodeModelIndex::iterate(const QXmlNodeModelIndex::Axis axis) const |
| 448 | { |
| 449 | return m_storage.model->iterate(ni: *this, axis); |
| 450 | } |
| 451 | |
| 452 | inline QUrl QXmlNodeModelIndex::documentUri() const |
| 453 | { |
| 454 | return m_storage.model->documentUri(ni: *this); |
| 455 | } |
| 456 | |
| 457 | inline QUrl QXmlNodeModelIndex::baseUri() const |
| 458 | { |
| 459 | return m_storage.model->baseUri(ni: *this); |
| 460 | } |
| 461 | |
| 462 | inline QXmlNodeModelIndex::NodeKind QXmlNodeModelIndex::kind() const |
| 463 | { |
| 464 | return m_storage.model->kind(ni: *this); |
| 465 | } |
| 466 | |
| 467 | inline bool QXmlNodeModelIndex::isDeepEqual(const QXmlNodeModelIndex &other) const |
| 468 | { |
| 469 | return m_storage.model->isDeepEqual(ni1: *this, ni2: other); |
| 470 | } |
| 471 | |
| 472 | inline QXmlNodeModelIndex::DocumentOrder QXmlNodeModelIndex::compareOrder(const QXmlNodeModelIndex &other) const |
| 473 | { |
| 474 | Q_ASSERT_X(model() == other.model(), Q_FUNC_INFO, "The API docs guarantees the two nodes are from the same model" ); |
| 475 | return m_storage.model->compareOrder(ni1: *this, ni2: other); |
| 476 | } |
| 477 | |
| 478 | inline bool QXmlNodeModelIndex::is(const QXmlNodeModelIndex &other) const |
| 479 | { |
| 480 | return m_storage.model == other.m_storage.model && |
| 481 | m_storage.data == other.m_storage.data && |
| 482 | m_storage.additionalData == other.m_storage.additionalData; |
| 483 | } |
| 484 | |
| 485 | inline void QXmlNodeModelIndex::sendNamespaces(QAbstractXmlReceiver *const receiver) const |
| 486 | { |
| 487 | m_storage.model->sendNamespaces(n: *this, receiver); |
| 488 | } |
| 489 | |
| 490 | inline QVector<QXmlName> QXmlNodeModelIndex::namespaceBindings() const |
| 491 | { |
| 492 | return m_storage.model->namespaceBindings(n: *this); |
| 493 | } |
| 494 | |
| 495 | inline QXmlName::NamespaceCode QXmlNodeModelIndex::namespaceForPrefix(const QXmlName::PrefixCode prefix) const |
| 496 | { |
| 497 | return m_storage.model->namespaceForPrefix(ni: *this, prefix); |
| 498 | } |
| 499 | |
| 500 | inline QString QXmlNodeModelIndex::stringValue() const |
| 501 | { |
| 502 | return m_storage.model->stringValue(n: *this); |
| 503 | } |
| 504 | |
| 505 | inline QPatternist::ItemType::Ptr QXmlNodeModelIndex::type() const |
| 506 | { |
| 507 | return m_storage.model->type(ni: *this); |
| 508 | } |
| 509 | |
| 510 | inline QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<QPatternist::Item> > QXmlNodeModelIndex::sequencedTypedValue() const |
| 511 | { |
| 512 | return m_storage.model->sequencedTypedValue(ni: *this); |
| 513 | } |
| 514 | |
| 515 | inline QXmlItem::QXmlItem(const QPatternist::Item &i) : m_node(i.node) |
| 516 | { |
| 517 | if(isAtomicValue()) |
| 518 | m_atomicValue->ref.ref(); |
| 519 | } |
| 520 | |
| 521 | Q_DECLARE_TYPEINFO(QPatternist::Item::Iterator::Ptr, Q_MOVABLE_TYPE); |
| 522 | Q_DECLARE_TYPEINFO(QPatternist::AtomicValue, Q_MOVABLE_TYPE); |
| 523 | |
| 524 | QT_END_NAMESPACE |
| 525 | |
| 526 | #endif |
| 527 | |