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>
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
402
404
return "_:" + blank;
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("\\\'") );
422
QHash<QVariant::Type, QString> typeHash;
426
typedef QHash<QChar, QString> CharStringHash;
427
Q_GLOBAL_STATIC_WITH_ARGS( CharStringHash, g_escapeHash, (createEscapeHash()) );
407
430
QString Soprano::Node::literalToN3( const LiteralValue& literal )
432
QString str = literal.toString();
435
s.reserve( str.size() );
410
438
// Escape control chars: \t \b \n \r \f \\ \" \'
412
QString s = literal.toString();
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() ) {
446
s.append( it.value() );
424
450
if( literal.isPlain() ) {
425
451
if ( literal.language().isEmpty() ) {
433
return QString( "\"%1\"^^<%2>" )
434
.arg( s, QString::fromAscii( literal.dataTypeUri().toEncoded() ) );
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 );
471
return QString::fromLatin1( "\"%1\"^^<%2>" ) .arg( s, type );