/builddir/build/BUILD/raul-0.4.0/raul/AtomRDF.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
00003  * 
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  * 
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  * 
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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         // xsd:integer -> pretty integer literals in Turtle
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         // xsd:decimal -> pretty decimal (float) literals in Turtle
00078         type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#decimal"));
00079         break;
00080     case Atom::BOOL:
00081         // xsd:boolean -> pretty boolean literals in Turtle
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 } // namespace AtomRDF
00105 } // namespace Raul
00106 
00107 #endif // RAUL_ATOM_RDF_HPP
00108 

Generated on Wed Apr 9 08:14:41 2008 for RAUL by  doxygen 1.5.1