Qt 4.8
Public Functions | Private Functions | List of all members
QXmlFormatter Class Reference

The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into formatted XML. More...

#include <qxmlformatter.h>

Inheritance diagram for QXmlFormatter:
QXmlSerializer QAbstractXmlReceiver

Public Functions

virtual void atomicValue (const QVariant &value)
 Reimplemented Function More...
 
virtual void attribute (const QXmlName &name, const QStringRef &value)
 Reimplemented Function More...
 
virtual void characters (const QStringRef &value)
 Reimplemented Function More...
 
virtual void comment (const QString &value)
 Reimplemented Function More...
 
virtual void endDocument ()
 Reimplemented Function More...
 
virtual void endElement ()
 Reimplemented Function More...
 
virtual void endOfSequence ()
 Reimplemented Function More...
 
int indentationDepth () const
 Returns the number of spaces QXmlFormatter will output for each indentation level. More...
 
virtual void item (const QPatternist::Item &item)
 
virtual void processingInstruction (const QXmlName &name, const QString &value)
 Reimplemented Function More...
 
 QXmlFormatter (const QXmlQuery &query, QIODevice *outputDevice)
 Constructs a formatter that uses the name pool and message handler in query, and writes the result to outputDevice as formatted XML. More...
 
void setIndentationDepth (int depth)
 Sets depth to be the number of spaces QXmlFormatter will output for level of indentation. More...
 
virtual void startDocument ()
 Reimplemented Function More...
 
virtual void startElement (const QXmlName &name)
 Reimplemented Function More...
 
virtual void startOfSequence ()
 Reimplemented Function More...
 
- Public Functions inherited from QXmlSerializer
const QTextCodeccodec () const
 Returns the codec being used by the serializer for encoding its XML output. More...
 
virtual void namespaceBinding (const QXmlName &nb)
 Reimplemented Function More...
 
QIODeviceoutputDevice () const
 Returns a pointer to the output device. More...
 
 QXmlSerializer (const QXmlQuery &query, QIODevice *outputDevice)
 Constructs a serializer that uses the name pool and message handler in query, and writes the output to outputDevice. More...
 
void setCodec (const QTextCodec *codec)
 Sets the codec the serializer will use for encoding its XML output. More...
 
- Public Functions inherited from QAbstractXmlReceiver
 QAbstractXmlReceiver ()
 Constructs an abstract xml receiver. More...
 
virtual void whitespaceOnly (const QStringRef &value)
 This function may be called instead of characters() if, and only if, value consists only of whitespace. More...
 
virtual ~QAbstractXmlReceiver ()
 Destroys the xml receiver. More...
 

Private Functions

void startFormattingContent ()
 

Additional Inherited Members

- Protected Functions inherited from QXmlSerializer
 QXmlSerializer (QAbstractXmlReceiverPrivate *d)
 
- Protected Functions inherited from QAbstractXmlReceiver
 QAbstractXmlReceiver (QAbstractXmlReceiverPrivate *d)
 
void sendAsNode (const QPatternist::Item &outputItem)
 Treats outputItem as a node and calls the appropriate function, e. More...
 
- Protected Variables inherited from QAbstractXmlReceiver
QScopedPointer< QAbstractXmlReceiverPrivated_ptr
 

Detailed Description

The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into formatted XML.

Note
This class or function is reentrant.
Since
4.4

QXmlFormatter is a subclass of QXmlSerializer that formats the XML output to make it easier for humans to read.

QXmlSerializer outputs XML without adding unnecessary whitespace. In particular, it does not add {newlines} and indentation. To make the XML output easier to read, QXmlFormatter adds newlines and indentation by adding, removing, and modifying XQuery Sequence{sequence nodes} that only consist of whitespace. It also modifies whitespace in other places where it is not significant; e.g., between attributes and in the document prologue.

For example, where the base class QXmlSerializer would output this:

<a><b/><c/><p>Some Text</p></a>

QXmlFormatter outputs this:

<a>
<b/>
<c/>
<p>Some Text</p>
</a>

