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