Use an enumeration type rather than unnamed integers to specify the different types...
[synfig.git] / synfig-core / trunk / src / synfig / loadcanvas.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file loadcanvas.cpp
3 **      \brief writeme
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <cstdlib>
34 #include <cstdio>
35 #include <cstring>
36 #include <ETL/stringf>
37 #include <libxml++/libxml++.h>
38 #include <vector>
39 #include <stdexcept>
40 #include <iostream>
41
42 #include "loadcanvas.h"
43 #include "valuenode.h"
44 #include "valuenode_subtract.h"
45 #include "valuenode_animated.h"
46 #include "valuenode_composite.h"
47 #include "valuenode_const.h"
48 #include "valuenode_linear.h"
49 #include "valuenode_dynamiclist.h"
50 #include "valuenode_reference.h"
51 #include "valuenode_scale.h"
52 #include "valuenode_timedswap.h"
53 #include "valuenode_twotone.h"
54 #include "valuenode_stripes.h"
55 #include "valuenode_segcalctangent.h"
56 #include "valuenode_segcalcvertex.h"
57 #include "valuenode_bline.h"
58
59 #include "layer.h"
60 #include "string.h"
61
62 #include "exception.h"
63
64 #include "gradient.h"
65
66 #include <map>
67 #include <sigc++/bind.h>
68
69 #endif
70
71 /* === U S I N G =========================================================== */
72
73 using namespace std;
74 using namespace synfig;
75 using namespace etl;
76
77 /*
78 class test_class {
79 static int bleh;
80 public:
81         test_class() { assert(!bleh); bleh++; synfig::info("test_class: initi: %d",bleh); }
82         ~test_class() { assert(bleh); synfig::info("test_class: uninit: %d",bleh); bleh--; }
83 };
84 int test_class::bleh(0);
85
86 test_class test_class_instance;
87 */
88
89 /* === M A C R O S ========================================================= */
90
91 inline bool is_whitespace(char x) { return ((x)=='\n' || (x)=='\t' || (x)==' '); }
92
93 /* === P R O C E D U R E S ================================================= */
94
95 static std::map<String, Canvas::LooseHandle>* open_canvas_map_(0);
96
97 std::map<synfig::String, etl::loose_handle<Canvas> >& synfig::get_open_canvas_map()
98 {
99         if(!open_canvas_map_)
100                 open_canvas_map_=new std::map<String, Canvas::LooseHandle>;
101         return *open_canvas_map_;
102 }
103
104 static void _remove_from_open_canvas_map(Canvas *x) { get_open_canvas_map().erase(etl::absolute_path(x->get_file_name())); }
105
106 static void _canvas_file_name_changed(Canvas *x)
107 {
108         std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
109
110         for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
111                 if(iter->second==x)
112                         break;
113         assert(iter!=get_open_canvas_map().end());
114         if(iter==get_open_canvas_map().end())
115                 return;
116         get_open_canvas_map().erase(iter->first);
117         get_open_canvas_map()[etl::absolute_path(x->get_file_name())]=x;
118
119 }
120
121 Canvas::Handle
122 synfig::open_canvas(const String &filename)
123 {
124         CanvasParser parser;
125
126         parser.set_allow_errors(true);
127
128         Canvas::Handle canvas=parser.parse_from_file(filename);
129
130         if(parser.error_count())
131                 return Canvas::Handle();
132
133         return canvas;
134 }
135
136 Canvas::Handle
137 synfig::open_canvas_as(const String &filename,const String &as)
138 {
139         CanvasParser parser;
140
141         parser.set_allow_errors(true);
142
143         Canvas::Handle canvas=parser.parse_from_file_as(filename,as);
144
145         if(parser.error_count())
146                 return Canvas::Handle();
147
148         return canvas;
149 }
150
151 Canvas::Handle
152 synfig::string_to_canvas(const String &data)
153 {
154         CanvasParser parser;
155
156         parser.set_allow_errors(true);
157
158         Canvas::Handle canvas=parser.parse_from_string(data);
159
160         if(parser.error_count())
161                 return Canvas::Handle();
162
163         return canvas;
164 }
165
166 /* === M E T H O D S ======================================================= */
167
168 void
169 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got, const String &expected)
170 {
171         error(element,strprintf(_("Unexpected element <%s>, Expected <%s>"),got.c_str(),expected.c_str()));
172 }
173
174 void
175 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got)
176 {
177         error(element,strprintf(_("Unexpected element <%s>"),got.c_str()));
178 }
179
180 void
181 CanvasParser::warning(xmlpp::Node *element, const String &text)
182 {
183         string str=strprintf("%s:<%s>:%d: warning: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
184         //synfig::warning(str);
185         cerr<<str<<endl;
186         total_warnings_++;
187         if(total_warnings_>=max_warnings_)
188                 fatal_error(element, _("Too many warnings"));
189 }
190
191 void
192 CanvasParser::error(xmlpp::Node *element, const String &text)
193 {
194         string str=strprintf("%s:<%s>:%d: error: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
195         total_errors_++;
196         if(!allow_errors_)
197                 throw runtime_error(str);
198         cerr<<str<<endl;
199         //      synfig::error(str);
200 }
201
202 void
203 CanvasParser::fatal_error(xmlpp::Node *element, const String &text)
204 {
205         string str=strprintf("%s:<%s>:%d:",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
206         throw runtime_error(str);
207 }
208
209
210
211 Keyframe
212 CanvasParser::parse_keyframe(xmlpp::Element *element,Canvas::Handle canvas)
213 {
214         assert(element->get_name()=="keyframe");
215
216         if(!element->get_attribute("time"))
217         {
218                 error(element,strprintf(_("<%s> is missing \"%s\" attribute"),"real","time"));
219                 return Keyframe();
220         }
221
222         Keyframe ret(Time(element->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate()));
223
224
225         if(element->get_children().empty())
226                 return ret;
227
228         if(element->get_child_text()->get_content().empty())
229                 return ret;
230
231         ret.set_description(element->get_child_text()->get_content());
232
233         return ret;
234 }
235
236
237 Real
238 CanvasParser::parse_real(xmlpp::Element *element)
239 {
240         assert(element->get_name()=="real");
241
242         if(!element->get_children().empty())
243                 warning(element, strprintf(_("<%s> should not contain anything"),"real"));
244
245         if(!element->get_attribute("value"))
246         {
247                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"real"));
248                 return false;
249         }
250
251         string val=element->get_attribute("value")->get_value();
252
253         return atof(val.c_str());
254 }
255
256 Time
257 CanvasParser::parse_time(xmlpp::Element *element,Canvas::Handle canvas)
258 {
259         assert(element->get_name()=="time");
260
261         if(!element->get_children().empty())
262                 warning(element, strprintf(_("<%s> should not contain anything"),"time"));
263
264         if(!element->get_attribute("value"))
265         {
266                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"time"));
267                 return false;
268         }
269
270         string val=element->get_attribute("value")->get_value();
271
272         return Time(val,canvas->rend_desc().get_frame_rate());
273 }
274
275 int
276 CanvasParser::parse_integer(xmlpp::Element *element)
277 {
278         assert(element->get_name()=="integer");
279
280         if(!element->get_children().empty())
281                 warning(element, strprintf(_("<%s> should not contain anything"),"integer"));
282
283         if(!element->get_attribute("value"))
284         {
285                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"integer"));
286                 return false;
287         }
288
289         string val=element->get_attribute("value")->get_value();
290
291         return atoi(val.c_str());
292 }
293
294 // see 'minor hack' at the end of parse_vector() below
295 // making this 'static' to give it file local scope
296 // stops it working (where working means working around
297 // bug #1509627)
298 Vector &canvas_parser_vector_id(Vector &vector)
299 {
300         return vector;
301 }
302
303 Vector
304 CanvasParser::parse_vector(xmlpp::Element *element)
305 {
306         assert(element->get_name()=="vector");
307
308         if(element->get_children().empty())
309         {
310                 error(element, "Undefined value in <vector>");
311                 return Vector();
312         }
313
314         Vector vect;
315
316         xmlpp::Element::NodeList list = element->get_children();
317         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
318         {
319                 xmlpp::Element *child=dynamic_cast<xmlpp::Element*>((xmlpp::Node*)*iter);
320                 if(!child)
321                         continue;
322                 else
323                 if(child->get_name()=="x")
324                 {
325                         if(child->get_children().empty())
326                         {
327                                 error(element, "Undefined value in <x>");
328                                 return Vector();
329                         }
330                         vect[0]=atof(child->get_child_text()->get_content().c_str());
331                 }
332                 else
333                 if(child->get_name()=="y")
334                 {
335                         if(child->get_children().empty())
336                         {
337                                 error(element, "Undefined value in <y>");
338                                 return Vector();
339                         }
340                         vect[1]=atof(child->get_child_text()->get_content().c_str());
341                 }
342                 else
343                         error_unexpected_element(child,child->get_name());
344         }
345         // Minor hack - gcc 4.1.2 and earlier think that we're not using
346         // 'vect' and optimize it out at -O2 and higher.  This convinces
347         // them that we are really using it.
348         return canvas_parser_vector_id(vect);
349         // When the bug is fixed, we can just do this instead:
350         // return vect;
351 }
352
353 Color
354 CanvasParser::parse_color(xmlpp::Element *element)
355 {
356         assert(element->get_name()=="color");
357
358         if(element->get_children().empty())
359         {
360                 error(element, "Undefined value in <color>");
361                 return Color();
362         }
363
364         Color color(0);
365
366         xmlpp::Element::NodeList list = element->get_children();
367         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
368         {
369                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
370                 if(!child)
371                         continue;
372                 else
373                 if(child->get_name()=="r")
374                 {
375                         if(child->get_children().empty())
376                         {
377                                 error(element, "Undefined value in <r>");
378                                 return Color();
379                         }
380                         color.set_r(atof(child->get_child_text()->get_content().c_str()));
381                 }
382                 else
383                 if(child->get_name()=="g")
384                 {
385                         if(child->get_children().empty())
386                         {
387                                 error(element, "Undefined value in <g>");
388                                 return Color();
389                         }
390                         color.set_g(atof(child->get_child_text()->get_content().c_str()));
391                 }
392                 else
393                 if(child->get_name()=="b")
394                 {
395                         if(child->get_children().empty())
396                         {
397                                 error(element, "Undefined value in <b>");
398                                 return Color();
399                         }
400                         color.set_b(atof(child->get_child_text()->get_content().c_str()));
401                 }
402                 else
403                 if(child->get_name()=="a")
404                 {
405                         if(child->get_children().empty())
406                         {
407                                 error(element, "Undefined value in <a>");
408                                 return Color();
409                         }
410                         color.set_a(atof(child->get_child_text()->get_content().c_str()));
411                 }
412                 else
413                         error_unexpected_element(child,child->get_name());
414         }
415
416         return color;
417 }
418
419 synfig::String
420 CanvasParser::parse_string(xmlpp::Element *element)
421 {
422         assert(element->get_name()=="string");
423
424         if(element->get_children().empty())
425         {
426                 warning(element, "Undefined value in <string>");
427                 return synfig::String();
428         }
429
430         if(element->get_child_text()->get_content().empty())
431         {
432                 warning(element, "Content element of <string> appears to be empty");
433                 return synfig::String();
434         }
435
436         return element->get_child_text()->get_content();
437 }
438
439 bool
440 CanvasParser::parse_bool(xmlpp::Element *element)
441 {
442         assert(element->get_name()=="bool");
443
444         if(!element->get_children().empty())
445                 warning(element, strprintf(_("<%s> should not contain anything"),"bool"));
446
447         if(!element->get_attribute("value"))
448         {
449                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"bool"));
450                 return false;
451         }
452
453         string val=element->get_attribute("value")->get_value();
454
455         if(val=="true" || val=="1")
456                 return true;
457         if(val=="false" || val=="0")
458                 return false;
459
460         error(element,strprintf(_("Bad value \"%s\" in <%s>"),val.c_str(),"bool"));
461
462         return false;
463 }
464
465 Gradient
466 CanvasParser::parse_gradient(xmlpp::Element *node)
467 {
468         assert(node->get_name()=="gradient");
469         Gradient ret;
470
471         xmlpp::Element::NodeList list = node->get_children();
472         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
473         {
474                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
475                 if(!child)
476                         continue;
477                 else
478                 {
479                         Gradient::CPoint cpoint;
480                         cpoint.color=parse_color(child);
481
482                         if(!child->get_attribute("pos"))
483                         {
484                                 error(child,strprintf(_("<%s> is missing \"pos\" attribute"),"gradient"));
485                                 return Gradient();
486                         }
487
488                         cpoint.pos=atof(child->get_attribute("pos")->get_value().c_str());
489
490                         ret.push_back(cpoint);
491                 }
492         }
493         ret.sort();
494         return ret;
495 }
496
497 ValueBase
498 CanvasParser::parse_list(xmlpp::Element *element,Canvas::Handle canvas)
499 {
500         vector<ValueBase> value_list;
501
502         xmlpp::Element::NodeList list = element->get_children();
503         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
504         {
505                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
506                 if(!child)
507                         continue;
508                 else
509                 {
510                         value_list.push_back(parse_value(child,canvas));
511                         if(!value_list.back().is_valid())
512                         {
513                                 value_list.pop_back();
514                                 error(child,"Bad ValueBase");
515                                 continue;
516                         }
517                 }
518         }
519         return value_list;
520 }
521
522 Segment
523 CanvasParser::parse_segment(xmlpp::Element *element)
524 {
525         assert(element->get_name()=="segment");
526
527         if(element->get_children().empty())
528         {
529                 error(element, "Undefined value in <segment>");
530                 return Segment();
531         }
532
533         Segment seg;
534
535         xmlpp::Element::NodeList list = element->get_children();
536         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
537         {
538                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
539                 if(!child)
540                         continue;
541                 else
542                 if(child->get_name()=="p1")
543                 {
544                         xmlpp::Element::NodeList list = child->get_children();
545                         xmlpp::Element::NodeList::iterator iter;
546
547                         // Search for the first non-text XML element
548                         for(iter = list.begin(); iter != list.end(); ++iter)
549                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
550
551                         if(iter==list.end())
552                         {
553                                 error(element, "Undefined value in <p1>");
554                                 continue;
555                         }
556
557                         if((*iter)->get_name()!="vector")
558                         {
559                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
560                                 continue;
561                         }
562
563                         seg.p1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
564                 }
565                 else
566                 if(child->get_name()=="t1")
567                 {
568                         xmlpp::Element::NodeList list = child->get_children();
569                         xmlpp::Element::NodeList::iterator iter;
570
571                         // Search for the first non-text XML element
572                         for(iter = list.begin(); iter != list.end(); ++iter)
573                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
574
575                         if(iter==list.end())
576                         {
577                                 error(element, "Undefined value in <t1>");
578                                 continue;
579                         }
580
581                         if((*iter)->get_name()!="vector")
582                         {
583                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
584                                 continue;
585                         }
586
587                         seg.t1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
588                 }
589                 else
590                 if(child->get_name()=="p2")
591                 {
592                         xmlpp::Element::NodeList list = child->get_children();
593                         xmlpp::Element::NodeList::iterator iter;
594
595                         // Search for the first non-text XML element
596                         for(iter = list.begin(); iter != list.end(); ++iter)
597                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
598
599                         if(iter==list.end())
600                         {
601                                 error(element, "Undefined value in <p2>");
602                                 continue;
603                         }
604
605                         if((*iter)->get_name()!="vector")
606                         {
607                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
608                                 continue;
609                         }
610
611                         seg.p2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
612                 }
613                 else
614                 if(child->get_name()=="t2")
615                 {
616                         xmlpp::Element::NodeList list = child->get_children();
617                         xmlpp::Element::NodeList::iterator iter;
618
619                         // Search for the first non-text XML element
620                         for(iter = list.begin(); iter != list.end(); ++iter)
621                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
622
623                         if(iter==list.end())
624                         {
625                                 error(element, "Undefined value in <t2>");
626                                 continue;
627                         }
628
629                         if((*iter)->get_name()!="vector")
630                         {
631                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
632                                 continue;
633                         }
634
635                         seg.t2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
636                 }
637                 else
638                         error_unexpected_element(child,child->get_name());
639         }
640         return seg;
641 }
642
643 BLinePoint
644 CanvasParser::parse_bline_point(xmlpp::Element *element)
645 {
646         assert(element->get_name()=="bline_point");
647         if(element->get_children().empty())
648         {
649                 error(element, "Undefined value in <bline_point>");
650                 return BLinePoint();
651         }
652
653         BLinePoint ret;
654         ret.set_split_tangent_flag(false);
655
656         xmlpp::Element::NodeList list = element->get_children();
657         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
658         {
659                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
660                 if(!child)
661                         continue;
662                 else
663                 // Vertex
664                 if(child->get_name()[0]=='v' || child->get_name()=="p1")
665                 {
666                         xmlpp::Element::NodeList list = child->get_children();
667                         xmlpp::Element::NodeList::iterator iter;
668
669                         // Search for the first non-text XML element
670                         for(iter = list.begin(); iter != list.end(); ++iter)
671                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
672
673                         if(iter==list.end())
674                         {
675                                 error(element, "Undefined value in <vertex>");
676                                 continue;
677                         }
678
679                         if((*iter)->get_name()!="vector")
680                         {
681                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
682                                 continue;
683                         }
684
685                         ret.set_vertex(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
686                 }
687                 else
688                 // Tangent 1
689                 if(child->get_name()=="t1" || child->get_name()=="tangent")
690                 {
691                         xmlpp::Element::NodeList list = child->get_children();
692                         xmlpp::Element::NodeList::iterator iter;
693
694                         // Search for the first non-text XML element
695                         for(iter = list.begin(); iter != list.end(); ++iter)
696                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
697
698                         if(iter==list.end())
699                         {
700                                 error(element, "Undefined value in <t1>");
701                                 continue;
702                         }
703
704                         if((*iter)->get_name()!="vector")
705                         {
706                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
707                                 continue;
708                         }
709
710                         ret.set_tangent1(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
711                 }
712                 else
713                 // Tangent 2
714                 if(child->get_name()=="t2")
715                 {
716                         xmlpp::Element::NodeList list = child->get_children();
717                         xmlpp::Element::NodeList::iterator iter;
718
719                         // Search for the first non-text XML element
720                         for(iter = list.begin(); iter != list.end(); ++iter)
721                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
722
723                         if(iter==list.end())
724                         {
725                                 error(element, "Undefined value in <t2>");
726                                 continue;
727                         }
728
729                         if((*iter)->get_name()!="vector")
730                         {
731                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
732                                 continue;
733                         }
734
735                         ret.set_tangent2(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
736                         ret.set_split_tangent_flag(true);
737                 }
738                 else
739                 // width
740                 if(child->get_name()=="width")
741                 {
742                         xmlpp::Element::NodeList list = child->get_children();
743                         xmlpp::Element::NodeList::iterator iter;
744
745                         // Search for the first non-text XML element
746                         for(iter = list.begin(); iter != list.end(); ++iter)
747                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
748
749                         if(iter==list.end())
750                         {
751                                 error(element, "Undefined value in <width>");
752                                 continue;
753                         }
754
755                         if((*iter)->get_name()!="real")
756                         {
757                                 error_unexpected_element((*iter),(*iter)->get_name(),"real");
758                                 continue;
759                         }
760
761                         ret.set_width(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
762                 }
763                 else
764                 // origin
765                 if(child->get_name()=="origin")
766                 {
767                         xmlpp::Element::NodeList list = child->get_children();
768                         xmlpp::Element::NodeList::iterator iter;
769
770                         // Search for the first non-text XML element
771                         for(iter = list.begin(); iter != list.end(); ++iter)
772                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
773
774                         if(iter==list.end())
775                         {
776                                 error(element, "Undefined value in <origin>");
777                                 continue;
778                         }
779
780                         if((*iter)->get_name()!="real")
781                         {
782                                 error_unexpected_element((*iter),(*iter)->get_name(),"real");
783                                 continue;
784                         }
785
786                         ret.set_origin(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
787                 }
788                 else
789                         error_unexpected_element(child,child->get_name());
790         }
791         return ret;
792 }
793
794 Angle
795 CanvasParser::parse_angle(xmlpp::Element *element)
796 {
797         assert(element->get_name()=="angle");
798
799         if(!element->get_children().empty())
800                 warning(element, strprintf(_("<%s> should not contain anything"),"angle"));
801
802         if(!element->get_attribute("value"))
803         {
804                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"angle"));
805                 return Angle();
806         }
807
808         string val=element->get_attribute("value")->get_value();
809
810         return Angle::deg(atof(val.c_str()));
811 }
812
813 ValueBase
814 CanvasParser::parse_value(xmlpp::Element *element,Canvas::Handle canvas)
815 {
816         if(element->get_name()=="real")
817                 return parse_real(element);
818         else
819         if(element->get_name()=="time")
820                 return parse_time(element,canvas);
821         else
822         if(element->get_name()=="integer")
823                 return parse_integer(element);
824         else
825         if(element->get_name()=="string")
826                 return parse_string(element);
827         else
828         if(element->get_name()=="vector")
829                 return parse_vector(element);
830         else
831         if(element->get_name()=="color")
832                 return parse_color(element);
833         else
834         if(element->get_name()=="segment")
835                 return parse_segment(element);
836         else
837         if(element->get_name()=="list")
838                 return parse_list(element,canvas);
839         else
840         if(element->get_name()=="gradient")
841                 return parse_gradient(element);
842         else
843         if(element->get_name()=="bool")
844                 return parse_bool(element);
845         else
846         //if(element->get_name()=="canvas")
847         //      return parse_canvas(element,canvas,true);       // inline canvas
848         //else
849         if(element->get_name()=="angle" || element->get_name()=="degrees" || element->get_name()=="radians" || element->get_name()=="rotations")
850                 return parse_angle(element);
851         else
852         if(element->get_name()=="bline_point")
853                 return parse_bline_point(element);
854         else
855         if(element->get_name()=="canvas")
856                 return ValueBase(parse_canvas(element,canvas,true));
857         else
858         {
859                 DEBUGPOINT();
860                 error_unexpected_element(element,element->get_name());
861         }
862
863         return ValueBase();
864 }
865
866
867
868
869 ValueNode_Animated::Handle
870 CanvasParser::parse_animated(xmlpp::Element *element,Canvas::Handle canvas)
871 {
872         assert(element->get_name()=="hermite" || element->get_name()=="animated");
873
874         if(!element->get_attribute("type"))
875         {
876                 error(element,"Missing attribute \"type\" in <animated>");
877                 return ValueNode_Animated::Handle();
878         }
879
880         ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
881
882         if(!type)
883         {
884                 error(element,"Bad type in <animated>");
885                 return ValueNode_Animated::Handle();
886         }
887
888         ValueNode_Animated::Handle value_node=ValueNode_Animated::create(type);
889
890         if(!value_node)
891         {
892                 error(element,strprintf(_("Unable to create <animated> with type \"%s\""),ValueBase::type_name(type).c_str()));
893                 return ValueNode_Animated::Handle();
894         }
895
896         value_node->set_root_canvas(canvas->get_root());
897
898         xmlpp::Element::NodeList list = element->get_children();
899         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
900         {
901                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
902                 if(!child)
903                         continue;
904                 else
905                 if(child->get_name()=="waypoint")
906                 {
907                         if(!child->get_attribute("time"))
908                         {
909                                 error(child,_("<waypoint> is missing attribute \"time\""));
910                                 continue;
911                         }
912
913                         Time time(child->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate());
914
915
916                         ValueNode::Handle waypoint_value_node;
917                         xmlpp::Element::NodeList list = child->get_children();
918
919                         if(child->get_attribute("use"))
920                         {
921                                 if(!list.empty())
922                                         warning(child,_("Found \"use\" attribute for <waypoint>, but it wasn't empty. Ignoring contents..."));
923
924                                 // the waypoint might look like this, in which case we won't find "mycanvas" in the list of valuenodes, 'cos it's a canvas
925                                 //
926                                 //      <animated type="canvas">
927                                 //        <waypoint time="0s" use="mycanvas"/>
928                                 //      </animated>
929                                 if (type==ValueBase::TYPE_CANVAS)
930                                         waypoint_value_node=ValueNode_Const::create(canvas->surefind_canvas(child->get_attribute("use")->get_value()));
931                                 else
932                                         waypoint_value_node=canvas->surefind_value_node(child->get_attribute("use")->get_value());
933                         }
934                         else
935                         {
936                                 if(child->get_children().empty())
937                                 {
938                                         error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
939                                         continue;
940                                 }
941
942                                 xmlpp::Element::NodeList::iterator iter;
943
944                                 // Search for the first non-text XML element
945                                 for(iter = list.begin(); iter != list.end(); ++iter)
946                                         if(dynamic_cast<xmlpp::Element*>(*iter)) break;
947
948                                 if(iter==list.end())
949                                 {
950                                         error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
951                                         continue;
952                                 }
953
954                                 waypoint_value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
955
956                                 /*
957                                 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
958
959                                 if(!data.is_valid())
960                                 {
961                                         error(child,_("Bad data for <waypoint>"));
962                                         continue;
963                                 }
964                                 */
965                                 if(!waypoint_value_node)
966                                 {
967                                         error(child,_("Bad data for <waypoint>"));
968                                         continue;
969                                 }
970
971                                 /*! HACK -- This is a temporary fix to help repair some
972                                 **      weirdness that is currently going on (10-21-2004).
973                                 **      This short circuits the linking of waypoints,
974                                 **      a feature which is so obscure that we can get
975                                 **      away with something like this pretty easily.
976                                 */
977                                 waypoint_value_node=waypoint_value_node->clone();
978
979                                 // Warn if there is trash after the param value
980                                 for(iter++; iter != list.end(); ++iter)
981                                         if(dynamic_cast<xmlpp::Element*>(*iter))
982                                                 warning((*iter),strprintf(_("Unexpected element <%s> after <waypoint> data, ignoring..."),(*iter)->get_name().c_str()));
983                         }
984
985
986                         try {
987                                 ValueNode_Animated::WaypointList::iterator waypoint=value_node->new_waypoint(time,waypoint_value_node);
988
989                         if(child->get_attribute("tension"))
990                         {
991                                 synfig::String str(child->get_attribute("tension")->get_value());
992                                 waypoint->set_tension(atof(str.c_str()));
993                         }
994                         if(child->get_attribute("temporal-tension"))
995                         {
996                                 synfig::String str(child->get_attribute("temporal-tension")->get_value());
997                                 waypoint->set_time_tension(atof(str.c_str()));
998                         }
999                         if(child->get_attribute("continuity"))
1000                         {
1001                                 synfig::String str(child->get_attribute("continuity")->get_value());
1002                                 waypoint->set_continuity(atof(str.c_str()));
1003                         }
1004                         if(child->get_attribute("bias"))
1005                         {
1006                                 synfig::String str(child->get_attribute("bias")->get_value());
1007                                 waypoint->set_bias(atof(str.c_str()));
1008                         }
1009
1010                         if(child->get_attribute("before"))
1011                         {
1012                                 string val=child->get_attribute("before")->get_value();
1013                                 if(val=="halt")
1014                                         waypoint->set_before(INTERPOLATION_HALT);
1015                                 else if(val=="constant")
1016                                         waypoint->set_before(INTERPOLATION_CONSTANT);
1017                                 else if(val=="linear")
1018                                         waypoint->set_before(INTERPOLATION_LINEAR);
1019                                 else if(val=="manual")
1020                                         waypoint->set_before(INTERPOLATION_MANUAL);
1021                                 else if(val=="auto")
1022                                         waypoint->set_before(INTERPOLATION_TCB);
1023                                 else
1024                                         error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1025                         }
1026
1027                         if(child->get_attribute("after"))
1028                         {
1029                                 string val=child->get_attribute("after")->get_value();
1030                                 if(val=="halt")
1031                                         waypoint->set_after(INTERPOLATION_HALT);
1032                                 else if(val=="constant")
1033                                         waypoint->set_after(INTERPOLATION_CONSTANT);
1034                                 else if(val=="linear")
1035                                         waypoint->set_after(INTERPOLATION_LINEAR);
1036                                 else if(val=="manual")
1037                                         waypoint->set_after(INTERPOLATION_MANUAL);
1038                                 else if(val=="auto")
1039                                         waypoint->set_after(INTERPOLATION_TCB);
1040                                 else
1041                                         error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1042                         }
1043                         }
1044                         catch(Exception::BadTime x)
1045                         {
1046                                 warning(child,x.what());
1047                         }
1048                         continue;
1049
1050                 }
1051                 else
1052                         error_unexpected_element(child,child->get_name());
1053         }
1054         value_node->changed();
1055         return value_node;
1056 }
1057
1058 etl::handle<LinkableValueNode>
1059 CanvasParser::parse_linkable_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1060 {
1061         handle<LinkableValueNode> value_node;
1062         ValueBase::Type type;
1063
1064         // Determine the type
1065         if(element->get_attribute("type"))
1066         {
1067                 type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1068
1069                 if(!type)
1070                 {
1071                         error(element,"Bad type in ValueNode");
1072                         return 0;
1073                 }
1074         }
1075         else
1076         {
1077                 error(element,"Missing type in ValueNode");
1078                 return 0;
1079         }
1080
1081         value_node=LinkableValueNode::create(element->get_name(),type);
1082
1083         if(!value_node)
1084         {
1085                 error(element,"Unknown ValueNode type "+element->get_name());
1086                 return 0;
1087         }
1088
1089         if(value_node->get_type()!=type)
1090         {
1091                 error(element,"ValueNode did not accept type");
1092                 return 0;
1093         }
1094
1095         value_node->set_root_canvas(canvas->get_root());
1096
1097         int i;
1098         for(i=0;i<value_node->link_count();i++)
1099         {
1100                 if(element->get_attribute(value_node->link_name(i)))
1101                 try {
1102                         String id(element->get_attribute(value_node->link_name(i))->get_value());
1103
1104                         if(!value_node->set_link(i,
1105                                         canvas->surefind_value_node(
1106                                                 id
1107                                         )
1108                                 )
1109                         ) error(element,strprintf(_("Unable to set link \"%s\" to ValueNode \"%s\" (link #%d in \"%s\")"),value_node->link_name(i).c_str(),id.c_str(),i,value_node->get_name().c_str()));
1110                 }
1111                 catch(Exception::IDNotFound)
1112                 {
1113                         error(element,"Unable to resolve "+element->get_attribute(value_node->link_name(i))->get_value());
1114                 }
1115                 catch(Exception::FileNotFound)
1116                 {
1117                         error(element,"Unable to open file referenced in "+element->get_attribute(value_node->link_name(i))->get_value());
1118                 }
1119                 catch(...)
1120                 {
1121                         error(element,strprintf(_("Unknown Exception thrown when referencing ValueNode \"%s\""),
1122                                 element->get_attribute(value_node->link_name(i))->get_value().c_str()));
1123                         throw;
1124                 }
1125         }
1126
1127
1128
1129         xmlpp::Element::NodeList list = element->get_children();
1130         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1131         {
1132                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1133                 try
1134                 {
1135                         if(!child)
1136                                 continue;
1137                         int index=value_node->get_link_index_from_name(child->get_name());
1138
1139                         xmlpp::Element::NodeList list = child->get_children();
1140                         xmlpp::Element::NodeList::iterator iter;
1141
1142                         // Search for the first non-text XML element
1143                         for(iter = list.begin(); iter != list.end(); ++iter)
1144                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1145
1146                         if(iter==list.end())
1147                         {
1148                                 error(child,_("element is missing its contents"));
1149                                 continue;
1150                         }
1151
1152                         ValueNode::Handle link=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1153
1154                         if(!link)
1155                         {
1156                                 error((*iter),"Parse of ValueNode failed");
1157                         }
1158                         else
1159                         if(!value_node->set_link(index,link))
1160                         {
1161                                 //error(dynamic_cast<xmlpp::Element*>(*iter),strprintf("Unable to connect value node ('%s' of type '%s') to link %d",link->get_name().c_str(),ValueBase::type_name(link->get_type()).c_str(),index));
1162                                 error(element,strprintf("Unable to connect value node ('%s' of type '%s') to link %d",link->get_name().c_str(),ValueBase::type_name(link->get_type()).c_str(),index));
1163                         }
1164
1165                         // \todo do a search for more elements and warn if they are found
1166
1167                 }
1168                 catch(Exception::BadLinkName)
1169                 {
1170                         error_unexpected_element(child,child->get_name());
1171                 }
1172                 catch(...)
1173                 {
1174                         error(child,strprintf(_("Unknown Exception thrown when working on element \"%s\""),child->get_name().c_str()));
1175                         throw;
1176                 }
1177         }
1178
1179         return value_node;
1180 }
1181
1182 handle<ValueNode_Composite>
1183 CanvasParser::parse_composite(xmlpp::Element *element,Canvas::Handle canvas)
1184 {
1185         assert(element->get_name()=="composite");
1186
1187         if(!element->get_attribute("type"))
1188         {
1189                 error(element,"Missing attribute \"type\" in <composite>");
1190                 return handle<ValueNode_Composite>();
1191         }
1192
1193         ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1194
1195         if(!type)
1196         {
1197                 error(element,"Bad type in <composite>");
1198                 return handle<ValueNode_Composite>();
1199         }
1200
1201         handle<ValueNode_Composite> value_node=ValueNode_Composite::create(type);
1202         handle<ValueNode> c[6];
1203
1204         if(!value_node)
1205         {
1206                 error(element,strprintf(_("Unable to create <composite>")));
1207                 return handle<ValueNode_Composite>();
1208         }
1209
1210         int i;
1211
1212         for(i=0;i<value_node->link_count();i++)
1213         {
1214                 string name=strprintf("c%d",i+1);
1215                 if(c[i])
1216                 {
1217                         error(element,name+" was already defined in <composite>");
1218                         continue;
1219                 }
1220                 if(element->get_attribute(name))
1221                 {
1222                         c[i]=canvas->surefind_value_node(element->get_attribute(name)->get_value());
1223                         if(c[i])
1224                         {
1225                                 if(!value_node->set_link(i,c[i]))
1226                                 {
1227                                         error(element,'"'+name+"\" attribute in <composite> has bad type");
1228                                 }
1229                         }
1230                         else
1231                                 error(element,'"'+name+"\" attribute in <composite> references unknown ID");
1232                 }
1233         }
1234
1235         xmlpp::Element::NodeList list = element->get_children();
1236         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1237         {
1238                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1239                 if(!child)
1240                         continue;
1241                 else
1242                 for(i=0;i<value_node->link_count();i++)
1243                 {
1244                         string name=strprintf("c%d",i+1);
1245                         if(child->get_name()==name)
1246                         {
1247                                 if(c[i])
1248                                 {
1249                                         error(child,name+" was already defined in <composite>");
1250                                         break;
1251                                 }
1252
1253                                 xmlpp::Element::NodeList list = child->get_children();
1254                                 xmlpp::Element::NodeList::iterator iter;
1255
1256                                 // Search for the first non-text XML element
1257                                 for(iter = list.begin(); iter != list.end(); ++iter)
1258                                         if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1259
1260                                 if(iter==list.end())
1261                                 {
1262                                         error(child,strprintf(_("<%s> is missing its contents"),name.c_str()));
1263                                         break;
1264                                 }
1265
1266                                 c[i]=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1267
1268                                 if(!c[i])
1269                                 {
1270                                         error((*iter),"Parse of "+name+" ValueNode failed");
1271                                         break;
1272                                 }
1273
1274                                 if(!value_node->set_link(i,c[i]))
1275                                 {
1276                                         error(child,strprintf(_("<%s> has a bad value"),name.c_str()));
1277                                         break;
1278                                 }
1279
1280                                 // \todo do a search for more elements and warn if they are found
1281                                 break;
1282                         }
1283                 }
1284                 // somewhat of a hack, but it works
1285                 if(i==value_node->link_count()) error_unexpected_element(child,child->get_name());
1286         }
1287
1288         switch(value_node->link_count())
1289         {
1290         case 1:
1291                 if(!value_node->get_link(0))
1292                 {
1293                         error(element,"<composite> is missing parameters");
1294                         return handle<ValueNode_Composite>();
1295                 }
1296                 break;
1297         case 2:
1298                 if(!value_node->get_link(0) ||!value_node->get_link(1))
1299                 {
1300                         error(element,"<composite> is missing parameters");
1301                         return handle<ValueNode_Composite>();
1302                 }
1303                 break;
1304         case 3:
1305                 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2))
1306                 {
1307                         error(element,"<composite> is missing parameters");
1308                         return handle<ValueNode_Composite>();
1309                 }
1310                 break;
1311         case 4:
1312                 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2) ||!value_node->get_link(3))
1313                 {
1314                         error(element,"<composite> is missing parameters");
1315                         return handle<ValueNode_Composite>();
1316                 }
1317                 break;
1318     }
1319         return value_node;
1320 }
1321
1322 // This will also parse a bline
1323 handle<ValueNode_DynamicList>
1324 CanvasParser::parse_dynamic_list(xmlpp::Element *element,Canvas::Handle canvas)
1325 {
1326         assert(element->get_name()=="dynamic_list" || element->get_name()=="bline");
1327
1328         const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
1329
1330         if(!element->get_attribute("type"))
1331         {
1332                 error(element,"Missing attribute \"type\" in <dynamic_list>");
1333                 return handle<ValueNode_DynamicList>();
1334         }
1335
1336         ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1337
1338         if(!type)
1339         {
1340                 error(element,"Bad type in <dynamic_list>");
1341                 return handle<ValueNode_DynamicList>();
1342         }
1343
1344         handle<ValueNode_DynamicList> value_node;
1345         handle<ValueNode_BLine> bline_value_node;
1346
1347         if(element->get_name()=="bline")
1348         {
1349                 value_node=bline_value_node=ValueNode_BLine::create();
1350                 if(element->get_attribute("loop"))
1351                 {
1352                         String loop=element->get_attribute("loop")->get_value();
1353                         if(loop=="true" || loop=="1" || loop=="TRUE" || loop=="True")
1354                                 bline_value_node->set_loop(true);
1355                         else
1356                                 bline_value_node->set_loop(false);
1357                 }
1358
1359         }
1360         else
1361                 value_node=ValueNode_DynamicList::create(type);
1362
1363         if(!value_node)
1364         {
1365                 error(element,strprintf(_("Unable to create <dynamic_list>")));
1366                 return handle<ValueNode_DynamicList>();
1367         }
1368
1369         value_node->set_root_canvas(canvas->get_root());
1370
1371         xmlpp::Element::NodeList list = element->get_children();
1372         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1373         {
1374                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1375                 if(!child)
1376                         continue;
1377                 else
1378                 if(child->get_name()=="entry")
1379                 {
1380                         ValueNode_DynamicList::ListEntry list_entry;
1381
1382                         // Parse begin/end waypoints
1383                         {
1384                                 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
1385                                 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
1386
1387                                 String begin_sequence;
1388                                 String end_sequence;
1389
1390                                 ActivepointList &timing_info(list_entry.timing_info);
1391
1392                                 if(child->get_attribute("begin"))
1393                                         begin_sequence=child->get_attribute("begin")->get_value();
1394
1395                                 if(child->get_attribute("on"))
1396                                         begin_sequence=child->get_attribute("on")->get_value();
1397
1398                                 if(child->get_attribute("end"))
1399                                         end_sequence=child->get_attribute("end")->get_value();
1400
1401                                 if(child->get_attribute("off"))
1402                                         end_sequence=child->get_attribute("off")->get_value();
1403
1404                                 // clear out any auto-start
1405                                 if(!begin_sequence.empty())
1406                                         timing_info.clear();
1407
1408                                 //! \optimize
1409                                 while(!begin_sequence.empty())
1410                                 {
1411                                         String::iterator iter(find(begin_sequence.begin(),begin_sequence.end(),','));
1412                                         String timecode(begin_sequence.begin(), iter);
1413                                         int priority=0;
1414
1415                                         // skip whitespace before checking for a priority
1416                                         while (isspace(timecode[0]))
1417                                                 timecode=timecode.substr(1);
1418
1419                                         // If there is a priority, then grab it and remove
1420                                         // it from the timecode
1421                                         if(timecode[0]=='p')
1422                                         {
1423                                                 //priority=timecode[1]-'0';
1424                                                 //timecode=String(timecode.begin()+3,timecode.end());
1425                                                 int space=timecode.find_first_of(' ');
1426                                                 priority=atoi(String(timecode,1,space-1).c_str());
1427                                                 timecode=String(timecode.begin()+space+1,timecode.end());
1428                                                 //synfig::info("priority: %d      timecode: %s",priority,timecode.c_str());
1429                                         }
1430
1431                                         timing_info.push_back(
1432                                                 Activepoint(
1433                                                         Time(
1434                                                                 timecode,
1435                                                                 fps
1436                                                         ),
1437                                                         true,   // Mark as a "on" activepoint
1438                                                         priority
1439                                                 )
1440                                         );
1441
1442                                         if(iter==begin_sequence.end())
1443                                                 begin_sequence.clear();
1444                                         else
1445                                                 begin_sequence=String(iter+1,begin_sequence.end());
1446                                 }
1447
1448                                 //! \optimize
1449                                 while(!end_sequence.empty())
1450                                 {
1451                                         String::iterator iter(find(end_sequence.begin(),end_sequence.end(),','));
1452                                         String timecode(end_sequence.begin(),   iter);
1453                                         int priority=0;
1454
1455                                         // skip whitespace before checking for a priority
1456                                         while (isspace(timecode[0]))
1457                                                 timecode=timecode.substr(1);
1458
1459                                         // If there is a priority, then grab it and remove
1460                                         // it from the timecode
1461                                         if(timecode[0]=='p')
1462                                         {
1463                                                 //priority=timecode[1]-'0';
1464                                                 //timecode=String(timecode.begin()+3,timecode.end());
1465                                                 int space=timecode.find_first_of(' ');
1466                                                 priority=atoi(String(timecode,1,space-1).c_str());
1467                                                 timecode=String(timecode.begin()+space+1,timecode.end());
1468                                                 //synfig::info("priority: %d      timecode: %s",priority,timecode.c_str());
1469                                         }
1470
1471                                         timing_info.push_back(
1472                                                 Activepoint(
1473                                                         Time(
1474                                                                 timecode,
1475                                                                 fps
1476                                                         ),
1477                                                         false,  // Mark as a "off" activepoint
1478                                                         priority
1479                                                 )
1480                                         );
1481                                         if(iter==end_sequence.end())
1482                                                 end_sequence.clear();
1483                                         else
1484                                                 end_sequence=String(iter+1,end_sequence.end());
1485                                 }
1486
1487                                 timing_info.sort();
1488                         }
1489
1490                         if(child->get_attribute("use"))
1491                         {
1492                                 // \todo does this need to be able to read 'use="canvas"', like waypoints can now?  (see 'surefind_canvas' in this file)
1493                                 string id=child->get_attribute("use")->get_value();
1494                                 try
1495                                 {
1496                                         list_entry.value_node=canvas->surefind_value_node(id);
1497                                 }
1498                                 catch(Exception::IDNotFound)
1499                                 {
1500                                         error(child,"\"use\" attribute in <entry> references unknown ID -- "+id);
1501                                         continue;
1502                                 }
1503                         }
1504                         else
1505                         {
1506                                 xmlpp::Element::NodeList list = child->get_children();
1507                                 xmlpp::Element::NodeList::iterator iter;
1508
1509                                 // Search for the first non-text XML element
1510                                 for(iter = list.begin(); iter != list.end(); ++iter)
1511                                         if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1512
1513                                 if(iter==list.end())
1514                                 {
1515                                         error(child,strprintf(_("<entry> is missing its contents or missing \"use\" element")));
1516                                         continue;
1517                                 }
1518
1519                                 list_entry.value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1520
1521                                 if(!list_entry.value_node)
1522                                         error((*iter),"Parse of ValueNode failed");
1523
1524                                 // \todo do a search for more elements and warn if they are found
1525
1526                         }
1527
1528                         value_node->add(list_entry);
1529                         value_node->set_link(value_node->link_count()-1,list_entry.value_node);
1530                 }
1531                 else
1532                         error_unexpected_element(child,child->get_name());
1533         }
1534         return value_node;
1535 }
1536
1537 handle<ValueNode>
1538 CanvasParser::parse_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1539 {
1540         handle<ValueNode> value_node;
1541         assert(element);
1542
1543         GUID guid;
1544
1545         if(element->get_attribute("guid"))
1546         {
1547                 guid=GUID(element->get_attribute("guid")->get_value())^canvas->get_root()->get_guid();
1548                 value_node=guid_cast<ValueNode>(guid);
1549                 if(value_node)
1550                         return value_node;
1551         }
1552
1553         // If ValueBase::ident_type() recognises the name, then we know it's a ValueBase
1554         if(element->get_name()!="canvas" && ValueBase::ident_type(element->get_name()))
1555         {
1556                 ValueBase data=parse_value(element,canvas);
1557
1558                 if(!data.is_valid())
1559                 {
1560                         error(element,strprintf(_("Bad data in <%s>"),element->get_name().c_str()));
1561                         return value_node;
1562                 }
1563
1564                 // We want to convert this ValueBase into a
1565                 // ValueNode_Const. That way, we can treat the
1566                 // ID like any other Datanode. Think of this
1567                 // as a shorthand for creating constant ValueNodes.
1568
1569                 value_node=ValueNode_Const::create(data);
1570         }
1571         else
1572         if(element->get_name()=="hermite" || element->get_name()=="animated")
1573                 value_node=parse_animated(element,canvas);
1574         else
1575         if(element->get_name()=="composite")
1576                 value_node=parse_composite(element,canvas);
1577         else
1578         if(element->get_name()=="dynamic_list")
1579                 value_node=parse_dynamic_list(element,canvas);
1580         else
1581         if(element->get_name()=="bline") // This is not a typo. The dynamic list parser will parse a bline.
1582                 value_node=parse_dynamic_list(element,canvas);
1583         else
1584         if(LinkableValueNode::book().count(element->get_name()))
1585                 value_node=parse_linkable_value_node(element,canvas);
1586         else
1587         if(element->get_name()=="canvas")
1588                 value_node=ValueNode_Const::create(parse_canvas(element,canvas,true));
1589         else
1590         {
1591                 error_unexpected_element(element,element->get_name());
1592                 error(element, "Expected a ValueNode");
1593         }
1594
1595
1596         value_node->set_root_canvas(canvas->get_root());
1597
1598
1599         // If we were successful, and our element has
1600         // an ID attribute, go ahead and add it to the
1601         // value_node list
1602         if(value_node && element->get_attribute("id"))
1603         {
1604                 string id=element->get_attribute("id")->get_value();
1605
1606                 //value_node->set_id(id);
1607
1608                 // If there is already a value_node in the list
1609                 // with the same ID, then that is an error
1610                 try { canvas->add_value_node(value_node,id); }
1611                 catch(Exception::BadLinkName)
1612                 {
1613                         warning(element,strprintf(_("Bad ID \"%s\""),id.c_str()));
1614                         return value_node;
1615                 }
1616                 catch(Exception::IDAlreadyExists)
1617                 {
1618                         error(element,strprintf(_("Duplicate ID \"%s\""),id.c_str()));
1619                         return value_node;
1620                 }
1621                 catch(...)
1622                 {
1623                         error(element,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id.c_str()));
1624                         throw;
1625                 }
1626         }
1627         value_node->set_guid(guid);
1628         return value_node;
1629 }
1630
1631 void
1632 CanvasParser::parse_canvas_defs(xmlpp::Element *element,Canvas::Handle canvas)
1633 {
1634         assert(element->get_name()=="defs");
1635         xmlpp::Element::NodeList list = element->get_children();
1636         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1637         {
1638                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1639                 if(!child)
1640                         continue;
1641                 else
1642                 if(child->get_name()=="canvas")
1643                         parse_canvas(child, canvas);
1644                 else
1645                         parse_value_node(child,canvas);
1646         }
1647 }
1648
1649 Layer::Handle
1650 CanvasParser::parse_layer(xmlpp::Element *element,Canvas::Handle canvas)
1651 {
1652
1653         assert(element->get_name()=="layer");
1654         Layer::Handle layer;
1655
1656         if(!element->get_attribute("type"))
1657         {
1658                 error(element,_("Missing \"type\" attribute to \"layer\" element"));
1659                 return Layer::Handle();
1660         }
1661
1662         layer=Layer::create(element->get_attribute("type")->get_value());
1663         layer->set_canvas(canvas);
1664
1665         if(element->get_attribute("group"))
1666         {
1667                 layer->add_to_group(
1668                         element->get_attribute("group")->get_value()
1669                 );
1670         }
1671
1672         // Handle the version attribute
1673         if(element->get_attribute("version"))
1674         {
1675                 String version(element->get_attribute("version")->get_value());
1676                 if(version>layer->get_version())
1677                         warning(element,_("Installed layer version is larger than layer version in file"));
1678                 if(version!=layer->get_version())
1679                         layer->set_version(version);
1680         }
1681
1682         // Handle the description
1683         if(element->get_attribute("desc"))
1684                 layer->set_description(element->get_attribute("desc")->get_value());
1685
1686         if(element->get_attribute("active"))
1687                 layer->set_active(element->get_attribute("active")->get_value()=="false"?false:true);
1688
1689         xmlpp::Element::NodeList list = element->get_children();
1690         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1691         {
1692                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1693                 if(!child)
1694                         continue;
1695                 else
1696                 if(child->get_name()=="name")
1697                         warning(child,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1698                 else
1699                 if(child->get_name()=="desc")
1700                         warning(child,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1701                 else
1702                 if(child->get_name()=="param")
1703                 {
1704                         xmlpp::Element::NodeList list = child->get_children();
1705
1706                         if(!child->get_attribute("name"))
1707                         {
1708                                 error(child,_("Missing \"name\" attribute for <param>."));
1709                                 continue;
1710                         }
1711
1712                         String param_name=child->get_attribute("name")->get_value();
1713
1714                         if(child->get_attribute("use"))
1715                         {
1716                                 // If the "use" attribute is used, then the
1717                                 // element should be empty. Warn the user if
1718                                 // we find otherwise.
1719                                 if(!list.empty())
1720                                         warning(child,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1721
1722                                 String str=     child->get_attribute("use")->get_value();
1723
1724                                 if(layer->get_param(param_name).get_type()==ValueBase::TYPE_CANVAS)
1725                                 {
1726                                         if(!layer->set_param(param_name,canvas->surefind_canvas(str)))
1727                                                 error((*iter),_("Layer rejected canvas link"));
1728                                 }
1729                                 else
1730                                 try
1731                                 {
1732                                         handle<ValueNode> value_node=canvas->surefind_value_node(str);
1733
1734                                         // Assign the value_node to the dynamic parameter list
1735                                         layer->connect_dynamic_param(param_name,value_node);
1736                         }
1737                                 catch(Exception::IDNotFound)
1738                                 {
1739                                         error(child,strprintf(_("Unknown ID (%s) referenced in <param>"),str.c_str()));
1740                                 }
1741
1742                                 continue;
1743                         }
1744
1745                         xmlpp::Element::NodeList::iterator iter;
1746
1747                         // Search for the first non-text XML element
1748                         for(iter = list.begin(); iter != list.end(); ++iter)
1749                                 if(dynamic_cast<xmlpp::Element*>(*iter))
1750                                         break;
1751                                 //if(!(!dynamic_cast<xmlpp::Element*>(*iter) && (*iter)->get_name()=="text"||(*iter)->get_name()=="comment"   )) break;
1752
1753                         if(iter==list.end())
1754                         {
1755                                 error(child,_("<param> is either missing its contents, or missing a \"use\" attribute."));
1756                                 continue;
1757                         }
1758
1759                         // If we recognise the element name as a
1760                         // ValueBase, then treat is at one
1761                         if(/*(*iter)->get_name()!="canvas" && */ValueBase::ident_type((*iter)->get_name()) && !dynamic_cast<xmlpp::Element*>(*iter)->get_attribute("guid"))
1762                         {
1763                                 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1764
1765                                 if(!data.is_valid())
1766                                 {
1767                                         error((*iter),_("Bad data for <param>"));
1768                                         continue;
1769                                 }
1770
1771                                 // Set the layer's parameter, and make sure that
1772                                 // the layer liked it
1773                                 if(!layer->set_param(param_name,data))
1774                                 {
1775                                         warning((*iter),_("Layer rejected value for <param>"));
1776                                         continue;
1777                                 }
1778                         }
1779                         else    // ... otherwise, we assume that it is a ValueNode
1780                         {
1781                                 handle<ValueNode> value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1782
1783                                 if(!value_node)
1784                                 {
1785                                         error((*iter),_("Bad data for <param>"));
1786                                         continue;
1787                                 }
1788
1789                                 // Assign the value_node to the dynamic parameter list
1790                                 layer->connect_dynamic_param(param_name,value_node);
1791                         }
1792
1793                         // Warn if there is trash after the param value
1794                         for(iter++; iter != list.end(); ++iter)
1795                                 if(dynamic_cast<xmlpp::Element*>(*iter))
1796                                         warning((*iter),strprintf(_("Unexpected element <%s> after <param> data, ignoring..."),(*iter)->get_name().c_str()));
1797                         continue;
1798                 }
1799                 else
1800                         error_unexpected_element(child,child->get_name());
1801         }
1802
1803         layer->reset_version();
1804         return layer;
1805 }
1806
1807 Canvas::Handle
1808 CanvasParser::parse_canvas(xmlpp::Element *element,Canvas::Handle parent,bool inline_, String filename)
1809 {
1810
1811         if(element->get_name()!="canvas")
1812         {
1813                 error_unexpected_element(element,element->get_name(),"canvas");
1814                 return Canvas::Handle();
1815         }
1816         Canvas::Handle canvas;
1817
1818
1819
1820         if(parent && (element->get_attribute("id") || inline_))
1821         {
1822                 if(inline_)
1823                 {
1824                         canvas=Canvas::create_inline(parent);
1825                 }
1826                 else
1827                 {
1828                         try
1829                         {
1830                                 canvas=parent->find_canvas(element->get_attribute("id")->get_value());
1831                         }
1832                         catch(...)
1833                         {
1834                                 canvas=parent->new_child_canvas(element->get_attribute("id")->get_value());
1835                         }
1836                 }
1837                 canvas->rend_desc().clear_flags();
1838         }
1839         else
1840         {
1841                 canvas=Canvas::create();
1842                 if(filename=="/dev/stdin")
1843                         canvas->set_file_name("./stdin.sif");
1844                 else
1845                         canvas->set_file_name(filename);
1846                 canvas->rend_desc().clear_flags();
1847         }
1848
1849         if(element->get_attribute("guid"))
1850         {
1851                 GUID guid(element->get_attribute("guid")->get_value());
1852                 if(guid_cast<Canvas>(guid))
1853                         return guid_cast<Canvas>(guid);
1854                 else
1855                         canvas->set_guid(guid);
1856         }
1857
1858         if(element->get_attribute("width"))
1859                 canvas->rend_desc().set_w(atoi(element->get_attribute("width")->get_value().c_str()));
1860
1861         if(element->get_attribute("height"))
1862                 canvas->rend_desc().set_h(atoi(element->get_attribute("height")->get_value().c_str()));
1863
1864         if(element->get_attribute("xres"))
1865                 canvas->rend_desc().set_x_res(atof(element->get_attribute("xres")->get_value().c_str()));
1866
1867         if(element->get_attribute("yres"))
1868                 canvas->rend_desc().set_y_res(atof(element->get_attribute("yres")->get_value().c_str()));
1869
1870
1871         if(element->get_attribute("fps"))
1872                 canvas->rend_desc().set_frame_rate(atof(element->get_attribute("fps")->get_value().c_str()));
1873
1874         if(element->get_attribute("start-time"))
1875                 canvas->rend_desc().set_time_start(Time(element->get_attribute("start-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1876
1877         if(element->get_attribute("begin-time"))
1878                 canvas->rend_desc().set_time_start(Time(element->get_attribute("begin-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1879
1880         if(element->get_attribute("end-time"))
1881                 canvas->rend_desc().set_time_end(Time(element->get_attribute("end-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1882
1883         if(element->get_attribute("antialias"))
1884                 canvas->rend_desc().set_antialias(atoi(element->get_attribute("antialias")->get_value().c_str()));
1885
1886         if(element->get_attribute("view-box"))
1887         {
1888                 string values=element->get_attribute("view-box")->get_value();
1889                 Vector
1890                         tl,
1891                         br;
1892                 tl[0]=atof(string(values.data(),values.find(' ')).c_str());
1893                 values=string(values.begin()+values.find(' ')+1,values.end());
1894                 tl[1]=atof(string(values.data(),values.find(' ')).c_str());
1895                 values=string(values.begin()+values.find(' ')+1,values.end());
1896                 br[0]=atof(string(values.data(),values.find(' ')).c_str());
1897                 values=string(values.begin()+values.find(' ')+1,values.end());
1898                 br[1]=atof(values.c_str());
1899
1900                 canvas->rend_desc().set_tl(tl);
1901                 canvas->rend_desc().set_br(br);
1902         }
1903
1904         if(element->get_attribute("bgcolor"))
1905         {
1906                 string values=element->get_attribute("bgcolor")->get_value();
1907                 Color bg;
1908
1909                 bg.set_r(atof(string(values.data(),values.find(' ')).c_str()));
1910                 values=string(values.begin()+values.find(' ')+1,values.end());
1911
1912                 bg.set_g(atof(string(values.data(),values.find(' ')).c_str()));
1913                 values=string(values.begin()+values.find(' ')+1,values.end());
1914
1915                 bg.set_b(atof(string(values.data(),values.find(' ')).c_str()));
1916                 values=string(values.begin()+values.find(' ')+1,values.end());
1917
1918                 bg.set_a(atof(values.c_str()));
1919
1920                 canvas->rend_desc().set_bg_color(bg);
1921         }
1922
1923         if(element->get_attribute("focus"))
1924         {
1925                 string values=element->get_attribute("focus")->get_value();
1926                 Vector focus;
1927
1928                 focus[0]=atof(string(values.data(),values.find(' ')).c_str());
1929                 values=string(values.begin()+values.find(' ')+1,values.end());
1930                 focus[1]=atof(values.c_str());
1931
1932                 canvas->rend_desc().set_focus(focus);
1933         }
1934
1935         canvas->rend_desc().set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
1936
1937         xmlpp::Element::NodeList list = element->get_children();
1938         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1939         {
1940                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1941                 if(child)
1942                 {
1943                         if(child->get_name()=="defs")
1944                         {
1945                                 if(canvas->is_inline())
1946                                         error(child,_("Inline canvas cannot have a <defs> section"));
1947                                 parse_canvas_defs(child, canvas);
1948                         }
1949                         else
1950                         if(child->get_name()=="keyframe")
1951                         {
1952                                 if(canvas->is_inline())
1953                                 {
1954                                         warning(child,_("Inline canvas cannot have keyframes"));
1955                                         continue;
1956                                 }
1957
1958                                 canvas->keyframe_list().add(parse_keyframe(child,canvas));
1959                                 canvas->keyframe_list().sync();
1960                         }
1961                         else
1962                         if(child->get_name()=="meta")
1963                         {
1964                                 if(canvas->is_inline())
1965                                 {
1966                                         warning(child,_("Inline canvases cannot have metadata"));
1967                                         continue;
1968                                 }
1969
1970                                 String name,content;
1971
1972                                 if(!child->get_attribute("name"))
1973                                 {
1974                                         warning(child,_("<meta> must have a name"));
1975                                         continue;
1976                                 }
1977
1978                                 if(!child->get_attribute("content"))
1979                                 {
1980                                         warning(child,_("<meta> must have content"));
1981                                         continue;
1982                                 }
1983
1984                                 canvas->set_meta_data(child->get_attribute("name")->get_value(),child->get_attribute("content")->get_value());
1985                         }
1986                         else if(child->get_name()=="name")
1987                         {
1988                                 xmlpp::Element::NodeList list = child->get_children();
1989
1990                                 // If we don't have any name, warn
1991                                 if(list.empty())
1992                                         warning(child,_("blank \"name\" entitity"));
1993
1994                                 string tmp;
1995                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1996                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
1997                                 canvas->set_name(tmp);
1998                         }
1999                         else
2000                         if(child->get_name()=="desc")
2001                         {
2002
2003                                 xmlpp::Element::NodeList list = child->get_children();
2004
2005                                 // If we don't have any description, warn
2006                                 if(list.empty())
2007                                         warning(child,_("blank \"desc\" entitity"));
2008
2009                                 string tmp;
2010                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2011                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2012                                 canvas->set_description(tmp);
2013                         }
2014                         else
2015                         if(child->get_name()=="author")
2016                         {
2017
2018                                 xmlpp::Element::NodeList list = child->get_children();
2019
2020                                 // If we don't have any description, warn
2021                                 if(list.empty())
2022                                         warning(child,_("blank \"author\" entitity"));
2023
2024                                 string tmp;
2025                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2026                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2027                                 canvas->set_author(tmp);
2028                         }
2029                         else
2030                         if(child->get_name()=="layer")
2031                         {
2032                                 //if(canvas->is_inline())
2033                                 //      canvas->push_front(parse_layer(child,canvas->parent()));
2034                                 //else
2035                                         canvas->push_front(parse_layer(child,canvas));
2036                         }
2037                         else
2038                                 error_unexpected_element(child,child->get_name());
2039                 }
2040 //              else
2041 //              if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2042 //                      continue;
2043         }
2044
2045         if(canvas->value_node_list().placeholder_count())
2046         {
2047                 error(element,strprintf(_("Canvas %s has undefined ValueNodes"),canvas->get_id().c_str()));
2048         }
2049
2050         return canvas;
2051 }
2052
2053 Canvas::Handle
2054 CanvasParser::parse_from_file(const String &file)
2055 {
2056         return parse_from_file_as(file,file);
2057 }
2058
2059 Canvas::Handle
2060 CanvasParser::parse_from_file_as(const String &file_,const String &as_)
2061 {
2062         CHECK_EXPIRE_TIME();
2063         try
2064         {
2065         ChangeLocale change_locale(LC_NUMERIC, "C");
2066                 String file(unix_to_local_path(file_));
2067                 String as(unix_to_local_path(as_));
2068
2069                 if(get_open_canvas_map().count(etl::absolute_path(as)))
2070                         return get_open_canvas_map()[etl::absolute_path(as)];
2071
2072                 filename=as;
2073                 total_warnings_=0;
2074                 xmlpp::DomParser parser(file);
2075                 if(parser)
2076                 {
2077                         Canvas::Handle canvas(parse_canvas(parser.get_document()->get_root_node(),0,false,as));
2078                         get_open_canvas_map()[etl::absolute_path(as)]=canvas;
2079                         canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2080                         canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2081
2082
2083
2084                         const ValueNodeList& value_node_list(canvas->value_node_list());
2085
2086                         again:
2087                         ValueNodeList::const_iterator iter;
2088                         for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2089                         {
2090                                 ValueNode::Handle value_node(*iter);
2091                                 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2092                                 {
2093                                         canvas->remove_value_node(value_node);
2094                                         goto again;
2095                                 }
2096                         }
2097
2098                         return canvas;
2099                 }
2100         }
2101         catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2102         catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2103         catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2104         catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2105         catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2106         catch(const std::exception& ex)
2107         {
2108                 synfig::error("Standard Exception: "+String(ex.what()));
2109                 return Canvas::Handle();
2110         }
2111         catch(const String& str)
2112         {
2113                 cerr<<str<<endl;
2114                 //      synfig::error(str);
2115                 return Canvas::Handle();
2116         }
2117         return Canvas::Handle();
2118 }
2119
2120 Canvas::Handle
2121 CanvasParser::parse_from_string(const String &data)
2122 {
2123         CHECK_EXPIRE_TIME();
2124
2125         try
2126         {
2127         ChangeLocale change_locale(LC_NUMERIC, "C");
2128                 filename=_("<INTERNAL>");
2129                 total_warnings_=0;
2130                 xmlpp::DomParser parser;
2131                 parser.parse_memory(data);
2132                 xmlpp::Element *root=parser.get_document()->get_root_node();
2133                 if(parser)
2134                 {
2135                         Canvas::Handle canvas(parse_canvas(root));
2136                         canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2137                         canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2138
2139                         const ValueNodeList& value_node_list(canvas->value_node_list());
2140                         again:
2141                         ValueNodeList::const_iterator iter;
2142                         for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2143                         {
2144                                 ValueNode::Handle value_node(*iter);
2145                                 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2146                                 {
2147                                         canvas->remove_value_node(value_node);
2148                                         goto again;
2149                                 }
2150                         }
2151
2152                         return canvas;
2153                 }
2154         }
2155         catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2156         catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2157         catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2158         catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2159         catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2160         catch(const std::exception& ex)
2161         {
2162                 synfig::error("Standard Exception: "+String(ex.what()));
2163                 return Canvas::Handle();
2164         }
2165         catch(const String& str)
2166         {
2167                 cerr<<str<<endl;
2168                 //      synfig::error(str);
2169                 return Canvas::Handle();
2170         }
2171         return Canvas::Handle();
2172 }