If you just want to serialize your XML in a human-readable format, use QXmlFormatter as it is. The default indentation level is 4 spaces, but you can set your own indentation value setIndentationDepth().

The newlines and indentation added by QXmlFormatter are suitable for common formats, such as XHTML, SVG, or Docbook, where whitespace is not significant. However, if your XML will be used as input where whitespace is significant, then you must write your own subclass of QXmlSerializer or QAbstractXmlReceiver.

Note that using QXmlFormatter instead of QXmlSerializer will increase computational overhead and document storage size due to the insertion of whitespace.

Note also that the indentation style used by QXmlFormatter remains loosely defined and may change in future versions of Qt. If a specific indentation style is required then either use the base class QXmlSerializer directly, or write your own subclass of QXmlSerializer or QAbstractXmlReceiver. Alternatively, you can subclass QXmlFormatter and reimplement the callbacks there.

QXmlQuery query;
query.setQuery("doc('index.html')/html/body/p[1]");
QXmlFormatter formatter(query, myOutputDevice);
formatter.setIndentationDepth(2);
query.evaluateTo(&formatter);

Definition at line 58 of file qxmlformatter.h.

Constructors and Destructors

◆ QXmlFormatter()

QXmlFormatter::QXmlFormatter ( const QXmlQuery query,
QIODevice outputDevice 
)

Constructs a formatter that uses the name pool and message handler in query, and writes the result to outputDevice as formatted XML.

outputDevice is passed directly to QXmlSerializer's constructor.

See also
QXmlSerializer

Definition at line 146 of file qxmlformatter.cpp.

147  : QXmlSerializer(new QXmlFormatterPrivate(query, outputDevice))
148 {
149 }
QXmlSerializer(const QXmlQuery &query, QIODevice *outputDevice)
Constructs a serializer that uses the name pool and message handler in query, and writes the output t...

Functions

◆ atomicValue()

void QXmlFormatter::atomicValue ( const QVariant value)
virtual

Reimplemented Function

Reimplemented from QXmlSerializer.

Definition at line 250 of file qxmlformatter.cpp.

