source: trunk/src/xmlpatterns/acceltree/qacceltreebuilder.cpp@ 98

Last change on this file since 98 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 15.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/**
43 * @file
44 * @short This file is included by qacceltreebuilder_p.h.
45 * If you need includes in this file, put them in qacceltreebuilder_p.h, outside of the namespace.
46 */
47
48template <bool FromDocument>
49AccelTreeBuilder<FromDocument>::AccelTreeBuilder(const QUrl &docURI,
50 const QUrl &baseURI,
51 const NamePool::Ptr &np,
52 ReportContext *const context) : m_preNumber(-1)
53 , m_isPreviousAtomic(false)
54 , m_hasCharacters(false)
55 , m_isCharactersCompressed(false)
56 , m_namePool(np)
57 , m_document(new AccelTree(docURI, baseURI))
58 , m_skippedDocumentNodes(0)
59 , m_documentURI(docURI)
60 , m_context(context)
61{
62 Q_ASSERT(m_namePool);
63
64 /* TODO Perhaps we can merge m_ancestors and m_size
65 * into one, and store a struct for the two instead? */
66 m_ancestors.reserve(DefaultNodeStackSize);
67 m_ancestors.push(-1);
68
69 m_size.reserve(DefaultNodeStackSize);
70 m_size.push(0);
71}
72
73template <bool FromDocument>
74void AccelTreeBuilder<FromDocument>::startStructure()
75{
76 if(m_hasCharacters)
77 {
78 /* We create a node even if m_characters is empty.
79 * Remember that `text {""}' creates one text node
80 * with string value "". */
81
82 ++m_preNumber;
83 m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(),
84 currentParent(),
85 QXmlNodeModelIndex::Text,
86 m_isCharactersCompressed ? AccelTree::IsCompressed : 0));
87 m_document->data.insert(m_preNumber, m_characters);
88 ++m_size.top();
89
90 m_characters.clear(); /* We don't want it added twice. */
91 m_hasCharacters = false;
92
93 if(m_isCharactersCompressed)
94 m_isCharactersCompressed = false;
95 }
96}
97
98template <bool FromDocument>
99void AccelTreeBuilder<FromDocument>::item(const Item &it)
100{
101 Q_ASSERT(it);
102
103 if(it.isAtomicValue())
104 {
105 if(m_isPreviousAtomic)
106 {
107 m_characters += QLatin1Char(' ');
108 m_characters += it.stringValue();
109 }
110 else
111 {
112 m_isPreviousAtomic = true;
113 const QString sv(it.stringValue());
114
115 if(!sv.isEmpty())
116 {
117 m_characters += sv;
118 m_hasCharacters = true;
119 }
120 }
121 }
122 else
123 sendAsNode(it);
124}
125
126template <bool FromDocument>
127void AccelTreeBuilder<FromDocument>::startElement(const QXmlName &name)
128{
129 startStructure();
130
131 m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(), currentParent(), QXmlNodeModelIndex::Element, -1, name));
132
133 ++m_preNumber;
134 m_ancestors.push(m_preNumber);
135
136 ++m_size.top();
137 m_size.push(0);
138
139 /* With node constructors, we can receive names for which we have no namespace
140 * constructors, such as in the query '<xs:space/>'. Since the 'xs' prefix has no
141 * NamespaceConstructor in this case, we synthesize the namespace.
142 *
143 * In case we're constructing from an XML document we avoid the call because
144 * although it's redundant, it's on extra virtual call for each element. */
145 if(!FromDocument)
146 namespaceBinding(QXmlName(name.namespaceURI(), 0, name.prefix()));
147
148 m_isPreviousAtomic = false;
149}
150
151template <bool FromDocument>
152void AccelTreeBuilder<FromDocument>::endElement()
153{
154 startStructure();
155 const AccelTree::PreNumber index = m_ancestors.pop();
156 AccelTree::BasicNodeData &data = m_document->basicData[index];
157
158 /* Sub trees needs to be included in upper trees, so we add the count of this element
159 * to our parent. */
160 m_size[m_size.count() - 2] += m_size.top();
161
162 data.setSize(m_size.pop());
163 m_isPreviousAtomic = false;
164}
165
166template <bool FromDocument>
167void AccelTreeBuilder<FromDocument>::attribute(const QXmlName &name, const QStringRef &value)
168{
169 /* Attributes adds a namespace binding, so lets synthesize one.
170 *
171 * We optimize by checking whether we have a namespace for which a binding would
172 * be generated. Happens relatively rarely. */
173 if(name.hasPrefix())
174 namespaceBinding(QXmlName(name.namespaceURI(), 0, name.prefix()));
175
176 m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(), currentParent(), QXmlNodeModelIndex::Attribute, 0, name));
177 ++m_preNumber;
178 ++m_size.top();
179
180 m_isPreviousAtomic = false;
181
182 if(name.namespaceURI() == StandardNamespaces::xml && name.localName() == StandardLocalNames::id)
183 {
184 const QString normalized(value.toString().simplified());
185
186 if(QXmlUtils::isNCName(normalized))
187 {
188 const QXmlName::LocalNameCode id = m_namePool->allocateLocalName(normalized);
189
190 const int oldSize = m_document->m_IDs.count();
191 m_document->m_IDs.insert(id, currentParent());
192 /* We don't run the value through m_attributeCompress here, because
193 * the likelyhood of it deing identical to another attribute is
194 * very small. */
195 m_document->data.insert(m_preNumber, normalized);
196
197 /**
198 * In the case that we're called for doc-available(), m_context is
199 * null, and we need to flag somehow that we failed to load this
200 * document.
201 */
202 if(oldSize == m_document->m_IDs.count() && m_context) // TODO
203 {
204 Q_ASSERT(m_context);
205 m_context->error(QtXmlPatterns::tr("An %1-attribute with value %2 has already been declared.")