File: variant.cpp

package info (click to toggle)
qxmlrpc 0.0.svn6-2
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 392 kB
  • ctags: 200
  • sloc: cpp: 1,621; python: 29; makefile: 23
file content (441 lines) | stat: -rw-r--r-- 10,871 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// vim:tabstop=4:shiftwidth=4:foldmethod=marker:expandtab:cinoptions=(s,U1,m1
// Copyright (C) 2005 Dmitry Poplavsky <dima@thekompany.com>

#include <QtXml>
#include <QtCore>

#include "xmlrpc/variant.h"

namespace  xmlrpc {

Variant::Variant()
: QVariant()
{
}

/** Constructs a copy of the variant, p, passed as the argument
 *  to this constructor. */
Variant::Variant( const Variant& val )
: QVariant( val )
{
}

Variant::Variant( const QVariant& val )
: QVariant( val )
{
}


/** Constructs a new variant with an integer value, val. */
Variant::Variant ( int val )
: QVariant( val )
{
}

/** Constructs a new variant with an integer value, val. */
Variant::Variant ( uint val )
: QVariant( val )
{
}

/** Constructs a new variant with a boolean value, val. */
Variant::Variant ( bool val )
: QVariant( val )
{
}


/** Constructs a new variant with a double value, val. */
Variant::Variant ( double val )
: QVariant( val )
{
}

/** Constructs a new variant with an QByteArray value, val. */
Variant::Variant ( const QByteArray & val )
: QVariant( val )
{
}

/** Constructs a new variant with a string value, val. */
Variant::Variant ( const QString & val )
: QVariant( val )
{
}

/** Constructs a new variant with a strings list value, val. */
Variant::Variant ( const QStringList & val )
: QVariant( val )
{
}

/** Constructs a new variant with a char value, val. XmlRPC
string type will be used. */ 
Variant::Variant ( const QChar & c )
: QVariant( QString(c) ) 
{ 
}

/** Constructs a new variant with a date time value, val. */
Variant::Variant ( const QDateTime & val )
: QVariant( val )
{
}

/** Constructs a new variant with a list value, val. */
Variant::Variant ( const QList<Variant> & listVal )
: QVariant()
{
    QList<QVariant> qVal;
    foreach( Variant val, listVal ) {
        qVal.append(val);
    }

    setValue(qVal);
}

/** Constructs a new variant with a map value, val. */
Variant::Variant ( const QMap<QString, Variant> & mapVal  )
: QVariant()
{
    QMap<QString, QVariant> qVal;

    QMapIterator<QString, Variant> i(mapVal);
    while (i.hasNext()) {
        i.next();
        qVal.insert( i.key(), i.value() );
    }

    setValue(qVal);
}


/**
 * Constructs a new variant from XmlRPC xml element node.
 * note name must be "value"
 */
Variant::Variant( const QDomElement& node )
{
    Q_ASSERT( node.nodeName() == "value" );

    if ( node.nodeName() == "value" ) {
        QDomElement data = node.firstChild().toElement();
        if ( data.isNull() ) {
            (*this) = Variant(node.text());
        } else {
            QString tagName = data.tagName();
            QString tagText = data.text();
            bool ok = true;

            if ( tagName == "i4" || tagName == "int" ) {
                (*this) = Variant( tagText.toInt(&ok) );
            } else 
            if ( tagName == "string" ) {
                (*this) = Variant( tagText );
            } else 
            if ( tagName == "double" ) {
                (*this) = Variant( tagText.toDouble(&ok) );
            } else 
            if ( tagName == "base64" ) {
                (*this) = Variant( QByteArray::fromBase64( tagText.toAscii() ) );
            } else 
            if ( tagName == "boolean" ) {
                bool val = tagText.toInt(&ok) == 1;
                (*this) = Variant( val );
            } else 
            if ( tagName == "dateTime.iso8601" ) {
                (*this) = Variant( decodeDateTimeIso8601(tagText,&ok) );
            } else
            if ( tagName == "struct" ) {
                (*this) = decodeStruct( data );
            } else 
            if ( tagName == "array" ) {
                (*this) = decodeArray( data );
            } else {
                qDebug() << "unexpected node:" << tagName;
                (*this) = Variant(QVariant::Invalid);
            }
        }
    } else {
        qDebug() << "'value' node expected, got"<<node.nodeName();
        (*this) = Variant(QVariant::Invalid);
    }
}

Variant::~Variant()
{
}


QString Variant::encodeDateTimeIso8601(QDateTime date)
{
    return date.toString("yyyyMMddThh:mm:ss");
}

QDateTime Variant::decodeDateTimeIso8601(QString sdate, bool *ok)
{
    // convert from XML-RPC yyyyMMddThh:mm:ss format to Qt's yyyy-MM-ddThh:mm:ss
    sdate = sdate.left(4)+'-'+sdate.mid(4,2)+'-'+sdate.mid(6);
    QDateTime res = QDateTime::fromString( sdate, Qt::ISODate );
    if ( ok ) 
        *ok = res.isValid();

    return res;
}

Variant Variant::decodeStruct( const QDomElement& node )
{
    Q_ASSERT( node.tagName() == "struct" );

    QMap<QString, Variant> res;
    QDomElement member = node.firstChild().toElement();
    while ( !member.isNull() ) {

        Q_ASSERT( member.tagName() == "member" );

        QString name;
        Variant value;
        QDomElement child = member.firstChild().toElement();
        while ( !child.isNull() ) {
            if ( child.tagName() == "name" ) {
                name = child.text();
            }
            if ( child.tagName() == "value" )
                value = Variant( child );

            child = child.nextSibling().toElement();
        }

        if ( !name.isNull() && value.isValid() ) {
            res[name] = value;
        }

        member = member.nextSibling().toElement();

    }

    return Variant(res);
}

Variant Variant::decodeArray( const QDomElement& node )
{
    Q_ASSERT( node.tagName() == "array" );
    Q_ASSERT( node.firstChildElement().tagName() == "data" );
    QList<Variant> res;
    QDomElement data = node.firstChildElement().firstChildElement(); // <array><data><value>


    while ( !data.isNull() ) {

        if ( data.tagName() == "value" ) {
            res.append( Variant( data ) );
        } else 
            qDebug() << "unexpected tag:<"+data.tagName()+">, <value> expected";


        Q_ASSERT( data.tagName() == "value" );

        data = data.nextSibling().toElement();
    }

    return Variant( res );
}

/**
 * Generate QDomElement with XmlRPC presentation of variant
 * value.
 */
QDomElement Variant::toDomElement( QDomDocument& doc ) const
{
    Q_ASSERT( isValid() );

    QDomElement valueEl = doc.createElement("value");
    QDomElement data;

    switch ( type() ) {
    case Int:
    case UInt:
        data = doc.createElement("int");
        data.appendChild( doc.createTextNode(toString()) );
        break;

    case String:
        data = doc.createElement("string");
        data.appendChild( doc.createTextNode(toString()) );
        break;

    case Double:
        data = doc.createElement("double");
        data.appendChild( doc.createTextNode(toString()) );
        break;

    case DateTime:
        data = doc.createElement("dateTime.iso8601");
        data.appendChild( doc.createTextNode( encodeDateTimeIso8601( toDateTime() ) ) );
        break;

    case Bool:
        data = doc.createElement("boolean");
        data.appendChild( doc.createTextNode( toBool() ? "1" : "0"  ) );
        break;

    case ByteArray:
        data = doc.createElement("base64");
        data.appendChild( doc.createTextNode( toByteArray().toBase64() ) );
        break;

    case List:
    case StringList:
        {
        
            data = doc.createElement("array");
            QDomElement arrayData = doc.createElement("data");
            data.appendChild( arrayData );
            QList<QVariant> list = toList();
            QList<QVariant>::iterator it;
            for ( it = list.begin(); it!=list.end(); ++it ) {
                arrayData.appendChild( Variant(*it).toDomElement(doc) );
            }
            break;
        }

    case Map:
        {
            data = doc.createElement("struct");
            QMap<QString,QVariant> map = toMap();
            QMap<QString,QVariant>::Iterator it;
            for ( it = map.begin(); it!=map.end(); ++it ) {
                QDomElement member = doc.createElement("member");
                data.appendChild( member );
    
                QDomElement name = doc.createElement("name");
                name.appendChild( doc.createTextNode( it.key() ) );
                QDomElement value = Variant(it.value()).toDomElement(doc);
    
                member.appendChild( name );
                member.appendChild( value );
            }
            break;
        }
    default: 
        {
            // this should never be called
            qWarning("type %s is not supported", typeName() );
            Q_ASSERT_X( false, "Variant::toDomElement", "type not supported" );
        }
     };

     if ( !data.isNull() ) {
         valueEl.appendChild( data );
     }

     return valueEl;

}


/**
 * Return  human readable presentation of xmlrpc::Variant value
 * in JSON like format.
 */
QString Variant::pprint()
{
    return pprint(0);
}

QString Variant::pprint( int column )
{
    column = qMin( column, 48 );
    
    QStringList items;

    switch ( type() ) {
    case Int:
    case UInt:
        items << QString::number( toInt() );
        break;

    case String:
        items << QString("\"%1\"").arg( toString() );
        break;

    case Double:
        items << QString::number( toDouble() );
        break;

    case DateTime:
        items << QString("'%1'").arg( toDateTime().toString() );
        break;

    case Bool:
        items << ( toBool() ? "true" : "false" );
        break;

    case ByteArray:
        {
            QString data = toByteArray().toBase64();
            if ( data.length() > 128 ) {
                data = data.left(64)+"...";
            }
            items << data;
            break;
        }

    case List:
    case StringList:
        {
            items << "[ ";
            QListIterator<QVariant> it( toList() );
            while( it.hasNext() ) {
                QString val = Variant(it.next()).pprint( column+2 );
                if ( it.hasNext() )
                    val += ","; 

                items << val+" ";
            }

            items << "]";
            break;
        }

    case Map:
        {
            items << "{ ";
            QMapIterator<QString,QVariant> it(toMap());
            while( it.hasNext() ) {
                it.next();
                QString val = Variant(it.value()).pprint(column+2);
                if ( it.hasNext() )
                    val += ","; 

                items << it.key()+"="+val+" ";
            }
            items << "}";
            break;
        }
    default:
        items << "unexpected element " + toString(); 
    };

    QString res;

    int columnPos = column;
    foreach ( QString item, items ) {
        if ( columnPos + item.length() > 128 ) {
            // move to the new line
            res += "\n";
            res += QString( column, ' ' );
            res += item;
            columnPos = column + item.length();
        } else {
            res += item;
            columnPos += item.length();
        }
    }

    return res;
}

}