example/TwitterAuthApp/src/xmlParser.cpp
changeset 26 83d6a149c755
equal deleted inserted replaced
25:a180113055cb 26:83d6a149c755
       
     1 /**
       
     2 * Copyright (c) 2010 Sasken Communication Technologies Ltd.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "{License}"
       
     6 * which accompanies  this distribution, and is available
       
     7 * at the URL "{LicenseUrl}".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Narasimhulu Kavadapu, Sasken Communication Technologies Ltd - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 * Siddhartha Chandra, Sasken Communication Technologies Ltd
       
    14 * Description:
       
    15 * XML Parser class
       
    16 */
       
    17 #include "xmlParser.h"
       
    18 
       
    19 #include <QVariantList>
       
    20 ///////////////////////////////////////////////////////////////////////////////////////////////////
       
    21 // instance public functions
       
    22 
       
    23 FBXMLHandler::FBXMLHandler() : iError(false)
       
    24 {
       
    25     initWhiteSpaceHash();
       
    26 }
       
    27 
       
    28 FBXMLHandler::~FBXMLHandler()
       
    29 {
       
    30     iWhiteSpaceAndNewLineCharSet.clear();
       
    31 }
       
    32 
       
    33 ///////////////////////////////////////////////////////////////////////////////////////////////////
       
    34 // instance private functions
       
    35 
       
    36 void FBXMLHandler::initWhiteSpaceHash()
       
    37 {
       
    38     iWhiteSpaceAndNewLineCharSet.insert(QChar(0x20), true);
       
    39     iWhiteSpaceAndNewLineCharSet.insert(QChar(0x9), true);
       
    40     iWhiteSpaceAndNewLineCharSet.insert(QChar(0xA), true);
       
    41     iWhiteSpaceAndNewLineCharSet.insert(QChar(0xB), true);
       
    42     iWhiteSpaceAndNewLineCharSet.insert(QChar(0xC), true);
       
    43     iWhiteSpaceAndNewLineCharSet.insert(QChar(0xD), true);
       
    44     iWhiteSpaceAndNewLineCharSet.insert(QChar(0x85), true);
       
    45 }
       
    46 
       
    47 /* methods from QXmlErrorHandler */
       
    48 bool FBXMLHandler::error(const QXmlParseException& exception)
       
    49 {
       
    50     iParseErrorMessage = exception.message();
       
    51     iError = true;
       
    52     return false;
       
    53 }
       
    54 
       
    55 bool FBXMLHandler::fatalError(const QXmlParseException& exception)
       
    56 {
       
    57     iParseErrorMessage = exception.message();
       
    58     iError = true;
       
    59     return false;
       
    60 }
       
    61 
       
    62 /* methods from QXmlDefaultHandler */
       
    63 bool FBXMLHandler::startElement( const QString & /*namespaceURI*/,
       
    64                                  const QString & localName,
       
    65                                  const QString & /*qName*/,
       
    66                                  const QXmlAttributes & atts)
       
    67 {
       
    68     flushCharacters();
       
    69 
       
    70     QVariant item ;
       
    71 
       
    72     if  (atts.value("list").compare("true")==0)
       
    73     {
       
    74         item = QVariantList();
       
    75     }
       
    76 
       
    77     iStack.append(item);
       
    78     iNameStack.append(localName);
       
    79 
       
    80     return true;
       
    81 }
       
    82 
       
    83 bool FBXMLHandler::characters(const QString& aText)
       
    84 {
       
    85     iChars.append(aText);
       
    86     return true;
       
    87 }
       
    88 
       
    89 bool FBXMLHandler::endElement( const QString & /*namespaceURI*/,
       
    90                                const QString & /*localName*/,
       
    91                                const QString & /*qName*/ )
       
    92 {
       
    93     flushCharacters();
       
    94 
       
    95     QVariant c = iStack [iStack.count() - 1] ;
       
    96     QString name = topName();
       
    97 
       
    98     iStack.removeLast();
       
    99     iNameStack.removeLast();
       
   100 
       
   101     if (!iStack.count())
       
   102     {
       
   103         iRootObject = c;
       
   104         iRootName = name;
       
   105     }
       
   106     else
       
   107     {
       
   108         QVariant tC = iStack[iStack.count() - 1] ;
       
   109         if (tC.isNull())
       
   110         {
       
   111             tC = QVariantHash();
       
   112             iStack.replace(iStack.count() - 1, tC);
       
   113         }
       
   114 
       
   115         if (tC.type() == QVariant::List)
       
   116         {
       
   117             QVariantList list = tC.toList();
       
   118             list.append( c.toHash() );
       
   119 
       
   120             iStack.replace( iStack.count() - 1 , list);
       
   121 
       
   122         }
       
   123         else if (tC.type() == QVariant::Hash)
       
   124         {
       
   125             QVariantHash hash = tC.toHash();
       
   126             if (c.isNull())
       
   127             {
       
   128                 c  = QString("");
       
   129             }
       
   130             hash.insert( name, c );
       
   131 
       
   132             iStack.replace( iStack.count() - 1 , hash);
       
   133         }
       
   134     }
       
   135 
       
   136     return true;
       
   137 }
       
   138 
       
   139 
       
   140 /* */
       
   141 const QString& FBXMLHandler::topName() const
       
   142 {
       
   143     return iNameStack.last();
       
   144 }
       
   145 
       
   146 QVariant FBXMLHandler::topObject(bool /*aCreate*/)
       
   147 {
       
   148     QVariant item ;
       
   149     {
       
   150         iStack.replace(iStack.count() - 1, item);
       
   151     }
       
   152     return item;
       
   153 }
       
   154 
       
   155 QVariant FBXMLHandler::topContainer()
       
   156 {
       
   157     if (iStack.count() < 2)
       
   158     {
       
   159         return QVariant();
       
   160     }
       
   161     else
       
   162     {
       
   163 
       
   164         QVariant item = iStack[iStack.count() - 2 ];
       
   165         {
       
   166             iStack.replace( iStack.count() - 2 , item);
       
   167         }
       
   168 
       
   169         return item;
       
   170     }
       
   171 }
       
   172 
       
   173 void FBXMLHandler::flushCharacters()
       
   174 {
       
   175     for ( int i = 0; i < iChars.length(); i ++)
       
   176     {
       
   177         QChar uniChar = iChars.at(i);
       
   178 
       
   179         if (!iWhiteSpaceAndNewLineCharSet.contains(uniChar))
       
   180         {
       
   181 
       
   182             QVariant container;
       
   183             if (iStack.count() >= 2)
       
   184             {
       
   185                 container = iStack[iStack.count() - 2];
       
   186                 if (container.isNull())
       
   187                 {
       
   188                     container = QVariantHash();
       
   189                     iStack.replace( iStack.count() - 2 , container);
       
   190                 }
       
   191             }
       
   192 
       
   193             if (container.type() == QVariant::List)
       
   194             {                
       
   195                 QVariantHash object;
       
   196                 object.insert( topName(), iChars );
       
   197                 iStack.replace (iStack.count() - 1, object);
       
   198             } else {
       
   199                 QVariant object(iChars);
       
   200                 iStack.replace (iStack.count() - 1, object);
       
   201             }
       
   202             break;
       
   203         }
       
   204 
       
   205     }
       
   206 
       
   207     iChars.clear();
       
   208 }