00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef RAUL_ATOM_RDF_HPP
00019 #define RAUL_ATOM_RDF_HPP
00020
00021 #include <cstring>
00022 #include <string>
00023 #include <sstream>
00024
00025 #include CONFIG_H_PATH
00026
00027 #include <redlandmm/Node.hpp>
00028 #include <redlandmm/World.hpp>
00029
00030 #define CUC(x) ((const unsigned char*)(x))
00031
00032 namespace Raul {
00033
00038 namespace AtomRDF {
00039
00041 inline Atom
00042 node_to_atom(const Redland::Node& node)
00043 {
00044 if (node.type() == Redland::Node::RESOURCE)
00045 return Atom(node.to_c_string());
00046 else if (node.is_float())
00047 return Atom(node.to_float());
00048 else if (node.is_int())
00049 return Atom(node.to_int());
00050 else if (node.is_bool())
00051 return Atom(node.to_bool());
00052 else
00053 return Atom(node.to_c_string());
00054 }
00055
00056
00058 inline Redland::Node
00059 atom_to_node(Redland::World& world, const Atom& atom)
00060 {
00061 std::ostringstream os;
00062 std::string str;
00063 librdf_uri* type = NULL;
00064 librdf_node* node = NULL;
00065
00066 switch (atom.type()) {
00067 case Atom::INT:
00068 os << atom.get_int32();
00069 str = os.str();
00070
00071 type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#integer"));
00072 break;
00073 case Atom::FLOAT:
00074 os.precision(20);
00075 os << atom.get_float();
00076 str = os.str();
00077
00078 type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#decimal"));
00079 break;
00080 case Atom::BOOL:
00081
00082 if (atom.get_bool())
00083 str = "true";
00084 else
00085 str = "false";
00086 type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#boolean"));
00087 break;
00088 case Atom::STRING:
00089 str = atom.get_string();
00090 break;
00091 case Atom::BLOB:
00092 case Atom::NIL:
00093 default:
00094 std::cerr << "WARNING: Unserializable Atom!" << std::endl;
00095 }
00096
00097 if (str != "")
00098 node = librdf_new_node_from_typed_literal(world.world(), CUC(str.c_str()), NULL, type);
00099
00100 return Redland::Node(world, node);
00101 }
00102
00103
00104 }
00105 }
00106
00107 #endif // RAUL_ATOM_RDF_HPP
00108