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 #include <cmath>
00025
00026 #include "raul/Atom.hpp"
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.is_bool())
00045 return Atom(bool(node.to_bool()));
00046 else if (node.is_resource())
00047 return Atom(Atom::URI, node.world()->qualify(node.to_c_string()));
00048 else if (node.is_float())
00049 return Atom(node.to_float());
00050 else if (node.is_int())
00051 return Atom(node.to_int());
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 if (std::isnan(atom.get_float()) || std::isinf(atom.get_float()))
00077 break;
00078 os.precision(8);
00079 os << atom.get_float();
00080 str = os.str();
00081 if (str.find(".") == std::string::npos)
00082 str += ".0";
00083
00084 type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#decimal"));
00085 break;
00086 case Atom::BOOL:
00087
00088 if (atom.get_bool())
00089 str = "true";
00090 else
00091 str = "false";
00092 type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#boolean"));
00093 break;
00094 case Atom::URI:
00095 str = atom.get_uri();
00096 node = librdf_new_node_from_uri_string(world.world(), CUC(world.expand_uri(str).c_str()));
00097 break;
00098 case Atom::STRING:
00099 str = atom.get_string();
00100 break;
00101 case Atom::BLOB:
00102 case Atom::NIL:
00103 default:
00104
00105 break;
00106 }
00107
00108 if (!node && str != "")
00109 node = librdf_new_node_from_typed_literal(world.world(), CUC(str.c_str()), NULL, type);
00110
00111 return Redland::Node(world, node);
00112 }
00113
00114
00115 }
00116 }
00117
00118 #endif // RAUL_ATOM_RDF_HPP
00119