source: trunk/src/xmlpatterns/api/qxmlformatter.cpp@ 477

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

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

File size: 9.1 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#include <QtDebug>
43
44#include "qxmlformatter.h"
45#include "qxpathhelper_p.h"
46#include "qxmlserializer_p.h"
47
48QT_BEGIN_NAMESPACE
49
50using namespace QPatternist;
51
52class QXmlFormatterPrivate : public QXmlSerializerPrivate
53{
54public:
55 inline QXmlFormatterPrivate(const QXmlQuery &q,
56 QIODevice *const outputDevice);
57
58 int indentationDepth;
59 int currentDepth;
60 QString characterBuffer;
61 QString indentString;
62
63 /**
64 * Whether we /have/ sent nodes like processing instructions and comments
65 * to QXmlSerializer.
66 */
67 QStack<bool> canIndent;
68};
69
70QXmlFormatterPrivate::QXmlFormatterPrivate(const QXmlQuery &query,
71 QIODevice *const outputDevice) : QXmlSerializerPrivate(query, outputDevice)
72 , indentationDepth(4)
73 , currentDepth(0)
74{
75 indentString.reserve(30);
76 indentString.resize(1);
77 indentString[0] = QLatin1Char('\n');
78 canIndent.push(false);
79}
80
81/*!
82 \class QXmlFormatter
83 \brief The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into formatted XML.
84 \reentrant
85 \since 4.4
86 \ingroup xml-tools
87
88 QXmlFormatter is a subclass of QXmlSerializer that formats the XML
89 output to make it easier for humans to read.
90
91 QXmlSerializer outputs XML without adding unnecessary whitespace.
92 In particular, it does not add \e {newlines} and indentation.
93 To make the XML output easier to read, QXmlFormatter adds \e{newlines}
94 and indentation by adding, removing, and modifying
95 \l{XQuery Sequence}{sequence nodes} that only consist of whitespace.
96 It also modifies whitespace in other places where it is not
97 significant; e.g., between attributes and in the document prologue.
98
99 For example, where the base class QXmlSerializer would
100 output this:
101
102 \quotefile doc/src/snippets/patternist/notIndented.xml
103
104 QXmlFormatter outputs this:
105
106 \quotefile doc/src/snippets/patternist/indented.xml
107
108 If you just want to serialize your XML in a human-readable
109 format, use QXmlFormatter as it is. The default indentation
110 level is 4 spaces, but you can set your own indentation value
111 setIndentationDepth().
112
113 The \e{newlines} and indentation added by QXmlFormatter are
114 suitable for common formats, such as XHTML, SVG, or Docbook,
115 where whitespace is not significant. However, if your XML will
116 be used as input where whitespace is significant, then you must
117 write your own subclass of QXmlSerializer or QAbstractXmlReceiver.
118
119 Note that using QXmlFormatter instead of QXmlSerializer will
120 increase computational overhead and document storage size due
121 to the insertion of whitespace.
122
123 Note also that the indentation style used by QXmlFormatter
124 remains loosely defined and may change in future versions of
125 Qt. If a specific indentation style is required then either
126 use the base class QXmlSerializer directly, or write your own
127 subclass of QXmlSerializer or QAbstractXmlReceiver.
128 Alternatively, you can subclass QXmlFormatter and reimplement
129 the callbacks there.
130
131 \snippet doc/src/snippets/code/src_xmlpatterns_api_qxmlformatter.cpp 0
132*/
133
134/*!
135 Constructs a formatter that uses the name pool and message
136 handler in \a query, and writes the result to \a outputDevice
137 as formatted XML.
138
139 \a outputDevice is passed directly to QXmlSerializer's constructor.
140
141 \sa QXmlSerializer
142 */
143QXmlFormatter::QXmlFormatter(const QXmlQuery &query,
144 QIODevice *outputDevice) : QXmlSerializer(new QXmlFormatterPrivate(query, outputDevice))
145{
146}
147
148/*!
149 \internal
150 */
151void QXmlFormatter::startFormattingContent()
152{
153 Q_D(QXmlFormatter);
154
155 if(QPatternist::XPathHelper::isWhitespaceOnly(d->characterBuffer))
156 {
157 if(d->canIndent.top())