doc/src/snippets/xml/simpleparse/handler.cpp
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 /*
       
    43 handler.cpp
       
    44 
       
    45 Provides a handler for processing XML elements found by the reader.
       
    46 
       
    47 The handler stores the names of elements it finds and their indentation
       
    48 levels. The indentation level is initially set to zero.
       
    49 When a starting element is found, the indentation level is increased;
       
    50 when an ending element is found, the indentation level is decreased.
       
    51 */
       
    52 
       
    53 #include <QDebug>
       
    54 #include "handler.h"
       
    55 
       
    56 /*!
       
    57     Reset the state of the handler to ensure that new documents are
       
    58     read correctly.
       
    59 
       
    60     We return true to indicate that parsing should continue.
       
    61 */
       
    62 
       
    63 bool Handler::startDocument()
       
    64 {
       
    65     elementName.clear();
       
    66     elementIndentation.clear();
       
    67     indentationLevel = 0;
       
    68 
       
    69     return true;
       
    70 }
       
    71 
       
    72 /*!
       
    73     Process each starting element in the XML document.
       
    74 
       
    75     Append the element name to the list of elements found; add its
       
    76     corresponding indentation level to the list of indentation levels.
       
    77 
       
    78     Increase the level of indentation by one column.
       
    79 
       
    80     We return true to indicate that parsing should continue.
       
    81 */
       
    82 
       
    83 bool Handler::startElement(const QString &, const QString &,
       
    84     const QString & qName, const QXmlAttributes &)
       
    85 {
       
    86     elementName.append(qName);
       
    87     elementIndentation.append(indentationLevel);
       
    88     indentationLevel += 1;
       
    89 
       
    90     return true;
       
    91 }
       
    92 
       
    93 /*!
       
    94     Process each ending element in the XML document.
       
    95 
       
    96     Decrease the level of indentation by one column.
       
    97 
       
    98     We return true to indicate that parsing should continue.
       
    99 */
       
   100 
       
   101 bool Handler::endElement(const QString &, const QString &,
       
   102     const QString &)
       
   103 {
       
   104     indentationLevel -= 1;
       
   105 
       
   106     return true;
       
   107 }
       
   108 
       
   109 /*!
       
   110     Report a fatal parsing error, and return false to indicate to the reader
       
   111     that parsing should stop.
       
   112 */
       
   113 
       
   114 bool Handler::fatalError (const QXmlParseException & exception)
       
   115 {
       
   116     qWarning() << QString("Fatal error on line %1, column %2: %3").arg(
       
   117         exception.lineNumber()).arg(exception.columnNumber()).arg(
       
   118         exception.message());
       
   119 
       
   120     return false;
       
   121 }
       
   122 
       
   123 /*!
       
   124     Return the list of element names found.
       
   125 */
       
   126 
       
   127 QStringList& Handler::names ()
       
   128 {
       
   129     return elementName;
       
   130 }
       
   131 
       
   132 /*!
       
   133     Return the list of indentations used for each element found.
       
   134 */
       
   135 
       
   136 QList<int>& Handler::indentations ()
       
   137 {
       
   138     return elementIndentation;
       
   139 }