2 $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
4 This file is part of the Strigi project.
5 Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of
10 the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this library; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
25 #include <strigi/variant.h>
26 #include <strigi/fieldtypes.h>
28 #include <QtCore/QUrl>
29 #include <QtCore/QFile>
30 #include <QtCore/QFileInfo>
31 #include <QtCore/QUuid>
32 #include <QtCore/QDebug>
34 #include <Soprano/Index/CLuceneIndex>
35 #include <Soprano/Model>
36 #include <Soprano/Vocabulary/RDF>
39 #define STRIGI_NS "http://www.strigi.org/data#"
41 QUrl
Strigi::Soprano::Util::fieldUri( const std::string
& s
)
43 QString qKey
= QString::fromUtf8( s
.c_str() );
46 // very naive test for proper URI
47 if ( qKey
.contains( ":/" ) ) {
51 url
= STRIGI_NS
+ qKey
;
55 if ( url
.isRelative() ) {
56 url
.setScheme( "http" );
63 QUrl
Strigi::Soprano::Util::fileUrl( const std::string
& filename
)
65 QUrl url
= QUrl::fromLocalFile( QFileInfo( QString::fromUtf8( filename
.c_str() ) ).absoluteFilePath() );
66 url
.setScheme( "file" );
71 std::string
Strigi::Soprano::Util::fieldName( const QUrl
& uri
)
73 QString s
= uri
.toString();
74 if ( s
.startsWith( STRIGI_NS
) ) {
75 s
= s
.mid( strlen( STRIGI_NS
) );
77 return s
.toUtf8().data();
81 TString
Strigi::Soprano::Util::convertSearchField( const std::string
& field
)
83 if ( QString::fromUtf8( field
.c_str() ) == ::Soprano::Index::CLuceneIndex::defaultSearchField() ) {
84 return TString::fromUtf8( field
.c_str() );
86 else if ( QString( field
.c_str() ) == ::Soprano::Vocabulary::RDF::type().toString() ) {
87 // see sopranoindexwriter:addValue for details in this conversion
88 return TString( "http://strigi.sourceforge.net/fields#rdf-string-type" );
91 return fieldUri( field
).toString();
96 QUrl
Strigi::Soprano::Util::uniqueUri( const QString
& ns
, ::Soprano::Model
* model
)
100 QString uid
= QUuid::createUuid().toString();
101 uri
= ( ns
+ uid
.mid( 1, uid
.length()-2 ) );
102 } while ( model
->containsAnyStatement( ::Soprano::Statement( uri
, ::Soprano::Node(), ::Soprano::Node() ) ) );
107 Strigi::Variant
Strigi::Soprano::Util::nodeToVariant( const ::Soprano::Node
& node
)
109 if ( node
.isLiteral() ) {
110 switch( node
.literal().type() ) {
113 case QVariant::LongLong
: // FIXME: no perfect conversion :(
114 case QVariant::ULongLong
:
115 return Strigi::Variant( node
.literal().toInt() );
118 return Strigi::Variant( node
.literal().toBool() );
121 return Strigi::Variant( node
.literal().toString().toUtf8().data() );
125 qWarning() << "(Soprano::Util::nodeToVariant) cannot convert non-literal node to variant.";
126 return Strigi::Variant();