[go: up one dir, main page]

Menu

[6cc447]: / lib / parse.cpp  Maximize  Restore  History

Download this file

144 lines (128 with data), 4.5 kB

  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
/** @file "/owl_cpp/lib/parse.cpp"
part of owl_cpp project.
@n Distributed under the Boost Software License, Version 1.0; see doc/license.txt.
@n Copyright Mikhail K Levin 2010
*******************************************************************************/
#include "parse.hpp"
#include <iostream>
#include "boost/lexical_cast.hpp"
namespace b = boost;
#include "raptor2.h"
namespace owl_cpp { namespace{
/**@brief error message handler
*******************************************************************************/
void handle_error(void* data, raptor_log_message* msg) {
BOOST_THROW_EXCEPTION(
Rdf_parser::Err()
<< Rdf_parser::Err::msg_t(msg->text)
);
}
}//namespace anonymous
/*
*******************************************************************************/
Rdf_parser::Rdf_parser(const char* type, const string_t& base_uri)
: world_(0), parser_(0), base_uri_(base_uri) {
world_ = raptor_new_world();
if( ! world_ )
BOOST_THROW_EXCEPTION(Err()<<Err::msg_t("raptor initialization failure"));
raptor_world_set_log_handler(world_, 0, &handle_error);
parser_ = raptor_new_parser(world_, type);
if( ! parser_ )
BOOST_THROW_EXCEPTION(Err()<<Err::msg_t("raptor initialization failure"));
raptor_parser_set_option(parser_, RAPTOR_OPTION_STRICT, 0, 1);
}
/**
*******************************************************************************/
Rdf_parser::~Rdf_parser() {
if( parser_ ) raptor_free_parser(parser_);
if( world_ ) raptor_free_world(world_);
}
/*
*******************************************************************************/
Rdf_parser Rdf_parser::rdfxml(const string_t& base_uri) {
return Rdf_parser("rdfxml", base_uri);
}
/*
*******************************************************************************/
void Rdf_parser::parse(
std::istream& stream,
void* sink,
handle_statement_fun_t handle_statement_fun,
stop_parsing_fun_t stop_parsing_fun,
const unsigned base
) {
if( stream.fail() )
BOOST_THROW_EXCEPTION( Err() << Err::msg_t("read error") );
raptor_parser_set_statement_handler(
parser_,
sink,
reinterpret_cast<raptor_statement_handler>(handle_statement_fun)
);
const std::string base_str = b::lexical_cast<std::string>(base) + "_";
char base_c[10];
std::strcpy(base_c, base_str.c_str());
raptor_world_set_generate_bnodeid_parameters(world_, base_c, 0);
raptor_uri* uri = raptor_new_uri(world_, base_uri_.c_str());
raptor_parser_parse_start( parser_, uri );
std::string str;
for(unsigned ln = 1; stream; ++ln) {
if( stop_parsing_fun(sink) ) {
raptor_parser_parse_abort(parser_);
return;
}
std::getline(stream, str);
//raptor does not like empty chunks(lines) in the middle of document
str += '\n';
try{
const int i = raptor_parser_parse_chunk(
parser_,
reinterpret_cast<const unsigned char*>(str.data()),
stream ? str.size() : 0,
stream ? 0 : 1
);
//exceptions should originate from Raptor log handler
if( i != 0 ) BOOST_THROW_EXCEPTION(Err()<<Err::msg_t("unknown error"));
} catch(Err& e) {
BOOST_THROW_EXCEPTION(
Err()
<< Err::msg_t("RDF error")
<< Err::line_num_t(ln)
// << Err::str1_t(str)
<< Err::nested_t(b::copy_exception(e))
);
}
}
}
namespace{
/**
*******************************************************************************/
inline Term convert_term(
const raptor_term* rt
) {
switch (rt->type) {
case RAPTOR_TERM_TYPE_URI:
return Term(Resource, raptor_uri_as_string(rt->value.uri));
case RAPTOR_TERM_TYPE_LITERAL:
return Term(Literal, rt->value.literal.string);
case RAPTOR_TERM_TYPE_BLANK:
return Term(Anonymous, rt->value.blank.string);
case RAPTOR_TERM_TYPE_UNKNOWN:
default:
return Term(Unknown, "");
}
}
}//namespace anonymous
/*
*******************************************************************************/
triple_t Rdf_parser::convert_statement(const void* rs) {
const raptor_statement* stat = reinterpret_cast<const raptor_statement*>(rs);
triple_t triple(
convert_term(stat->subject),
convert_term(stat->predicate),
convert_term(stat->object)
);
return triple;
}
/**
*******************************************************************************/
}//namespace owl_cpp