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 * Jon Phillips <jon@rejon.org>
9 *
10 * Copyright (C) 2004 Kees Cook <kees@outflux.net>
11 * Copyright (C) 2006 Jon Phillips <jon@rejon.org>
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 *
15 */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
23 #include "xml/repr.h"
24 #include "rdf.h"
25 #include "sp-item-group.h"
26 #include "prefs-utils.h"
28 /*
30 Example RDF XML from various places...
32 <rdf:RDF xmlns="http://creativecommons.org/ns#"
33 xmlns:dc="http://purl.org/dc/elements/1.1/"
34 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
35 <Work rdf:about="">
36 <dc:title>title of work</dc:title>
37 <dc:date>year</dc:date>
38 <dc:description>description of work</dc:description>
39 <dc:creator><Agent>
40 <dc:title>creator</dc:title>
41 </Agent></dc:creator>
42 <dc:rights><Agent>
43 <dc:title>holder</dc:title>
44 </Agent></dc:rights>
45 <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
46 <dc:source rdf:resource="source"/>
47 <license rdf:resource="http://creativecommons.org/licenses/by/2.0/"
48 />
49 </Work>
52 <rdf:RDF xmlns="http://creativecommons.org/ns#"
53 xmlns:dc="http://purl.org/dc/elements/1.1/"
54 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
55 <Work rdf:about="">
56 <dc:title>SVG Road Signs</dc:title>
57 <dc:rights><Agent>
58 <dc:title>John Cliff</dc:title>
59 </Agent></dc:rights>
60 <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
61 <license rdf:resource="http://creativecommons.org/ns#PublicDomain" />
62 </Work>
64 <License rdf:about="http://creativecommons.org/ns#PublicDomain">
65 <permits rdf:resource="http://creativecommons.org/ns#Reproduction" />
66 <permits rdf:resource="http://creativecommons.org/ns#Distribution" />
67 <permits rdf:resource="http://creativecommons.org/ns#DerivativeWorks" />
68 </License>
70 </rdf:RDF>
73 Bag example:
75 <dc:subject>
76 <rdf:Bag>
77 <rdf:li>open clip art logo</rdf:li>
78 <rdf:li>images</rdf:li>
79 <rdf:li>logo</rdf:li>
80 <rdf:li>clip art</rdf:li>
81 <rdf:li>ocal</rdf:li>
82 <rdf:li>logotype</rdf:li>
83 <rdf:li>filetype</rdf:li>
84 </rdf:Bag>
85 </dc:subject>
88 */
90 struct rdf_double_t rdf_license_empty [] = {
91 { NULL, NULL }
92 };
94 struct rdf_double_t rdf_license_cc_a [] = {
95 { "cc:permits", "http://creativecommons.org/ns#Reproduction", },
96 { "cc:permits", "http://creativecommons.org/ns#Distribution", },
97 { "cc:requires", "http://creativecommons.org/ns#Notice", },
98 { "cc:requires", "http://creativecommons.org/ns#Attribution", },
99 { "cc:permits", "http://creativecommons.org/ns#DerivativeWorks", },
100 { NULL, NULL }
101 };
103 struct rdf_double_t rdf_license_cc_a_sa [] = {
104 { "cc:permits", "http://creativecommons.org/ns#Reproduction", },
105 { "cc:permits", "http://creativecommons.org/ns#Distribution", },
106 { "cc:requires", "http://creativecommons.org/ns#Notice", },
107 { "cc:requires", "http://creativecommons.org/ns#Attribution", },
108 { "cc:permits", "http://creativecommons.org/ns#DerivativeWorks", },
109 { "cc:requires", "http://creativecommons.org/ns#ShareAlike", },
110 { NULL, NULL }
111 };
113 struct rdf_double_t rdf_license_cc_a_nd [] = {
114 { "cc:permits", "http://creativecommons.org/ns#Reproduction", },
115 { "cc:permits", "http://creativecommons.org/ns#Distribution", },
116 { "cc:requires", "http://creativecommons.org/ns#Notice", },
117 { "cc:requires", "http://creativecommons.org/ns#Attribution", },
118 { NULL, NULL }
119 };
121 struct rdf_double_t rdf_license_cc_a_nc [] = {
122 { "cc:permits", "http://creativecommons.org/ns#Reproduction", },
123 { "cc:permits", "http://creativecommons.org/ns#Distribution", },
124 { "cc:requires", "http://creativecommons.org/ns#Notice", },
125 { "cc:requires", "http://creativecommons.org/ns#Attribution", },
126 { "cc:prohibits", "http://creativecommons.org/ns#CommercialUse", },
127 { "cc:permits", "http://creativecommons.org/ns#DerivativeWorks", },
128 { NULL, NULL }
129 };
131 struct rdf_double_t rdf_license_cc_a_nc_sa [] = {
132 { "cc:permits", "http://creativecommons.org/ns#Reproduction", },
133 { "cc:permits", "http://creativecommons.org/ns#Distribution", },
134 { "cc:requires", "http://creativecommons.org/ns#Notice", },
135 { "cc:requires", "http://creativecommons.org/ns#Attribution", },
136 { "cc:prohibits", "http://creativecommons.org/ns#CommercialUse", },
137 { "cc:permits", "http://creativecommons.org/ns#DerivativeWorks", },
138 { "cc:requires", "http://creativecommons.org/ns#ShareAlike", },
139 { NULL, NULL }
140 };
142 struct rdf_double_t rdf_license_cc_a_nc_nd [] = {
143 { "cc:permits", "http://creativecommons.org/ns#Reproduction", },
144 { "cc:permits", "http://creativecommons.org/ns#Distribution", },
145 { "cc:requires", "http://creativecommons.org/ns#Notice", },
146 { "cc:requires", "http://creativecommons.org/ns#Attribution", },
147 { "cc:prohibits", "http://creativecommons.org/ns#CommercialUse", },
148 { NULL, NULL }
149 };
151 struct rdf_double_t rdf_license_pd [] = {
152 { "cc:permits", "http://creativecommons.org/ns#Reproduction", },
153 { "cc:permits", "http://creativecommons.org/ns#Distribution", },
154 { "cc:permits", "http://creativecommons.org/ns#DerivativeWorks", },
155 { NULL, NULL }
156 };
158 struct rdf_double_t rdf_license_freeart [] = {
159 { "cc:permits", "http://creativecommons.org/ns#Reproduction", },
160 { "cc:permits", "http://creativecommons.org/ns#Distribution", },
161 { "cc:permits", "http://creativecommons.org/ns#DerivativeWorks", },
162 { "cc:requires", "http://creativecommons.org/ns#ShareAlike", },
163 { "cc:requires", "http://creativecommons.org/ns#Notice", },
164 { "cc:requires", "http://creativecommons.org/ns#Attribution", },
165 { NULL, NULL }
166 };
168 struct rdf_double_t rdf_license_ofl [] = {
169 { "cc:permits", "http://scripts.sil.org/pub/OFL/Reproduction", },
170 { "cc:permits", "http://scripts.sil.org/pub/OFL/Distribution", },
171 { "cc:permits", "http://scripts.sil.org/pub/OFL/Embedding", },
172 { "cc:permits", "http://scripts.sil.org/pub/OFL/DerivativeWorks", },
173 { "cc:requires", "http://scripts.sil.org/pub/OFL/Notice", },
174 { "cc:requires", "http://scripts.sil.org/pub/OFL/Attribution", },
175 { "cc:requires", "http://scripts.sil.org/pub/OFL/ShareAlike", },
176 { "cc:requires", "http://scripts.sil.org/pub/OFL/DerivativeRenaming", },
177 { "cc:requires", "http://scripts.sil.org/pub/OFL/BundlingWhenSelling", },
178 { NULL, NULL }
179 };
181 struct rdf_license_t rdf_licenses [] = {
182 { N_("CC Attribution"),
183 "http://creativecommons.org/licenses/by/3.0/",
184 rdf_license_cc_a,
185 },
187 { N_("CC Attribution-ShareAlike"),
188 "http://creativecommons.org/licenses/by-sa/3.0/",
189 rdf_license_cc_a_sa,
190 },
192 { N_("CC Attribution-NoDerivs"),
193 "http://creativecommons.org/licenses/by-nd/3.0/",
194 rdf_license_cc_a_nd,
195 },
197 { N_("CC Attribution-NonCommercial"),
198 "http://creativecommons.org/licenses/by-nc/3.0/",
199 rdf_license_cc_a_nc,
200 },
202 { N_("CC Attribution-NonCommercial-ShareAlike"),
203 "http://creativecommons.org/licenses/by-nc-sa/3.0/",
204 rdf_license_cc_a_nc_sa,
205 },
207 { N_("CC Attribution-NonCommercial-NoDerivs"),
208 "http://creativecommons.org/licenses/by-nc-nd/3.0/",
209 rdf_license_cc_a_nc_nd,
210 },
212 { N_("Public Domain"),
213 "http://creativecommons.org/licenses/publicdomain/",
214 rdf_license_pd,
215 },
217 { N_("FreeArt"),
218 "http://artlibre.org/licence.php/lalgb.html",
219 rdf_license_freeart,
220 },
222 { N_("Open Font License"),
223 "http://scripts.sil.org/OFL",
224 rdf_license_ofl,
225 },
227 { NULL, NULL, rdf_license_empty, }
228 };
230 #define XML_TAG_NAME_SVG "svg:svg"
231 #define XML_TAG_NAME_METADATA "svg:metadata"
232 #define XML_TAG_NAME_RDF "rdf:RDF"
233 #define XML_TAG_NAME_WORK "cc:Work"
234 #define XML_TAG_NAME_LICENSE "cc:License"
236 // Remember when using the "title" and "tip" elements to pass them through
237 // the localization functions when you use them!
238 struct rdf_work_entity_t rdf_work_entities [] = {
239 { "title", N_("Title"), "dc:title", RDF_CONTENT,
240 N_("Name by which this document is formally known."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
241 },
242 { "date", N_("Date"), "dc:date", RDF_CONTENT,
243 N_("Date associated with the creation of this document (YYYY-MM-DD)."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
244 },
245 { "format", N_("Format"), "dc:format", RDF_CONTENT,
246 N_("The physical or digital manifestation of this document (MIME type)."), RDF_FORMAT_LINE, RDF_EDIT_HARDCODED,
247 },
248 { "type", N_("Type"), "dc:type", RDF_RESOURCE,
249 N_("Type of document (DCMI Type)."), RDF_FORMAT_LINE, RDF_EDIT_HARDCODED,
250 },
252 { "creator", N_("Creator"), "dc:creator", RDF_AGENT,
253 N_("Name of entity primarily responsible for making the content of this document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
254 },
255 { "rights", N_("Rights"), "dc:rights", RDF_AGENT,
256 N_("Name of entity with rights to the Intellectual Property of this document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
257 },
258 { "publisher", N_("Publisher"), "dc:publisher", RDF_AGENT,
259 N_("Name of entity responsible for making this document available."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
260 },
262 { "identifier", N_("Identifier"), "dc:identifier", RDF_CONTENT,
263 N_("Unique URI to reference this document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
264 },
265 { "source", N_("Source"), "dc:source", RDF_CONTENT,
266 N_("Unique URI to reference the source of this document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
267 },
268 { "relation", N_("Relation"), "dc:relation", RDF_CONTENT,
269 N_("Unique URI to a related document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
270 },
271 { "language", N_("Language"), "dc:language", RDF_CONTENT,
272 N_("Two-letter language tag with optional subtags for the language of this document. (e.g. 'en-GB')"), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
273 },
274 { "subject", N_("Keywords"), "dc:subject", RDF_BAG,
275 N_("The topic of this document as comma-separated key words, phrases, or classifications."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
276 },
277 // TRANSLATORS: "Coverage": the spatial or temporal characteristics of the content.
278 // For info, see Appendix D of http://www.w3.org/TR/1998/WD-rdf-schema-19980409/
279 { "coverage", N_("Coverage"), "dc:coverage", RDF_CONTENT,
280 N_("Extent or scope of this document."), RDF_FORMAT_LINE, RDF_EDIT_GENERIC,
281 },
283 { "description", N_("Description"), "dc:description", RDF_CONTENT,
284 N_("A short account of the content of this document."), RDF_FORMAT_MULTILINE, RDF_EDIT_GENERIC,
285 },
287 // FIXME: need to handle 1 agent per line of input
288 { "contributor", N_("Contributors"), "dc:contributor", RDF_AGENT,
289 N_("Names of entities responsible for making contributions to the content of this document."), RDF_FORMAT_MULTILINE, RDF_EDIT_GENERIC,
290 },
292 // TRANSLATORS: URL to a page that defines the license for the document
293 { "license_uri", N_("URI"), "cc:license", RDF_RESOURCE,
294 // TRANSLATORS: this is where you put a URL to a page that defines the license
295 N_("URI to this document's license's namespace definition."), RDF_FORMAT_LINE, RDF_EDIT_SPECIAL,
296 },
298 // TRANSLATORS: fragment of XML representing the license of the document
299 { "license_fragment", N_("Fragment"), "License", RDF_XML,
300 N_("XML fragment for the RDF 'License' section."), RDF_FORMAT_MULTILINE, RDF_EDIT_SPECIAL,
301 },
303 { NULL, NULL, NULL, RDF_CONTENT,
304 NULL, RDF_FORMAT_LINE, RDF_EDIT_HARDCODED,
305 }
306 };
308 /**
309 * \brief Retrieves a known RDF/Work entity by name
310 * \return A pointer to an RDF/Work entity
311 * \param name The desired RDF/Work entity
312 *
313 */
314 struct rdf_work_entity_t *
315 rdf_find_entity(gchar const * name)
316 {
317 struct rdf_work_entity_t *entity;
318 for (entity=rdf_work_entities; entity->name; entity++) {
319 if (strcmp(entity->name,name)==0) break;
320 }
321 if (entity->name) return entity;
322 return NULL;
323 }
325 /*
326 * Takes the inkscape rdf struct and spits out a static RDF, which is only
327 * useful for testing. We must merge the rdf struct into the XML DOM for
328 * changes to be saved.
329 */
330 /*
332 Since g_markup_printf_escaped doesn't exist for most people's glib
333 right now, this function will remain commented out since it's only
334 for generic debug anyway. --Kees
336 gchar *
337 rdf_string(struct rdf_t * rdf)
338 {
339 gulong overall=0;
340 gchar *string=NULL;
342 gchar *rdf_head="\
343 <rdf:RDF xmlns=\"http://creativecommons.org/ns#\"\
344 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\
345 xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\
346 ";
347 gchar *work_head="\
348 <Work rdf:about=\"\">\
349 <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" />\
350 ";
351 gchar *work_title=NULL;
352 gchar *work_date=NULL;
353 gchar *work_description=NULL;
354 gchar *work_creator=NULL;
355 gchar *work_owner=NULL;
356 gchar *work_source=NULL;
357 gchar *work_license=NULL;
358 gchar *license_head=NULL;
359 gchar *license=NULL;
360 gchar *license_end="</License>\n";
361 gchar *work_end="</Work>\n";
362 gchar *rdf_end="</rdf:RDF>\n";
364 if (rdf && rdf->work_title && rdf->work_title[0]) {
365 work_title=g_markup_printf_escaped(" <dc:title>%s</dc:title>\n",
366 rdf->work_title);
367 overall+=strlen(work_title);
368 }
369 if (rdf && rdf->work_date && rdf->work_date[0]) {
370 work_date=g_markup_printf_escaped(" <dc:date>%s</dc:date>\n",
371 rdf->work_date);
372 overall+=strlen(work_date);
373 }
374 if (rdf && rdf->work_description && rdf->work_description[0]) {
375 work_description=g_markup_printf_escaped(" <dc:description>%s</dc:description>\n",
376 rdf->work_description);
377 overall+=strlen(work_description);
378 }
379 if (rdf && rdf->work_creator && rdf->work_creator[0]) {
380 work_creator=g_markup_printf_escaped(" <dc:creator><Agent>\
381 <dc:title>%s</dc:title>\
382 </Agent></dc:creator>\n",
383 rdf->work_creator);
384 overall+=strlen(work_creator);
385 }
386 if (rdf && rdf->work_owner && rdf->work_owner[0]) {
387 work_owner=g_markup_printf_escaped(" <dc:rights><Agent>\
388 <dc:title>%s</dc:title>\
389 </Agent></dc:rights>\n",
390 rdf->work_owner);
391 overall+=strlen(work_owner);
392 }
393 if (rdf && rdf->work_source && rdf->work_source[0]) {
394 work_source=g_markup_printf_escaped(" <dc:source rdf:resource=\"%s\" />\n",
395 rdf->work_source);
396 overall+=strlen(work_source);
397 }
398 if (rdf && rdf->license && rdf->license->work_rdf && rdf->license->work_rdf[0]) {
399 work_license=g_markup_printf_escaped(" <license rdf:resource=\"%s\" />\n",
400 rdf->license->work_rdf);
401 overall+=strlen(work_license);
403 license_head=g_markup_printf_escaped("<License rdf:about=\"%s\">\n",
404 rdf->license->work_rdf);
405 overall+=strlen(license_head);
406 overall+=strlen(rdf->license->license_rdf);
407 overall+=strlen(license_end);
408 }
410 overall+=strlen(rdf_head)+strlen(rdf_end);
411 overall+=strlen(work_head)+strlen(work_end);
413 overall++; // NULL term
415 if (!(string=(gchar*)g_malloc(overall))) {
416 return NULL;
417 }
419 string[0]='\0';
420 strcat(string,rdf_head);
421 strcat(string,work_head);
423 if (work_title) strcat(string,work_title);
424 if (work_date) strcat(string,work_date);
425 if (work_description) strcat(string,work_description);
426 if (work_creator) strcat(string,work_creator);
427 if (work_owner) strcat(string,work_owner);
428 if (work_source) strcat(string,work_source);
429 if (work_license) strcat(string,work_license);
431 strcat(string,work_end);
432 if (license_head) {
433 strcat(string,license_head);
434 strcat(string,rdf->license->license_rdf);
435 strcat(string,license_end);
436 }
437 strcat(string,rdf_end);
439 return string;
440 }
441 */
444 /**
445 * \brief Pull the text out of an RDF entity, depends on how it's stored
446 * \return A pointer to the entity's static contents as a string
447 * \param repr The XML element to extract from
448 * \param entity The desired RDF/Work entity
449 *
450 */
451 const gchar *
452 rdf_get_repr_text ( Inkscape::XML::Node * repr, struct rdf_work_entity_t * entity )
453 {
454 g_return_val_if_fail (repr != NULL, NULL);
455 g_return_val_if_fail (entity != NULL, NULL);
456 static gchar * bag = NULL;
457 gchar * holder = NULL;
459 Inkscape::XML::Node * temp=NULL;
460 switch (entity->datatype) {
461 case RDF_CONTENT:
462 temp = sp_repr_children(repr);
463 if ( temp == NULL ) return NULL;
465 return temp->content();
467 case RDF_AGENT:
468 temp = sp_repr_lookup_name ( repr, "cc:Agent", 1 );
469 if ( temp == NULL ) return NULL;
471 temp = sp_repr_lookup_name ( temp, "dc:title", 1 );
472 if ( temp == NULL ) return NULL;
474 temp = sp_repr_children(temp);
475 if ( temp == NULL ) return NULL;
477 return temp->content();
479 case RDF_RESOURCE:
480 return repr->attribute("rdf:resource");
482 case RDF_XML:
483 return "xml goes here";
485 case RDF_BAG:
486 /* clear the static string. yucky. */
487 if (bag) g_free(bag);
488 bag = NULL;
490 temp = sp_repr_lookup_name ( repr, "rdf:Bag", 1 );
491 if ( temp == NULL ) {
492 /* backwards compatible: read contents */
493 temp = sp_repr_children(repr);
494 if ( temp == NULL ) return NULL;
496 return temp->content();
497 }
499 for ( temp = sp_repr_children(temp) ;
500 temp ;
501 temp = sp_repr_next(temp) ) {
502 if (!strcmp(temp->name(),"rdf:li") &&
503 temp->firstChild()) {
504 const gchar * str = temp->firstChild()->content();
505 if (bag) {
506 holder = bag;
507 bag = g_strconcat(holder, ", ", str, NULL);
508 g_free(holder);
509 }
510 else {
511 bag = g_strdup(str);
512 }
513 }
514 }
515 return bag;
517 default:
518 break;
519 }
520 return NULL;
521 }
523 unsigned int
524 rdf_set_repr_text ( Inkscape::XML::Node * repr,
525 struct rdf_work_entity_t * entity,
526 gchar const * text )
527 {
528 g_return_val_if_fail ( repr != NULL, 0);
529 g_return_val_if_fail ( entity != NULL, 0);
530 g_return_val_if_fail ( text != NULL, 0);
531 gchar * str = NULL;
532 gchar** strlist = NULL;
533 int i;
535 Inkscape::XML::Node * temp=NULL;
536 Inkscape::XML::Node * child=NULL;
537 Inkscape::XML::Node * parent=repr;
539 Inkscape::XML::Document * xmldoc = parent->document();
540 g_return_val_if_fail (xmldoc != NULL, FALSE);
542 switch (entity->datatype) {
543 case RDF_CONTENT:
544 temp = sp_repr_children(parent);
545 if ( temp == NULL ) {
546 temp = xmldoc->createTextNode( text );
547 g_return_val_if_fail (temp != NULL, FALSE);
549 parent->appendChild(temp);
550 Inkscape::GC::release(temp);
552 return TRUE;
553 }
554 else {
555 temp->setContent(text);
556 return TRUE;
557 }
559 case RDF_AGENT:
560 temp = sp_repr_lookup_name ( parent, "cc:Agent", 1 );
561 if ( temp == NULL ) {
562 temp = xmldoc->createElement ( "cc:Agent" );
563 g_return_val_if_fail (temp != NULL, FALSE);
565 parent->appendChild(temp);
566 Inkscape::GC::release(temp);
567 }
568 parent = temp;
570 temp = sp_repr_lookup_name ( parent, "dc:title", 1 );
571 if ( temp == NULL ) {
572 temp = xmldoc->createElement ( "dc:title" );
573 g_return_val_if_fail (temp != NULL, FALSE);
575 parent->appendChild(temp);
576 Inkscape::GC::release(temp);
577 }
578 parent = temp;
580 temp = sp_repr_children(parent);
581 if ( temp == NULL ) {
582 temp = xmldoc->createTextNode( text );
583 g_return_val_if_fail (temp != NULL, FALSE);
585 parent->appendChild(temp);
586 Inkscape::GC::release(temp);
588 return TRUE;
589 }
590 else {
591 temp->setContent(text);
592 return TRUE;
593 }
595 case RDF_RESOURCE:
596 parent->setAttribute("rdf:resource", text );
597 return true;
599 case RDF_XML:
600 return 1;
602 case RDF_BAG:
603 /* find/create the rdf:Bag item */
604 temp = sp_repr_lookup_name ( parent, "rdf:Bag", 1 );
605 if ( temp == NULL ) {
606 /* backward compatibility: drop the dc:subject contents */
607 while ( (temp = sp_repr_children( parent )) ) {
608 parent->removeChild(temp);
609 }
611 temp = xmldoc->createElement ( "rdf:Bag" );
612 g_return_val_if_fail (temp != NULL, FALSE);
614 parent->appendChild(temp);
615 Inkscape::GC::release(temp);
616 }
617 parent = temp;
619 /* toss all the old list items */
620 while ( (temp = sp_repr_children( parent )) ) {
621 parent->removeChild(temp);
622 }
624 /* chop our list up on commas */
625 strlist = g_strsplit( text, ",", 0);
627 for (i = 0; (str = strlist[i]); i++) {
628 temp = xmldoc->createElement ( "rdf:li" );
629 g_return_val_if_fail (temp != NULL, 0);
631 parent->appendChild(temp);
632 Inkscape::GC::release(temp);
634 child = xmldoc->createTextNode( g_strstrip(str) );
635 g_return_val_if_fail (child != NULL, 0);
637 temp->appendChild(child);
638 Inkscape::GC::release(child);
639 }
640 g_strfreev( strlist );
642 return 1;
644 default:
645 break;
646 }
647 return 0;
648 }
650 Inkscape::XML::Node *
651 rdf_get_rdf_root_repr ( SPDocument * doc, bool build )
652 {
653 g_return_val_if_fail (doc != NULL, NULL);
654 g_return_val_if_fail (doc->rroot != NULL, NULL);
656 Inkscape::XML::Document * xmldoc = sp_document_repr_doc(doc);
657 g_return_val_if_fail (xmldoc != NULL, NULL);
659 Inkscape::XML::Node * rdf = sp_repr_lookup_name ( doc->rroot, XML_TAG_NAME_RDF );
661 if (rdf == NULL) {
662 //printf("missing XML '%s'\n",XML_TAG_NAME_RDF);
663 if (!build) return NULL;
665 Inkscape::XML::Node * svg = sp_repr_lookup_name ( doc->rroot, XML_TAG_NAME_SVG );
666 g_return_val_if_fail ( svg != NULL, NULL );
668 Inkscape::XML::Node * parent = sp_repr_lookup_name ( svg, XML_TAG_NAME_METADATA );
669 if ( parent == NULL ) {
670 parent = xmldoc->createElement( XML_TAG_NAME_METADATA );
671 g_return_val_if_fail ( parent != NULL, NULL);
673 svg->appendChild(parent);
674 Inkscape::GC::release(parent);
675 }
677 Inkscape::XML::Document * xmldoc = parent->document();
678 g_return_val_if_fail (xmldoc != NULL, FALSE);
680 rdf = xmldoc->createElement( XML_TAG_NAME_RDF );
681 g_return_val_if_fail (rdf != NULL, NULL);
683 parent->appendChild(rdf);
684 Inkscape::GC::release(rdf);
685 }
687 /*
688 * some implementations do not put RDF stuff inside <metadata>,
689 * so we need to check for it and add it if we don't see it
690 */
691 Inkscape::XML::Node * want_metadata = sp_repr_parent ( rdf );
692 g_return_val_if_fail (want_metadata != NULL, NULL);
693 if (strcmp( want_metadata->name(), XML_TAG_NAME_METADATA )) {
694 Inkscape::XML::Node * metadata = xmldoc->createElement( XML_TAG_NAME_METADATA );
695 g_return_val_if_fail (metadata != NULL, NULL);
697 /* attach the metadata node */
698 want_metadata->appendChild(metadata);
699 Inkscape::GC::release(metadata);
701 /* move the RDF into it */
702 Inkscape::GC::anchor(rdf);
703 sp_repr_unparent ( rdf );
704 metadata->appendChild(rdf);
705 Inkscape::GC::release(rdf);
706 }
708 return rdf;
709 }
711 Inkscape::XML::Node *
712 rdf_get_xml_repr( SPDocument * doc, gchar const * name, bool build )
713 {
714 g_return_val_if_fail (name != NULL, NULL);
715 g_return_val_if_fail (doc != NULL, NULL);
716 g_return_val_if_fail (doc->rroot != NULL, NULL);
718 Inkscape::XML::Node * rdf = rdf_get_rdf_root_repr ( doc, build );
719 if (!rdf) return NULL;
721 Inkscape::XML::Node * xml = sp_repr_lookup_name ( rdf, name );
722 if (xml == NULL) {
723 //printf("missing XML '%s'\n",name);
724 if (!build) return NULL;
726 Inkscape::XML::Document * xmldoc = sp_document_repr_doc(doc);
727 g_return_val_if_fail (xmldoc != NULL, NULL);
729 xml = xmldoc->createElement( name );
730 g_return_val_if_fail (xml != NULL, NULL);
732 xml->setAttribute("rdf:about", "" );
734 rdf->appendChild(xml);
735 Inkscape::GC::release(xml);
736 }
738 return xml;
739 }
741 Inkscape::XML::Node *
742 rdf_get_work_repr( SPDocument * doc, gchar const * name, bool build )
743 {
744 g_return_val_if_fail (name != NULL, NULL);
745 g_return_val_if_fail (doc != NULL, NULL);
746 g_return_val_if_fail (doc->rroot != NULL, NULL);
748 Inkscape::XML::Node * work = rdf_get_xml_repr ( doc, XML_TAG_NAME_WORK, build );
749 if (!work) return NULL;
751 Inkscape::XML::Node * item = sp_repr_lookup_name ( work, name, 1 );
752 if (item == NULL) {
753 //printf("missing XML '%s'\n",name);
754 if (!build) return NULL;
756 Inkscape::XML::Document * xmldoc = sp_document_repr_doc(doc);
757 g_return_val_if_fail (xmldoc != NULL, NULL);
759 item = xmldoc->createElement( name );
760 g_return_val_if_fail (item != NULL, NULL);
762 work->appendChild(item);
763 Inkscape::GC::release(item);
764 }
766 return item;
767 }
771 /**
772 * \brief Retrieves a known RDF/Work entity's contents from the document XML by name
773 * \return A pointer to the entity's static contents as a string, or NULL if no entity exists
774 * \param entity The desired RDF/Work entity
775 *
776 */
777 const gchar *
778 rdf_get_work_entity(SPDocument * doc, struct rdf_work_entity_t * entity)
779 {
780 g_return_val_if_fail (doc != NULL, NULL);
781 if ( entity == NULL ) return NULL;
782 //printf("want '%s'\n",entity->title);
784 Inkscape::XML::Node * item;
785 if ( entity->datatype == RDF_XML ) {
786 item = rdf_get_xml_repr ( doc, entity->tag, FALSE );
787 }
788 else {
789 item = rdf_get_work_repr( doc, entity->tag, FALSE );
790 }
791 if ( item == NULL ) return NULL;
793 const gchar * result = rdf_get_repr_text ( item, entity );
794 //printf("found '%s' == '%s'\n", entity->title, result );
795 return result;
796 }
798 /**
799 * \brief Stores a string into a named RDF/Work entity in the document XML
800 * \param entity The desired RDF/Work entity to replace
801 * \param string The string to replace the entity contents with
802 *
803 */
804 unsigned int
805 rdf_set_work_entity(SPDocument * doc, struct rdf_work_entity_t * entity,
806 const gchar * text)
807 {
808 g_return_val_if_fail ( entity != NULL, 0 );
809 if (text == NULL) {
810 // FIXME: on a "NULL" text, delete the entity. For now, blank it.
811 text="";
812 }
814 /*
815 printf("changing '%s' (%s) to '%s'\n",
816 entity->title,
817 entity->tag,
818 text);
819 */
821 Inkscape::XML::Node * item = rdf_get_work_repr( doc, entity->tag, TRUE );
822 g_return_val_if_fail ( item != NULL, 0 );
824 return rdf_set_repr_text ( item, entity, text );
825 }
827 #undef DEBUG_MATCH
829 bool
830 rdf_match_license ( Inkscape::XML::Node * repr, struct rdf_license_t * license )
831 {
832 g_assert ( repr != NULL );
833 g_assert ( license != NULL );
835 bool result=TRUE;
836 #ifdef DEBUG_MATCH
837 printf("checking against '%s'\n",license->name);
838 #endif
840 int count = 0;
841 for (struct rdf_double_t * details = license->details;
842 details->name; details++ ) {
843 count++;
844 }
845 bool * matched = (bool*)calloc(count,sizeof(bool));
847 for (Inkscape::XML::Node * current = sp_repr_children ( repr );
848 current;
849 current = sp_repr_next ( current ) ) {
851 gchar const * attr = current->attribute("rdf:resource");
852 if ( attr == NULL ) continue;
854 #ifdef DEBUG_MATCH
855 printf("\texamining '%s' => '%s'\n", current->name(), attr);
856 #endif
858 bool found_match=FALSE;
859 for (int i=0; i<count; i++) {
860 // skip already matched items
861 if (matched[i]) continue;
863 #ifdef DEBUG_MATCH
864 printf("\t\t'%s' vs '%s'\n", current->name(), license->details[i].name);
865 printf("\t\t'%s' vs '%s'\n", attr, license->details[i].resource);
866 #endif
868 if (!strcmp( current->name(),
869 license->details[i].name ) &&
870 !strcmp( attr,
871 license->details[i].resource )) {
872 matched[i]=TRUE;
873 found_match=TRUE;
874 #ifdef DEBUG_MATCH
875 printf("\t\tgood!\n");
876 #endif
877 break;
878 }
879 }
880 if (!found_match) {
881 // if we checked each known item of the license
882 // and didn't find it, we must abort
883 result=FALSE;
884 #ifdef DEBUG_MATCH
885 printf("\t\tno '%s' element matched XML (bong)!\n",license->name);
886 #endif
887 break;
888 }
889 }
890 #ifdef DEBUG_MATCH
891 if (result) printf("\t\tall XML found matching elements!\n");
892 #endif
893 for (int i=0; result && i<count; i++) {
894 // scan looking for an unmatched item
895 if (matched[i]==0) {
896 result=FALSE;
897 #ifdef DEBUG_MATCH
898 printf("\t\tnot all '%s' elements used to match (bong)!\n", license->name);
899 #endif
900 }
901 }
903 #ifdef DEBUG_MATCH
904 printf("\t\tall '%s' elements used to match!\n",license->name);
905 #endif
907 free(matched);
909 #ifdef DEBUG_MATCH
910 if (result) printf("matched '%s'\n",license->name);
911 #endif
912 return result;
913 }
915 /**
916 * \brief Attempts to match and retrieve a known RDF/License from the document XML
917 * \return A pointer to the static RDF license structure
918 *
919 */
920 struct rdf_license_t *
921 rdf_get_license(SPDocument * document)
922 {
923 Inkscape::XML::Node * repr = rdf_get_xml_repr ( document, XML_TAG_NAME_LICENSE, FALSE );
924 if (repr) {
925 for (struct rdf_license_t * license = rdf_licenses;
926 license->name; license++ ) {
927 if ( rdf_match_license ( repr, license ) ) return license;
928 }
929 }
930 #ifdef DEBUG_MATCH
931 else {
932 printf("no license XML\n");
933 }
934 #endif
935 return NULL;
936 }
938 /**
939 * \brief Stores an RDF/License XML in the document XML
940 * \param document Which document to update
941 * \param license The desired RDF/License structure to store; NULL drops old license, so can be used for proprietary license.
942 *
943 */
944 void
945 rdf_set_license(SPDocument * doc, struct rdf_license_t const * license)
946 {
947 // drop old license section
948 Inkscape::XML::Node * repr = rdf_get_xml_repr ( doc, XML_TAG_NAME_LICENSE, FALSE );
949 if (repr) sp_repr_unparent(repr);
951 if (!license) return;
953 // build new license section
954 repr = rdf_get_xml_repr ( doc, XML_TAG_NAME_LICENSE, TRUE );
955 g_assert ( repr );
957 repr->setAttribute("rdf:about", license->uri );
959 Inkscape::XML::Document * xmldoc = sp_document_repr_doc(doc);
960 g_return_if_fail (xmldoc != NULL);
962 for (struct rdf_double_t * detail = license->details;
963 detail->name; detail++) {
964 Inkscape::XML::Node * child = xmldoc->createElement( detail->name );
965 g_assert ( child != NULL );
967 child->setAttribute("rdf:resource", detail->resource );
968 repr->appendChild(child);
969 Inkscape::GC::release(child);
970 }
971 }
973 struct rdf_entity_default_t {
974 gchar const * name;
975 gchar const * text;
976 };
977 struct rdf_entity_default_t rdf_defaults[] = {
978 { "format", "image/svg+xml", },
979 { "type", "http://purl.org/dc/dcmitype/StillImage", },
980 { "title", "", },
981 { "date", "", },
982 { "creator", "", },
983 { "rights", "", },
984 { "publisher", "", },
985 { "identifier", "", },
986 { "source", "", },
987 { "relation", "", },
988 { "language", "", },
989 { "subject", "", },
990 { "coverage", "", },
991 { "description", "", },
992 { "contributor", "", },
993 { "license_uri", "", },
994 { NULL, NULL, }
995 };
997 void
998 rdf_set_defaults ( SPDocument * doc )
999 {
1000 g_assert ( doc != NULL );
1002 // Create metadata node if it doesn't already exist
1003 if (!sp_item_group_get_child_by_name ((SPGroup *) doc->root, NULL,
1004 XML_TAG_NAME_METADATA)) {
1005 // create repr
1006 Inkscape::XML::Document * xmldoc = sp_document_repr_doc(doc);
1007 g_return_if_fail (xmldoc != NULL);
1008 Inkscape::XML::Node * rnew = xmldoc->createElement (XML_TAG_NAME_METADATA);
1009 // insert into the document
1010 doc->rroot->addChild(rnew, NULL);
1011 // clean up
1012 Inkscape::GC::release(rnew);
1013 }
1015 /* install defaults */
1016 for ( struct rdf_entity_default_t * rdf_default = rdf_defaults;
1017 rdf_default->name;
1018 rdf_default++) {
1019 struct rdf_work_entity_t * entity = rdf_find_entity ( rdf_default->name );
1020 g_assert ( entity != NULL );
1022 //get default matedata defined by user preferences
1023 const gchar * text = prefs_get_string_attribute ("options.defaultmetadata", rdf_default->name);
1025 if ( rdf_get_work_entity ( doc, entity ) == NULL ) {
1026 rdf_set_work_entity ( doc, entity, text ? text : rdf_default->text );
1027 }
1028 }
1029 }
1032 /*
1033 Local Variables:
1034 mode:c++
1035 c-file-style:"stroustrup"
1036 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1037 indent-tabs-mode:nil
1038 fill-column:99
1039 End:
1040 */
1041 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :