/** * raptor_new_iostream_to_string: * @string_p: pointer to location to hold string * @length_p: pointer to location to hold length of string (or NULL) * @malloc_handler: pointer to malloc to use to make string (or NULL) * * Constructor - create a new iostream writing to a string. * * If @malloc_handler is null, raptor will allocate it using it's * own memory allocator. *@string_p is set to NULL on failure (and * *@length_p to 0 if @length_p is not NULL). * * Return value: new #raptor_iostream object or NULL on failure **/ RAPTOR_EXTERN_C raptor_iostream* raptor_new_iostream_to_string(void **string_p, size_t *length_p, void *(*malloc_handler)(size_t size)) { raptor_iostream* iostr; struct raptor_write_string_iostream_context* con; const raptor_iostream_handler2* handler2=&raptor_iostream_write_string_handler; const unsigned int mode=RAPTOR_IOSTREAM_MODE_WRITE; if(!raptor_iostream_check_handler(handler2, mode)) return NULL; iostr=(raptor_iostream*)RAPTOR_CALLOC(raptor_iostream, 1, sizeof(raptor_iostream)); if(!iostr) return NULL; con=(struct raptor_write_string_iostream_context*)RAPTOR_CALLOC(raptor_write_string_iostream_context, 1, sizeof(struct raptor_write_string_iostream_context)); if(!con) { RAPTOR_FREE(raptor_iostream, iostr); return NULL; } con->sb=raptor_new_stringbuffer(); if(!con->sb) { RAPTOR_FREE(raptor_iostream, iostr); RAPTOR_FREE(raptor_write_string_iostream_context, con); return NULL; } con->string_p=string_p; *string_p=NULL; con->length_p=length_p; if(length_p) *length_p=0; if(malloc_handler) con->malloc_handler=malloc_handler; else con->malloc_handler=raptor_alloc_memory; iostr->handler=handler2; iostr->user_data=(void*)con; iostr->mode = mode; if(iostr->handler->init && iostr->handler->init(iostr->user_data)) { raptor_free_iostream(iostr); return NULL; } return iostr; }
int raptor_rss_common_init(raptor_world* world) { int i; raptor_uri *namespace_uri; if(world->rss_common_initialised++) return 0; world->rss_namespaces_info_uris = (raptor_uri**)RAPTOR_CALLOC(raptor_uri* array, RAPTOR_RSS_NAMESPACES_SIZE, sizeof(raptor_uri*)); if(!world->rss_namespaces_info_uris) return -1; for(i = 0; i < RAPTOR_RSS_NAMESPACES_SIZE; i++) { const char *uri_string = raptor_rss_namespaces_info[i].uri_string; if(uri_string) { world->rss_namespaces_info_uris[i] = raptor_new_uri(world, (const unsigned char*)uri_string); if(!world->rss_namespaces_info_uris[i]) return -1; } } world->rss_types_info_uris = (raptor_uri**)RAPTOR_CALLOC(raptor_uri* array, RAPTOR_RSS_COMMON_SIZE, sizeof(raptor_uri*)); if(!world->rss_types_info_uris) return -1; for(i = 0; i< RAPTOR_RSS_COMMON_SIZE; i++) { int n = raptor_rss_items_info[i].nspace; namespace_uri = world->rss_namespaces_info_uris[n]; if(namespace_uri) { world->rss_types_info_uris[i] = raptor_new_uri_from_uri_local_name(world, namespace_uri, (const unsigned char*)raptor_rss_items_info[i].name); if(!world->rss_types_info_uris[i]) return -1; } } world->rss_fields_info_uris = (raptor_uri**)RAPTOR_CALLOC(raptor_uri* array, RAPTOR_RSS_FIELDS_SIZE, sizeof(raptor_uri*)); if(!world->rss_fields_info_uris) return -1; for(i = 0; i< RAPTOR_RSS_FIELDS_SIZE; i++) { namespace_uri = world->rss_namespaces_info_uris[raptor_rss_fields_info[i].nspace]; if(namespace_uri) { world->rss_fields_info_uris[i] = raptor_new_uri_from_uri_local_name(world, namespace_uri, (const unsigned char*)raptor_rss_fields_info[i].name); if(!world->rss_fields_info_uris[i]) return -1; } } return 0; }
/** * raptor_new_iostream_from_handler2: * @user_data: pointer to context information to pass in to calls * @handler2: pointer to handler methods * * Create a new iostream over a user-defined handler * * Return value: new #raptor_iostream object or NULL on failure **/ raptor_iostream* raptor_new_iostream_from_handler2(void *user_data, const raptor_iostream_handler2 * const handler2) { raptor_iostream* iostr; if(!raptor_iostream_check_handler(handler2, 0)) return NULL; iostr=(raptor_iostream*)RAPTOR_CALLOC(raptor_iostream, 1, sizeof(raptor_iostream)); if(!iostr) return NULL; iostr->handler=handler2; iostr->user_data=(void*)user_data; iostr->mode = raptor_iostream_calculate_modes(handler2); if(iostr->handler->init && iostr->handler->init(iostr->user_data)) { RAPTOR_FREE(raptor_iostream, iostr); return NULL; } return iostr; }
/** * raptor_new_iostream_from_handler: * @context: pointer to context information to pass in to calls * @handler: pointer to handler methods * * Create a new iostream over a user-defined handler. * * @deprecated: Use raptor_new_iostream_from_handler2() instead * * Return value: new #raptor_iostream object or NULL on failure **/ raptor_iostream* raptor_new_iostream_from_handler(void *user_data, const raptor_iostream_handler *handler) { raptor_iostream* iostr=NULL; raptor_iostream_handler2 *handler2; if(!handler) return NULL; handler2=(raptor_iostream_handler2*)RAPTOR_CALLOC(raptor_iostream_handler2, 1, sizeof(raptor_iostream_handler2*)); if(!handler2) return NULL; /* Copy V1 functions to V2 structure */ handler2->init = handler->init; handler2->finish = handler->finish; handler2->write_byte = handler->write_byte; handler2->write_bytes = handler->write_bytes; handler2->write_end = handler->write_end; iostr=raptor_new_iostream_from_handler2(user_data, handler2); if(iostr) { /* Ensure newly alloced structure is freed on iostream destruction */ iostr->flags |= RAPTOR_IOSTREAM_FLAGS_FREE_HANDLER; } else /* failure: so delete it now */ RAPTOR_FREE(raptor_iostream_handler2, handler2); return iostr; }
/** * raptor_new_iostream_to_filename: * @filename: Output filename to open and write to * * Constructor - create a new iostream writing to a filename. * * Return value: new #raptor_iostream object or NULL on failure **/ raptor_iostream* raptor_new_iostream_to_filename(const char *filename) { FILE *handle; raptor_iostream* iostr; const raptor_iostream_handler2* handler2=&raptor_iostream_write_filename_handler; const unsigned int mode=RAPTOR_IOSTREAM_MODE_WRITE; if(!raptor_iostream_check_handler(handler2, mode)) return NULL; handle=fopen(filename, "wb"); if(!handle) return NULL; iostr=(raptor_iostream*)RAPTOR_CALLOC(raptor_iostream, 1, sizeof(raptor_iostream)); if(!iostr) { fclose(handle); return NULL; } iostr->handler=handler2; iostr->user_data=(void*)handle; iostr->mode=mode; if(iostr->handler->init && iostr->handler->init(iostr->user_data)) { raptor_free_iostream(iostr); return NULL; } return iostr; }
/** * raptor_new_iostream_from_file_handle: * @handle: Input file_handle to open and read from * * Constructor - create a new iostream reading from a file_handle. * * The @handle must already be open for reading. * NOTE: This does not fclose the @handle when it is finished. * * Return value: new #raptor_iostream object or NULL on failure **/ raptor_iostream* raptor_new_iostream_from_file_handle(FILE *handle) { raptor_iostream* iostr; const raptor_iostream_handler2* handler2=&raptor_iostream_read_file_handle_handler; const unsigned int mode=RAPTOR_IOSTREAM_MODE_READ; if(!handle) return NULL; if(!raptor_iostream_check_handler(handler2, mode)) return NULL; iostr=(raptor_iostream*)RAPTOR_CALLOC(raptor_iostream, 1, sizeof(raptor_iostream)); if(!iostr) return NULL; iostr->handler=handler2; iostr->user_data=(void*)handle; iostr->mode = mode; if(iostr->handler->init && iostr->handler->init(iostr->user_data)) { RAPTOR_FREE(raptor_iostream, iostr); return NULL; } return iostr; }
static int raptor_sequence_ensure(raptor_sequence *seq, int capacity, int grow_at_front) { void **new_sequence; int offset; RAPTOR_ASSERT_OBJECT_POINTER_RETURN_VALUE(seq, raptor_sequence, 1); if(capacity && seq->capacity >= capacity) return 0; /* POLICY - minimum size */ if(capacity < 8) capacity=8; new_sequence=(void**)RAPTOR_CALLOC(ptrarray, capacity, sizeof(void*)); if(!new_sequence) return 1; offset=(grow_at_front ? (capacity-seq->capacity) : 0)+seq->start; if(seq->size) { memcpy(&new_sequence[offset], &seq->sequence[seq->start], sizeof(void*)*seq->size); RAPTOR_FREE(ptrarray, seq->sequence); } seq->start=offset; seq->sequence=new_sequence; seq->capacity=capacity; return 0; }
raptor_rss_field* raptor_rss_new_field(raptor_world* world) { raptor_rss_field* field = (raptor_rss_field*)RAPTOR_CALLOC(raptor_rss_field, 1, sizeof(*field)); if(field) field->world = world; return field; }
/** * raptor_new_id_set: * @world: raptor_world object * * INTERNAL - Constructor - create a new ID set. * * Return value: non 0 on failure **/ raptor_id_set* raptor_new_id_set(raptor_world* world) { raptor_id_set* set = (raptor_id_set*)RAPTOR_CALLOC(raptor_id_set, 1, sizeof(*set)); if(!set) return NULL; set->world = world; return set; }
/** * raptor_new_sequence: * @free_handler: handler to free a sequence item * @print_handler: handler to print a sequence item to a FILE* * * Constructor - create a new sequence with the given handlers. * * Return value: a new #raptor_sequence or NULL on failure **/ raptor_sequence* raptor_new_sequence(raptor_sequence_free_handler *free_handler, raptor_sequence_print_handler *print_handler) { raptor_sequence* seq=(raptor_sequence*)RAPTOR_CALLOC(raptor_sequence, 1, sizeof(raptor_sequence)); if(!seq) return NULL; seq->free_handler=free_handler; seq->print_handler=print_handler; return seq; }
/** * raptor_new_iostream_from_string: * @string: pointer to string * @length: length of string * * Constructor - create a new iostream reading from a string. * * Return value: new #raptor_iostream object or NULL on failure **/ raptor_iostream* raptor_new_iostream_from_string(void *string, size_t length) { raptor_iostream* iostr; struct raptor_read_string_iostream_context* con; const raptor_iostream_handler2* handler2=&raptor_iostream_read_string_handler; const unsigned int mode=RAPTOR_IOSTREAM_MODE_READ; if(!string) return NULL; if(!raptor_iostream_check_handler(handler2, mode)) return NULL; iostr=(raptor_iostream*)RAPTOR_CALLOC(raptor_iostream, 1, sizeof(raptor_iostream)); if(!iostr) return NULL; con=(struct raptor_read_string_iostream_context*)RAPTOR_CALLOC(raptor_read_string_iostream_context, 1, sizeof(struct raptor_read_string_iostream_context)); if(!con) { RAPTOR_FREE(raptor_iostream, iostr); return NULL; } con->string=string; con->length=length; iostr->handler=handler2; iostr->user_data=(void*)con; iostr->mode = mode; if(iostr->handler->init && iostr->handler->init(iostr->user_data)) { raptor_free_iostream(iostr); return NULL; } return iostr; }
/** * raptor_new_sequence_v2: * @free_handler: handler to free a sequence item * @print_handler: handler to print a sequence item to a FILE* * @handler_context: context information to pass to free/print handlers * * Constructor - create a new sequence with the given handlers and handler context. * * Return value: a new #raptor_sequence or NULL on failure **/ raptor_sequence* raptor_new_sequence_v2(raptor_sequence_free_handler_v2 *free_handler, raptor_sequence_print_handler_v2 *print_handler, void *handler_context) { raptor_sequence* seq=(raptor_sequence*)RAPTOR_CALLOC(raptor_sequence, 1, sizeof(raptor_sequence)); if(!seq) return NULL; seq->free_handler_v2=free_handler; seq->print_handler_v2=print_handler; seq->handler_context=handler_context; return seq; }
/** * raptor_new_turtle_writer: * @world: raptor_world object * @base_uri: Base URI for the writer (or NULL) * @write_base_uri: non-0 to write '@base' directive to output * @nstack: Namespace stack for the writer to start with (or NULL) * @iostr: I/O stream to write to * * Constructor - Create a new Turtle Writer writing Turtle to a raptor_iostream * * Return value: a new #raptor_turtle_writer object or NULL on failure **/ raptor_turtle_writer* raptor_new_turtle_writer(raptor_world* world, raptor_uri* base_uri, int write_base_uri, raptor_namespace_stack *nstack, raptor_iostream* iostr) { raptor_turtle_writer* turtle_writer; RAPTOR_CHECK_CONSTRUCTOR_WORLD(world); if(!nstack || !iostr) return NULL; raptor_world_open(world); turtle_writer = (raptor_turtle_writer*)RAPTOR_CALLOC(raptor_turtle_writer, 1, sizeof(*turtle_writer)); if(!turtle_writer) return NULL; turtle_writer->world = world; turtle_writer->nstack_depth = 0; turtle_writer->nstack = nstack; if(!turtle_writer->nstack) { turtle_writer->nstack = raptor_new_namespaces(world, 1); turtle_writer->my_nstack = 1; } turtle_writer->iostr = iostr; turtle_writer->flags = 0; turtle_writer->indent = 2; turtle_writer->base_uri = NULL; /* Ensure any initial base URI is not written relative */ if(base_uri && write_base_uri) raptor_turtle_writer_base(turtle_writer, base_uri); turtle_writer->base_uri = base_uri; turtle_writer->xsd_boolean_uri = raptor_new_uri(world, (const unsigned char*)"http://www.w3.org/2001/XMLSchema#boolean"); turtle_writer->xsd_decimal_uri = raptor_new_uri(world, (const unsigned char*)"http://www.w3.org/2001/XMLSchema#decimal"); turtle_writer->xsd_double_uri = raptor_new_uri(world, (const unsigned char*)"http://www.w3.org/2001/XMLSchema#double"); turtle_writer->xsd_integer_uri = raptor_new_uri(world, (const unsigned char*)"http://www.w3.org/2001/XMLSchema#integer"); return turtle_writer; }
/* * raptor_new_rss_block: * @world: world * @type: RSS block type * @block_term: Block subject term (shared) * * INTERNAL - Create a new RSS Block such as <author> etc * * Return value: new RSS block or NULL on failure */ raptor_rss_block* raptor_new_rss_block(raptor_world* world, raptor_rss_type type, raptor_term* block_term) { raptor_rss_block *block; block = (raptor_rss_block*)RAPTOR_CALLOC(raptor_rss_block, 1, sizeof(*block)); if(block) { block->rss_type = type; block->node_type = world->rss_types_info_uris[type]; block->identifier = raptor_term_copy(block_term); } return block; }
raptor_rss_item* raptor_new_rss_item(raptor_world* world) { raptor_rss_item* item; item = (raptor_rss_item*)RAPTOR_CALLOC(raptor_rss_item, 1, sizeof(*item)); if(!item) return NULL; item->world = world; item->triples = raptor_new_sequence((raptor_data_free_handler)raptor_free_statement, (raptor_data_print_handler)raptor_statement_print); if(!item->triples) { RAPTOR_FREE(raptor_rss_item, item); return NULL; } return item; }
/* * raptor_abbrev_subject implementation * * The subject of triples, with all predicates and values * linked from them. * **/ raptor_abbrev_subject* raptor_new_abbrev_subject(raptor_abbrev_node* node) { raptor_abbrev_subject* subject; if(!(node->type == RAPTOR_IDENTIFIER_TYPE_RESOURCE || node->type == RAPTOR_IDENTIFIER_TYPE_ANONYMOUS || node->type == RAPTOR_IDENTIFIER_TYPE_ORDINAL)) { RAPTOR_FATAL1("Subject node must be a resource, blank, or ordinal\n"); return NULL; } subject = (raptor_abbrev_subject*)RAPTOR_CALLOC(raptor_subject, 1, sizeof(raptor_abbrev_subject)); if(subject) { subject->node = node; subject->node->ref_count++; subject->node->count_as_subject++; subject->node_type = NULL; subject->properties = raptor_new_avltree((raptor_data_compare_function)raptor_compare_abbrev_po, (raptor_data_free_function)raptor_free_abbrev_po, 0); #ifdef RAPTOR_DEBUG raptor_avltree_set_print_handler(subject->properties, (raptor_data_print_function)raptor_print_abbrev_po); #endif subject->list_items = raptor_new_sequence((raptor_sequence_free_handler *)raptor_free_abbrev_node, NULL); if(!subject->node || !subject->properties || !subject->list_items) { raptor_free_abbrev_subject(subject); subject = NULL; } } return subject; }
/** * raptor_new_xml_writer: * @world: raptor_world object * @nstack: Namespace stack for the writer to start with (or NULL) * @iostr: I/O stream to write to * * Constructor - Create a new XML Writer writing XML to a raptor_iostream * * Return value: a new #raptor_xml_writer object or NULL on failure **/ raptor_xml_writer* raptor_new_xml_writer(raptor_world* world, raptor_namespace_stack *nstack, raptor_iostream* iostr) { raptor_xml_writer* xml_writer; RAPTOR_CHECK_CONSTRUCTOR_WORLD(world); if(!iostr) return NULL; raptor_world_open(world); xml_writer = (raptor_xml_writer*)RAPTOR_CALLOC(raptor_xml_writer, 1, sizeof(*xml_writer)); if(!xml_writer) return NULL; xml_writer->world = world; xml_writer->nstack_depth = 0; xml_writer->nstack = nstack; if(!xml_writer->nstack) { xml_writer->nstack = raptor_new_namespaces(world, 1); xml_writer->my_nstack = 1; } xml_writer->iostr = iostr; raptor_object_options_init(&xml_writer->options, RAPTOR_OPTION_AREA_XML_WRITER); RAPTOR_OPTIONS_SET_NUMERIC(xml_writer, RAPTOR_OPTION_WRITER_INDENT_WIDTH, 2); RAPTOR_OPTIONS_SET_NUMERIC(xml_writer, RAPTOR_OPTION_WRITER_XML_VERSION, 10); /* Write XML declaration */ RAPTOR_OPTIONS_SET_NUMERIC(xml_writer, RAPTOR_OPTION_WRITER_XML_DECLARATION, 1); return xml_writer; }
static void raptor_rss_item_add(raptor_rss_parser_context *rss_parser) { raptor_rss_item* item=(raptor_rss_item*)RAPTOR_CALLOC(raptor_rss_item, 1, sizeof(raptor_rss_item)); item->next=NULL; /* new list */ if(!rss_parser->items) rss_parser->items=item; /* join last item to this one */ if(rss_parser->last) rss_parser->last->next=item; /* this is now the last item */ rss_parser->last=item; rss_parser->items_count++; RAPTOR_DEBUG2("Added item %d\n", rss_parser->items_count); }
/** * raptor_new_sax2: * @user_data: pointer context information to pass to handlers * @error_handlers: error handlers pointer * * Constructor - Create a new SAX2 with error handlers * * Return value: new #raptor_sax2 object or NULL on failure */ raptor_sax2* raptor_new_sax2(void* user_data, raptor_error_handlers* error_handlers) { raptor_sax2* sax2; sax2=(raptor_sax2*)RAPTOR_CALLOC(raptor_sax2, 1, sizeof(raptor_sax2)); if(!sax2) return NULL; #ifdef RAPTOR_XML_LIBXML sax2->magic=RAPTOR_LIBXML_MAGIC; #endif sax2->world=error_handlers->world; sax2->user_data=user_data; sax2->locator=error_handlers->locator; sax2->error_handlers=error_handlers; #ifdef RAPTOR_XML_LIBXML if(sax2->world->libxml_flags & RAPTOR_LIBXML_FLAGS_STRUCTURED_ERROR_SAVE) { sax2->saved_structured_error_context = xmlGenericErrorContext; sax2->saved_structured_error_handler = xmlStructuredError; /* sets xmlGenericErrorContext and xmlStructuredError */ xmlSetStructuredErrorFunc(&sax2->error_handlers, (xmlStructuredErrorFunc)raptor_libxml_xmlStructuredErrorFunc); } if(sax2->world->libxml_flags & RAPTOR_LIBXML_FLAGS_GENERIC_ERROR_SAVE) { sax2->saved_generic_error_context = xmlGenericErrorContext; sax2->saved_generic_error_handler = xmlGenericError; /* sets xmlGenericErrorContext and xmlGenericError */ xmlSetGenericErrorFunc(&sax2->error_handlers, (xmlGenericErrorFunc)raptor_libxml_generic_error); } #endif return sax2; }
/** * raptor_new_www_with_connection: * @world: raptor_world object * @connection: external WWW connection object. * * Constructor - create a new #raptor_www object over an existing WWW connection. * * At present this only works with a libcurl CURL handle object * when raptor is compiled with libcurl suppport. Otherwise the * @connection is ignored. This allows such things as setting * up special flags on the curl handle before passing into the constructor. * * Return value: a new #raptor_www object or NULL on failure. **/ raptor_www* raptor_new_www_with_connection(raptor_world* world, void *connection) { raptor_www* www; RAPTOR_CHECK_CONSTRUCTOR_WORLD(world); raptor_world_open(world); www = (raptor_www* )RAPTOR_CALLOC(www, 1, sizeof(*www)); if(!www) return NULL; www->world = world; www->type = NULL; www->free_type = 1; /* default is to free content type */ www->total_bytes = 0; www->failed = 0; www->status_code = 0; www->write_bytes = NULL; www->content_type = NULL; www->uri_filter = NULL; www->connection_timeout = 10; www->cache_control = NULL; #ifdef RAPTOR_WWW_LIBCURL www->curl_handle = (CURL*)connection; raptor_www_curl_init(www); #endif #ifdef RAPTOR_WWW_LIBXML raptor_www_libxml_init(www); #endif #ifdef RAPTOR_WWW_LIBFETCH raptor_www_libfetch_init(www); #endif return www; }
/** * raptor_new_term_from_uri: * @world: raptor world * @uri: uri * * Constructor - create a new URI statement term * * Takes a copy (reference) of the passed in @uri * * Return value: new term or NULL on failure */ raptor_term* raptor_new_term_from_uri(raptor_world* world, raptor_uri* uri) { raptor_term *t; RAPTOR_CHECK_CONSTRUCTOR_WORLD(world); if(!uri) return NULL; raptor_world_open(world); t = (raptor_term*)RAPTOR_CALLOC(raptor_term, 1, sizeof(*t)); if(!t) return NULL; t->usage = 1; t->world = world; t->type = RAPTOR_TERM_TYPE_URI; t->value.uri = raptor_uri_copy(uri); return t; }
/** * raptor_new_term_from_counted_blank: * @world: raptor world * @blank: UTF-8 encoded blank node identifier (or NULL) * @length: length of identifier (or 0) * * Constructor - create a new blank node statement term from a counted UTF-8 encoded blank node ID * * Takes a copy of the passed in @blank * * If @blank is NULL, creates a new internal identifier and uses it. * This will use the handler set with * raptor_world_set_generate_bnodeid_parameters() * * Note: The @blank need not be NULL terminated - a NULL will be * added to the copied string used. * * Return value: new term or NULL on failure */ raptor_term* raptor_new_term_from_counted_blank(raptor_world* world, const unsigned char* blank, size_t length) { raptor_term *t; unsigned char* new_id; RAPTOR_CHECK_CONSTRUCTOR_WORLD(world); raptor_world_open(world); if (blank) { new_id = (unsigned char*)RAPTOR_MALLOC(cstring, length + 1); if(!new_id) return NULL; memcpy(new_id, blank, length); new_id[length] = '\0'; } else { new_id = raptor_world_generate_bnodeid(world); length = strlen((const char*)new_id); } t = (raptor_term*)RAPTOR_CALLOC(raptor_term, 1, sizeof(*t)); if(!t) { RAPTOR_FREE(cstring, new_id); return NULL; } t->usage = 1; t->world = world; t->type = RAPTOR_TERM_TYPE_BLANK; t->value.blank.string = new_id; t->value.blank.string_len = length; return t; }
/* * raptor_dot_serializer_new_node implementation: * @node_type: Raptor identifier type * @node_data: For node_type RAPTOR_IDENTIFIER_TYPE_ORDINAL, int* to the * ordinal. * @datatype: Literal datatype or NULL * @language: Literal language or NULL * * Parts of this is taken from redland librdf_node.h and librdf_node.c * * Return value: a new raptor_dot_serializer_node * **/ static raptor_dot_serializer_node * raptor_dot_serializer_new_node(raptor_identifier_type node_type, const void* node_data, raptor_uri* datatype, const unsigned char *language) { unsigned char *string; raptor_dot_serializer_node* node; if(node_type == RAPTOR_IDENTIFIER_TYPE_UNKNOWN) return 0; node = (raptor_dot_serializer_node *)RAPTOR_CALLOC(raptor_dot_serializer_node, 1, sizeof(raptor_dot_serializer_node)); if(node) { node->type = node_type; switch (node_type) { case RAPTOR_IDENTIFIER_TYPE_PREDICATE: node->type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; /* intentional fall through */ case RAPTOR_IDENTIFIER_TYPE_RESOURCE: node->value.resource.uri = raptor_uri_copy((raptor_uri*)node_data); break; case RAPTOR_IDENTIFIER_TYPE_ANONYMOUS: string=(unsigned char*)RAPTOR_MALLOC(blank, strlen((char*)node_data)+1); strcpy((char*)string, (const char*) node_data); node->value.blank.string = string; break; case RAPTOR_IDENTIFIER_TYPE_LITERAL: case RAPTOR_IDENTIFIER_TYPE_XML_LITERAL: string = (unsigned char*)RAPTOR_MALLOC(literal, strlen((char*)node_data)+1); strcpy((char*)string, (const char*)node_data); node->value.literal.string = string; if(datatype) node->value.literal.datatype = raptor_uri_copy(datatype); if(language) { unsigned char *lang; lang = (unsigned char*)RAPTOR_MALLOC(language, strlen((const char*)language)+1); strcpy((char*)lang, (const char*)language); node->value.literal.language = lang; } break; case RAPTOR_IDENTIFIER_TYPE_ORDINAL: case RAPTOR_IDENTIFIER_TYPE_UNKNOWN: default: RAPTOR_FREE(raptor_dot_serializer_node, node); } } return node; }
/** * raptor_new_avltree_iterator: * @tree: #raptor_avltree object * @range: range * @range_free_handler: function to free @range object * @direction: <0 to go 'backwards' otherwise 'forwards' * * Get an in-order iterator for the start of a range, or the entire contents * * If range is NULL, the entire tree is walked in order. If range * specifies a range (i.e. the tree comparison function will 'match' * (return 0 for) range and /several/ nodes), the iterator will be * placed at the leftmost child matching range, and * raptor_avltree_iterator_next will iterate over all nodes (and only * nodes) that match range. * * Return value: a new #raptor_avltree_iterator object or NULL on failure **/ raptor_avltree_iterator* raptor_new_avltree_iterator(raptor_avltree* tree, void* range, raptor_data_free_handler range_free_handler, int direction) { raptor_avltree_iterator* iterator; iterator = (raptor_avltree_iterator*)RAPTOR_CALLOC(raptor_avltree_iterator, 1, sizeof(*iterator)); if(!iterator) return NULL; iterator->is_finished = 0; iterator->current = NULL; iterator->tree = tree; iterator->range = range; iterator->range_free_handler = range_free_handler; iterator->direction = direction; if(range) { /* find the topmost match (range is contained entirely in tree * rooted here) */ iterator->current = raptor_avltree_search_internal(tree, tree->root, range); } else { iterator->current = tree->root; } iterator->root = iterator->current; if(iterator->current) { if(iterator->direction < 0) { /* go down to find END of range (or tree) */ while(1) { raptor_avltree_node* pred; iterator->current = raptor_avltree_node_rightmost(tree, iterator->current, range); /* move left until a match is found */ pred = raptor_avltree_node_search_left(tree, iterator->current->right, range); if(pred && tree->compare_handler(range, pred->data) == 0) iterator->current = pred; else break; } } else { /* go down to find START of range (or tree) */ while(1) { raptor_avltree_node* pred; iterator->current = raptor_avltree_node_leftmost(tree, iterator->current, range); /* move right until a match is found */ pred = raptor_avltree_node_search_right(tree, iterator->current->left, range); if(pred && tree->compare_handler(range, pred->data) == 0) iterator->current = pred; else break; } } } return iterator; }
static int raptor_xml_writer_start_element_common(raptor_xml_writer* xml_writer, raptor_xml_element* element, int auto_empty) { raptor_iostream* iostr = xml_writer->iostr; raptor_namespace_stack *nstack = xml_writer->nstack; int depth = xml_writer->depth; int auto_indent = XML_WRITER_AUTO_INDENT(xml_writer); int xml_version = XML_WRITER_XML_VERSION(xml_writer); struct nsd *nspace_declarations = NULL; size_t nspace_declarations_count = 0; unsigned int i; /* max is 1 per element and 1 for each attribute + size of declared */ if(nstack) { int nspace_max_count = element->attribute_count+1; if(element->declared_nspaces) nspace_max_count += raptor_sequence_size(element->declared_nspaces); nspace_declarations = (struct nsd*)RAPTOR_CALLOC(nsdarray, nspace_max_count, sizeof(struct nsd)); if(!nspace_declarations) return 1; } if(element->name->nspace) { if(nstack && !raptor_namespaces_namespace_in_scope(nstack, element->name->nspace)) { nspace_declarations[0].declaration= raptor_namespace_format_as_xml(element->name->nspace, &nspace_declarations[0].length); if(!nspace_declarations[0].declaration) goto error; nspace_declarations[0].nspace = element->name->nspace; nspace_declarations_count++; } } if(element->attributes) { for(i = 0; i < element->attribute_count; i++) { /* qname */ if(element->attributes[i]->nspace) { if(nstack && !raptor_namespaces_namespace_in_scope(nstack, element->attributes[i]->nspace) && element->attributes[i]->nspace != element->name->nspace) { /* not in scope and not same as element (so already going to be declared)*/ unsigned int j; int declare_me = 1; /* check it wasn't an earlier declaration too */ for(j = 0; j < nspace_declarations_count; j++) if(nspace_declarations[j].nspace == element->attributes[j]->nspace) { declare_me = 0; break; } if(declare_me) { nspace_declarations[nspace_declarations_count].declaration= raptor_namespace_format_as_xml(element->attributes[i]->nspace, &nspace_declarations[nspace_declarations_count].length); if(!nspace_declarations[nspace_declarations_count].declaration) goto error; nspace_declarations[nspace_declarations_count].nspace = element->attributes[i]->nspace; nspace_declarations_count++; } } } } } if(nstack && element->declared_nspaces && raptor_sequence_size(element->declared_nspaces) > 0) { for(i = 0; i< (unsigned int)raptor_sequence_size(element->declared_nspaces); i++) { raptor_namespace* nspace = (raptor_namespace*)raptor_sequence_get_at(element->declared_nspaces, i); unsigned int j; int declare_me = 1; /* check it wasn't an earlier declaration too */ for(j = 0; j < nspace_declarations_count; j++) if(nspace_declarations[j].nspace == nspace) { declare_me = 0; break; } if(declare_me) { nspace_declarations[nspace_declarations_count].declaration= raptor_namespace_format_as_xml(nspace, &nspace_declarations[nspace_declarations_count].length); if(!nspace_declarations[nspace_declarations_count].declaration) goto error; nspace_declarations[nspace_declarations_count].nspace = nspace; nspace_declarations_count++; } } } raptor_iostream_write_byte('<', iostr); if(element->name->nspace && element->name->nspace->prefix_length > 0) { raptor_iostream_counted_string_write((const char*)element->name->nspace->prefix, element->name->nspace->prefix_length, iostr); raptor_iostream_write_byte(':', iostr); } raptor_iostream_counted_string_write((const char*)element->name->local_name, element->name->local_name_length, iostr); /* declare namespaces */ if(nspace_declarations_count) { /* sort them into the canonical order */ qsort((void*)nspace_declarations, nspace_declarations_count, sizeof(struct nsd), raptor_xml_writer_nsd_compare); /* add them */ for(i = 0; i < nspace_declarations_count; i++) { if(auto_indent && nspace_declarations_count > 1) { /* indent xmlns namespace attributes */ raptor_xml_writer_newline(xml_writer); xml_writer->depth++; raptor_xml_writer_indent(xml_writer); xml_writer->depth--; } raptor_iostream_write_byte(' ', iostr); raptor_iostream_counted_string_write((const char*)nspace_declarations[i].declaration, nspace_declarations[i].length, iostr); RAPTOR_FREE(cstring, nspace_declarations[i].declaration); nspace_declarations[i].declaration = NULL; if(raptor_namespace_stack_start_namespace(nstack, (raptor_namespace*)nspace_declarations[i].nspace, depth)) goto error; } } if(element->attributes) { for(i = 0; i < element->attribute_count; i++) { raptor_iostream_write_byte(' ', iostr); if(element->attributes[i]->nspace && element->attributes[i]->nspace->prefix_length > 0) { raptor_iostream_counted_string_write((char*)element->attributes[i]->nspace->prefix, element->attributes[i]->nspace->prefix_length, iostr); raptor_iostream_write_byte(':', iostr); } raptor_iostream_counted_string_write((const char*)element->attributes[i]->local_name, element->attributes[i]->local_name_length, iostr); raptor_iostream_counted_string_write("=\"", 2, iostr); raptor_xml_escape_string_any_write(element->attributes[i]->value, element->attributes[i]->value_length, '"', xml_version, iostr); raptor_iostream_write_byte('"', iostr); } } if(!auto_empty) raptor_iostream_write_byte('>', iostr); if(nstack) RAPTOR_FREE(stringarray, nspace_declarations); return 0; /* Clean up nspace_declarations on error */ error: for(i = 0; i < nspace_declarations_count; i++) { if(nspace_declarations[i].declaration) RAPTOR_FREE(cstring, nspace_declarations[i].declaration); } if(nspace_declarations) RAPTOR_FREE(stringarray, nspace_declarations); return 1; }
int main(int argc, char *argv[]) { raptor_world *world; const char *program = raptor_basename(argv[0]); raptor_iostream *iostr; raptor_namespace_stack *nstack; raptor_namespace* foo_ns; raptor_xml_writer* xml_writer; raptor_uri* base_uri; raptor_qname* el_name; raptor_xml_element *element; unsigned long offset; raptor_qname **attrs; raptor_uri* base_uri_copy = NULL; /* for raptor_new_iostream_to_string */ void *string = NULL; size_t string_len = 0; world = raptor_new_world(); if(!world || raptor_world_open(world)) exit(1); iostr = raptor_new_iostream_to_string(world, &string, &string_len, NULL); if(!iostr) { fprintf(stderr, "%s: Failed to create iostream to string\n", program); exit(1); } nstack = raptor_new_namespaces(world, 1); xml_writer = raptor_new_xml_writer(world, nstack, iostr); if(!xml_writer) { fprintf(stderr, "%s: Failed to create xml_writer to iostream\n", program); exit(1); } base_uri = raptor_new_uri(world, base_uri_string); foo_ns = raptor_new_namespace(nstack, (const unsigned char*)"foo", (const unsigned char*)"http://example.org/foo-ns#", 0); el_name = raptor_new_qname_from_namespace_local_name(world, foo_ns, (const unsigned char*)"bar", NULL); base_uri_copy = base_uri ? raptor_uri_copy(base_uri) : NULL; element = raptor_new_xml_element(el_name, NULL, /* language */ base_uri_copy); raptor_xml_writer_start_element(xml_writer, element); raptor_xml_writer_cdata_counted(xml_writer, (const unsigned char*)"hello\n", 6); raptor_xml_writer_comment_counted(xml_writer, (const unsigned char*)"comment", 7); raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n"); raptor_xml_writer_end_element(xml_writer, element); raptor_free_xml_element(element); raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n"); el_name = raptor_new_qname(nstack, (const unsigned char*)"blah", NULL /* no attribute value - element */); base_uri_copy = base_uri ? raptor_uri_copy(base_uri) : NULL; element = raptor_new_xml_element(el_name, NULL, /* language */ base_uri_copy); attrs = (raptor_qname **)RAPTOR_CALLOC(qnamearray, 1, sizeof(raptor_qname*)); attrs[0] = raptor_new_qname(nstack, (const unsigned char*)"a", (const unsigned char*)"b" /* attribute value */); raptor_xml_element_set_attributes(element, attrs, 1); raptor_xml_writer_empty_element(xml_writer, element); raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n"); raptor_free_xml_writer(xml_writer); raptor_free_xml_element(element); raptor_free_namespace(foo_ns); raptor_free_namespaces(nstack); raptor_free_uri(base_uri); offset = raptor_iostream_tell(iostr); #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Freeing iostream\n", program); #endif raptor_free_iostream(iostr); if(offset != OUT_BYTES_COUNT) { fprintf(stderr, "%s: I/O stream wrote %d bytes, expected %d\n", program, (int)offset, (int)OUT_BYTES_COUNT); fputs("[[", stderr); (void)fwrite(string, 1, string_len, stderr); fputs("]]\n", stderr); return 1; } if(!string) { fprintf(stderr, "%s: I/O stream failed to create a string\n", program); return 1; } string_len = strlen((const char*)string); if(string_len != offset) { fprintf(stderr, "%s: I/O stream created a string length %d, expected %d\n", program, (int)string_len, (int)offset); return 1; } #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Made XML string of %d bytes\n", program, (int)string_len); fputs("[[", stderr); (void)fwrite(string, 1, string_len, stderr); fputs("]]\n", stderr); #endif raptor_free_memory(string); raptor_free_world(world); /* keep gcc -Wall happy */ return(0); }
/* start of an element */ void raptor_sax2_start_element(void* user_data, const unsigned char *name, const unsigned char **atts) { raptor_sax2* sax2=(raptor_sax2*)user_data; raptor_qname* el_name; unsigned char **xml_atts_copy=NULL; size_t xml_atts_size=0; int all_atts_count=0; int ns_attributes_count=0; raptor_qname** named_attrs=NULL; raptor_xml_element* xml_element=NULL; unsigned char *xml_language=NULL; raptor_uri *xml_base=NULL; if(sax2->failed) return; #ifdef RAPTOR_XML_EXPAT #ifdef EXPAT_UTF8_BOM_CRASH sax2->tokens_count++; #endif #endif #ifdef RAPTOR_XML_LIBXML if(atts) { int i; /* Do XML attribute value normalization */ for (i = 0; atts[i]; i+=2) { unsigned char *value=(unsigned char*)atts[i+1]; unsigned char *src = value; unsigned char *dst = xmlStrdup(value); if(!dst) { raptor_log_error_to_handlers(sax2->world, sax2->error_handlers, RAPTOR_LOG_LEVEL_FATAL, sax2->locator, "Out of memory"); return; } atts[i+1]=dst; while (*src == 0x20 || *src == 0x0d || *src == 0x0a || *src == 0x09) src++; while (*src) { if (*src == 0x20 || *src == 0x0d || *src == 0x0a || *src == 0x09) { while (*src == 0x20 || *src == 0x0d || *src == 0x0a || *src == 0x09) src++; if (*src) *dst++ = 0x20; } else { *dst++ = *src++; } } *dst = '\0'; xmlFree(value); } } #endif raptor_sax2_inc_depth(sax2); if(atts) { int i; /* Save passed in XML attributes pointers so we can * NULL the pointers when they get handled below (various atts[i]=NULL) */ for (i = 0; atts[i]; i++) ; xml_atts_size=sizeof(unsigned char*) * i; if(xml_atts_size) { xml_atts_copy=(unsigned char**)RAPTOR_MALLOC(cstringpointer,xml_atts_size); if(!xml_atts_copy) goto fail; memcpy(xml_atts_copy, atts, xml_atts_size); } /* XML attributes processing: * xmlns* - XML namespaces (Namespaces in XML REC) * Deleted and used to synthesise namespaces declarations * xml:lang - XML language (XML REC) * Deleted and optionally normalised to lowercase * xml:base - XML Base (XML Base REC) * Deleted and used to set the in-scope base URI for this XML element */ for (i = 0; atts[i]; i+= 2) { all_atts_count++; if(strncmp((char*)atts[i], "xml", 3)) { /* count and skip non xml* attributes */ ns_attributes_count++; continue; } /* synthesise the XML namespace events */ if(!memcmp((const char*)atts[i], "xmlns", 5)) { const unsigned char *prefix=atts[i][5] ? &atts[i][6] : NULL; const unsigned char *namespace_name=atts[i+1]; raptor_namespace* nspace; nspace=raptor_new_namespace(&sax2->namespaces, prefix, namespace_name, raptor_sax2_get_depth(sax2)); if(nspace) { raptor_namespaces_start_namespace(&sax2->namespaces, nspace); if(sax2->namespace_handler) (*sax2->namespace_handler)(sax2->user_data, nspace); } } else if(!strcmp((char*)atts[i], "xml:lang")) { xml_language=(unsigned char*)RAPTOR_MALLOC(cstring, strlen((char*)atts[i+1])+1); if(!xml_language) { raptor_log_error_to_handlers(sax2->world, sax2->error_handlers, RAPTOR_LOG_LEVEL_FATAL, sax2->locator, "Out of memory"); goto fail; } /* optionally normalize language to lowercase */ if(sax2->feature_normalize_language) { unsigned char *from=(unsigned char*)atts[i+1]; unsigned char *to=xml_language; while(*from) { if(isupper(*from)) *to++ =tolower(*from++); else *to++ =*from++; } *to='\0'; } else strcpy((char*)xml_language, (char*)atts[i+1]); } else if(!strcmp((char*)atts[i], "xml:base")) { raptor_uri* base_uri; raptor_uri* xuri; base_uri=raptor_sax2_inscope_base_uri(sax2); xuri=raptor_new_uri_relative_to_base_v2(sax2->world, base_uri, atts[i+1]); xml_base=raptor_new_uri_for_xmlbase_v2(sax2->world, xuri); raptor_free_uri_v2(sax2->world, xuri); } /* delete all xml attributes whether processed above or not */ atts[i]=NULL; } } /* Create new element structure */ el_name=raptor_new_qname(&sax2->namespaces, name, NULL, (raptor_simple_message_handler)raptor_sax2_simple_error, sax2); if(!el_name) goto fail; xml_element=raptor_new_xml_element(el_name, xml_language, xml_base); if(!xml_element) { raptor_free_qname(el_name); goto fail; } /* xml_language,xml_base now owned by xml_element */ xml_language = NULL; xml_base = NULL; /* Turn string attributes into namespaced-attributes */ if(ns_attributes_count) { int i; int offset = 0; /* Allocate new array to hold namespaced-attributes */ named_attrs=(raptor_qname**)RAPTOR_CALLOC(raptor_qname_array, ns_attributes_count, sizeof(raptor_qname*)); if(!named_attrs) { raptor_log_error_to_handlers(sax2->world, sax2->error_handlers, RAPTOR_LOG_LEVEL_FATAL, sax2->locator, "Out of memory"); goto fail; } for (i = 0; i < all_atts_count; i++) { raptor_qname* attr; /* Skip previously processed attributes */ if(!atts[i<<1]) continue; /* namespace-name[i] stored in named_attrs[i] */ attr=raptor_new_qname(&sax2->namespaces, atts[i<<1], atts[(i<<1)+1], (raptor_simple_message_handler)raptor_sax2_simple_error, sax2); if(!attr) { /* failed - tidy up and return */ int j; for (j=0; j < i; j++) RAPTOR_FREE(raptor_qname, named_attrs[j]); RAPTOR_FREE(raptor_qname_array, named_attrs); goto fail; } named_attrs[offset++]=attr; } } /* end if ns_attributes_count */ if(named_attrs) raptor_xml_element_set_attributes(xml_element, named_attrs, ns_attributes_count); raptor_xml_element_push(sax2, xml_element); if(sax2->start_element_handler) sax2->start_element_handler(sax2->user_data, xml_element); if(xml_atts_copy) { /* Restore passed in XML attributes, free the copy */ memcpy((void*)atts, xml_atts_copy, xml_atts_size); RAPTOR_FREE(cstringpointer, xml_atts_copy); } return; fail: if(xml_atts_copy) RAPTOR_FREE(cstringpointer, xml_atts_copy); if(xml_base) raptor_free_uri_v2(sax2->world, xml_base); if(xml_language) RAPTOR_FREE(cstring, xml_language); if(xml_element) raptor_free_xml_element(xml_element); }
/** * raptor_new_term_from_counted_literal: * @world: raptor world * @literal: UTF-8 encoded literal string (or NULL for empty literal) * @literal_len: length of literal * @datatype: literal datatype URI (or NULL) * @language: literal language (or NULL for no language) * @language_len: literal language length * * Constructor - create a new literal statement term from a counted UTF-8 encoded literal string * * Takes copies of the passed in @literal, @datatype, @language * * Only one of @language or @datatype may be given. If both are * given, NULL is returned. If @language is the empty string, it is * the equivalent to NULL. * * Note: The @literal need not be NULL terminated - a NULL will be * added to the copied string used. * * Return value: new term or NULL on failure */ raptor_term* raptor_new_term_from_counted_literal(raptor_world* world, const unsigned char* literal, size_t literal_len, raptor_uri* datatype, const unsigned char* language, unsigned char language_len) { raptor_term *t; unsigned char* new_literal = NULL; unsigned char* new_language = NULL; RAPTOR_CHECK_CONSTRUCTOR_WORLD(world); raptor_world_open(world); if(language && !*language) language = NULL; if(language && datatype) return NULL; new_literal = (unsigned char*)RAPTOR_MALLOC(cstring, literal_len + 1); if(!new_literal) return NULL; if(!literal || !*literal) literal_len = 0; if(literal_len) { memcpy(new_literal, literal, literal_len); new_literal[literal_len] = '\0'; } else *new_literal = '\0'; if(language) { new_language = (unsigned char*)RAPTOR_MALLOC(cstring, language_len + 1); if(!new_language) { RAPTOR_FREE(cstring, new_literal); return NULL; } memcpy(new_language, language, language_len); new_language[language_len] = '\0'; } else language_len = 0; if(datatype) datatype = raptor_uri_copy(datatype); t = (raptor_term*)RAPTOR_CALLOC(raptor_term, 1, sizeof(*t)); if(!t) { if(new_literal) RAPTOR_FREE(cstring, new_literal); if(new_language) RAPTOR_FREE(cstring, new_language); if(datatype) raptor_free_uri(datatype); return NULL; } t->usage = 1; t->world = world; t->type = RAPTOR_TERM_TYPE_LITERAL; t->value.literal.string = new_literal; t->value.literal.string_len = literal_len; t->value.literal.language = new_language; t->value.literal.language_len = language_len; t->value.literal.datatype = datatype; return t; }
/** * raptor_id_set_add: * @set: #raptor_id_set * @base_uri: base #raptor_uri of identifier * @id: identifier name * @id_len: length of identifier * * INTERNAL - Add an item to the set. * * Return value: <0 on failure, 0 on success, 1 if already present **/ int raptor_id_set_add(raptor_id_set* set, raptor_uri *base_uri, const unsigned char *id, size_t id_len) { raptor_base_id_set *base; char* item; if(!base_uri || !id || !id_len) return -1; base = set->first; while(base) { if(raptor_uri_equals(base->uri, base_uri)) break; base = base->next; } if(!base) { /* a set for this base_uri not found */ base = (raptor_base_id_set*)RAPTOR_CALLOC(raptor_base_id_set, 1, sizeof(*base)); if(!base) return -1; base->world = set->world; base->uri = raptor_uri_copy(base_uri); base->tree = raptor_new_avltree((raptor_data_compare_handler)strcmp, free, 0); /* Add to the start of the list */ if(set->first) set->first->prev = base; /* base->prev = NULL; */ base->next = set->first; set->first = base; } else { /* If not at the start of the list, move there */ if(base != set->first) { /* remove from the list */ base->prev->next = base->next; if(base->next) base->next->prev = base->prev; /* add at the start of the list */ set->first->prev = base; base->prev = NULL; base->next = set->first; } } item = (char*)raptor_avltree_search(base->tree, id); /* if already there, error */ if(item) { #if RAPTOR_DEBUG > 1 set->misses++; #endif return 1; } #if RAPTOR_DEBUG > 1 set->hits++; #endif item = (char*)RAPTOR_MALLOC(cstring, id_len+1); if(!item) return 1; memcpy(item, id, id_len + 1); return raptor_avltree_add(base->tree, item); }
raptor_abbrev_node* raptor_new_abbrev_node(raptor_identifier_type node_type, const void *node_data, raptor_uri *datatype, const unsigned char *language) { unsigned char *string; raptor_abbrev_node* node=NULL; if(node_type == RAPTOR_IDENTIFIER_TYPE_UNKNOWN) return 0; node = (raptor_abbrev_node*)RAPTOR_CALLOC(raptor_abbrev_node, 1, sizeof(raptor_abbrev_node)); if(node) { node->ref_count = 1; node->type = node_type; switch (node_type) { case RAPTOR_IDENTIFIER_TYPE_PREDICATE: node->type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; /* intentional fall through */ case RAPTOR_IDENTIFIER_TYPE_RESOURCE: node->value.resource.uri = raptor_uri_copy((raptor_uri*)node_data); break; case RAPTOR_IDENTIFIER_TYPE_ANONYMOUS: string=(unsigned char*)RAPTOR_MALLOC(blank, strlen((char*)node_data)+1); if(!string) goto oom; strcpy((char*)string, (const char*) node_data); node->value.blank.string = string; break; case RAPTOR_IDENTIFIER_TYPE_ORDINAL: node->value.ordinal.ordinal = *(int *)node_data; break; case RAPTOR_IDENTIFIER_TYPE_LITERAL: case RAPTOR_IDENTIFIER_TYPE_XML_LITERAL: string = (unsigned char*)RAPTOR_MALLOC(literal, strlen((char*)node_data)+1); if(!string) goto oom; strcpy((char*)string, (const char*)node_data); node->value.literal.string = string; if(datatype) { node->value.literal.datatype = raptor_uri_copy(datatype); } if(language) { unsigned char *lang; lang=(unsigned char*)RAPTOR_MALLOC(language, strlen((const char*)language)+1); if(!lang) { RAPTOR_FREE(literal, string); goto oom; } strcpy((char*)lang, (const char*)language); node->value.literal.language = lang; } break; case RAPTOR_IDENTIFIER_TYPE_UNKNOWN: default: RAPTOR_FREE(raptor_abbrev_node, node); } } return node; /* out of memory - clean up and return NULL */ oom: RAPTOR_FREE(raptor_abbrev_node, node); return NULL; }