tests/auto/qxmlstream/qc14n.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 test suite 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 QT_FORWARD_DECLARE_CLASS(QIODevice)
       
    43 QT_FORWARD_DECLARE_CLASS(QString)
       
    44 
       
    45 #include <QtCore/QFlags>
       
    46 
       
    47 class QC14N
       
    48 {
       
    49 public:
       
    50     enum Option
       
    51     {
       
    52         IgnoreProcessingInstruction,
       
    53         IgnoreComments
       
    54     };
       
    55     typedef QFlags<Option> Options;
       
    56 
       
    57     static bool isEqual(QIODevice *const firstDocument,
       
    58                         QIODevice *const secondDocument,
       
    59                         QString *const message = 0,
       
    60                         const Options options = Options());
       
    61 
       
    62 private:
       
    63     static bool isDifferent(const QXmlStreamReader &r1,
       
    64                             const QXmlStreamReader &r2,
       
    65                             QString *const message);
       
    66     static bool isAttributesEqual(const QXmlStreamReader &r1,
       
    67                                   const QXmlStreamReader &r2,
       
    68                                   QString *const message);
       
    69 };
       
    70 
       
    71 #include <QXmlStreamReader>
       
    72 
       
    73 /*! \internal
       
    74 
       
    75   \a firstDocument and \a secondDocument must be pointers to opened devices.
       
    76  */
       
    77 bool QC14N::isEqual(QIODevice *const firstDocument,
       
    78                     QIODevice *const secondDocument,
       
    79                     QString *const message,
       
    80                     const Options options)
       
    81 {
       
    82     qDebug() << Q_FUNC_INFO;
       
    83     Q_ASSERT_X(firstDocument, Q_FUNC_INFO,
       
    84                "A valid QIODevice pointer must be supplied");
       
    85     Q_ASSERT_X(secondDocument, Q_FUNC_INFO,
       
    86                "A valid QIODevice pointer must be supplied");
       
    87     Q_ASSERT_X(firstDocument->isReadable(), Q_FUNC_INFO, "The device must be readable.");
       
    88     Q_ASSERT_X(secondDocument->isReadable(), Q_FUNC_INFO, "The device must be readable.");
       
    89                
       
    90     Q_ASSERT_X(options == Options(), Q_FUNC_INFO,
       
    91                "Not yet implemented.");
       
    92     Q_UNUSED(options);
       
    93 
       
    94     QXmlStreamReader r1(firstDocument);
       
    95     QXmlStreamReader r2(secondDocument);
       
    96 
       
    97     while(!r1.atEnd())
       
    98     {
       
    99         if(r1.error())
       
   100         {
       
   101             if(message)
       
   102                 *message = r1.errorString();
       
   103 
       
   104             return false;
       
   105         }
       
   106         else if(r2.error())
       
   107         {
       
   108             if(message)
       
   109                 *message = r1.errorString();
       
   110 
       
   111             return false;
       
   112         }
       
   113         else
       
   114         {
       
   115             if(isDifferent(r1, r2, message))
       
   116                 return true;
       
   117         }
       
   118 
       
   119         r1.readNext();
       
   120         r2.readNext();
       
   121     }
       
   122 
       
   123     if(!r2.atEnd())
       
   124     {
       
   125         if(message)
       
   126             *message = QLatin1String("Reached the end of the first document, while there was still content left in the second");
       
   127 
       
   128         return false;
       
   129     }
       
   130 
       
   131     /* And they lived happily ever after. */
       
   132     return true;
       
   133 }
       
   134 
       
   135 /*! \internal
       
   136  */
       
   137 bool QC14N::isAttributesEqual(const QXmlStreamReader &r1,
       
   138                               const QXmlStreamReader &r2,
       
   139                               QString *const message)
       
   140 {
       
   141     Q_UNUSED(message);
       
   142 
       
   143     const QXmlStreamAttributes &attrs1 = r1.attributes();
       
   144     const QXmlStreamAttributes &attrs2 = r2.attributes();
       
   145     const int len = attrs1.size();
       
   146 
       
   147     if(len != attrs2.size())
       
   148         return false;
       
   149 
       
   150     for(int i = 0; i < len; ++i)
       
   151     {
       
   152         if(!attrs2.contains(attrs1.at(i)))
       
   153             return false;
       
   154     }
       
   155 
       
   156     return true;
       
   157 }
       
   158 
       
   159 bool QC14N::isDifferent(const QXmlStreamReader &r1,
       
   160                         const QXmlStreamReader &r2,
       
   161                         QString *const message)
       
   162 {
       
   163     // TODO error reporting can be a lot better here.
       
   164     if(r1.tokenType() != r2.tokenType())
       
   165         return false;
       
   166 
       
   167     switch(r1.tokenType())
       
   168     {
       
   169         case QXmlStreamReader::NoToken:
       
   170         /* Fallthrough. */
       
   171         case QXmlStreamReader::StartDocument:
       
   172         /* Fallthrough. */
       
   173         case QXmlStreamReader::EndDocument:
       
   174         /* Fallthrough. */
       
   175         case QXmlStreamReader::DTD:
       
   176             return true;
       
   177         case QXmlStreamReader::Invalid:
       
   178             return false;
       
   179         case QXmlStreamReader::StartElement:
       
   180         {
       
   181             return r1.qualifiedName() == r2.qualifiedName()
       
   182                    /* Yes, the namespace test below should be redundant, but with it we
       
   183                     * trap namespace bugs in QXmlStreamReader, if any. */
       
   184                    && r1.namespaceUri() == r2.namespaceUri()
       
   185                    && isAttributesEqual(r1, r2, message);
       
   186 
       
   187         }
       
   188         case QXmlStreamReader::EndElement:
       
   189         {
       
   190             return r1.qualifiedName() == r2.qualifiedName()
       
   191                    && r1.namespaceUri() == r2.namespaceUri()
       
   192                    && r1.name() == r2.name();
       
   193         }
       
   194         case QXmlStreamReader::Characters:
       
   195         /* Fallthrough. */
       
   196         case QXmlStreamReader::Comment:
       
   197             return r1.text() == r2.text();
       
   198         case QXmlStreamReader::EntityReference:
       
   199         case QXmlStreamReader::ProcessingInstruction:
       
   200         {
       
   201             return r1.processingInstructionTarget() == r2.processingInstructionTarget() &&
       
   202                    r2.processingInstructionData() == r2.processingInstructionData();
       
   203 
       
   204         }
       
   205     }
       
   206 
       
   207     Q_ASSERT_X(false, Q_FUNC_INFO, "This line should never be reached");
       
   208     return false;
       
   209 }
       
   210