9e4da59c5d0fc3c27ef2cb9445dd5a6dc200a7f4
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / acceltree / qacceltreebuilder_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 //
43 //  W A R N I N G
44 //  -------------
45 //
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.
49 //
50 // We mean it.
51
52 #ifndef Patternist_AccelTreeBuilder_H
53 #define Patternist_AccelTreeBuilder_H
54
55 #include <QSet>
56 #include <QStack>
57
58 #include <private/qxmlutils_p.h>
59 #include <private/qacceltree_p.h>
60 #include <private/qbuiltintypes_p.h>
61 #include <private/qcompressedwhitespace_p.h>
62 #include <private/qnamepool_p.h>
63 #include <private/qnodebuilder_p.h>
64 #include <private/qreportcontext_p.h>
65 #include <private/qsourcelocationreflection_p.h>
66 #include <private/qpatternistlocale_p.h>
67 #include <QtDebug>
68
69 QT_BEGIN_HEADER
70
71 QT_BEGIN_NAMESPACE
72
73 namespace QPatternist
74 {
75     /**
76      * @short Builds an AccelTree from a stream of XML/Item events
77      * received through the NodeBuilder interface.
78      *
79      * If FromDocument is @c true, it is assumed that AccelTreeBuilder is fed
80      * events from an XML document, otherwise it is assumed the events
81      * are from node constructor expressions.
82      *
83      * @author Frans Englich <frans.englich@nokia.com>
84      */
85     template<bool FromDocument>
86     class AccelTreeBuilder : public NodeBuilder
87                            , public SourceLocationReflection
88     {
89     public:
90         typedef QExplicitlySharedDataPointer<AccelTreeBuilder> Ptr;
91
92         /**
93          * Describes the memory relevant features the builder shall support.
94          */
95         enum Feature
96         {
97             NoneFeature,                ///< No special features are enabled.
98             SourceLocationsFeature = 1  ///< The accel tree builder will store source locations for each start element.
99         };
100         Q_DECLARE_FLAGS(Features, Feature)
101
102         /**
103          * @param context may be @c null.
104          */
105         AccelTreeBuilder(const QUrl &docURI,
106                          const QUrl &baseURI,
107                          const NamePool::Ptr &np,
108                          ReportContext *const context,
109                          Features features = NoneFeature);
110         virtual void startDocument();
111         virtual void endDocument();
112         virtual void startElement(const QXmlName &name);
113         void startElement(const QXmlName &name, qint64 line, qint64 column);
114         virtual void endElement();
115         virtual void attribute(const QXmlName &name, const QStringRef &value);
116         virtual void characters(const QStringRef &ch);
117         virtual void whitespaceOnly(const QStringRef &ch);
118         virtual void processingInstruction(const QXmlName &target,
119                                            const QString &data);
120         virtual void namespaceBinding(const QXmlName &nb);
121         virtual void comment(const QString &content);
122         virtual void item(const Item &it);
123
124         virtual QAbstractXmlNodeModel::Ptr builtDocument();
125         virtual NodeBuilder::Ptr create(const QUrl &baseURI) const;
126         virtual void startOfSequence();
127         virtual void endOfSequence();
128
129         inline AccelTree::Ptr builtDocument() const
130         {
131             return m_document;
132         }
133
134         virtual void atomicValue(const QVariant &value);
135
136         virtual const SourceLocationReflection *actualReflection() const;
137         virtual QSourceLocation sourceLocation() const;
138
139     private:
140         inline void startStructure();
141
142         inline AccelTree::PreNumber currentDepth() const
143         {
144             return m_ancestors.count() -1;
145         }
146
147         inline AccelTree::PreNumber currentParent() const
148         {
149             return m_ancestors.isEmpty() ? -1 : m_ancestors.top();
150         }
151
152         enum Constants
153         {
154             DefaultNodeStackSize = 10,
155             SizeIsEmpty = 0
156         };
157
158         AccelTree::PreNumber            m_preNumber;
159         bool                            m_isPreviousAtomic;
160         bool                            m_hasCharacters;
161         /**
162          * Whether m_characters has been run through
163          * CompressedWhitespace::compress().
164          */
165         bool                            m_isCharactersCompressed;
166         QString                         m_characters;
167         NamePool::Ptr                   m_namePool;
168         AccelTree::Ptr                  m_document;
169         QStack<AccelTree::PreNumber>    m_ancestors;
170         QStack<AccelTree::PreNumber>    m_size;
171
172         /** If we have already commenced a document, we don't want to
173          * add more document nodes. We keep track of them with this
174          * counter, which ensures that startDocument() and endDocument()
175          * are skipped consistently. */
176         AccelTree::PreNumber            m_skippedDocumentNodes;
177
178         /**
179          * All attribute values goes through this set such that we store only
180          * one QString for identical attribute values.
181          */
182         QSet<QString>                   m_attributeCompress;
183         const QUrl                      m_documentURI;
184         /**
185          * We don't store a reference pointer here because then we get a
186          * circular reference with GenericDynamicContext, when it stores us as
187          * a member.
188          */
189         ReportContext *const            m_context;
190
191         Features                        m_features;
192     };
193
194     Q_DECLARE_OPERATORS_FOR_FLAGS(AccelTreeBuilder<true>::Features)
195     Q_DECLARE_OPERATORS_FOR_FLAGS(AccelTreeBuilder<false>::Features)
196
197 #include "qacceltreebuilder_tpl_p.h"
198 }
199
200 QT_END_NAMESPACE
201
202 QT_END_HEADER
203
204 #endif