Esempio n. 1
0
/*
 * raptor_abbrev_node_lookup:
 * @nodes: Sequence of nodes to search
 * @node_type: Raptor identifier type
 * @node_value: Node value to search with (using raptor_abbrev_node_matches).
 * @datatype: Literal datatype or NULL
 * @language: Literal language or NULL
 *
 * Return value: non-zero if @node matches the node described by the rest of
 *   the parameters or NULL on failure.
 */
raptor_abbrev_node* 
raptor_abbrev_node_lookup(raptor_avltree* nodes,
                          raptor_identifier_type node_type,
                          const void *node_value, raptor_uri *datatype,
                          const unsigned char *language)
{
  raptor_abbrev_node *lookup_node;
  raptor_abbrev_node *rv_node;

  /* Create a temporary node for search comparison. */
  lookup_node = raptor_new_abbrev_node(node_type, node_value, datatype, language);
  
  if(!lookup_node)
    return NULL;

  rv_node=(raptor_abbrev_node*)raptor_avltree_search(nodes, lookup_node);
  
  /* If not found, insert/return a new one */
  if(!rv_node) {
    
    if(raptor_avltree_add(nodes, lookup_node)) {
      /* Insert failed */
      raptor_free_abbrev_node(lookup_node);
      return NULL;
    } else {
      return lookup_node;
    }

  /* Found */
  } else {
    raptor_free_abbrev_node(lookup_node);
    return rv_node;
  }
}
/* create a new serializer */
static int
raptor_turtle_serialize_init(raptor_serializer* serializer, const char *name)
{
  raptor_turtle_context* context=(raptor_turtle_context*)serializer->context;
  raptor_uri *rdf_type_uri;

  context->nstack=raptor_new_namespaces_v2(serializer->world,
                                           (raptor_simple_message_handler)raptor_serializer_simple_error,
                                           serializer,
                                           1);
  if(!context->nstack)
    return 1;
  context->rdf_nspace=raptor_new_namespace(context->nstack,
                                           (const unsigned char*)"rdf",
                                           (const unsigned char*)raptor_rdf_namespace_uri,
                                           0);

  context->namespaces=raptor_new_sequence(NULL, NULL);

  context->subjects =
    raptor_new_avltree(serializer->world,
		       (raptor_data_compare_function)raptor_abbrev_subject_cmp,
		       (raptor_data_free_function)raptor_free_abbrev_subject, 0);

  context->blanks =
    raptor_new_avltree(serializer->world,
		       (raptor_data_compare_function)raptor_abbrev_subject_cmp,
		       (raptor_data_free_function)raptor_free_abbrev_subject, 0);

  context->nodes =
    raptor_new_avltree(serializer->world,
                       (raptor_data_compare_function)raptor_abbrev_node_cmp,
                       (raptor_data_free_function)raptor_free_abbrev_node, 0);

  rdf_type_uri = raptor_new_uri_for_rdf_concept_v2(serializer->world, "type");
  if(rdf_type_uri) {
    context->rdf_type = raptor_new_abbrev_node(serializer->world,
                                               RAPTOR_IDENTIFIER_TYPE_RESOURCE,
                                               rdf_type_uri, NULL, NULL);
    raptor_free_uri_v2(serializer->world, rdf_type_uri);
  } else
    context->rdf_type = NULL;

  context->rdf_xml_literal_uri=raptor_new_uri_v2(serializer->world, raptor_xml_literal_datatype_uri_string);
  context->rdf_first_uri=raptor_new_uri_v2(serializer->world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#first");
  context->rdf_rest_uri=raptor_new_uri_v2(serializer->world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest");
  context->rdf_nil_uri=raptor_new_uri_v2(serializer->world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#nil");

  if(!context->rdf_nspace || !context->namespaces ||
     !context->subjects || !context->blanks || !context->nodes ||
     !context->rdf_xml_literal_uri || !context->rdf_first_uri ||
     !context->rdf_rest_uri || !context->rdf_nil_uri || !context->rdf_type)
  {
    raptor_turtle_serialize_terminate(serializer);
    return 1;
  }

  /* Note: item 0 in the list is rdf:RDF's namespace */
  if(raptor_sequence_push(context->namespaces, context->rdf_nspace)) {
    raptor_turtle_serialize_terminate(serializer);
    return 1;
  }

  return 0;
}