~ubuntu-branches/ubuntu/gutsy/soprano/gutsy

« back to all changes in this revision

Viewing changes to soprano/redland/redlandutil.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-12 14:43:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071012144348-yajzi51v4k23ahxf
Tags: 1.95.0~beta2-1ubuntu1
* Sync with Debian
* Add versioned build-dep on raptor

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of Soprano Project
3
 
 *
4
 
 * Copyright (C) 2006 Daniele Galdi <daniele.galdi@gmail.com>
5
 
 * Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
6
 
 *
7
 
 * This library is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Library General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2 of the License, or (at your option) any later version.
11
 
 *
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.
16
 
 *
17
 
 * You should have received a copy of the GNU Library General Public
18
 
 * License along with this library; see the file COPYING.LIB.  If
19
 
 * not, write to the Free Software Foundation, Inc., 51 Franklin
20
 
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
 
 */
22
 
 
23
 
#include "redlandutil.h"
24
 
 
25
 
#include "node.h"
26
 
#include "query.h"
27
 
#include "statement.h"
28
 
#include "redlandworld.h"
29
 
 
30
 
 
31
 
Soprano::Node Soprano::Redland::Util::createNode( librdf_node *node )
32
 
{
33
 
  if ( librdf_node_is_resource( node ) ) {
34
 
    librdf_uri *uri = librdf_node_get_uri( node );
35
 
    return Soprano::Node( QUrl::fromEncoded( (const char *)librdf_uri_as_string(uri), QUrl::StrictMode ) );
36
 
  }
37
 
  else if ( librdf_node_is_blank( node ) ) {
38
 
    return Soprano::Node( QUrl::fromEncoded( (const char *)librdf_node_get_blank_identifier( node ), QUrl::StrictMode ), Node::BlankNode );
39
 
  }
40
 
  else if ( librdf_node_is_literal( node ) ) {
41
 
    librdf_uri* datatype = librdf_node_get_literal_value_datatype_uri( node );
42
 
    if ( !datatype )
43
 
    {
44
 
      return Soprano::Node( Soprano::LiteralValue( (const char *)librdf_node_get_literal_value( node ) ) );
45
 
    }
46
 
    return Soprano::Node( Soprano::LiteralValue::fromString( QString::fromUtf8( (const char *)librdf_node_get_literal_value( node ) ),
47
 
                                                             QUrl::fromEncoded( (const char *)librdf_uri_as_string( datatype ),
48
 
                                                                                QUrl::StrictMode ) ),
49
 
                          QString::fromUtf8( librdf_node_get_literal_value_language( node ) ) );
50
 
  }
51
 
 
52
 
  return Soprano::Node();
53
 
}
54
 
 
55
 
librdf_node *Soprano::Redland::Util::createNode( const Node &node )
56
 
{
57
 
  librdf_world *world = World::self()->worldPtr();
58
 
 
59
 
  if ( node.isResource() ) {
60
 
      return librdf_new_node_from_uri_string( world, (unsigned char *)node.uri().toEncoded().data() );
61
 
  }
62
 
  else if ( node.isBlank() ) {
63
 
      return librdf_new_node_from_blank_identifier( world, (unsigned char *) node.uri().toEncoded().data() );
64
 
  }
65
 
  else if ( node.isLiteral() ) {
66
 
      return librdf_new_node_from_typed_literal( world,
67
 
                                                 (unsigned char *)node.literal().toString().toUtf8().data(),
68
 
                                                 node.language().toUtf8().data(),
69
 
                                                 librdf_new_uri( world, (const unsigned char*)node.dataType().toEncoded().data() ) );
70
 
  }
71
 
 
72
 
  return 0;
73
 
}
74
 
 
75
 
librdf_statement *Soprano::Redland::Util::createStatement( const Statement &statement )
76
 
{
77
 
  librdf_world *world = World::self()->worldPtr();
78
 
 
79
 
  librdf_node *subject = createNode( statement.subject() );
80
 
  librdf_node *predicate = createNode( statement.predicate() );
81
 
  librdf_node *object = createNode( statement.object() );
82
 
 
83
 
  return librdf_new_statement_from_nodes( world, subject, predicate, object );
84
 
}
85
 
 
86
 
Soprano::Statement Soprano::Redland::Util::createStatement( librdf_statement *st )
87
 
{
88
 
  librdf_node *subject = librdf_statement_get_subject( st );
89
 
  librdf_node *predicate = librdf_statement_get_predicate( st );
90
 
  librdf_node *object = librdf_statement_get_object( st );
91
 
 
92
 
  return Soprano::Statement( createNode( subject), createNode( predicate), createNode( object ) );
93
 
}
94
 
 
95
 
const char *Soprano::Redland::Util::queryType( const Query &query )
96
 
{
97
 
  if ( query.type() == Query::RDQL )
98
 
  {
99
 
    return "rdql";
100
 
  }
101
 
  else if (query.type() == Query::SPARQL )
102
 
  {
103
 
    return "sparql";
104
 
  }
105
 
  return 0L;
106
 
}
107
 
 
108
 
void Soprano::Redland::Util::freeNode( librdf_node* node )
109
 
{
110
 
  if( node ) {
111
 
    librdf_free_node( node );
112
 
  }
113
 
}
114
 
 
115
 
 
116
 
void Soprano::Redland::Util::freeStatement( librdf_statement* statement )
117
 
{
118
 
  if( statement ) {
119
 
    librdf_free_statement( statement );
120
 
  }
121
 
}