source: trunk/doc/src/snippets/xml/simpleparse/handler.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 4.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41/*
42handler.cpp
43
44Provides a handler for processing XML elements found by the reader.
45
46The handler stores the names of elements it finds and their indentation
47levels. The indentation level is initially set to zero.
48When a starting element is found, the indentation level is increased;
49when an ending element is found, the indentation level is decreased.
50*/
51
52#include <QDebug>
53#include "handler.h"
54
55/*!
56 Reset the state of the handler to ensure that new documents are
57 read correctly.
58
59 We return true to indicate that parsing should continue.
60*/
61
62bool Handler::startDocument()
63{
64 elementName.clear();
65 elementIndentation.clear();
66 indentationLevel = 0;
67
68 return true;
69}
70
71/*!
72 Process each starting element in the XML document.
73
74 Append the element name to the list of elements found; add its
75 corresponding indentation level to the list of indentation levels.
76
77 Increase the level of indentation by one column.
78
79 We return true to indicate that parsing should continue.
80*/
81
82bool Handler::startElement(const QString &, const QString &,
83 const QString & qName, const QXmlAttributes &)
84{
85 elementName.append(qName);
86 elementIndentation.append(indentationLevel);
87 indentationLevel += 1;
88
89 return true;
90}
91
92/*!
93 Process each ending element in the XML document.
94
95 Decrease the level of indentation by one column.
96
97 We return true to indicate that parsing should continue.
98*/
99
100bool Handler::endElement(const QString &, const QString &,
101 const QString &)
102{
103 indentationLevel -= 1;
104
105 return true;
106}
107
108/*!
109 Report a fatal parsing error, and return false to indicate to the reader
110 that parsing should stop.
111*/
112
113bool Handler::fatalError (const QXmlParseException & exception)
114{
115 qWarning() << QString("Fatal error on line %1, column %2: %3").arg(
116 exception.lineNumber()).arg(exception.columnNumber()).arg(
117 exception.message());
118
119 return false;
120}
121
122/*!
123 Return the list of element names found.
124*/
125
126QStringList& Handler::names ()
127{
128 return elementName;
129}
130
131/*!
132 Return the list of indentations used for each element found.
133*/
134
135QList<int>& Handler::indentations ()
136{
137 return elementIndentation;
138}
Note: See TracBrowser for help on using the repository browser.