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 |
|
---|
48 | template <bool FromDocument>
|
---|
49 | AccelTreeBuilder<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 |
|
---|
73 | template <bool FromDocument>
|
---|
74 | void 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 |
|
---|
98 | template <bool FromDocument>
|
---|
99 | void 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 |
|
---|
126 | template <bool FromDocument>
|
---|
127 | void 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 |
|
---|
151 | template <bool FromDocument>
|
---|
152 | void 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 |
|
---|
166 | template <bool FromDocument>
|
---|
167 | void 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.")
|
---|
206 | .arg(formatKeyword("xml:id"),
|
---|
207 | formatData(normalized)),
|
---|
208 | FromDocument ? ReportContext::FODC0002 : ReportContext::XQDY0091,
|
---|
209 | this);
|
---|
210 | }
|
---|
211 | }
|
---|
212 | else if(m_context) // TODO
|
---|
213 | {
|
---|
214 | Q_ASSERT(m_context);
|
---|
215 |
|
---|
216 | /* If we're building from an XML Document(e.g, we're fed from QXmlStreamReader, we raise FODC0002,
|
---|
217 | * otherwise XQDY0091. */
|
---|
218 | m_context->error(QtXmlPatterns::tr("An %1-attribute must have a "
|
---|
219 | "valid %2 as value, which %3 isn't.").arg(formatKeyword("xml:id"),
|
---|
220 | formatType(m_namePool, BuiltinTypes::xsNCName),
|
---|
221 | formatData(value.toString())),
|
---|
222 | FromDocument ? ReportContext::FODC0002 : ReportContext::XQDY0091,
|
---|
223 | this);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | else
|
---|
227 | m_document->data.insert(m_preNumber, *m_attributeCompress.insert(value.toString()));
|
---|
228 | }
|
---|
229 |
|
---|
230 | template <bool FromDocument>
|
---|
231 | void AccelTreeBuilder<FromDocument>::characters(const QStringRef &ch)
|
---|
232 | {
|
---|
233 |
|
---|
234 | /* If a text node constructor appears by itself, a node needs to
|
---|
235 | * be created. Therefore, we set m_hasCharacters
|
---|
236 | * if we're the only node.
|
---|
237 | * However, if the text node appears as a child of a document or element
|
---|
238 | * node it is discarded if it's empty.
|
---|
239 | */
|
---|
240 | if(m_hasCharacters && m_isCharactersCompressed)
|
---|
241 | {
|
---|
242 | m_characters = CompressedWhitespace::decompress(m_characters);
|
---|
243 | m_isCharactersCompressed = false;
|
---|
244 | }
|
---|
245 |
|
---|
246 | m_characters += ch;
|
---|
247 |
|
---|
248 | m_isPreviousAtomic = false;
|
---|
249 | m_hasCharacters = !m_characters.isEmpty() || m_preNumber == -1; /* -1 is our start value. */
|
---|
250 | }
|
---|
251 |
|
---|
252 | template <bool FromDocument>
|
---|
253 | void AccelTreeBuilder<FromDocument>::whitespaceOnly(const QStringRef &ch)
|
---|
254 | {
|
---|
255 | Q_ASSERT(!ch.isEmpty());
|
---|
256 | Q_ASSERT(ch.toString().trimmed().isEmpty());
|
---|
257 |
|
---|
258 | /* This gets problematic due to how QXmlStreamReader works(which
|
---|
259 | * is the only one we get whitespaceOnly() events from). Namely, text intermingled
|
---|
260 | * with CDATA gets reported as individual Characters events, and
|
---|
261 | * QXmlStreamReader::isWhitespace() can return differently for each of those. However,
|
---|
262 | * it will occur very rarely, so this workaround of 1) mistakenly compressing 2) decompressing 3)
|
---|
263 | * appending, will happen infrequently.
|
---|
264 | */
|
---|
265 | if(m_hasCharacters)
|
---|
266 | {
|
---|
267 | if(m_isCharactersCompressed)
|
---|
268 | {
|
---|
269 | m_characters = CompressedWhitespace::decompress(m_characters);
|
---|
270 | m_isCharactersCompressed = false;
|
---|
271 | }
|
---|
272 |
|
---|
273 | m_characters.append(ch.toString());
|
---|
274 | }
|
---|
275 | else
|
---|
276 | {
|
---|
277 | /* We haven't received a text node previously. */
|
---|
278 | m_characters = CompressedWhitespace::compress(ch);
|
---|
279 | m_isCharactersCompressed = true;
|
---|
280 | m_isPreviousAtomic = false;
|
---|
281 | m_hasCharacters = true;
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | template <bool FromDocument>
|
---|
286 | void AccelTreeBuilder<FromDocument>::processingInstruction(const QXmlName &target,
|
---|
287 | const QString &data)
|
---|
288 | {
|
---|
289 | startStructure();
|
---|
290 | ++m_preNumber;
|
---|
291 | m_document->data.insert(m_preNumber, data);
|
---|
292 |
|
---|
293 | m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(),
|
---|
294 | currentParent(),
|
---|
295 | QXmlNodeModelIndex::ProcessingInstruction,
|
---|
296 | 0,
|
---|
297 | target));
|
---|
298 | ++m_size.top();
|
---|
299 | m_isPreviousAtomic = false;
|
---|
300 | }
|
---|
301 |
|
---|
302 | template <bool FromDocument>
|
---|
303 | void AccelTreeBuilder<FromDocument>::comment(const QString &content)
|
---|
304 | {
|
---|
305 | startStructure();
|
---|
306 | m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(), currentParent(), QXmlNodeModelIndex::Comment, 0));
|
---|
307 | ++m_preNumber;
|
---|
308 | m_document->data.insert(m_preNumber, content);
|
---|
309 | ++m_size.top();
|
---|
310 | }
|
---|
311 |
|
---|
312 | template <bool FromDocument>
|
---|
313 | void AccelTreeBuilder<FromDocument>::namespaceBinding(const QXmlName &nb)
|
---|
314 | {
|
---|
315 | /* Note, because attribute() sometimes generate namespaceBinding() calls, this function
|
---|
316 | * can be called after attributes, in contrast to what the class documentation says. This is ok,
|
---|
317 | * as long as we're not dealing with public API. */
|
---|
318 |
|
---|
319 | /* If we've received attributes, it means the element's size have changed and m_preNumber have advanced,
|
---|
320 | * so "reverse back" to the actual element. */
|
---|
321 | const AccelTree::PreNumber pn = m_preNumber - m_size.top();
|
---|
322 |
|
---|
323 | QVector<QXmlName> &nss = m_document->namespaces[pn];
|
---|
324 |
|
---|
325 | /* "xml" hasn't been declared for each node, AccelTree::namespaceBindings() adds it, so avoid it
|
---|
326 | * such that we don't get duplicates. */
|
---|
327 | if(nb.prefix() == StandardPrefixes::xml)
|
---|
328 | return;
|
---|
329 |
|
---|
330 | /* If we already have the binding, skip it. */
|
---|
331 | const int len = nss.count();
|
---|
332 | for(int i = 0; i < len; ++i)
|
---|
333 | {
|
---|
334 | if(nss.at(i).prefix() == nb.prefix())
|
---|
335 | return;
|
---|
336 | }
|
---|
337 |
|
---|
338 | nss.append(nb);
|
---|
339 | }
|
---|
340 |
|
---|
341 | template <bool FromDocument>
|
---|
342 | void AccelTreeBuilder<FromDocument>::startDocument()
|
---|
343 | {
|
---|
344 | /* If we have already received nodes, we can't add a document node. */
|
---|
345 | if(m_preNumber == -1) /* -1 is our start value. */
|
---|
346 | {
|
---|
347 | m_size.push(0);
|
---|
348 | m_document->basicData.append(AccelTree::BasicNodeData(0, -1, QXmlNodeModelIndex::Document, -1));
|
---|
349 | ++m_preNumber;
|
---|
350 | m_ancestors.push(m_preNumber);
|
---|
351 | }
|
---|
352 | else
|
---|
353 | ++m_skippedDocumentNodes;
|
---|
354 |
|
---|
355 | m_isPreviousAtomic = false;
|
---|
356 | }
|
---|
357 |
|
---|
358 | template <bool FromDocument>
|
---|
359 | void AccelTreeBuilder<FromDocument>::endDocument()
|
---|
360 | {
|
---|
361 | if(m_skippedDocumentNodes == 0)
|
---|
362 | {
|
---|
363 | /* Create text nodes, if we've received any. We do this only if we're the
|
---|
364 | * top node because if we're getting this event as being a child of an element,
|
---|
365 | * text nodes or atomic values can appear after us, and which must get
|
---|
366 | * merged with the previous text.
|
---|
367 | *
|
---|
368 | * We call startStructure() before we pop the ancestor, such that the text node becomes
|
---|
369 | * a child of this document node. */
|
---|
370 | startStructure();
|
---|
371 |
|
---|
372 | m_document->basicData.first().setSize(m_size.pop());
|
---|
373 | m_ancestors.pop();
|
---|
374 | }
|
---|
375 | else
|
---|
376 | --m_skippedDocumentNodes;
|
---|
377 |
|
---|
378 | m_isPreviousAtomic = false;
|
---|
379 | }
|
---|
380 |
|
---|
381 | template <bool FromDocument>
|
---|
382 | void AccelTreeBuilder<FromDocument>::atomicValue(const QVariant &value)
|
---|
383 | {
|
---|
384 | Q_UNUSED(value);
|
---|
385 | // TODO
|
---|
386 | }
|
---|
387 |
|
---|
388 | template <bool FromDocument>
|
---|
389 | QAbstractXmlNodeModel::Ptr AccelTreeBuilder<FromDocument>::builtDocument()
|
---|
390 | {
|
---|
391 | /* Create a text node, if we have received text in some way. */
|
---|
392 | startStructure();
|
---|
393 | m_document->printStats(m_namePool);
|
---|
394 |
|
---|
395 | return m_document;
|
---|
396 | }
|
---|
397 |
|
---|
398 | template <bool FromDocument>
|
---|
399 | NodeBuilder::Ptr AccelTreeBuilder<FromDocument>::create(const QUrl &baseURI) const
|
---|
400 | {
|
---|
401 | Q_UNUSED(baseURI);
|
---|
402 | return NodeBuilder::Ptr(new AccelTreeBuilder(QUrl(), baseURI, m_namePool, m_context));
|
---|
403 | }
|
---|
404 |
|
---|
405 | template <bool FromDocument>
|
---|
406 | void AccelTreeBuilder<FromDocument>::startOfSequence()
|
---|
407 | {
|
---|
408 | }
|
---|
409 |
|
---|
410 | template <bool FromDocument>
|
---|
411 | void AccelTreeBuilder<FromDocument>::endOfSequence()
|
---|
412 | {
|
---|
413 | }
|
---|
414 |
|
---|
415 | template <bool FromDocument>
|
---|
416 | const SourceLocationReflection *AccelTreeBuilder<FromDocument>::actualReflection() const
|
---|
417 | {
|
---|
418 | return this;
|
---|
419 | }
|
---|
420 |
|
---|
421 | template <bool FromDocument>
|
---|
422 | QSourceLocation AccelTreeBuilder<FromDocument>::sourceLocation() const
|
---|
423 | {
|
---|
424 | if(m_documentURI.isEmpty())
|
---|
425 | return QSourceLocation(QUrl(QLatin1String("AnonymousNodeTree")));
|
---|
426 | else
|
---|
427 | return QSourceLocation(m_documentURI);
|
---|
428 | }
|
---|
429 |
|
---|