Prevent a compiler warning.
[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
1055         // in canvas version 0.1, angles used to wrap, so to get from -179
1056         // degrees to 180 degrees meant a 1 degree change
1057         // in canvas version 0.2 they don't, so that's a 359 degree change
1058
1059         // when loading a version 0.1 canvas, modify constant angle
1060         // waypoints to that they are within 180 degrees of the previous
1061         // waypoint's value
1062         if (type == ValueBase::TYPE_ANGLE) 
1063         {
1064                 Canvas::Handle parent = canvas;
1065                 while (!parent->is_root())
1066                         parent = parent->parent();
1067
1068                 if (parent->get_version() == "0.1")
1069                 {
1070                         bool first = true;
1071                         Real angle, prev = 0;
1072                         WaypointList &wl = value_node->waypoint_list();
1073                         for (WaypointList::iterator iter = wl.begin(); iter != wl.end(); iter++)
1074                         {
1075                                 angle = Angle::deg(iter->get_value(iter->get_time()).get(Angle())).get();
1076                                 if (first)
1077                                         first = false;
1078                                 else if (iter->get_value_node()->get_name() == "constant")
1079                                         if (angle - prev > 180)
1080                                         {
1081                                                 while (angle - prev > 180) angle -= 360;
1082                                                 iter->set_value(Angle::deg(angle));
1083                                         }
1084                                         else if (prev - angle > 180)
1085                                         {
1086                                                 while (prev - angle > 180) angle += 360;
1087                                                 iter->set_value(Angle::deg(angle));
1088                                         }
1089                                 prev = angle;
1090                         }
1091                 }
1092         }
1093
1094         value_node->changed();
1095         return value_node;
1096 }
1097
1098 etl::handle<LinkableValueNode>
1099 CanvasParser::parse_linkable_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1100 {
1101         handle<LinkableValueNode> value_node;
1102         ValueBase::Type type;
1103
1104         // Determine the type
1105         if(element->get_attribute("type"))
1106         {
1107                 type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1108
1109                 if(!type)
1110                 {
1111                         error(element,"Bad type in ValueNode");
1112                         return 0;
1113                 }
1114         }
1115         else
1116         {
1117                 error(element,"Missing type in ValueNode");
1118                 return 0;
1119         }
1120
1121         value_node=LinkableValueNode::create(element->get_name(),type);
1122
1123         if(!value_node)
1124         {
1125                 error(element,"Unknown ValueNode type "+element->get_name());
1126                 return 0;
1127         }
1128
1129         if(value_node->get_type()!=type)
1130         {
1131                 error(element,"ValueNode did not accept type");
1132                 return 0;
1133         }
1134
1135         value_node->set_root_canvas(canvas->get_root());
1136
1137         int i;
1138         for(i=0;i<value_node->link_count();i++)
1139         {
1140                 if(element->get_attribute(value_node->link_name(i)))
1141                 try {
1142                         String id(element->get_attribute(value_node->link_name(i))->get_value());
1143
1144                         if(!value_node->set_link(i,
1145                                         canvas->surefind_value_node(
1146                                                 id
1147                                         )
1148                                 )
1149                         ) 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()));
1150                 }
1151                 catch(Exception::IDNotFound)
1152                 {
1153                         error(element,"Unable to resolve "+element->get_attribute(value_node->link_name(i))->get_value());
1154                 }
1155                 catch(Exception::FileNotFound)
1156                 {
1157                         error(element,"Unable to open file referenced in "+element->get_attribute(value_node->link_name(i))->get_value());
1158                 }
1159                 catch(...)
1160                 {
1161                         error(element,strprintf(_("Unknown Exception thrown when referencing ValueNode \"%s\""),
1162                                 element->get_attribute(value_node->link_name(i))->get_value().c_str()));
1163                         throw;
1164                 }
1165         }
1166
1167
1168
1169         xmlpp::Element::NodeList list = element->get_children();
1170         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1171         {
1172                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1173                 try
1174                 {
1175                         if(!child)
1176                                 continue;
1177                         int index=value_node->get_link_index_from_name(child->get_name());
1178
1179                         xmlpp::Element::NodeList list = child->get_children();
1180                         xmlpp::Element::NodeList::iterator iter;
1181
1182                         // Search for the first non-text XML element
1183                         for(iter = list.begin(); iter != list.end(); ++iter)
1184                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1185
1186                         if(iter==list.end())
1187                         {
1188                                 error(child,_("element is missing its contents"));
1189                                 continue;
1190                         }
1191
1192                         ValueNode::Handle link=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1193
1194                         if(!link)
1195                         {
1196                                 error((*iter),"Parse of ValueNode failed");
1197                         }
1198                         else
1199                         if(!value_node->set_link(index,link))
1200                         {
1201                                 //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));
1202                                 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));
1203                         }
1204
1205                         // \todo do a search for more elements and warn if they are found
1206
1207                 }
1208                 catch(Exception::BadLinkName)
1209                 {
1210                         error_unexpected_element(child,child->get_name());
1211                 }
1212                 catch(...)
1213                 {
1214                         error(child,strprintf(_("Unknown Exception thrown when working on element \"%s\""),child->get_name().c_str()));
1215                         throw;
1216                 }
1217         }
1218
1219         return value_node;
1220 }
1221
1222 handle<ValueNode_Composite>
1223 CanvasParser::parse_composite(xmlpp::Element *element,Canvas::Handle canvas)
1224 {
1225         assert(element->get_name()=="composite");
1226
1227         if(!element->get_attribute("type"))
1228         {
1229                 error(element,"Missing attribute \"type\" in <composite>");
1230                 return handle<ValueNode_Composite>();
1231         }
1232
1233         ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1234
1235         if(!type)
1236         {
1237                 error(element,"Bad type in <composite>");
1238                 return handle<ValueNode_Composite>();
1239         }
1240
1241         handle<ValueNode_Composite> value_node=ValueNode_Composite::create(type);
1242         handle<ValueNode> c[6];
1243
1244         if(!value_node)
1245         {
1246                 error(element,strprintf(_("Unable to create <composite>")));
1247                 return handle<ValueNode_Composite>();
1248         }
1249
1250         int i;
1251
1252         for(i=0;i<value_node->link_count();i++)
1253         {
1254                 string name=strprintf("c%d",i+1);
1255                 if(c[i])
1256                 {
1257                         error(element,name+" was already defined in <composite>");
1258                         continue;
1259                 }
1260                 if(element->get_attribute(name))
1261                 {
1262                         c[i]=canvas->surefind_value_node(element->get_attribute(name)->get_value());
1263                         if(c[i])
1264                         {
1265                                 if(!value_node->set_link(i,c[i]))
1266                                 {
1267                                         error(element,'"'+name+"\" attribute in <composite> has bad type");
1268                                 }
1269                         }
1270                         else
1271                                 error(element,'"'+name+"\" attribute in <composite> references unknown ID");
1272                 }
1273         }
1274
1275         xmlpp::Element::NodeList list = element->get_children();
1276         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1277         {
1278                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1279                 if(!child)
1280                         continue;
1281                 else
1282                 for(i=0;i<value_node->link_count();i++)
1283                 {
1284                         string name=strprintf("c%d",i+1);
1285                         if(child->get_name()==name)
1286                         {
1287                                 if(c[i])
1288                                 {
1289                                         error(child,name+" was already defined in <composite>");
1290                                         break;
1291                                 }
1292
1293                                 xmlpp::Element::NodeList list = child->get_children();
1294                                 xmlpp::Element::NodeList::iterator iter;
1295
1296                                 // Search for the first non-text XML element
1297                                 for(iter = list.begin(); iter != list.end(); ++iter)
1298                                         if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1299
1300                                 if(iter==list.end())
1301                                 {
1302                                         error(child,strprintf(_("<%s> is missing its contents"),name.c_str()));
1303                                         break;
1304                                 }
1305
1306                                 c[i]=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1307
1308                                 if(!c[i])
1309                                 {
1310                                         error((*iter),"Parse of "+name+" ValueNode failed");
1311                                         break;
1312                                 }
1313
1314                                 if(!value_node->set_link(i,c[i]))
1315                                 {
1316                                         error(child,strprintf(_("<%s> has a bad value"),name.c_str()));
1317                                         break;
1318                                 }
1319
1320                                 // \todo do a search for more elements and warn if they are found
1321                                 break;
1322                         }
1323                 }
1324                 // somewhat of a hack, but it works
1325                 if(i==value_node->link_count()) error_unexpected_element(child,child->get_name());
1326         }
1327
1328         switch(value_node->link_count())
1329         {
1330         case 1:
1331                 if(!value_node->get_link(0))
1332                 {
1333                         error(element,"<composite> is missing parameters");
1334                         return handle<ValueNode_Composite>();
1335                 }
1336                 break;
1337         case 2:
1338                 if(!value_node->get_link(0) ||!value_node->get_link(1))
1339                 {
1340                         error(element,"<composite> is missing parameters");
1341                         return handle<ValueNode_Composite>();
1342                 }
1343                 break;
1344         case 3:
1345                 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2))
1346                 {
1347                         error(element,"<composite> is missing parameters");
1348                         return handle<ValueNode_Composite>();
1349                 }
1350                 break;
1351         case 4:
1352                 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2) ||!value_node->get_link(3))
1353                 {
1354                         error(element,"<composite> is missing parameters");
1355                         return handle<ValueNode_Composite>();
1356                 }
1357                 break;
1358     }
1359         return value_node;
1360 }
1361
1362 // This will also parse a bline
1363 handle<ValueNode_DynamicList>
1364 CanvasParser::parse_dynamic_list(xmlpp::Element *element,Canvas::Handle canvas)
1365 {
1366         assert(element->get_name()=="dynamic_list" || element->get_name()=="bline");
1367
1368         const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
1369
1370         if(!element->get_attribute("type"))
1371         {
1372                 error(element,"Missing attribute \"type\" in <dynamic_list>");
1373                 return handle<ValueNode_DynamicList>();
1374         }
1375
1376         ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1377
1378         if(!type)
1379         {
1380                 error(element,"Bad type in <dynamic_list>");
1381                 return handle<ValueNode_DynamicList>();
1382         }
1383
1384         handle<ValueNode_DynamicList> value_node;
1385         handle<ValueNode_BLine> bline_value_node;
1386
1387         if(element->get_name()=="bline")
1388         {
1389                 value_node=bline_value_node=ValueNode_BLine::create();
1390                 if(element->get_attribute("loop"))
1391                 {
1392                         String loop=element->get_attribute("loop")->get_value();
1393                         if(loop=="true" || loop=="1" || loop=="TRUE" || loop=="True")
1394                                 bline_value_node->set_loop(true);
1395                         else
1396                                 bline_value_node->set_loop(false);
1397                 }
1398
1399         }
1400         else
1401                 value_node=ValueNode_DynamicList::create(type);
1402
1403         if(!value_node)
1404         {
1405                 error(element,strprintf(_("Unable to create <dynamic_list>")));
1406                 return handle<ValueNode_DynamicList>();
1407         }
1408
1409         value_node->set_root_canvas(canvas->get_root());
1410
1411         xmlpp::Element::NodeList list = element->get_children();
1412         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1413         {
1414                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1415                 if(!child)
1416                         continue;
1417                 else
1418                 if(child->get_name()=="entry")
1419                 {
1420                         ValueNode_DynamicList::ListEntry list_entry;
1421
1422                         // Parse begin/end waypoints
1423                         {
1424                                 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
1425                                 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
1426
1427                                 String begin_sequence;
1428                                 String end_sequence;
1429
1430                                 ActivepointList &timing_info(list_entry.timing_info);
1431
1432                                 if(child->get_attribute("begin"))
1433                                         begin_sequence=child->get_attribute("begin")->get_value();
1434
1435                                 if(child->get_attribute("on"))
1436                                         begin_sequence=child->get_attribute("on")->get_value();
1437
1438                                 if(child->get_attribute("end"))
1439                                         end_sequence=child->get_attribute("end")->get_value();
1440
1441                                 if(child->get_attribute("off"))
1442                                         end_sequence=child->get_attribute("off")->get_value();
1443
1444                                 // clear out any auto-start
1445                                 if(!begin_sequence.empty())
1446                                         timing_info.clear();
1447
1448                                 //! \optimize
1449                                 while(!begin_sequence.empty())
1450                                 {
1451                                         String::iterator iter(find(begin_sequence.begin(),begin_sequence.end(),','));
1452                                         String timecode(begin_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                                                         true,   // Mark as a "on" activepoint
1478                                                         priority
1479                                                 )
1480                                         );
1481
1482                                         if(iter==begin_sequence.end())
1483                                                 begin_sequence.clear();
1484                                         else
1485                                                 begin_sequence=String(iter+1,begin_sequence.end());
1486                                 }
1487
1488                                 //! \optimize
1489                                 while(!end_sequence.empty())
1490                                 {
1491                                         String::iterator iter(find(end_sequence.begin(),end_sequence.end(),','));
1492                                         String timecode(end_sequence.begin(),   iter);
1493                                         int priority=0;
1494
1495                                         // skip whitespace before checking for a priority
1496                                         while (isspace(timecode[0]))
1497                                                 timecode=timecode.substr(1);
1498
1499                                         // If there is a priority, then grab it and remove
1500                                         // it from the timecode
1501                                         if(timecode[0]=='p')
1502                                         {
1503                                                 //priority=timecode[1]-'0';
1504                                                 //timecode=String(timecode.begin()+3,timecode.end());
1505                                                 int space=timecode.find_first_of(' ');
1506                                                 priority=atoi(String(timecode,1,space-1).c_str());
1507                                                 timecode=String(timecode.begin()+space+1,timecode.end());
1508                                                 //synfig::info("priority: %d      timecode: %s",priority,timecode.c_str());
1509                                         }
1510
1511                                         timing_info.push_back(
1512                                                 Activepoint(
1513                                                         Time(
1514                                                                 timecode,
1515                                                                 fps
1516                                                         ),
1517                                                         false,  // Mark as a "off" activepoint
1518                                                         priority
1519                                                 )
1520                                         );
1521                                         if(iter==end_sequence.end())
1522                                                 end_sequence.clear();
1523                                         else
1524                                                 end_sequence=String(iter+1,end_sequence.end());
1525                                 }
1526
1527                                 timing_info.sort();
1528                         }
1529
1530                         if(child->get_attribute("use"))
1531                         {
1532                                 // \todo does this need to be able to read 'use="canvas"', like waypoints can now?  (see 'surefind_canvas' in this file)
1533                                 string id=child->get_attribute("use")->get_value();
1534                                 try
1535                                 {
1536                                         list_entry.value_node=canvas->surefind_value_node(id);
1537                                 }
1538                                 catch(Exception::IDNotFound)
1539                                 {
1540                                         error(child,"\"use\" attribute in <entry> references unknown ID -- "+id);
1541                                         continue;
1542                                 }
1543                         }
1544                         else
1545                         {
1546                                 xmlpp::Element::NodeList list = child->get_children();
1547                                 xmlpp::Element::NodeList::iterator iter;
1548
1549                                 // Search for the first non-text XML element
1550                                 for(iter = list.begin(); iter != list.end(); ++iter)
1551                                         if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1552
1553                                 if(iter==list.end())
1554                                 {
1555                                         error(child,strprintf(_("<entry> is missing its contents or missing \"use\" element")));
1556                                         continue;
1557                                 }
1558
1559                                 list_entry.value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1560
1561                                 if(!list_entry.value_node)
1562                                         error((*iter),"Parse of ValueNode failed");
1563
1564                                 // \todo do a search for more elements and warn if they are found
1565
1566                         }
1567
1568                         value_node->add(list_entry);
1569                         value_node->set_link(value_node->link_count()-1,list_entry.value_node);
1570                 }
1571                 else
1572                         error_unexpected_element(child,child->get_name());
1573         }
1574         return value_node;
1575 }
1576
1577 handle<ValueNode>
1578 CanvasParser::parse_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1579 {
1580         handle<ValueNode> value_node;
1581         assert(element);
1582
1583         GUID guid;
1584
1585         if(element->get_attribute("guid"))
1586         {
1587                 guid=GUID(element->get_attribute("guid")->get_value())^canvas->get_root()->get_guid();
1588                 value_node=guid_cast<ValueNode>(guid);
1589                 if(value_node)
1590                         return value_node;
1591         }
1592
1593         // If ValueBase::ident_type() recognizes the name, then we know it's a ValueBase
1594         if(element->get_name()!="canvas" && ValueBase::ident_type(element->get_name()))
1595         {
1596                 ValueBase data=parse_value(element,canvas);
1597
1598                 if(!data.is_valid())
1599                 {
1600                         error(element,strprintf(_("Bad data in <%s>"),element->get_name().c_str()));
1601                         return value_node;
1602                 }
1603
1604                 // We want to convert this ValueBase into a
1605                 // ValueNode_Const. That way, we can treat the
1606                 // ID like any other Datanode. Think of this
1607                 // as a shorthand for creating constant ValueNodes.
1608
1609                 value_node=ValueNode_Const::create(data);
1610         }
1611         else
1612         if(element->get_name()=="hermite" || element->get_name()=="animated")
1613                 value_node=parse_animated(element,canvas);
1614         else
1615         if(element->get_name()=="composite")
1616                 value_node=parse_composite(element,canvas);
1617         else
1618         if(element->get_name()=="dynamic_list")
1619                 value_node=parse_dynamic_list(element,canvas);
1620         else
1621         if(element->get_name()=="bline") // This is not a typo. The dynamic list parser will parse a bline.
1622                 value_node=parse_dynamic_list(element,canvas);
1623         else
1624         if(LinkableValueNode::book().count(element->get_name()))
1625                 value_node=parse_linkable_value_node(element,canvas);
1626         else
1627         if(element->get_name()=="canvas")
1628                 value_node=ValueNode_Const::create(parse_canvas(element,canvas,true));
1629         else
1630         {
1631                 error_unexpected_element(element,element->get_name());
1632                 error(element, "Expected a ValueNode");
1633         }
1634
1635
1636         value_node->set_root_canvas(canvas->get_root());
1637
1638
1639         // If we were successful, and our element has
1640         // an ID attribute, go ahead and add it to the
1641         // value_node list
1642         if(value_node && element->get_attribute("id"))
1643         {
1644                 string id=element->get_attribute("id")->get_value();
1645
1646                 //value_node->set_id(id);
1647
1648                 // If there is already a value_node in the list
1649                 // with the same ID, then that is an error
1650                 try { canvas->add_value_node(value_node,id); }
1651                 catch(Exception::BadLinkName)
1652                 {
1653                         warning(element,strprintf(_("Bad ID \"%s\""),id.c_str()));
1654                         return value_node;
1655                 }
1656                 catch(Exception::IDAlreadyExists)
1657                 {
1658                         error(element,strprintf(_("Duplicate ID \"%s\""),id.c_str()));
1659                         return value_node;
1660                 }
1661                 catch(...)
1662                 {
1663                         error(element,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id.c_str()));
1664                         throw;
1665                 }
1666         }
1667         value_node->set_guid(guid);
1668         return value_node;
1669 }
1670
1671 void
1672 CanvasParser::parse_canvas_defs(xmlpp::Element *element,Canvas::Handle canvas)
1673 {
1674         assert(element->get_name()=="defs");
1675         xmlpp::Element::NodeList list = element->get_children();
1676         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1677         {
1678                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1679                 if(!child)
1680                         continue;
1681                 else
1682                 if(child->get_name()=="canvas")
1683                         parse_canvas(child, canvas);
1684                 else
1685                         parse_value_node(child,canvas);
1686         }
1687 }
1688
1689 Layer::Handle
1690 CanvasParser::parse_layer(xmlpp::Element *element,Canvas::Handle canvas)
1691 {
1692
1693         assert(element->get_name()=="layer");
1694         Layer::Handle layer;
1695
1696         if(!element->get_attribute("type"))
1697         {
1698                 error(element,_("Missing \"type\" attribute to \"layer\" element"));
1699                 return Layer::Handle();
1700         }
1701
1702         layer=Layer::create(element->get_attribute("type")->get_value());
1703         layer->set_canvas(canvas);
1704
1705         if(element->get_attribute("group"))
1706         {
1707                 layer->add_to_group(
1708                         element->get_attribute("group")->get_value()
1709                 );
1710         }
1711
1712         // Handle the version attribute
1713         if(element->get_attribute("version"))
1714         {
1715                 String version(element->get_attribute("version")->get_value());
1716                 if(version>layer->get_version())
1717                         warning(element,_("Installed layer version is smaller than layer version in file"));
1718                 if(version!=layer->get_version())
1719                         layer->set_version(version);
1720         }
1721
1722         // Handle the description
1723         if(element->get_attribute("desc"))
1724                 layer->set_description(element->get_attribute("desc")->get_value());
1725
1726         if(element->get_attribute("active"))
1727                 layer->set_active(element->get_attribute("active")->get_value()=="false"?false:true);
1728
1729         xmlpp::Element::NodeList list = element->get_children();
1730         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1731         {
1732                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1733                 if(!child)
1734                         continue;
1735                 else
1736                 if(child->get_name()=="name")
1737                         warning(child,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1738                 else
1739                 if(child->get_name()=="desc")
1740                         warning(child,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1741                 else
1742                 if(child->get_name()=="param")
1743                 {
1744                         xmlpp::Element::NodeList list = child->get_children();
1745
1746                         if(!child->get_attribute("name"))
1747                         {
1748                                 error(child,_("Missing \"name\" attribute for <param>."));
1749                                 continue;
1750                         }
1751
1752                         String param_name=child->get_attribute("name")->get_value();
1753
1754                         if(child->get_attribute("use"))
1755                         {
1756                                 // If the "use" attribute is used, then the
1757                                 // element should be empty. Warn the user if
1758                                 // we find otherwise.
1759                                 if(!list.empty())
1760                                         warning(child,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1761
1762                                 String str=     child->get_attribute("use")->get_value();
1763
1764                                 if(layer->get_param(param_name).get_type()==ValueBase::TYPE_CANVAS)
1765                                 {
1766                                         if(!layer->set_param(param_name,canvas->surefind_canvas(str)))
1767                                                 error((*iter),_("Layer rejected canvas link"));
1768                                 }
1769                                 else
1770                                 try
1771                                 {
1772                                         handle<ValueNode> value_node=canvas->surefind_value_node(str);
1773
1774                                         // Assign the value_node to the dynamic parameter list
1775                                         layer->connect_dynamic_param(param_name,value_node);
1776                         }
1777                                 catch(Exception::IDNotFound)
1778                                 {
1779                                         error(child,strprintf(_("Unknown ID (%s) referenced in <param>"),str.c_str()));
1780                                 }
1781
1782                                 continue;
1783                         }
1784
1785                         xmlpp::Element::NodeList::iterator iter;
1786
1787                         // Search for the first non-text XML element
1788                         for(iter = list.begin(); iter != list.end(); ++iter)
1789                                 if(dynamic_cast<xmlpp::Element*>(*iter))
1790                                         break;
1791                                 //if(!(!dynamic_cast<xmlpp::Element*>(*iter) && (*iter)->get_name()=="text"||(*iter)->get_name()=="comment"   )) break;
1792
1793                         if(iter==list.end())
1794                         {
1795                                 error(child,_("<param> is either missing its contents, or missing a \"use\" attribute."));
1796                                 continue;
1797                         }
1798
1799                         // If we recognize the element name as a
1800                         // ValueBase, then treat is at one
1801                         if(/*(*iter)->get_name()!="canvas" && */ValueBase::ident_type((*iter)->get_name()) && !dynamic_cast<xmlpp::Element*>(*iter)->get_attribute("guid"))
1802                         {
1803                                 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1804
1805                                 if(!data.is_valid())
1806                                 {
1807                                         error((*iter),_("Bad data for <param>"));
1808                                         continue;
1809                                 }
1810
1811                                 // Set the layer's parameter, and make sure that
1812                                 // the layer liked it
1813                                 if(!layer->set_param(param_name,data))
1814                                 {
1815                                         warning((*iter),_("Layer rejected value for <param>"));
1816                                         continue;
1817                                 }
1818                         }
1819                         else    // ... otherwise, we assume that it is a ValueNode
1820                         {
1821                                 handle<ValueNode> value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1822
1823                                 if(!value_node)
1824                                 {
1825                                         error((*iter),_("Bad data for <param>"));
1826                                         continue;
1827                                 }
1828
1829                                 // Assign the value_node to the dynamic parameter list
1830                                 layer->connect_dynamic_param(param_name,value_node);
1831                         }
1832
1833                         // Warn if there is trash after the param value
1834                         for(iter++; iter != list.end(); ++iter)
1835                                 if(dynamic_cast<xmlpp::Element*>(*iter))
1836                                         warning((*iter),strprintf(_("Unexpected element <%s> after <param> data, ignoring..."),(*iter)->get_name().c_str()));
1837                         continue;
1838                 }
1839                 else
1840                         error_unexpected_element(child,child->get_name());
1841         }
1842
1843         layer->reset_version();
1844         return layer;
1845 }
1846
1847 Canvas::Handle
1848 CanvasParser::parse_canvas(xmlpp::Element *element,Canvas::Handle parent,bool inline_, String filename)
1849 {
1850
1851         if(element->get_name()!="canvas")
1852         {
1853                 error_unexpected_element(element,element->get_name(),"canvas");
1854                 return Canvas::Handle();
1855         }
1856         Canvas::Handle canvas;
1857
1858
1859
1860         if(parent && (element->get_attribute("id") || inline_))
1861         {
1862                 if(inline_)
1863                 {
1864                         canvas=Canvas::create_inline(parent);
1865                 }
1866                 else
1867                 {
1868                         try
1869                         {
1870                                 canvas=parent->find_canvas(element->get_attribute("id")->get_value());
1871                         }
1872                         catch(...)
1873                         {
1874                                 canvas=parent->new_child_canvas(element->get_attribute("id")->get_value());
1875                         }
1876                 }
1877                 canvas->rend_desc().clear_flags();
1878         }
1879         else
1880         {
1881                 canvas=Canvas::create();
1882                 if(filename=="/dev/stdin")
1883                         canvas->set_file_name("./stdin.sif");
1884                 else
1885                         canvas->set_file_name(filename);
1886                 canvas->rend_desc().clear_flags();
1887         }
1888
1889         if(element->get_attribute("guid"))
1890         {
1891                 GUID guid(element->get_attribute("guid")->get_value());
1892                 if(guid_cast<Canvas>(guid))
1893                         return guid_cast<Canvas>(guid);
1894                 else
1895                         canvas->set_guid(guid);
1896         }
1897
1898         if(element->get_attribute("version"))
1899                 canvas->set_version(element->get_attribute("version")->get_value());
1900
1901         if(element->get_attribute("width"))
1902                 canvas->rend_desc().set_w(atoi(element->get_attribute("width")->get_value().c_str()));
1903
1904         if(element->get_attribute("height"))
1905                 canvas->rend_desc().set_h(atoi(element->get_attribute("height")->get_value().c_str()));
1906
1907         if(element->get_attribute("xres"))
1908                 canvas->rend_desc().set_x_res(atof(element->get_attribute("xres")->get_value().c_str()));
1909
1910         if(element->get_attribute("yres"))
1911                 canvas->rend_desc().set_y_res(atof(element->get_attribute("yres")->get_value().c_str()));
1912
1913
1914         if(element->get_attribute("fps"))
1915                 canvas->rend_desc().set_frame_rate(atof(element->get_attribute("fps")->get_value().c_str()));
1916
1917         if(element->get_attribute("start-time"))
1918                 canvas->rend_desc().set_time_start(Time(element->get_attribute("start-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1919
1920         if(element->get_attribute("begin-time"))
1921                 canvas->rend_desc().set_time_start(Time(element->get_attribute("begin-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1922
1923         if(element->get_attribute("end-time"))
1924                 canvas->rend_desc().set_time_end(Time(element->get_attribute("end-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1925
1926         if(element->get_attribute("antialias"))
1927                 canvas->rend_desc().set_antialias(atoi(element->get_attribute("antialias")->get_value().c_str()));
1928
1929         if(element->get_attribute("view-box"))
1930         {
1931                 string values=element->get_attribute("view-box")->get_value();
1932                 Vector
1933                         tl,
1934                         br;
1935                 tl[0]=atof(string(values.data(),values.find(' ')).c_str());
1936                 values=string(values.begin()+values.find(' ')+1,values.end());
1937                 tl[1]=atof(string(values.data(),values.find(' ')).c_str());
1938                 values=string(values.begin()+values.find(' ')+1,values.end());
1939                 br[0]=atof(string(values.data(),values.find(' ')).c_str());
1940                 values=string(values.begin()+values.find(' ')+1,values.end());
1941                 br[1]=atof(values.c_str());
1942
1943                 canvas->rend_desc().set_tl(tl);
1944                 canvas->rend_desc().set_br(br);
1945         }
1946
1947         if(element->get_attribute("bgcolor"))
1948         {
1949                 string values=element->get_attribute("bgcolor")->get_value();
1950                 Color bg;
1951
1952                 bg.set_r(atof(string(values.data(),values.find(' ')).c_str()));
1953                 values=string(values.begin()+values.find(' ')+1,values.end());
1954
1955                 bg.set_g(atof(string(values.data(),values.find(' ')).c_str()));
1956                 values=string(values.begin()+values.find(' ')+1,values.end());
1957
1958                 bg.set_b(atof(string(values.data(),values.find(' ')).c_str()));
1959                 values=string(values.begin()+values.find(' ')+1,values.end());
1960
1961                 bg.set_a(atof(values.c_str()));
1962
1963                 canvas->rend_desc().set_bg_color(bg);
1964         }
1965
1966         if(element->get_attribute("focus"))
1967         {
1968                 string values=element->get_attribute("focus")->get_value();
1969                 Vector focus;
1970
1971                 focus[0]=atof(string(values.data(),values.find(' ')).c_str());
1972                 values=string(values.begin()+values.find(' ')+1,values.end());
1973                 focus[1]=atof(values.c_str());
1974
1975                 canvas->rend_desc().set_focus(focus);
1976         }
1977
1978         canvas->rend_desc().set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
1979
1980         xmlpp::Element::NodeList list = element->get_children();
1981         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1982         {
1983                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1984                 if(child)
1985                 {
1986                         if(child->get_name()=="defs")
1987                         {
1988                                 if(canvas->is_inline())
1989                                         error(child,_("Inline canvas cannot have a <defs> section"));
1990                                 parse_canvas_defs(child, canvas);
1991                         }
1992                         else
1993                         if(child->get_name()=="keyframe")
1994                         {
1995                                 if(canvas->is_inline())
1996                                 {
1997                                         warning(child,_("Inline canvas cannot have keyframes"));
1998                                         continue;
1999                                 }
2000
2001                                 canvas->keyframe_list().add(parse_keyframe(child,canvas));
2002                                 canvas->keyframe_list().sync();
2003                         }
2004                         else
2005                         if(child->get_name()=="meta")
2006                         {
2007                                 if(canvas->is_inline())
2008                                 {
2009                                         warning(child,_("Inline canvases cannot have metadata"));
2010                                         continue;
2011                                 }
2012
2013                                 String name,content;
2014
2015                                 if(!child->get_attribute("name"))
2016                                 {
2017                                         warning(child,_("<meta> must have a name"));
2018                                         continue;
2019                                 }
2020
2021                                 if(!child->get_attribute("content"))
2022                                 {
2023                                         warning(child,_("<meta> must have content"));
2024                                         continue;
2025                                 }
2026
2027                                 canvas->set_meta_data(child->get_attribute("name")->get_value(),child->get_attribute("content")->get_value());
2028                         }
2029                         else if(child->get_name()=="name")
2030                         {
2031                                 xmlpp::Element::NodeList list = child->get_children();
2032
2033                                 // If we don't have any name, warn
2034                                 if(list.empty())
2035                                         warning(child,_("blank \"name\" entity"));
2036
2037                                 string tmp;
2038                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2039                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2040                                 canvas->set_name(tmp);
2041                         }
2042                         else
2043                         if(child->get_name()=="desc")
2044                         {
2045
2046                                 xmlpp::Element::NodeList list = child->get_children();
2047
2048                                 // If we don't have any description, warn
2049                                 if(list.empty())
2050                                         warning(child,_("blank \"desc\" entity"));
2051
2052                                 string tmp;
2053                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2054                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2055                                 canvas->set_description(tmp);
2056                         }
2057                         else
2058                         if(child->get_name()=="author")
2059                         {
2060
2061                                 xmlpp::Element::NodeList list = child->get_children();
2062
2063                                 // If we don't have any description, warn
2064                                 if(list.empty())
2065                                         warning(child,_("blank \"author\" entity"));
2066
2067                                 string tmp;
2068                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2069                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2070                                 canvas->set_author(tmp);
2071                         }
2072                         else
2073                         if(child->get_name()=="layer")
2074                         {
2075                                 //if(canvas->is_inline())
2076                                 //      canvas->push_front(parse_layer(child,canvas->parent()));
2077                                 //else
2078                                         canvas->push_front(parse_layer(child,canvas));
2079                         }
2080                         else
2081                                 error_unexpected_element(child,child->get_name());
2082                 }
2083 //              else
2084 //              if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2085 //                      continue;
2086         }
2087
2088         if(canvas->value_node_list().placeholder_count())
2089         {
2090                 String nodes;
2091                 for (ValueNodeList::const_iterator iter = canvas->value_node_list().begin(); iter != canvas->value_node_list().end(); iter++)
2092                         if(PlaceholderValueNode::Handle::cast_dynamic(*iter))
2093                         {
2094                                 if (nodes != "") nodes += ", ";
2095                                 nodes += "'" + (*iter)->get_id() + "'";
2096                         }
2097                 error(element,strprintf(_("Canvas '%s' has undefined %s: %s"),
2098                                                                 canvas->get_id().c_str(),
2099                                                                 canvas->value_node_list().placeholder_count() == 1 ? _("ValueNode") : _("ValueNodes"),
2100                                                                 nodes.c_str()));
2101         }
2102
2103         canvas->set_version(CURRENT_CANVAS_VERSION);
2104         return canvas;
2105 }
2106
2107 Canvas::Handle
2108 CanvasParser::parse_from_file(const String &file)
2109 {
2110         return parse_from_file_as(file,file);
2111 }
2112
2113 Canvas::Handle
2114 CanvasParser::parse_from_file_as(const String &file_,const String &as_)
2115 {
2116         try
2117         {
2118         ChangeLocale change_locale(LC_NUMERIC, "C");
2119                 String file(unix_to_local_path(file_));
2120                 String as(unix_to_local_path(as_));
2121
2122                 if(get_open_canvas_map().count(etl::absolute_path(as)))
2123                         return get_open_canvas_map()[etl::absolute_path(as)];
2124
2125                 filename=as;
2126                 total_warnings_=0;
2127                 xmlpp::DomParser parser(file);
2128                 if(parser)
2129                 {
2130                         Canvas::Handle canvas(parse_canvas(parser.get_document()->get_root_node(),0,false,as));
2131                         get_open_canvas_map()[etl::absolute_path(as)]=canvas;
2132                         canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2133                         canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2134
2135
2136
2137                         const ValueNodeList& value_node_list(canvas->value_node_list());
2138
2139                         again:
2140                         ValueNodeList::const_iterator iter;
2141                         for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2142                         {
2143                                 ValueNode::Handle value_node(*iter);
2144                                 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2145                                 {
2146                                         canvas->remove_value_node(value_node);
2147                                         goto again;
2148                                 }
2149                         }
2150
2151                         return canvas;
2152                 }
2153         }
2154         catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2155         catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2156         catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2157         catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2158         catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2159         catch(const std::exception& ex)
2160         {
2161                 synfig::error("Standard Exception: "+String(ex.what()));
2162                 return Canvas::Handle();
2163         }
2164         catch(const String& str)
2165         {
2166                 cerr<<str<<endl;
2167                 //      synfig::error(str);
2168                 return Canvas::Handle();
2169         }
2170         return Canvas::Handle();
2171 }
2172
2173 Canvas::Handle
2174 CanvasParser::parse_from_string(const String &data)
2175 {
2176         try
2177         {
2178         ChangeLocale change_locale(LC_NUMERIC, "C");
2179                 filename=_("<INTERNAL>");
2180                 total_warnings_=0;
2181                 xmlpp::DomParser parser;
2182                 parser.parse_memory(data);
2183                 xmlpp::Element *root=parser.get_document()->get_root_node();
2184                 if(parser)
2185                 {
2186                         Canvas::Handle canvas(parse_canvas(root));
2187                         canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2188                         canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2189
2190                         const ValueNodeList& value_node_list(canvas->value_node_list());
2191                         again:
2192                         ValueNodeList::const_iterator iter;
2193                         for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2194                         {
2195                                 ValueNode::Handle value_node(*iter);
2196                                 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2197                                 {
2198                                         canvas->remove_value_node(value_node);
2199                                         goto again;
2200                                 }
2201                         }
2202
2203                         return canvas;
2204                 }
2205         }
2206         catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2207         catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2208         catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2209         catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2210         catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2211         catch(const std::exception& ex)
2212         {
2213                 synfig::error("Standard Exception: "+String(ex.what()));
2214                 return Canvas::Handle();
2215         }
2216         catch(const String& str)
2217         {
2218                 cerr<<str<<endl;
2219                 //      synfig::error(str);
2220                 return Canvas::Handle();
2221         }
2222         return Canvas::Handle();
2223 }