src/xmlpatterns/api/qabstractxmlnodemodel.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QABSTRACTXMLNODEMODEL_H
       
    43 #define QABSTRACTXMLNODEMODEL_H
       
    44 
       
    45 #include <QtXmlPatterns/QXmlName>
       
    46 #include <QtCore/QSharedData>
       
    47 #include <QtCore/QScopedPointer>
       
    48 
       
    49 QT_BEGIN_HEADER
       
    50 QT_BEGIN_NAMESPACE
       
    51 
       
    52 QT_MODULE(XmlPatterns)
       
    53 
       
    54 /* This file contains the classes QXmlNodeModelIndex, QAbstractXmlNodeModel,
       
    55  * QXmlItem and QPatternist::NodeIndexStorage. */
       
    56 
       
    57 class QAbstractXmlNodeModel;
       
    58 class QAbstractXmlNodeModelPrivate;
       
    59 class QAbstractXmlReceiver;
       
    60 class QSourceLocation;
       
    61 class QUrl;
       
    62 class QXmlName;
       
    63 class QXmlNodeModelIndex;
       
    64 template<typename T> class QAbstractXmlForwardIterator;
       
    65 template<typename T> class QVector;
       
    66 
       
    67 /* The members in the namespace QPatternist are internal, not part of the public API, and
       
    68  * unsupported. Using them leads to undefined behavior. */
       
    69 namespace QPatternist
       
    70 {
       
    71     class DynamicContext;
       
    72     class Item;
       
    73     class ItemType;
       
    74     class XsdValidatedXmlNodeModel;
       
    75     template<typename TResult, typename TSource, typename TMapper, typename Context> class ItemMappingIterator;
       
    76     template<typename TResult, typename TSource, typename TMapper> class SequenceMappingIterator;
       
    77     typedef QExplicitlySharedDataPointer<ItemType> ItemTypePtr;
       
    78     typedef QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<Item> > ItemIteratorPtr;
       
    79     typedef QVector<QXmlName> QXmlNameVector;
       
    80 
       
    81     class NodeIndexStorage
       
    82     {
       
    83     public:
       
    84         typedef qint64 Data;
       
    85 
       
    86         /*!
       
    87           \note Changing merely the order of these two members, ptr and data,
       
    88                 is a binary incompatible change on Mac Power PC.
       
    89          */
       
    90         union
       
    91         {
       
    92             void *ptr; // Do not use ptr directy, use pointer() instead.
       
    93             Data data;
       
    94         };
       
    95         void *pointer() const
       
    96         {
       
    97             /* Constructing to qptrdiff means we avoid the warning "cast to pointer
       
    98              * from integer of different size."
       
    99              */
       
   100             return (void *)qptrdiff(data);
       
   101         }
       
   102 
       
   103         Data additionalData;
       
   104         const QAbstractXmlNodeModel *model;
       
   105 
       
   106         /* Implementation is in qabstractxmlnodemodel.cpp. */
       
   107         inline bool operator!=(const NodeIndexStorage &other) const;
       
   108     };
       
   109 }
       
   110 
       
   111 class Q_XMLPATTERNS_EXPORT QXmlNodeModelIndex
       
   112 {
       
   113     enum Constants
       
   114     {
       
   115         ForwardAxis         = 8192,
       
   116         ReverseAxis         = 16384
       
   117     };
       
   118 
       
   119 public:
       
   120     inline QXmlNodeModelIndex()
       
   121     {
       
   122         reset();
       
   123     }
       
   124 
       
   125     inline QXmlNodeModelIndex(const QXmlNodeModelIndex &other) : m_storage(other.m_storage)
       
   126     {
       
   127     }
       
   128 
       
   129     bool operator==(const QXmlNodeModelIndex &other) const;
       
   130     bool operator!=(const QXmlNodeModelIndex &other) const;
       
   131 
       
   132     typedef QAbstractXmlForwardIterator<QXmlNodeModelIndex> Iterator;
       
   133     typedef QList<QXmlNodeModelIndex> List;
       
   134 
       
   135     enum NodeKind
       
   136     {
       
   137         Attribute               = 1,
       
   138         Comment                 = 2,
       
   139         Document                = 4,
       
   140         Element                 = 8,
       
   141         Namespace               = 16,
       
   142         ProcessingInstruction   = 32,
       
   143         Text                    = 64
       
   144     };
       
   145 
       
   146     enum DocumentOrder
       
   147     {
       
   148         Precedes = -1,
       
   149         Is       = 0,
       
   150         Follows  = 1
       
   151     };
       
   152 
       
   153     enum Axis
       
   154     {
       
   155         AxisChild               = 1 | ForwardAxis,
       
   156         AxisDescendant          = 2 | ForwardAxis,
       
   157         AxisAttribute           = 4 | ForwardAxis,
       
   158         AxisSelf                = 8 | ForwardAxis,
       
   159         AxisDescendantOrSelf    = 16 | ForwardAxis,
       
   160         AxisFollowingSibling    = 32 | ForwardAxis,
       
   161         AxisNamespace           = 64 | ForwardAxis,
       
   162         AxisFollowing           = 128 | ReverseAxis,
       
   163         AxisParent              = 256 | ReverseAxis,
       
   164         AxisAncestor            = 512 | ReverseAxis,
       
   165         AxisPrecedingSibling    = 1024 | ReverseAxis,
       
   166         AxisPreceding           = 2048 | ReverseAxis,
       
   167         AxisAncestorOrSelf      = 4096 | ReverseAxis,
       
   168         /* Note that we cannot clash with the values of ForwardAxis and
       
   169          * ReverseAxis. */
       
   170         AxisChildOrTop          = 32768 | ForwardAxis,
       
   171         AxisAttributeOrTop      = 65536 | ForwardAxis
       
   172     };
       
   173 
       
   174     inline qint64 data() const
       
   175     {
       
   176         return m_storage.data;
       
   177     }
       
   178 
       
   179     inline void *internalPointer() const
       
   180     {
       
   181         return m_storage.pointer();
       
   182     }
       
   183 
       
   184     inline const QAbstractXmlNodeModel *model() const
       
   185     {
       
   186         return m_storage.model;
       
   187     }
       
   188 
       
   189     inline qint64 additionalData() const
       
   190     {
       
   191         return m_storage.additionalData;
       
   192     }
       
   193 
       
   194     inline bool isNull() const
       
   195     {
       
   196         return !m_storage.model;
       
   197     }
       
   198 
       
   199     /* The members below are internal, not part of the public API, and
       
   200      * unsupported. Using them leads to undefined behavior. */
       
   201 
       
   202     inline QXmlName name() const;
       
   203     inline QXmlNodeModelIndex root() const;
       
   204     inline QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<QXmlNodeModelIndex> > iterate(const Axis axis) const;
       
   205     inline QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<QPatternist::Item> > sequencedTypedValue() const;
       
   206     inline QUrl documentUri() const;
       
   207     inline QUrl baseUri() const;
       
   208     inline NodeKind kind() const;
       
   209     inline bool isDeepEqual(const QXmlNodeModelIndex &other) const;
       
   210     inline DocumentOrder compareOrder(const QXmlNodeModelIndex &other) const;
       
   211     inline void sendNamespaces(QAbstractXmlReceiver *const receiver) const;
       
   212     inline QVector<QXmlName> namespaceBindings() const;
       
   213     inline QXmlName::NamespaceCode namespaceForPrefix(const QXmlName::PrefixCode prefix) const;
       
   214     inline QString stringValue() const;
       
   215     inline QPatternist::ItemTypePtr type() const;
       
   216     inline bool is(const QXmlNodeModelIndex &other) const;
       
   217 
       
   218     inline void reset()
       
   219     {
       
   220         m_storage.data = 0;
       
   221         m_storage.additionalData = 0;
       
   222         m_storage.model = 0;
       
   223     }
       
   224 
       
   225 private:
       
   226     static inline QXmlNodeModelIndex create(const qint64 d,
       
   227                                             const QAbstractXmlNodeModel *const nm)
       
   228     {
       
   229         QXmlNodeModelIndex n;
       
   230         n.m_storage.data = d;
       
   231         n.m_storage.model = nm;
       
   232         n.m_storage.additionalData = 0;
       
   233         return n;
       
   234     }
       
   235 
       
   236     static inline QXmlNodeModelIndex create(const qint64 data,
       
   237                                             const QAbstractXmlNodeModel *const nm,
       
   238                                             const qint64 addData)
       
   239     {
       
   240         QXmlNodeModelIndex n;
       
   241         n.m_storage.data = data;
       
   242         n.m_storage.model = nm;
       
   243         n.m_storage.additionalData = addData;
       
   244         return n;
       
   245     }
       
   246 
       
   247     inline QXmlNodeModelIndex(const QPatternist::NodeIndexStorage &storage) : m_storage(storage)
       
   248     {
       
   249     }
       
   250 
       
   251     friend class QAbstractXmlNodeModel;
       
   252     friend class QPatternist::Item;
       
   253     friend class QXmlItem;
       
   254     inline operator int() const; // Disable
       
   255 
       
   256     QPatternist::NodeIndexStorage m_storage;
       
   257 };
       
   258 
       
   259 Q_XMLPATTERNS_EXPORT uint qHash(const QXmlNodeModelIndex &index);
       
   260 
       
   261 inline bool qIsForwardIteratorEnd(const QXmlNodeModelIndex &item)
       
   262 {
       
   263     return item.isNull();
       
   264 }
       
   265 
       
   266 class Q_XMLPATTERNS_EXPORT QAbstractXmlNodeModel : public QSharedData
       
   267 {
       
   268 public:
       
   269     enum SimpleAxis
       
   270     {
       
   271         Parent,
       
   272         FirstChild,
       
   273         PreviousSibling,
       
   274         NextSibling
       
   275     };
       
   276 
       
   277     typedef QExplicitlySharedDataPointer<QAbstractXmlNodeModel> Ptr;
       
   278     typedef QList<Ptr> List;
       
   279 
       
   280     QAbstractXmlNodeModel();
       
   281     virtual ~QAbstractXmlNodeModel();
       
   282 
       
   283     virtual QUrl baseUri(const QXmlNodeModelIndex &ni) const = 0;
       
   284     virtual QUrl documentUri(const QXmlNodeModelIndex &ni) const = 0;
       
   285     virtual QXmlNodeModelIndex::NodeKind kind(const QXmlNodeModelIndex &ni) const = 0;
       
   286     virtual QXmlNodeModelIndex::DocumentOrder compareOrder(const QXmlNodeModelIndex &ni1,
       
   287                                                            const QXmlNodeModelIndex &ni2) const = 0;
       
   288     virtual QXmlNodeModelIndex root(const QXmlNodeModelIndex &n) const = 0;
       
   289     virtual QXmlName name(const QXmlNodeModelIndex &ni) const = 0;
       
   290     virtual QString stringValue(const QXmlNodeModelIndex &n) const = 0;
       
   291     virtual QVariant typedValue(const QXmlNodeModelIndex &n) const = 0;
       
   292 
       
   293     /* The members below are internal, not part of the public API, and
       
   294      * unsupported. Using them leads to undefined behavior. */
       
   295     virtual QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<QXmlNodeModelIndex> > iterate(const QXmlNodeModelIndex &ni, QXmlNodeModelIndex::Axis axis) const;
       
   296     virtual QPatternist::ItemIteratorPtr sequencedTypedValue(const QXmlNodeModelIndex &ni) const;
       
   297     virtual QPatternist::ItemTypePtr type(const QXmlNodeModelIndex &ni) const;
       
   298     virtual QXmlName::NamespaceCode namespaceForPrefix(const QXmlNodeModelIndex &ni,
       
   299                                                        const QXmlName::PrefixCode prefix) const;
       
   300     virtual bool isDeepEqual(const QXmlNodeModelIndex &ni1,
       
   301                              const QXmlNodeModelIndex &ni2) const;
       
   302     virtual void sendNamespaces(const QXmlNodeModelIndex &n,
       
   303                                 QAbstractXmlReceiver *const receiver) const;
       
   304     virtual QVector<QXmlName> namespaceBindings(const QXmlNodeModelIndex &n) const = 0;
       
   305 
       
   306 
       
   307     virtual QXmlNodeModelIndex elementById(const QXmlName &NCName) const = 0;
       
   308     virtual QVector<QXmlNodeModelIndex> nodesByIdref(const QXmlName &NCName) const = 0;
       
   309 
       
   310     enum NodeCopySetting
       
   311     {
       
   312         InheritNamespaces   = 0x1,
       
   313         PreserveNamespaces  = 0x2
       
   314     };
       
   315 
       
   316     typedef QFlags<NodeCopySetting> NodeCopySettings;
       
   317     virtual void copyNodeTo(const QXmlNodeModelIndex &node,
       
   318                             QAbstractXmlReceiver *const receiver,
       
   319                             const NodeCopySettings &) const;
       
   320 
       
   321     QSourceLocation sourceLocation(const QXmlNodeModelIndex &index) const;
       
   322 
       
   323 protected:
       
   324 
       
   325     virtual QXmlNodeModelIndex nextFromSimpleAxis(SimpleAxis axis, const QXmlNodeModelIndex &origin) const = 0;
       
   326     virtual QVector<QXmlNodeModelIndex> attributes(const QXmlNodeModelIndex &element) const = 0;
       
   327 
       
   328     QAbstractXmlNodeModel(QAbstractXmlNodeModelPrivate *d);
       
   329 
       
   330     inline QXmlNodeModelIndex createIndex(qint64 data) const
       
   331     {
       
   332         return QXmlNodeModelIndex::create(data, this);
       
   333     }
       
   334 
       
   335     inline QXmlNodeModelIndex createIndex(void * pointer,
       
   336                                           qint64 additionalData = 0) const
       
   337     {
       
   338         return QXmlNodeModelIndex::create(qptrdiff(pointer), this, additionalData);
       
   339     }
       
   340 
       
   341     inline QXmlNodeModelIndex createIndex(qint64 data,
       
   342                                           qint64 additionalData) const
       
   343     {
       
   344         return QXmlNodeModelIndex::create(data, this, additionalData);
       
   345     }
       
   346 
       
   347     QScopedPointer<QAbstractXmlNodeModelPrivate> d_ptr;
       
   348 private:
       
   349     friend class QPatternist::ItemMappingIterator<QXmlNodeModelIndex, QXmlNodeModelIndex, const QAbstractXmlNodeModel *, QExplicitlySharedDataPointer<QPatternist::DynamicContext> >;
       
   350     friend class QPatternist::SequenceMappingIterator<QXmlNodeModelIndex, QXmlNodeModelIndex, const QAbstractXmlNodeModel *>;
       
   351     friend class QPatternist::XsdValidatedXmlNodeModel;
       
   352 
       
   353     inline QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<QXmlNodeModelIndex> > mapToSequence(const QXmlNodeModelIndex &ni,
       
   354                                                                            const QExplicitlySharedDataPointer<QPatternist::DynamicContext> &) const;
       
   355 
       
   356     static inline bool isIgnorableInDeepEqual(const QXmlNodeModelIndex &n);
       
   357     Q_DISABLE_COPY(QAbstractXmlNodeModel)
       
   358 };
       
   359 
       
   360 Q_DECLARE_TYPEINFO(QXmlNodeModelIndex, Q_MOVABLE_TYPE);
       
   361 
       
   362 template<typename T> class QAbstractXmlForwardIterator;
       
   363 class QVariant;
       
   364 class QXmlItemPrivate;
       
   365 
       
   366 namespace QPatternist
       
   367 {
       
   368     class AtomicValue;
       
   369     class VariableLoader;
       
   370     class IteratorBridge;
       
   371     class ToQXmlItemMapper;
       
   372     class ToItemMapper;
       
   373 }
       
   374 
       
   375 class Q_XMLPATTERNS_EXPORT QXmlItem
       
   376 {
       
   377 public:
       
   378     typedef QAbstractXmlForwardIterator<QXmlItem> Iterator;
       
   379 
       
   380     QXmlItem();
       
   381     QXmlItem(const QXmlItem &other);
       
   382     QXmlItem(const QXmlNodeModelIndex &node);
       
   383     QXmlItem(const QVariant &atomicValue);
       
   384     ~QXmlItem();
       
   385     QXmlItem &operator=(const QXmlItem &other);
       
   386 
       
   387     bool isNull() const;
       
   388     bool isNode() const;
       
   389     bool isAtomicValue() const;
       
   390 
       
   391     QVariant toAtomicValue() const;
       
   392     QXmlNodeModelIndex toNodeModelIndex() const;
       
   393 
       
   394 private:
       
   395     friend class QPatternist::IteratorBridge;
       
   396     friend class QPatternist::VariableLoader;
       
   397     friend class QPatternist::ToQXmlItemMapper;
       
   398     friend class QPatternist::ToItemMapper;
       
   399     friend class QPatternist::Item;
       
   400 
       
   401     inline bool internalIsAtomicValue() const;
       
   402 
       
   403     inline QXmlItem(const QPatternist::Item &i);
       
   404 
       
   405     union
       
   406     {
       
   407         QPatternist::NodeIndexStorage   m_node;
       
   408 
       
   409         /* These two sits at the position of NodeIndexStorage::data.
       
   410          * NodeIndexStorage::{additionalData,model} are free. */
       
   411         const QPatternist::AtomicValue *m_atomicValue;
       
   412         QXmlItemPrivate *               m_ptr; /* Not currently used. */
       
   413     };
       
   414 };
       
   415 
       
   416 inline bool qIsForwardIteratorEnd(const QXmlItem &item)
       
   417 {
       
   418     return item.isNull();
       
   419 }
       
   420 
       
   421 Q_DECLARE_TYPEINFO(QXmlItem, Q_MOVABLE_TYPE);
       
   422 
       
   423 QT_END_NAMESPACE
       
   424 
       
   425 Q_DECLARE_METATYPE(QXmlItem) /* This macro must appear after QT_END_NAMESPACE. */
       
   426 
       
   427 QT_END_HEADER
       
   428 
       
   429 #endif