1 /* -*- Mode: c; c-basic-offset: 2 -*-
2  *
3  * raptor_identifier.c - Raptor identifier classes
4  *
5  * Copyright (C) 2003-2009, David Beckett http://www.dajobe.org/
6  * Copyright (C) 2003-2005, University of Bristol, UK http://www.bristol.ac.uk/
7  *
8  * This package is Free Software and part of Redland http://librdf.org/
9  *
10  * It is licensed under the following three licenses as alternatives:
11  *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
12  *   2. GNU General Public License (GPL) V2 or any newer version
13  *   3. Apache License, V2.0 or any newer version
14  *
15  * You may not use this file except in compliance with at least one of
16  * the above three licenses.
17  *
18  * See LICENSE.html or LICENSE.txt at the top of this package for the
19  * complete terms and further detail along with the license texts for
20  * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
21  *
22  *
23  */
24 
25 
26 #ifdef HAVE_CONFIG_H
27 #include <raptor_config.h>
28 #endif
29 
30 #ifdef WIN32
31 #include <win32_raptor_config.h>
32 #endif
33 
34 
35 #include <stdio.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <stdarg.h>
39 #ifdef HAVE_STDLIB_H
40 #include <stdlib.h>
41 #endif
42 
43 /* Raptor includes */
44 #include "raptor.h"
45 #include "raptor_internal.h"
46 
47 
48 #ifndef RAPTOR_DISABLE_V1
49 /**
50  * raptor_new_identifier:
51  * @type: raptor_identifier_type of identifier
52  * @uri: #raptor_uri of identifier (if relevant) (SHARED)
53  * @uri_source: raptor_uri_source of URI (if relevant)
54  * @id: string for ID or genid (if relevant) (SHARED)
55  * @literal: string for literal (SHARED)
56  * @literal_datatype: #raptor_uri of identifier (SHARED)
57  * @literal_language: literal language (SHARED)
58  *
59  * Constructor - create a raptor_identifier.
60  *
61  * Constructs a new identifier copying the URI, ID fields.
62  * SHARED means raptor_new_identifier owns this argument after calling.
63  *
64  * raptor_init() MUST have been called before calling this function.
65  * Use raptor_new_identifier_v2() if using raptor_world APIs.
66  *
67  * Return value: a new raptor_identifier object or NULL on failure
68  **/
69 raptor_identifier*
raptor_new_identifier(raptor_identifier_type type,raptor_uri * uri,raptor_uri_source uri_source,const unsigned char * id,const unsigned char * literal,raptor_uri * literal_datatype,const unsigned char * literal_language)70 raptor_new_identifier(raptor_identifier_type type,
71                       raptor_uri *uri,
72                       raptor_uri_source uri_source,
73                       const unsigned char *id,
74                       const unsigned char *literal,
75                       raptor_uri *literal_datatype,
76                       const unsigned char *literal_language)
77 {
78   return raptor_new_identifier_v2(raptor_world_instance(),
79                                   type,
80                                   uri,
81                                   uri_source,
82                                   id,
83                                   literal,
84                                   literal_datatype,
85                                   literal_language);
86 }
87 #endif
88 
89 
90 /**
91  * raptor_new_identifier_v2:
92  * @world: raptor_world object
93  * @type: raptor_identifier_type of identifier
94  * @uri: #raptor_uri of identifier (if relevant) (SHARED)
95  * @uri_source: raptor_uri_source of URI (if relevant)
96  * @id: string for ID or genid (if relevant) (SHARED)
97  * @literal: string for literal (SHARED)
98  * @literal_datatype: #raptor_uri of identifier (SHARED)
99  * @literal_language: literal language (SHARED)
100  *
101  * Constructor - create a raptor_identifier.
102  *
103  * Constructs a new identifier copying the URI, ID fields.
104  * SHARED means raptor_new_identifier owns this argument after calling.
105  *
106  * Return value: a new raptor_identifier object or NULL on failure
107  **/
108 raptor_identifier*
raptor_new_identifier_v2(raptor_world * world,raptor_identifier_type type,raptor_uri * uri,raptor_uri_source uri_source,const unsigned char * id,const unsigned char * literal,raptor_uri * literal_datatype,const unsigned char * literal_language)109 raptor_new_identifier_v2(raptor_world* world,
110                          raptor_identifier_type type,
111                          raptor_uri *uri,
112                          raptor_uri_source uri_source,
113                          const unsigned char *id,
114                          const unsigned char *literal,
115                          raptor_uri *literal_datatype,
116                          const unsigned char *literal_language)
117 {
118   raptor_identifier *identifier;
119 
120   identifier=(raptor_identifier*)RAPTOR_CALLOC(raptor_identifier, 1,
121                                                sizeof(raptor_identifier));
122   if(!identifier) {
123     if(uri)
124       raptor_free_uri_v2(world, uri);
125     if(id)
126       RAPTOR_FREE(cstring, (void*)id);
127     if(literal)
128       RAPTOR_FREE(cstring, (void*)literal);
129     if(literal_datatype)
130       raptor_free_uri_v2(world, literal_datatype);
131     if(literal_language)
132       RAPTOR_FREE(cstring, (void*)literal_language);
133 
134     return NULL;
135   }
136 
137   identifier->world=world;
138   identifier->type=type;
139   identifier->is_malloced=1;
140 
141   /* SHARED */
142   identifier->uri=uri;
143   identifier->id=id;
144   identifier->literal=literal;
145   identifier->literal_datatype=literal_datatype;
146   identifier->literal_language=literal_language;
147 
148   return identifier;
149 }
150 
151 
152 /**
153  * raptor_copy_identifier:
154  * @dest: destination #raptor_identifier (previously created)
155  * @src:  source #raptor_identifier
156  *
157  * Copy raptor_identifiers.
158  *
159  * Return value: Non 0 on failure
160  **/
161 int
raptor_copy_identifier(raptor_identifier * dest,raptor_identifier * src)162 raptor_copy_identifier(raptor_identifier *dest, raptor_identifier *src)
163 {
164   int len;
165 
166   raptor_free_identifier(dest);
167 
168   dest->world=src->world;
169   dest->type=src->type;
170   dest->uri_source=src->uri_source;
171   dest->ordinal=src->ordinal;
172 
173   dest->uri=raptor_uri_copy_v2(src->world, src->uri);
174 
175   if(src->id) {
176     len=strlen((char*)src->id);
177 
178     dest->id=(unsigned char*)RAPTOR_MALLOC(cstring, len+1);
179     if(!dest->id) {
180       raptor_free_identifier(dest);
181       return 1;
182     }
183     strncpy((char*)dest->id, (char*)src->id, len+1);
184   }
185 
186   if(src->literal_language) {
187     len=strlen((char*)src->literal_language);
188 
189     dest->literal_language=(unsigned char*)RAPTOR_MALLOC(cstring, len+1);
190     if(!dest->literal_language) {
191       raptor_free_identifier(dest);
192       return 1;
193     }
194     strncpy((char*)dest->literal_language, (char*)src->literal_language, len+1);
195   }
196 
197   dest->literal_datatype=raptor_uri_copy_v2(src->world, src->literal_datatype);
198 
199   return 0;
200 }
201 
202 
203 /**
204  * raptor_free_identifier:
205  * @identifier: #raptor_identifier object
206  *
207  * Destructor - destroy a raptor_identifier object.
208  *
209  **/
210 void
raptor_free_identifier(raptor_identifier * identifier)211 raptor_free_identifier(raptor_identifier *identifier)
212 {
213   RAPTOR_ASSERT_OBJECT_POINTER_RETURN(identifier, raptor_identifier);
214 
215   if(identifier->uri) {
216     raptor_free_uri_v2(identifier->world, identifier->uri);
217     identifier->uri=NULL;
218   }
219 
220   if(identifier->id) {
221     RAPTOR_FREE(cstring, (void*)identifier->id);
222     identifier->id=NULL;
223   }
224 
225   if(identifier->literal) {
226     RAPTOR_FREE(cstring, (void*)identifier->literal);
227     identifier->literal=NULL;
228   }
229 
230   if(identifier->literal_datatype) {
231     raptor_free_uri_v2(identifier->world, identifier->literal_datatype);
232     identifier->literal_datatype=NULL;
233   }
234 
235   if(identifier->literal_language) {
236     RAPTOR_FREE(cstring, (void*)identifier->literal_language);
237     identifier->literal_language=NULL;
238   }
239 
240   if(identifier->is_malloced)
241     RAPTOR_FREE(identifier, (void*)identifier);
242 }
243 
244 
245 void
raptor_set_identifier_uri(raptor_identifier * identifier,raptor_uri * uri)246 raptor_set_identifier_uri(raptor_identifier *identifier, raptor_uri *uri)
247 {
248   identifier->uri = uri;
249   identifier->type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
250   identifier->uri_source = RAPTOR_URI_SOURCE_URI;
251 }
252 
253 
254 void
raptor_set_identifier_id(raptor_identifier * identifier,const unsigned char * id)255 raptor_set_identifier_id(raptor_identifier *identifier, const unsigned char *id)
256 {
257   identifier->id = id;
258   identifier->type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
259   identifier->uri_source = RAPTOR_URI_SOURCE_GENERATED;
260 }
261 
262 
263 #ifdef RAPTOR_DEBUG
264 void
raptor_identifier_print(FILE * stream,raptor_identifier * identifier)265 raptor_identifier_print(FILE *stream, raptor_identifier *identifier)
266 {
267   if(!identifier) {
268     fputs("-", stream);
269     return;
270   }
271 
272   if(identifier->type == RAPTOR_IDENTIFIER_TYPE_LITERAL ||
273      identifier->type == RAPTOR_IDENTIFIER_TYPE_XML_LITERAL) {
274     fputc('"', stream);
275     fputs((const char*)identifier->literal, stream);
276     fputc('"', stream);
277     if(identifier->literal_language)
278       fprintf(stream, "@%s", identifier->literal_language);
279     fputc('<', stream);
280     if(identifier->type == RAPTOR_IDENTIFIER_TYPE_XML_LITERAL)
281       fputs((const char*)raptor_xml_literal_datatype_uri_string, stream);
282     else if(identifier->literal_datatype)
283       fputs((const char*)raptor_uri_as_string_v2(identifier->world, identifier->literal_datatype), stream);
284     fputc('>', stream);
285   } else if(identifier->type == RAPTOR_IDENTIFIER_TYPE_ANONYMOUS)
286     fputs((const char*)identifier->id, stream);
287   else if(identifier->type == RAPTOR_IDENTIFIER_TYPE_ORDINAL)
288     fprintf(stream, "[rdf:_%d]", identifier->ordinal);
289   else {
290     fprintf(stream, "%s", raptor_uri_as_string_v2(identifier->world, identifier->uri));
291   }
292 }
293 #endif
294