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

source code of qtxmlpatterns/src/xmlpatterns/acceltree/qacceltreebuilder_p.h