コード例 #1
0
ファイル: utilities.c プロジェクト: h0st/m3sec
void statement_in_tl(GSList ** list_, librdf_statement * statement)
{
	GSList * list=*list_;

	ssTriple_t * triple;

	librdf_node* sub_node ;
	librdf_node* pred_node;
	librdf_node* obj_node ;

	sub_node  = librdf_statement_get_subject(statement);
	pred_node = librdf_statement_get_predicate(statement);
	obj_node  = librdf_statement_get_object(statement);

	triple  =(ssTriple_t *) g_new0(ssTriple_t,1);

	triple->subject=	g_strdup(librdf_uri_as_string( librdf_node_get_uri(sub_node)));
	triple->predicate =	g_strdup(librdf_uri_as_string( librdf_node_get_uri(pred_node)));

	if (librdf_node_is_literal(obj_node))
	{	triple->object =  g_strdup(librdf_node_get_literal_value(obj_node));
		triple->objType = ssElement_TYPE_LIT;
	}
	else
	{	triple->object =  g_strdup(librdf_uri_as_string( librdf_node_get_uri(obj_node)));
		triple->objType = ssElement_TYPE_URI;
	}

	//printf ("statement to TL %s_%s_%s_%d\n", (char *)triple->subject, (char *)triple->predicate, (char *)triple->object, (bool *)triple->objType);
	list=g_slist_prepend(list, triple);
	///////////////////////////////////////////////////////////

	*list_=list;

}
コード例 #2
0
ファイル: mitkRdfStore.cpp プロジェクト: Cdebus/MITK
  RdfNode RdfStorePrivate::LibRdfNodeToRdfNode(librdf_node* node) const
  {
    RdfNode mitkNode;

    if (!node) return mitkNode;

    if (librdf_node_is_resource(node))
    {
      mitkNode.SetType(RdfNode::URI);
      librdf_uri *uri = librdf_node_get_uri(node);
      mitkNode.SetValue(LibRdfUriToRdfUri(uri).ToString());
    }
    else if (librdf_node_is_literal(node))
    {
      mitkNode.SetType(RdfNode::LITERAL);
      std::string value = (const char*) librdf_node_get_literal_value(node);
      if (!value.empty()) mitkNode.SetValue(value);
      librdf_uri* typeUri = librdf_node_get_literal_value_datatype_uri(node);
      if (typeUri) mitkNode.SetDatatype(LibRdfUriToRdfUri(typeUri));
    }
    else if (librdf_node_is_blank(node))
    {
      mitkNode.SetType(RdfNode::BLANK);
      std::string str = (const char*) librdf_node_get_blank_identifier(node);
      if (!str.empty()) mitkNode.SetValue(str);
    }
    return mitkNode;
  }
コード例 #3
0
void output_node(librdf_node* n)
{

    if (librdf_node_is_literal(n)) {
	std::cout << librdf_node_get_literal_value(n);
    }

    if (librdf_node_is_resource(n)) {
	librdf_uri* uri = librdf_node_get_uri(n);
	std::cout << librdf_uri_as_string(uri);
    }

}
コード例 #4
0
ファイル: rdf_query_rasqal.c プロジェクト: Distrotech/librdf
rasqal_literal*
redland_node_to_rasqal_literal(librdf_world* world, librdf_node *node)
{
  rasqal_literal* l;
  
  if(librdf_node_is_resource(node)) {
    raptor_uri* uri=(raptor_uri*)librdf_new_uri_from_uri(librdf_node_get_uri(node));
    l=rasqal_new_uri_literal(world->rasqal_world_ptr, uri); /* transfer uri ownership to literal */
  } else if(librdf_node_is_literal(node)) {
    unsigned char *string;
    librdf_uri *uri;
    unsigned char *new_string;
    char *new_language=NULL;
    raptor_uri *new_datatype=NULL;
    size_t len;

    string=librdf_node_get_literal_value_as_counted_string(node, &len);
    new_string=(unsigned char*)rasqal_alloc_memory(len+1);
    if(!new_string)
      return NULL;
    strcpy((char*)new_string, (const char*)string);

    string=(unsigned char*)librdf_node_get_literal_value_language(node);
    if(string) {
      new_language=(char*)rasqal_alloc_memory(strlen((const char*)string)+1);
      if(!new_language) {
        rasqal_free_memory((void*)new_string);
        return NULL;
      }
      strcpy((char*)new_language, (const char*)string);
    }
    uri=librdf_node_get_literal_value_datatype_uri(node);
    if(uri)
      new_datatype=(raptor_uri*)librdf_new_uri_from_uri(uri);
    /* transfer new_string,new_language,new_datatype ownership to literal */
    l = rasqal_new_string_literal(world->rasqal_world_ptr, (const unsigned char*)new_string, new_language, new_datatype, NULL);
  } else {
    unsigned char *blank=librdf_node_get_blank_identifier(node);
    unsigned char *new_blank;
    if(!blank)
      return NULL;
    new_blank=(unsigned char*)rasqal_alloc_memory(strlen((const char*)blank)+1);
    if(!new_blank)
      return NULL;
    strcpy((char*)new_blank, (const char*)blank);
    /* transfer new_blank ownership to literal */
    l = rasqal_new_simple_literal(world->rasqal_world_ptr, RASQAL_LITERAL_BLANK, (const unsigned char*)new_blank);
  }

  return l;
}
コード例 #5
0
ファイル: rdf_storage_tstore.c プロジェクト: njh/librdf
/**
 * librdf_storage_tstore_context_add_statement:
 * @storage: #librdf_storage object
 * @context_node: #librdf_node object
 * @statement: #librdf_statement statement to add
 *
 * Add a statement to a storage context.
 * 
 * Return value: non 0 on failure
 **/
