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
00060 inline Redland::Node
00061 atom_to_node(Redland::World& world, const Atom& atom)
00062 {
00063 std::ostringstream os;
00064 std::string str;
00065 librdf_uri* type = NULL;
00066 librdf_node* node = NULL;
00067
00068 switch (atom.type()) {
00069 case Atom::INT:
00070 os << atom.get_int32();
00071 str = os.str();
00072
00073 type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#integer"));
00074 break;
00075 case Atom::FLOAT:
00076 os.precision(8);
00077 os << atom.get_float();
00078 str = os.str();
00079 if (str.find(".") == std::string::npos)
00080 str += ".0";
00081
00082 type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#decimal"));
00083 break;
00084 case Atom::BOOL:
00085
00086 if (atom.get_bool())
00087 str = "true";
00088 else
00089 str = "false";
00090 type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#boolean"));
00091 break;
00092 case Atom::STRING:
00093 str = atom.get_string();
00094 break;
00095 case Atom::BLOB:
00096 case Atom::NIL:
00097 default:
00098
00099 break;
00100 }
00101
00102 if (str != "")
00103 node = librdf_new_node_from_typed_literal(world.world(), CUC(str.c_str()), NULL, type);
00104
00105 return Redland::Node(world, node);
00106 }
00107
00108
00109 }
00110 }
00111
00112 #endif // RAUL_ATOM_RDF_HPP
00113