~ubuntu-branches/ubuntu/trusty/soprano/trusty

« back to all changes in this revision

Viewing changes to soprano/node.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2013-07-16 12:26:34 UTC
  • mfrom: (1.1.46)
  • Revision ID: package-import@ubuntu.com-20130716122634-1q74dtzgmgjxg4dt
Tags: 2.9.3+dfsg1-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *
4
4
 * Copyright (C) 2006-2007 Daniele Galdi <daniele.galdi@gmail.com>
5
5
 * Copyright (C) 2007-2009 Sebastian Trueg <trueg@kde.org>
 
6
 * Copyright (C) 2012 Vishesh Handa <me@vhanda.in>
6
7
 *
7
8
 * This library is free software; you can redistribute it and/or
8
9
 * modify it under the terms of the GNU Library General Public
26
27
#include <QtCore/QString>
27
28
#include <QtCore/QUrl>
28
29
#include <QtCore/QDebug>
 
30
#include <QMutex>
29
31
 
30
32
 
31
33
 
389
391
QString Soprano::Node::resourceToN3( const QUrl& uri )
390
392
{
391
393
    QByteArray a = uri.toEncoded();
392
 
    return '<' + QString::fromAscii( a ) + '>';
 
394
    return '<' + QString::fromLatin1( a ) + '>';
393
395
}
394
396
 
395
397
 
402
404
        return "_:" + blank;
403
405
}
404
406
 
 
407
namespace {
 
408
    QHash<QChar, QString> createEscapeHash() {
 
409
        QHash<QChar, QString> escapeHash;
 
410
        escapeHash.insert( QChar::fromLatin1('\\'), QString::fromLatin1("\\\\") );
 
411
        escapeHash.insert( QChar::fromLatin1('\n'), QString::fromLatin1("\\n") );
 
412
        escapeHash.insert( QChar::fromLatin1('\r'), QString::fromLatin1("\\r") );
 
413
        escapeHash.insert( QChar::fromLatin1('\b'), QString::fromLatin1("\\b") );
 
414
        escapeHash.insert( QChar::fromLatin1('\t'), QString::fromLatin1("\\t") );
 
415
        escapeHash.insert( QChar::fromLatin1('\f'), QString::fromLatin1("\\f") );
 
416
        escapeHash.insert( QChar::fromLatin1('\"'), QString::fromLatin1("\\\"") );
 
417
        escapeHash.insert( QChar::fromLatin1('\''), QString::fromLatin1("\\\'") );
 
418
 
 
419
        return escapeHash;
 
420
    }
 
421
 
 
422
    QHash<QVariant::Type, QString> typeHash;
 
423
    QMutex typeMutex;
 
424
}
 
425
 
 
426
typedef QHash<QChar, QString> CharStringHash;
 
427
Q_GLOBAL_STATIC_WITH_ARGS( CharStringHash, g_escapeHash, (createEscapeHash()) );
405
428
 
406
429
// static
407
430
QString Soprano::Node::literalToN3( const LiteralValue& literal )
408
431
{
 
432
    QString str = literal.toString();
 
433
 
 
434
    QString s;
 
435
    s.reserve( str.size() );
 
436
 
409
437
    //
410
438
    // Escape control chars:  \t \b \n \r \f \\ \" \'
411
439
    //
412
 
    QString s = literal.toString();
413
 
 
414
 
    // FIXME: this can be done faster by running through the string only once.
415
 
    s.replace( '\\', "\\\\" );
416
 
    s.replace( '\"', "\\\"" );
417
 
    s.replace( '\'', "\\\'" );
418
 
    s.replace( '\n', "\\n" );
419
 
    s.replace( '\r', "\\r" );
420
 
    s.replace( '\b', "\\b" );
421
 
    s.replace( '\t', "\\t" );
422
 
    s.replace( '\f', "\\f" );
 
440
    foreach(const QChar& ch, str) {
 
441
        QHash< QChar, QString >::const_iterator it = g_escapeHash()->constFind( ch );
 
442
        if( it == g_escapeHash()->constEnd() ) {
 
443
            s.append( ch );
 
444
        }
 
445
        else {
 
446
            s.append( it.value() );
 
447
        }
 
448
    }
423
449
 
424
450
    if( literal.isPlain() ) {
425
451
        if ( literal.language().isEmpty() ) {
430
456
        }
431
457
    }
432
458
    else {
433
 
        return QString( "\"%1\"^^<%2>" )
434
 
            .arg( s, QString::fromAscii( literal.dataTypeUri().toEncoded() ) );
 
459
        QString type;
 
460
 
 
461
        QMutexLocker lock( &typeMutex );
 
462
        QHash<QVariant::Type, QString>::const_iterator it = typeHash.constFind( literal.type() );
 
463
        if( it == typeHash.constEnd() ) {
 
464
            type = QString::fromAscii( literal.dataTypeUri().toEncoded() );
 
465
            typeHash.insert( literal.type(), type );
 
466
        }
 
467
        else {
 
468
            type = it.value();
 
469
        }
 
470
 
 
471
        return QString::fromLatin1( "\"%1\"^^<%2>" ) .arg( s, type );
435
472
    }
436
473
}
437
474