static int
librdf_storage_tstore_context_add_statement(librdf_storage* storage,
                                            librdf_node* context_node,
                                            librdf_statement* statement) 
{
  librdf_storage_tstore_instance* context=(librdf_storage_tstore_instance*)storage->instance;
  librdf_node *subject_node=statement->subject;
  librdf_node *predicate_node=statement->predicate;
  librdf_node *object_node=statement->object;
  char *subject;
  char *predicate;
  char *object;
  rs_obj_type type;

  
  if(librdf_node_is_blank(subject_node)) {
    subject=(char*)librdf_node_get_blank_identifier(subject_node);
  } else
    subject=(char*)librdf_uri_as_string(librdf_node_get_uri(subject_node));
  

  predicate=(char*)librdf_uri_as_string(librdf_node_get_uri(predicate_node));
  
  /* Assumptions - FIXME */
  if(librdf_node_is_literal(object_node)) {
    object=(char*)librdf_node_get_literal_value(object_node);
    type = ObjLiteral;
  } else if(librdf_node_is_blank(object_node)) {
    object=(char*)librdf_node_get_blank_identifier(object_node);
    type = ObjURI;
  } else {
    object=(char*)librdf_uri_as_string(librdf_node_get_uri(object_node));
    type = ObjURI;
  }
  

  if(rs_assert_triple(context->rdfsql, subject, predicate, object, type))
    return 1;

  return 0;
}
コード例 #6
0
ファイル: rdf_storage_tstore.c プロジェクト: njh/librdf
/* FIXME returns an alloced triple pointing to shared strings */
static rs_triple*
librdf_storage_tstore_statement_as_rs_triple(librdf_statement *statement)
{
  librdf_node *subject_node=statement->subject;
  librdf_node *predicate_node=statement->predicate;
  librdf_node *object_node=statement->object;
  rs_triple* triple=LIBRDF_MALLOC(rs_triple, sizeof(rs_triple));

  if(subject_node) {
    if(librdf_node_is_blank(subject_node))
      triple->subject=(char*)librdf_node_get_blank_identifier(subject_node);
    else
      triple->subject=(char*)librdf_uri_as_string(librdf_node_get_uri(subject_node));
  } else
    triple->subject=NULL;

  if(predicate_node)
    triple->predicate=(char*)librdf_uri_as_string(librdf_node_get_uri(predicate_node));
  else
    triple->predicate=NULL;
  
  /* Assumptions - FIXME */
  triple->literal = 0;
  if(object_node) {
    if(librdf_node_is_literal(object_node)) {
      triple->object=(char*)librdf_node_get_literal_value(object_node);
      triple->literal = 1;
    } else if(librdf_node_is_blank(object_node)) {
      triple->object=(char*)librdf_node_get_blank_identifier(object_node);
    } else {
      triple->object=(char*)librdf_uri_as_string(librdf_node_get_uri(object_node));
    }
  } else
    triple->object=NULL;
  
  return triple;
}
コード例 #7
0
UT_Error IE_Imp_OpenDocument::_handleRDFStreams ()
{
#ifndef WITH_REDLAND
    return UT_OK;
#else
    UT_Error error = UT_OK;
    
    UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams()\n"));

    // // DEBUG.
    // {
    //     PD_DocumentRDFHandle rdf = getDoc()->getDocumentRDF();
    //     PD_DocumentRDFMutationHandle m = rdf->createMutation();
    //     m->add( PD_URI("http://www.example.com/foo#bar" ),
    //             PD_URI("http://www.example.com/foo#bar" ),
    //             PD_Object("http://www.example.com/foo#bar" ) );
    //     m->commit();
    //     rdf->dumpModel("added foo");
    // }
    

    
    RDFArguments args;
    librdf_model* model = args.model;

    // check if we can load a manifest.rdf file
    GsfInput* pRdfManifest = gsf_infile_child_by_name(m_pGsfInfile, "manifest.rdf");
    if (pRdfManifest)
    {
        UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams() have manifest.rdf\n"));
        error = _loadRDFFromFile( pRdfManifest, "manifest.rdf", &args );
        g_object_unref (G_OBJECT (pRdfManifest));
        if (error != UT_OK)
            return error;
    }

    // find other RDF/XML files referenced in the manifest
    const char* query_string = ""
        "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"
        "prefix odf: <http://docs.oasis-open.org/opendocument/meta/package/odf#> \n"
        "prefix odfcommon: <http://docs.oasis-open.org/opendocument/meta/package/common#> \n"
        "select ?subj ?fileName \n"
        " where { \n"
        "  ?subj rdf:type odf:MetaDataFile . \n"
        "  ?subj odfcommon:path ?fileName  \n"
        " } \n";

    librdf_uri*   base_uri = 0;
    librdf_query* query = librdf_new_query( args.world, "sparql", 0,
                                            (unsigned char*)query_string,
                                            base_uri );
    librdf_query_results* results = librdf_query_execute( query, model );

    if( !results )
    {
        // Query failed is a failure to execute the SPARQL,
        // in which case there might be results but we couldn't find them
        UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams() SPARQL query to find auxillary RDF/XML files failed! q:%p\n", query ));
        error = UT_ERROR;
    }
    else
    {
        UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams() aux RDF/XML file count:%d\n",
                     librdf_query_results_get_count( results )));

        // parse auxillary RDF/XML files too
        for( ; !librdf_query_results_finished( results ) ;
             librdf_query_results_next( results ))
        {
            librdf_node* fnNode = librdf_query_results_get_binding_value_by_name
                ( results, "fileName" );
            UT_DEBUGMSG(("_handleRDFStreams() fnNode:%p\n", fnNode ));
            std::string fn = toString(fnNode);
        
            UT_DEBUGMSG(("_handleRDFStreams() loading auxilary RDF/XML file from:%s\n",
                         fn.c_str()));
            GsfInput* pAuxRDF = gsf_infile_child_by_name(m_pGsfInfile, fn.c_str());
            if (pAuxRDF) {
                error = _loadRDFFromFile( pAuxRDF, fn.c_str(), &args );
                g_object_unref (G_OBJECT (pAuxRDF));
                if( error != UT_OK )
                    break;
            } else {
                UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
                return UT_ERROR;
            }
        }
        librdf_free_query_results( results );
    }
    librdf_free_query( query );

    UT_DEBUGMSG(("_handleRDFStreams() error:%d model.sz:%d\n",
                 error, librdf_model_size( model )));
    if( error != UT_OK )
    {
        return error;
    }
    
    // convert the redland model into native AbiWord RDF triples
    {
        PD_DocumentRDFHandle rdf = getDoc()->getDocumentRDF();
        PD_DocumentRDFMutationHandle m = rdf->createMutation();
        librdf_statement* statement = librdf_new_statement( args.world );
        librdf_stream* stream = librdf_model_find_statements( model, statement );

		while (!librdf_stream_end(stream))
        {
            librdf_statement* current = librdf_stream_get_object( stream );

            int objectType = PD_Object::OBJECT_TYPE_URI;
            
            std::string xsdType = "";
            if( librdf_node_is_blank( librdf_statement_get_object( current )))
            {
                objectType = PD_Object::OBJECT_TYPE_BNODE;
            }
            if( librdf_node_is_literal( librdf_statement_get_object( current )))
            {
                objectType = PD_Object::OBJECT_TYPE_LITERAL;
                if( librdf_uri* u = librdf_node_get_literal_value_datatype_uri(
                        librdf_statement_get_object( current )))
                {
                    xsdType = toString(u);
                }
            }

            if( DEBUG_RDF_IO )
            {
                UT_DEBUGMSG(("_handleRDFStreams() adding s:%s p:%s o:%s rotv:%d otv:%d ots:%s\n",
                             toString( librdf_statement_get_subject( current )).c_str(),
                             toString( librdf_statement_get_predicate( current )).c_str(),
                             toString( librdf_statement_get_object( current )).c_str(),
                             librdf_node_get_type(librdf_statement_get_object( current )),
                             objectType,
                             xsdType.c_str()
                                ));
            }
            
            m->add( PD_URI( toString( librdf_statement_get_subject( current ))),
                    PD_URI( toString( librdf_statement_get_predicate( current ))),
                    PD_Object( toString( librdf_statement_get_object( current )), objectType, xsdType ));

            // m->add( PD_URI( toString( librdf_statement_get_subject( current ))),
            //         PD_URI( toString( librdf_statement_get_predicate( current ))),
            //         PD_Object( toString( librdf_statement_get_object( current )),
            //                    objectType,
            //                    xsdType ));

            librdf_stream_next(stream);
        }
        
        librdf_free_stream( stream );
        librdf_free_statement( statement );
        // m->add( PD_URI("http://www.example.com/foo#bar" ),
        //         PD_URI("http://www.example.com/foo#bar" ),
        //         PD_Object("http://www.example.com/foo#bar" ) );
        m->commit();
    }

    if( DEBUG_RDF_IO )
    {
        getDoc()->getDocumentRDF()->dumpModel("Loaded RDF from ODF file");
    }
    return error;
#endif
}