1 /**
2 * \brief RDF manipulation functions
3 *
4 * FIXME: move these to xml/ instead of dialogs/
5 *
6 * Authors:
7 * Kees Cook <kees@outflux.net>
8 *
9 * Copyright (C) 2004 Kees Cook <kees@outflux.net>
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 *
13 */
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
21 #include "xml/repr.h"
22 #include "rdf.h"
23 #include "sp-item-group.h"
25 /*
27 Example RDF XML from various places...
29 <rdf:RDF xmlns="http://web.resource.org/cc/"
30 xmlns:dc="http://purl.org/dc/elements/1.1/"
31 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
32 <Work rdf:about="">
33 <dc:title>title of work</dc:title>
34 <dc:date>year</dc:date>
35 <dc:description>description of work</dc:description>
36 <dc:creator><Agent>
37 <dc:title>creator</dc:title>
38 </Agent></dc:creator>
39 <dc:rights><Agent>
40 <dc:title>holder</dc:title>
41 </Agent></dc:rights>
42 <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
43 <dc:source rdf:resource="source"/>
44 <license rdf:resource="http://creativecommons.org/licenses/by/2.0/"
45 />
46 </Work>
49 <rdf:RDF xmlns="http://web.resource.org/cc/"
50 xmlns:dc="http://purl.org/dc/elements/1.1/"
51 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
52 <Work rdf:about="">
53 <dc:title>SVG Road Signs</dc:title>
54 <dc:rights><Agent>
55 <dc:title>John Cliff</dc:title>
56 </Agent></dc:rights>
57 <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
58 <license rdf:resource="http://web.resource.org/cc/PublicDomain" />
59 </Work>
61 <License rdf:about="http://web.resource.org/cc/PublicDomain">
62 <permits rdf:resource="http://web.resource.org/cc/Reproduction" />
63 <permits rdf:resource="http://web.resource.org/cc/Distribution" />
64 <permits rdf:resource="http://web.resource.org/cc/DerivativeWorks" />
65 </License>
67 </rdf:RDF>
70 Bag example:
72 <dc:subject>
73 <rdf:Bag>
74 <rdf:li>open clip art logo</rdf:li>
75 <rdf:li>images</rdf:li>
76 <rdf:li>logo</rdf:li>
77 <rdf:li>clip art</rdf:li>
78 <rdf:li>ocal</rdf:li>
79 <rdf:li>logotype</rdf:li>
80 <rdf:li>filetype</rdf:li>
81 </rdf:Bag>
82 </dc:subject>
85 */
87 struct rdf_double_t rdf_license_empty [] = {
88 { NULL, NULL }
89 };
91 struct rdf_double_t rdf_license_cc_a [] = {
92 { "cc:permits", "http://web.resource.org/cc/Reproduction", },
93 { "cc:permits", "http://web.resource.org/cc/Distribution", },
94 { "cc:requires", "http://web.resource.org/cc/Notice", },
95 { "cc:requires", "http://web.resource.org/cc/Attribution", },
96 { "cc:permits", "http://web.resource.org/cc/DerivativeWorks", },
97 { NULL, NULL }
98 };
100 struct rdf_double_t rdf_license_cc_a_sa [] = {
101 { "cc:permits", "http://web.resource.org/cc/Reproduction", },
102 { "cc:permits", "http://web.resource.org/cc/Distribution", },
103 { "cc:requires", "http://web.resource.org/cc/Notice", },
104 { "cc:requires", "http://web.resource.org/cc/Attribution", },
105 { "cc:permits", "http://web.resource.org/cc/DerivativeWorks", },
106 { "cc:requires", "http://web.resource.org/cc/ShareAlike", },
107 { NULL, NULL }
108 };
110 struct rdf_double_t rdf_license_cc_a_nd [] = {
111 { "cc:permits", "http://web.resource.org/cc/Reproduction", },
112 { "cc:permits", "http://web.resource.org/cc/Distribution", },
113 { "cc:requires", "http://web.resource.org/cc/Notice", },
114 { "cc:requires", "http://web.resource.org/cc/Attribution", },
115 { NULL, NULL }
116 };
118 struct rdf_double_t rdf_license_cc_a_nc [] = {
119 { "cc:permits", "http://web.resource.org/cc/Reproduction", },
120 { "cc:permits", "http://web.resource.org/cc/Distribution", },
121 { "cc:requires", "http://web.resource.org/cc/Notice", },
122 { "cc:requires", "http://web.resource.org/cc/Attribution", },
123 { "cc:prohibits", "http://web.resource.org/cc/CommercialUse", },
124 { "cc:permits", "http://web.resource.org/cc/DerivativeWorks", },
125 { NULL, NULL }
126 };
128 struct rdf_double_t rdf_license_cc_a_nc_sa [] = {
129 { "cc:permits", "http://web.resource.org/cc/Reproduction", },
130 { "cc:permits", "http://web.resource.org/cc/Distribution", },
131 { "cc:requires", "http://web.resource.org/cc/Notice", },
132 { "cc:requires", "http://web.resource.org/cc/Attribution", },
133 { "cc:prohibits", "http://web.resource.org/cc/CommercialUse", },
134 { "cc:permits", "http://web.resource.org/cc/DerivativeWorks", },
135 { "cc:requires", "http://web.resource.org/cc/ShareAlike", },
136 { NULL, NULL }
137 };
139 struct rdf_double_t rdf_license_cc_a_nc_nd [] = {
140 { "cc:permits", "http://web.resource.org/cc/Reproduction", },
141 { "cc:permits", "http://web.resource.org/cc/Distribution", },
142 { "cc:requires", "http://web.resource.org/cc/Notice", },
143 { "cc:requires", "http://web.resource.org/cc/Attribution", },
144 { "cc:prohibits", "http://web.resource.org/cc/CommercialUse", },
145 { NULL, NULL }
146 };
148 struct rdf_double_t rdf_license_gpl [] = {
149 { "cc:permits", "http://web.resource.org/cc/Reproduction", },
150 { "cc:permits", "http://web.resource.org/cc/Distribution", },
151 { "cc:requires", "http://web.resource.org/cc/Notice", },
152 { "cc:permits", "http://web.resource.org/cc/DerivativeWorks", },
153 { "cc:requires", "http://web.resource.org/cc/ShareAlike", },
154 { "cc:requires", "http://web.resource.org/cc/SourceCode", },
155 { NULL, NULL }
156 };
158 struct rdf_double_t rdf_license_pd [] = {
159 { "cc:permits", "http://web.resource.org/cc/Reproduction", },
160 { "cc:permits", "http://web.resource.org/cc/Distribution", },
161 { "cc:permits", "http://web.resource.org/cc/DerivativeWorks", },
162 { NULL, NULL }
163 };
165 struct rdf_double_t rdf_license_freeart [] = {
166 { "cc:permits", "http://web.resource.org/cc/Reproduction", },
167 { "cc:permits", "http://web.resource.org/cc/Distribution", },
168 { "cc:permits", "http://web.resource.org/cc/DerivativeWorks", },
169 { "cc:requires", "http://web.resource.org/cc/ShareAlike", },
170 { "cc:requires", "http://web.resource.org/cc/Notice", },
171 { "cc:requires", "http://web.resource.org/cc/Attribution", },
172 { NULL, NULL }
173 };
175 struct rdf_license_t rdf_licenses [] = {
176 { _("CC Attribution"),
177 "http://creativecommons.org/licenses/by/2.0/",
178 rdf_license_cc_a,
179 },
181 { _("CC Attribution-ShareAlike"),
182 "http://creativecommons.org/licenses/by-sa/2.0/",
183 rdf_license_cc_a_sa,
184 },
186 { _("CC Attribution-NoDerivs"),
187 "http://creativecommons.org/licenses/by-nd/2.0/",
188 rdf_license_cc_a_nd,
189 },
191 { _("CC Attribution-NonCommercial"),
192 "http://creativecommons.org/licenses/by-nc/2.0/",
193 rdf_license_cc_a_nc,
194 },
196 { _("CC Attribution-NonCommercial-ShareAlike"),
197 "http://creativecommons.org/licenses/by-nc-sa/2.0/",
198 rdf_license_cc_a_nc_sa,
199 },
201 { _("CC Attribution-NonCommercial-NoDerivs"),
202 "http://creativecommons.org/licenses/by-nc-nd/2.0/",
203 rdf_license_cc_a_nc_nd,
204 },
206 { _("GNU General Public License"),
207 "http://creativecommons.org/licenses/GPL/2.0/",
208 rdf_license_gpl,
209 },
211 { _("GNU Lesser General Public License"),
212 "http://creativecommons.org/licenses/LGPL/2.1/",
213 rdf_license_gpl,
214 },
216 { _("Public Domain"),
217 "http://web.resource.org/cc/PublicDomain",
218 rdf_license_pd,
219 },
221 { _("FreeArt"),
222 "http://artlibre.org/licence.php/lalgb.html",
223 rdf_license_freeart,
224 },
226 { NULL, NULL, rdf_license_empty, }
227 };
229 #define XML_TAG_NAME_SVG "svg:svg"
230 #define XML_TAG_NAME_METADATA "svg:metadata"
231 #define XML_TAG_NAME_RDF "rdf:RDF"
232 #define XML_TAG_NAME_WORK "cc:Work"
233 #define XML_TAG_NAME_LICENSE "cc:License"
235 // Remember when using the "title" and "tip" elements to pass them through
236 // the localization functions when you use them!
237 struct rdf_work_entity_t rdf_work_entities [] = {
238 { "title", N_("Title"), "dc:title", RDF_CONTENT,
239 N_("Name by which this document is formally known."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
240 },
241 { "date", N_("Date"), "dc:date", RDF_CONTENT,
242 N_("Date associated with the creation of this document (YYYY-MM-DD)."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
243 },
244 { "format", N_("Format"), "dc:format", RDF_CONTENT,
245 N_("The physical or digital manifestation of this document (MIME type)."), RDF_FORMAT_LINE, RDF_EDIT_HARDCODED,
246 },
247 { "type", N_("Type"), "dc:type", RDF_RESOURCE,
248 N_("Type of document (DCMI Type)."), RDF_FORMAT_LINE, RDF_EDIT_HARDCODED,
249 },
251 { "creator", N_("Creator"), "dc:creator", RDF_AGENT,
252 N_("Name of entity primarily responsible for making the content of this document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
253 },
254 { "rights", N_("Rights"), "dc:rights", RDF_AGENT,
255 N_("Name of entity with rights to the Intellectual Property of this document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
256 },
257 { "publisher", N_("Publisher"), "dc:publisher", RDF_AGENT,
258 N_("Name of entity responsible for making this document available."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
259 },
261 { "identifier", N_("Identifier"), "dc:identifier", RDF_CONTENT,
262 N_("Unique URI to reference this document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
263 },
264 { "source", N_("Source"), "dc:source", RDF_CONTENT,
265 N_("Unique URI to reference the source of this document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
266 },
267 { "relation", N_("Relation"), "dc:relation", RDF_CONTENT,
268 N_("Unique URI to a related document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
269 },
270 { "language", N_("Language"), "dc:language", RDF_CONTENT,
271 N_("Two-letter language tag with optional subtags for the language of this document. (e.g. 'en-GB')"), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
272 },
273 { "subject", N_("Keywords"), "dc:subject", RDF_BAG,
274 N_("The topic of this document as comma-separated key words, phrases, or classifications."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
275 },
276 // TRANSLATORS: "Coverage": the spatial or temporal characteristics of the content.
277 // For info, see Appendix D of http://www.w3.org/TR/1998/WD-rdf-schema-19980409/
278 { "coverage", N_("Coverage"), "dc:coverage", RDF_CONTENT,
279 N_("Extent or scope of this document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
280 },
282 { "description", N_("Description"), "dc:description", RDF_CONTENT,
283 N_("A short account of the content of this document."), RDF_FORMAT_MULTILINE, RDF_EDIT_GENERIC,
284 },
286 // FIXME: need to handle 1 agent per line of input
287 { "contributor", N_("Contributors"), "dc:contributor", RDF_AGENT,
288 N_("Names of entities responsible for making contributions to the content of this document."), RDF_FORMAT_MULTILINE, RDF_EDIT_GENERIC,
289 },
291 // TRANSLATORS: URL to a page that defines the license for the document
292 { "license_uri", N_("URI"), "cc:license", RDF_RESOURCE,
293 // TRANSLATORS: this is where you put a URL to a page that defines the license
294 N_("URI to this document's license's namespace definition."), RDF_FORMAT_LINE, RDF_EDIT_SPECIAL,
295 },
297 // TRANSLATORS: fragment of XML representing the license of the document
298 { "license_fragment", N_("Fragment"), "License", RDF_XML,
299 N_("XML fragment for the RDF 'License' section."), RDF_FORMAT_MULTILINE, RDF_EDIT_SPECIAL,
300 },
302 { NULL, NULL, NULL, RDF_CONTENT,
303 NULL, RDF_FORMAT_LINE, RDF_EDIT_HARDCODED,
304 }
305 };
307 /**
308 * \brief Retrieves a known RDF/Work entity by name
309 * \return A pointer to an RDF/Work entity
310 * \param name The desired RDF/Work entity
311 *
312 */
313 struct rdf_work_entity_t *
314 rdf_find_entity(gchar const * name)
315 {
316 struct rdf_work_entity_t *entity;
317 for (entity=rdf_work_entities; entity->name; entity++) {
318 if (strcmp(entity->name,name)==0) break;
319 }
320 if (entity->name) return entity;
321 return NULL;
322 }
324 /*
325 * Takes the inkscape rdf struct and spits out a static RDF, which is only
326 * useful for testing. We must merge the rdf struct into the XML DOM for
327 * changes to be saved.
328 */
329 /*
331 Since g_markup_printf_escaped doesn't exist for most people's glib
332 right now, this function will remain commented out since it's only
333 for generic debug anyway. --Kees
335 gchar *
336 rdf_string(struct rdf_t * rdf)
337 {
338 gulong overall=0;
339 gchar *string=NULL;
341 gchar *rdf_head="\
342 <rdf:RDF xmlns=\"http://web.resource.org/cc/\"\
343 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\
344 xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\
345 ";
346 gchar *work_head="\
347 <Work rdf:about=\"\">\
348 <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" />\
349 ";
350 gchar *work_title=NULL;
351 gchar *work_date=NULL;
352 gchar *work_description=NULL;
353 gchar *work_creator=NULL;
354 gchar *work_owner=NULL;
355 gchar *work_source=NULL;
356 gchar *work_license=NULL;
357 gchar *license_head=NULL;
358 gchar *license=NULL;
359 gchar *license_end="</License>\n";
360 gchar *work_end="</Work>\n";
361 gchar *rdf_end="</rdf:RDF>\n";
363 if (rdf && rdf->work_title && rdf->work_title[0]) {
364 work_title=g_markup_printf_escaped(" <dc:title>%s</dc:title>\n",
365 rdf->work_title);
366 overall+=strlen(work_title);
367 }
368 if (rdf && rdf->work_date && rdf->work_date[0]) {
369 work_date=g_markup_printf_escaped(" <dc:date>%s</dc:date>\n",
370 rdf->work_date);
371 overall+=strlen(work_date);
372 }
373 if (rdf && rdf->work_description && rdf->work_description[0]) {
374 work_description=g_markup_printf_escaped(" <dc:description>%s</dc:description>\n",
375 rdf->work_description);
376 overall+=strlen(work_description);
377 }
378 if (rdf && rdf->work_creator && rdf->work_creator[0]) {
379 work_creator=g_markup_printf_escaped(" <dc:creator><Agent>\
380 <dc:title>%s</dc:title>\
381 </Agent></dc:creator>\n",
382 rdf->work_creator);
383 overall+=strlen(work_creator);
384 }
385 if (rdf && rdf->work_owner && rdf->work_owner[0]) {
386 work_owner=g_markup_printf_escaped(" <dc:rights><Agent>\
387 <dc:title>%s</dc:title>\
388 </Agent></dc:rights>\n",
389 rdf->work_owner);
390 overall+=strlen(work_owner);
391 }
392 if (rdf && rdf->work_source && rdf->work_source[0]) {
393 work_source=g_markup_printf_escaped(" <dc:source rdf:resource=\"%s\" />\n",
394 rdf->work_source);
395 overall+=strlen(work_source);
396 }
397 if (rdf && rdf->license && rdf->license->work_rdf && rdf->license->work_rdf[0]) {
398 work_license=g_markup_printf_escaped(" <license rdf:resource=\"%s\" />\n",
399 rdf->license->work_rdf);
400 overall+=strlen(work_license);
402 license_head=g_markup_printf_escaped("<License rdf:about=\"%s\">\n",
403 rdf->license->work_rdf);
404 overall+=strlen(license_head);
405 overall+=strlen(rdf->license->license_rdf);
406 overall+=strlen(license_end);
407 }
409 overall+=strlen(rdf_head)+strlen(rdf_end);
410 overall+=strlen(work_head)+strlen(work_end);
412 overall++; // NULL term
414 if (!(string=(gchar*)g_malloc(overall))) {
415 return NULL;
416 }
418 string[0]='\0';
419 strcat(string,rdf_head);
420 strcat(string,work_head);
422 if (work_title) strcat(string,work_title);
423 if (work_date) strcat(string,work_date);
424 if (work_description) strcat(string,work_description);
425 if (work_creator) strcat(string,work_creator);
426 if (work_owner) strcat(string,work_owner);
427 if (work_source) strcat(string,work_source);
428 if (work_license) strcat(string,work_license);
430 strcat(string,work_end);
431 if (license_head) {
432 strcat(string,license_head);
433 strcat(string,rdf->license->license_rdf);
434 strcat(string,license_end);
435 }
436 strcat(string,rdf_end);
438 return string;
439 }
440 */
443 /**
444 * \brief Pull the text out of an RDF entity, depends on how it's stored
445 * \return A pointer to the entity's static contents as a string
446 * \param repr The XML element to extract from
447 * \param entity The desired RDF/Work entity
448 *
449 */
450 const gchar *
451 rdf_get_repr_text ( Inkscape::XML::Node * repr, struct rdf_work_entity_t * entity )
452 {
453 g_return_val_if_fail (repr != NULL, NULL);
454 g_return_val_if_fail (entity != NULL, NULL);
455 static gchar * bag = NULL;
456 gchar * holder = NULL;
458 Inkscape::XML::Node * temp=NULL;
459 switch (entity->datatype) {
460 case RDF_CONTENT:
461 temp = sp_repr_children(repr);
462 if ( temp == NULL ) return NULL;
464 return temp->content();
466 case RDF_AGENT:
467 temp = sp_repr_lookup_name ( repr, "cc:Agent", 1 );
468 if ( temp == NULL ) return NULL;
470 temp = sp_repr_lookup_name ( temp, "dc:title", 1 );
471 if ( temp == NULL ) return NULL;
473 temp = sp_repr_children(temp);
474 if ( temp == NULL ) return NULL;
476 return temp->content();
478 case RDF_RESOURCE:
479 return repr->attribute("rdf:resource");
481 case RDF_XML:
482 return "xml goes here";
484 case RDF_BAG:
485 /* clear the static string. yucky. */
486 if (bag) g_free(bag);
487 bag = NULL;
489 temp = sp_repr_lookup_name ( repr, "rdf:Bag", 1 );
490 if ( temp == NULL ) {
491 /* backwards compatible: read contents */
492 temp = sp_repr_children(repr);
493 if ( temp == NULL ) return NULL;
495 return temp->content();
496 }
498 for ( temp = sp_repr_children(temp) ;
499 temp ;
500 temp = sp_repr_next(temp) ) {
501 if (!strcmp(temp->name(),"rdf:li") &&
502 temp->firstChild()) {
503 const gchar * str = temp->firstChild()->content();
504 if (bag) {
505 holder = bag;
506 bag = g_strconcat(holder, ", ", str, NULL);
507 g_free(holder);
508 }
509 else {
510 bag = g_strdup(str);
511 }
512 }
513 }
514 return bag;
516 default:
517 break;
518 }
519 return NULL;
520 }
522 unsigned int
523 rdf_set_repr_text ( Inkscape::XML::Node * repr,
524 struct rdf_work_entity_t * entity,
525 gchar const * text )
526 {
527 g_return_val_if_fail ( repr != NULL, 0);
528 g_return_val_if_fail ( entity != NULL, 0);
529 g_return_val_if_fail ( text != NULL, 0);
530 gchar * str = NULL;
531 gchar** strlist = NULL;
532 int i;
534 Inkscape::XML::Node * temp=NULL;
535 Inkscape::XML::Node * child=NULL;
536 Inkscape::XML::Node * parent=repr;
537 switch (entity->datatype) {
538 case RDF_CONTENT:
539 temp = sp_repr_children(parent);
540 if ( temp == NULL ) {
541 temp = sp_repr_new_text( text );
542 g_return_val_if_fail (temp != NULL, 0);
544 parent->appendChild(temp);
545 Inkscape::GC::release(temp);
547 return TRUE;
548 }
549 else {
550 temp->setContent(text);
551 return TRUE;
552 }
554 case RDF_AGENT:
555 temp = sp_repr_lookup_name ( parent, "cc:Agent", 1 );
556 if ( temp == NULL ) {
557 temp = sp_repr_new ( "cc:Agent" );
558 g_return_val_if_fail (temp != NULL, 0);
560 parent->appendChild(temp);
561 Inkscape::GC::release(temp);
562 }
563 parent = temp;
565 temp = sp_repr_lookup_name ( parent, "dc:title", 1 );
566 if ( temp == NULL ) {
567 temp = sp_repr_new ( "dc:title" );
568 g_return_val_if_fail (temp != NULL, 0);
570 parent->appendChild(temp);
571 Inkscape::GC::release(temp);
572 }
573 parent = temp;
575 temp = sp_repr_children(parent);
576 if ( temp == NULL ) {
577 temp = sp_repr_new_text( text );
578 g_return_val_if_fail (temp != NULL, 0);
580 parent->appendChild(temp);
581 Inkscape::GC::release(temp);
583 return TRUE;
584 }
585 else {
586 temp->setContent(text);
587 return TRUE;
588 }
590 case RDF_RESOURCE:
591 parent->setAttribute("rdf:resource", text );
592 return true;
594 case RDF_XML:
595 return 1;
597 case RDF_BAG:
598 /* find/create the rdf:Bag item */
599 temp = sp_repr_lookup_name ( parent, "rdf:Bag", 1 );
600 if ( temp == NULL ) {
601 /* backward compatibility: drop the dc:subject contents */
602 while ( (temp = sp_repr_children( parent )) ) {
603 parent->removeChild(temp);
604 }
606 temp = sp_repr_new ( "rdf:Bag" );
607 g_return_val_if_fail (temp != NULL, 0);
609 parent->appendChild(temp);
610 Inkscape::GC::release(temp);
611 }
612 parent = temp;
614 /* toss all the old list items */
615 while ( (temp = sp_repr_children( parent )) ) {
616 parent->removeChild(temp);
617 }
619 /* chop our list up on commas */
620 strlist = g_strsplit( text, ",", 0);
622 for (i = 0; (str = strlist[i]); i++) {
623 temp = sp_repr_new ( "rdf:li" );
624 g_return_val_if_fail (temp != NULL, 0);
626 parent->appendChild(temp);
627 Inkscape::GC::release(temp);
629 child = sp_repr_new_text( g_strstrip(str) );
630 g_return_val_if_fail (child != NULL, 0);
632 temp->appendChild(child);
633 Inkscape::GC::release(child);
634 }
635 g_strfreev( strlist );
637 return 1;
639 default:
640 break;
641 }
642 return 0;
643 }
645 Inkscape::XML::Node *
646 rdf_get_rdf_root_repr ( SPDocument * doc, bool build )
647 {
648 g_return_val_if_fail (doc != NULL, NULL);
649 g_return_val_if_fail (doc->rroot != NULL, NULL);
651 Inkscape::XML::Node * rdf = sp_repr_lookup_name ( doc->rroot, XML_TAG_NAME_RDF );
653 if (rdf == NULL) {
654 //printf("missing XML '%s'\n",XML_TAG_NAME_RDF);
655 if (!build) return NULL;
657 Inkscape::XML::Node * svg = sp_repr_lookup_name ( doc->rroot, XML_TAG_NAME_SVG );
658 g_return_val_if_fail ( svg != NULL, NULL );
660 Inkscape::XML::Node * parent = sp_repr_lookup_name ( svg, XML_TAG_NAME_METADATA );
661 if ( parent == NULL ) {
662 parent = sp_repr_new( XML_TAG_NAME_METADATA );
663 g_return_val_if_fail ( parent != NULL, NULL);
665 svg->appendChild(parent);
666 Inkscape::GC::release(parent);
667 }
669 rdf = sp_repr_new( XML_TAG_NAME_RDF );
670 g_return_val_if_fail (rdf != NULL, NULL);
672 parent->appendChild(rdf);
673 Inkscape::GC::release(rdf);
674 }
676 /*
677 * some implementations do not put RDF stuff inside <metadata>,
678 * so we need to check for it and add it if we don't see it
679 */
680 Inkscape::XML::Node * want_metadata = sp_repr_parent ( rdf );
681 g_return_val_if_fail (want_metadata != NULL, NULL);
682 if (strcmp( want_metadata->name(), XML_TAG_NAME_METADATA )) {
683 Inkscape::XML::Node * metadata = sp_repr_new( XML_TAG_NAME_METADATA );
684 g_return_val_if_fail (metadata != NULL, NULL);
686 /* attach the metadata node */
687 want_metadata->appendChild(metadata);
688 Inkscape::GC::release(metadata);
690 /* move the RDF into it */
691 Inkscape::GC::anchor(rdf);
692 sp_repr_unparent ( rdf );
693 metadata->appendChild(rdf);
694 Inkscape::GC::release(rdf);
695 }
697 return rdf;
698 }
700 Inkscape::XML::Node *
701 rdf_get_xml_repr( SPDocument * doc, gchar const * name, bool build )
702 {
703 g_return_val_if_fail (name != NULL, NULL);
704 g_return_val_if_fail (doc != NULL, NULL);
705 g_return_val_if_fail (doc->rroot != NULL, NULL);
707 Inkscape::XML::Node * rdf = rdf_get_rdf_root_repr ( doc, build );
708 if (!rdf) return NULL;
710 Inkscape::XML::Node * xml = sp_repr_lookup_name ( rdf, name );
711 if (xml == NULL) {
712 //printf("missing XML '%s'\n",name);
713 if (!build) return NULL;
715 xml = sp_repr_new( name );
716 g_return_val_if_fail (xml != NULL, NULL);
718 xml->setAttribute("rdf:about", "" );
720 rdf->appendChild(xml);
721 Inkscape::GC::release(xml);
722 }
724 return xml;
725 }
727 Inkscape::XML::Node *
728 rdf_get_work_repr( SPDocument * doc, gchar const * name, bool build )
729 {
730 g_return_val_if_fail (name != NULL, NULL);
731 g_return_val_if_fail (doc != NULL, NULL);
732 g_return_val_if_fail (doc->rroot != NULL, NULL);
734 Inkscape::XML::Node * work = rdf_get_xml_repr ( doc, XML_TAG_NAME_WORK, build );
735 if (!work) return NULL;
737 Inkscape::XML::Node * item = sp_repr_lookup_name ( work, name, 1 );
738 if (item == NULL) {
739 //printf("missing XML '%s'\n",name);
740 if (!build) return NULL;
742 item = sp_repr_new( name );
743 g_return_val_if_fail (item != NULL, NULL);
745 work->appendChild(item);
746 Inkscape::GC::release(item);
747 }
749 return item;
750 }
754 /**
755 * \brief Retrieves a known RDF/Work entity's contents from the document XML by name
756 * \return A pointer to the entity's static contents as a string, or NULL if no entity exists
757 * \param entity The desired RDF/Work entity
758 *
759 */
760 const gchar *
761 rdf_get_work_entity(SPDocument * doc, struct rdf_work_entity_t * entity)
762 {
763 g_return_val_if_fail (doc != NULL, NULL);
764 if ( entity == NULL ) return NULL;
765 //printf("want '%s'\n",entity->title);
767 Inkscape::XML::Node * item;
768 if ( entity->datatype == RDF_XML ) {
769 item = rdf_get_xml_repr ( doc, entity->tag, FALSE );
770 }
771 else {
772 item = rdf_get_work_repr( doc, entity->tag, FALSE );
773 }
774 if ( item == NULL ) return NULL;
776 const gchar * result = rdf_get_repr_text ( item, entity );
777 //printf("found '%s' == '%s'\n", entity->title, result );
778 return result;
779 }
781 /**
782 * \brief Stores a string into a named RDF/Work entity in the document XML
783 * \param entity The desired RDF/Work entity to replace
784 * \param string The string to replace the entity contents with
785 *
786 */
787 unsigned int
788 rdf_set_work_entity(SPDocument * doc, struct rdf_work_entity_t * entity,
789 const gchar * text)
790 {
791 g_return_val_if_fail ( entity != NULL, 0 );
792 if (text == NULL) {
793 // FIXME: on a "NULL" text, delete the entity. For now, blank it.
794 text="";
795 }
797 /*
798 printf("changing '%s' (%s) to '%s'\n",
799 entity->title,
800 entity->tag,
801 text);
802 */
804 Inkscape::XML::Node * item = rdf_get_work_repr( doc, entity->tag, TRUE );
805 g_return_val_if_fail ( item != NULL, 0 );
807 return rdf_set_repr_text ( item, entity, text );
808 }
810 #undef DEBUG_MATCH
812 bool
813 rdf_match_license ( Inkscape::XML::Node * repr, struct rdf_license_t * license )
814 {
815 g_assert ( repr != NULL );
816 g_assert ( license != NULL );
818 bool result=TRUE;
819 #ifdef DEBUG_MATCH
820 printf("checking against '%s'\n",license->name);
821 #endif
823 int count = 0;
824 for (struct rdf_double_t * details = license->details;
825 details->name; details++ ) {
826 count++;
827 }
828 bool * matched = (bool*)calloc(count,sizeof(bool));
830 for (Inkscape::XML::Node * current = sp_repr_children ( repr );
831 current;
832 current = sp_repr_next ( current ) ) {
834 gchar const * attr = current->attribute("rdf:resource");
835 if ( attr == NULL ) continue;
837 #ifdef DEBUG_MATCH
838 printf("\texamining '%s' => '%s'\n", current->name(), attr);
839 #endif
841 bool found_match=FALSE;
842 for (int i=0; i<count; i++) {
843 // skip already matched items
844 if (matched[i]) continue;
846 #ifdef DEBUG_MATCH
847 printf("\t\t'%s' vs '%s'\n", current->name(), license->details[i].name);
848 printf("\t\t'%s' vs '%s'\n", attr, license->details[i].resource);
849 #endif
851 if (!strcmp( current->name(),
852 license->details[i].name ) &&
853 !strcmp( attr,
854 license->details[i].resource )) {
855 matched[i]=TRUE;
856 found_match=TRUE;
857 #ifdef DEBUG_MATCH
858 printf("\t\tgood!\n");
859 #endif
860 break;
861 }
862 }
863 if (!found_match) {
864 // if we checked each known item of the license
865 // and didn't find it, we must abort
866 result=FALSE;
867 #ifdef DEBUG_MATCH
868 printf("\t\tno '%s' element matched XML (bong)!\n",license->name);
869 #endif
870 break;
871 }
872 }
873 #ifdef DEBUG_MATCH
874 if (result) printf("\t\tall XML found matching elements!\n");
875 #endif
876 for (int i=0; result && i<count; i++) {
877 // scan looking for an unmatched item
878 if (matched[i]==0) {
879 result=FALSE;
880 #ifdef DEBUG_MATCH
881 printf("\t\tnot all '%s' elements used to match (bong)!\n", license->name);
882 #endif
883 }
884 }
886 #ifdef DEBUG_MATCH
887 printf("\t\tall '%s' elements used to match!\n",license->name);
888 #endif
890 free(matched);
892 #ifdef DEBUG_MATCH
893 if (result) printf("matched '%s'\n",license->name);
894 #endif
895 return result;
896 }
898 /**
899 * \brief Attempts to match and retrieve a known RDF/License from the document XML
900 * \return A pointer to the static RDF license structure
901 *
902 */
903 struct rdf_license_t *
904 rdf_get_license(SPDocument * document)
905 {
906 Inkscape::XML::Node * repr = rdf_get_xml_repr ( document, XML_TAG_NAME_LICENSE, FALSE );
907 if (repr) {
908 for (struct rdf_license_t * license = rdf_licenses;
909 license->name; license++ ) {
910 if ( rdf_match_license ( repr, license ) ) return license;
911 }
912 }
913 #ifdef DEBUG_MATCH
914 else {
915 printf("no license XML\n");
916 }
917 #endif
918 return NULL;
919 }
921 /**
922 * \brief Stores an RDF/License XML in the document XML
923 * \param document Which document to update
924 * \param license The desired RDF/License structure to store; NULL drops old license, so can be used for proprietary license.
925 *
926 */
927 void
928 rdf_set_license(SPDocument * document, struct rdf_license_t const * license)
929 {
930 // drop old license section
931 Inkscape::XML::Node * repr = rdf_get_xml_repr ( document, XML_TAG_NAME_LICENSE, FALSE );
932 if (repr) sp_repr_unparent(repr);
934 if (!license) return;
936 // build new license section
937 repr = rdf_get_xml_repr ( document, XML_TAG_NAME_LICENSE, TRUE );
938 g_assert ( repr );
940 repr->setAttribute("rdf:about", license->uri );
942 for (struct rdf_double_t * detail = license->details;
943 detail->name; detail++) {
944 Inkscape::XML::Node * child = sp_repr_new( detail->name );
945 g_assert ( child != NULL );
947 child->setAttribute("rdf:resource", detail->resource );
948 repr->appendChild(child);
949 Inkscape::GC::release(child);
950 }
951 }
953 struct rdf_entity_default_t {
954 gchar const * name;
955 gchar const * text;
956 };
957 struct rdf_entity_default_t rdf_defaults[] = {
958 { "format", "image/svg+xml", },
959 { "type", "http://purl.org/dc/dcmitype/StillImage", },
960 { NULL, NULL, }
961 };
963 void
964 rdf_set_defaults ( SPDocument * document )
965 {
966 g_assert ( document != NULL );
968 // Create metadata node if it doesn't already exist
969 if (!sp_item_group_get_child_by_name ((SPGroup *) document->root, NULL,
970 XML_TAG_NAME_METADATA)) {
971 // create repr
972 Inkscape::XML::Node * rnew = sp_repr_new (XML_TAG_NAME_METADATA);
973 // insert into the document
974 document->rroot->addChild(rnew, NULL);
975 // clean up
976 Inkscape::GC::release(rnew);
977 }
979 /* install defaults */
980 for ( struct rdf_entity_default_t * rdf_default = rdf_defaults;
981 rdf_default->name;
982 rdf_default++) {
983 struct rdf_work_entity_t * entity = rdf_find_entity ( rdf_default->name );
984 g_assert ( entity != NULL );
986 if ( rdf_get_work_entity ( document, entity ) == NULL ) {
987 rdf_set_work_entity ( document, entity, rdf_default->text );
988 }
989 }
990 }
993 /*
994 Local Variables:
995 mode:c++
996 c-file-style:"stroustrup"
997 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
998 indent-tabs-mode:nil
999 fill-column:99
1000 End:
1001 */
1002 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :