Qt 4.8
qxmlformatter.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <QtDebug>
43 
44 #include "qxmlformatter.h"
45 #include "qxpathhelper_p.h"
46 #include "qxmlserializer_p.h"
47 
49 
50 using namespace QPatternist;
51 
53 {
54 public:
55  inline QXmlFormatterPrivate(const QXmlQuery &q,
56  QIODevice *const outputDevice);
57 
62 
68 };
69 
71  QIODevice *const outputDevice) : QXmlSerializerPrivate(query, outputDevice)
72  , indentationDepth(4)
73  , currentDepth(0)
74 {
77  indentString[0] = QLatin1Char('\n');
78  canIndent.push(false);
79 }
80 
147  QIODevice *outputDevice) : QXmlSerializer(new QXmlFormatterPrivate(query, outputDevice))
148 {
149 }
150 
155 {
157 
158  if(QPatternist::XPathHelper::isWhitespaceOnly(d->characterBuffer))
159  {
160  if(d->canIndent.top())
161  QXmlSerializer::characters(QStringRef(&d->indentString));
162  }
163  else
164  {
165  if(!d->characterBuffer.isEmpty()) /* Significant data, we don't touch it. */
166  QXmlSerializer::characters(QStringRef(&d->characterBuffer));
167  }
168 
169  d->characterBuffer.clear();
170 }
171 
176 {
179  ++d->currentDepth;
180  d->indentString.append(QString(d->indentationDepth, QLatin1Char(' ')));
181  d->canIndent.push(true);
182 
184 }
185 
190 {
192  --d->currentDepth;
193  d->indentString.chop(d->indentationDepth);
194 
195  if(!d->hasClosedElement.top().second)
196  d->canIndent.top() = false;
197 
199 
200  d->canIndent.pop();
201  d->canIndent.top() = true;
203 }
204 
209  const QStringRef &value)
210 {
211  QXmlSerializer::attribute(name, value);
212 }
213 
217 void QXmlFormatter::comment(const QString &value)
218 {
222  d->canIndent.top() = true;
223 }
224 
229 {
231  d->isPreviousAtomic = false;
232  d->characterBuffer += value.toString();
233 }
234 
239  const QString &value)
240 {
244  d->canIndent.top() = true;
245 }
246 
251 {
253  d->canIndent.top() = false;
255 }
256 
261 {
263 }
264 
269 {
271 }
272 
277 {
279 }
280 
285 {
287 
288  /* Flush any buffered content. */
289  if(!d->characterBuffer.isEmpty())
290  QXmlSerializer::characters(QStringRef(&d->characterBuffer));
291 
292  d->write('\n');
294 }
295 
300 {
302 
303  if(item.isAtomicValue())
304  {
306  return;
307  else
308  {
309  d->canIndent.top() = false;
311  }
312  }
313 
314  QXmlSerializer::item(item);
315 }
316 
324 {
325  Q_D(const QXmlFormatter);
326  return d->indentationDepth;
327 }
328 
336 {
338  d->indentationDepth = depth;
339 }
340 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
virtual void atomicValue(const QVariant &value)
Reimplemented Function
double d
Definition: qnumeric_p.h:62
QString toString() const
Returns a copy of the string reference as a QString object.
Definition: qstring.cpp:8653
virtual void attribute(const QXmlName &name, const QStringRef &value)
Reimplemented Function
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QXmlFormatterPrivate(const QXmlQuery &q, QIODevice *const outputDevice)
virtual void characters(const QStringRef &value)
Reimplemented Function
virtual void comment(const QString &value)
Reimplemented Function
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual void endDocument()
Reimplemented Function
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void endElement()
Reimplemented Function
virtual void processingInstruction(const QXmlName &name, const QString &value)
Reimplemented Function
virtual void comment(const QString &value)
Reimplemented Function
QStack< bool > canIndent
virtual void startOfSequence()
Reimplemented Function
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QXmlSerializer class is an implementation of QAbstractXmlReceiver for transforming XQuery output ...
const char * name
virtual void attribute(const QXmlName &name, const QStringRef &value)
Reimplemented Function
The namespace for the internal API of QtXmlPatterns.
QString stringValue() const
Returns the string value of this Item.
Definition: qitem_p.h:302
virtual void endDocument()
Reimplemented Function
virtual void item(const QPatternist::Item &item)
void push(const T &t)
Adds element t to the top of the stack.
Definition: qstack.h:60
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
virtual void endElement()
Reimplemented Function
void setIndentationDepth(int depth)
Sets depth to be the number of spaces QXmlFormatter will output for level of indentation.
void resize(int size)
Sets the size of the string to size characters.
Definition: qstring.cpp:1353
bool isAtomicValue() const
Determines whether this item is an atomic value, or a node.
Definition: qitem_p.h:335
The QXmlName class represents the name of an XML node, in an efficient, namespace-aware way...
Definition: qxmlname.h:58
virtual void processingInstruction(const QXmlName &name, const QString &value)
Reimplemented Function
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
virtual void endOfSequence()
Reimplemented Function
virtual void characters(const QStringRef &value)
Reimplemented Function
virtual void atomicValue(const QVariant &value)
Reimplemented Function
QXmlFormatter(const QXmlQuery &query, QIODevice *outputDevice)
Constructs a formatter that uses the name pool and message handler in query, and writes the result to...
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58
virtual void startDocument()
Reimplemented Function
void startFormattingContent()
virtual void startOfSequence()
Reimplemented Function
virtual void endOfSequence()
Reimplemented Function
virtual void startElement(const QXmlName &name)
Reimplemented Function
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
static bool isWhitespaceOnly(const QStringRef &string)
Determines whether consists only of whitespace. Characters considered whitespace are the ones for whi...
virtual void startElement(const QXmlName &name)
Reimplemented Function
int indentationDepth() const
Returns the number of spaces QXmlFormatter will output for each indentation level.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
The QXmlQuery class performs XQueries on XML data, or on non-XML data modeled to look like XML...
Definition: qxmlquery.h:79
virtual void item(const QPatternist::Item &item)
virtual void startDocument()
Reimplemented Function