251 {
253  d->canIndent.top() = false;
255 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void atomicValue(const QVariant &value)
Reimplemented Function
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58

◆ attribute()

void QXmlFormatter::attribute ( const QXmlName name,
const QStringRef value 
)
virtual

Reimplemented Function

Reimplemented from QXmlSerializer.

Definition at line 208 of file qxmlformatter.cpp.

210 {
211  QXmlSerializer::attribute(name, value);
212 }
virtual void attribute(const QXmlName &name, const QStringRef &value)
Reimplemented Function

◆ characters()

void QXmlFormatter::characters ( const QStringRef value)
virtual

Reimplemented Function

Reimplemented from QXmlSerializer.

Definition at line 228 of file qxmlformatter.cpp.

229 {
231  d->isPreviousAtomic = false;
232  d->characterBuffer += value.toString();
233 }
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
#define Q_D(Class)
Definition: qglobal.h:2482
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58

◆ comment()

void QXmlFormatter::comment ( const QString value)
virtual

Reimplemented Function

Reimplemented from QXmlSerializer.

Definition at line 217 of file qxmlformatter.cpp.

218 {
222  d->canIndent.top() = true;
223 }
double d
Definition: qnumeric_p.h:62
virtual void comment(const QString &value)
Reimplemented Function
#define Q_D(Class)
Definition: qglobal.h:2482
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58
void startFormattingContent()

◆ endDocument()

void QXmlFormatter::endDocument ( )
virtual

Reimplemented Function

Reimplemented from QXmlSerializer.

Definition at line 268 of file qxmlformatter.cpp.

269 {
271 }
virtual void endDocument()
Reimplemented Function

◆ endElement()

void QXmlFormatter::endElement ( )
virtual

Reimplemented Function

Reimplemented from QXmlSerializer.

Definition at line 189 of file qxmlformatter.cpp.

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 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void endElement()
Reimplemented Function
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58
void startFormattingContent()

◆ endOfSequence()

void QXmlFormatter::endOfSequence ( )
virtual

Reimplemented Function

Reimplemented from QXmlSerializer.

Definition at line 284 of file qxmlformatter.cpp.

285 {
287 
288  /* Flush any buffered content. */
289  if(!d->characterBuffer.isEmpty())
290  QXmlSerializer::characters(QStringRef(&d->characterBuffer));
291 
292  d->write('\n');
294 }
double d
Definition: qnumeric_p.h:62
virtual void characters(const QStringRef &value)
Reimplemented Function
#define Q_D(Class)
Definition: qglobal.h:2482
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
virtual void endOfSequence()
Reimplemented Function
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58

◆ indentationDepth()

int QXmlFormatter::indentationDepth ( ) const

Returns the number of spaces QXmlFormatter will output for each indentation level.

The default is four.

See also
setIndentationDepth()

Definition at line 323 of file qxmlformatter.cpp.

324 {
325  Q_D(const QXmlFormatter);
326  return d->indentationDepth;
327 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58

◆ item()

void QXmlFormatter::item ( const QPatternist::Item item)
virtual
Warning
This function is not part of the public interface.

Reimplemented from QXmlSerializer.

Definition at line 299 of file qxmlformatter.cpp.

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 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
QString stringValue() const
Returns the string value of this Item.
Definition: qitem_p.h:302
virtual void item(const QPatternist::Item &item)
bool isAtomicValue() const
Determines whether this item is an atomic value, or a node.
Definition: qitem_p.h:335
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58
void startFormattingContent()
static bool isWhitespaceOnly(const QStringRef &string)
Determines whether consists only of whitespace. Characters considered whitespace are the ones for whi...

◆ processingInstruction()

void QXmlFormatter::processingInstruction ( const QXmlName name,
const QString value 
)
virtual

Reimplemented Function

Reimplemented from QXmlSerializer.

Definition at line 238 of file qxmlformatter.cpp.

240 {
244  d->canIndent.top() = true;
245 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void processingInstruction(const QXmlName &name, const QString &value)
Reimplemented Function
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58
void startFormattingContent()

◆ setIndentationDepth()

void QXmlFormatter::setIndentationDepth ( int  depth)

Sets depth to be the number of spaces QXmlFormatter will output for level of indentation.

The default is four.

See also
indentationDepth()

Definition at line 335 of file qxmlformatter.cpp.

336 {
338  d->indentationDepth = depth;
339 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58

◆ startDocument()

void QXmlFormatter::startDocument ( )
virtual

Reimplemented Function

Reimplemented from QXmlSerializer.

Definition at line 260 of file qxmlformatter.cpp.

261 {
263 }
virtual void startDocument()
Reimplemented Function

◆ startElement()

void QXmlFormatter::startElement ( const QXmlName name)
virtual

Reimplemented Function

Reimplemented from QXmlSerializer.

Definition at line 175 of file qxmlformatter.cpp.

176 {
179  ++d->currentDepth;
180  d->indentString.append(QString(d->indentationDepth, QLatin1Char(' ')));
181  d->canIndent.push(true);
182 
184 }
double d
Definition: qnumeric_p.h:62
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58
void startFormattingContent()
virtual void startElement(const QXmlName &name)
Reimplemented Function
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ startFormattingContent()

void QXmlFormatter::startFormattingContent ( )
inlineprivate
Warning
This function is not part of the public interface.

Definition at line 154 of file qxmlformatter.cpp.

Referenced by comment(), endElement(), item(), processingInstruction(), and startElement().

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 }
double d
Definition: qnumeric_p.h:62
virtual void characters(const QStringRef &value)
Reimplemented Function
#define Q_D(Class)
Definition: qglobal.h:2482
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58
static bool isWhitespaceOnly(const QStringRef &string)
Determines whether consists only of whitespace. Characters considered whitespace are the ones for whi...

◆ startOfSequence()

void QXmlFormatter::startOfSequence ( )
virtual

Reimplemented Function

Reimplemented from QXmlSerializer.

Definition at line 276 of file qxmlformatter.cpp.

277 {
279 }
virtual void startOfSequence()
Reimplemented Function

The documentation for this class was generated from the following files: