]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/WorkspaceIO.cxx
029ade420b79aa60ce57a8ca49fc491d16a5783e
[cpPlugins.git] / lib / cpPlugins / Interface / WorkspaceIO.cxx
1 #include <cpPlugins/Interface/Workspace.h>
2 #include <tinyxml2/tinyxml2.h>
3
4 // -------------------------------------------------------------------------
5 void cpPlugins::Interface::Workspace::
6 Load( const std::string& fname )
7 {
8   this->Clear( );
9   TInterface::Pointer interface = TInterface::New( );
10
11   // Read from disk
12   tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument( );
13   doc->LoadFile( fname.c_str( ) );
14   tinyxml2::XMLElement* root = doc->RootElement( );
15   if( root == NULL )
16   {
17     delete doc;
18     throw std::runtime_error( "cpPlugins::Workspace: No valid file" );
19
20   } // fi
21   if( std::string( root->Value( ) ) != "cpPlugins_Workspace" )
22   {
23     delete doc;
24     throw std::runtime_error( "cpPlugins::Workspace: Not a valid workspace" );
25
26   } // fi
27
28   // Load plugins
29   auto loaded_plugins = interface->GetPlugins( );
30   tinyxml2::XMLElement* plugins = root->FirstChildElement( "Plugins" );
31   std::string plugins_errors = "";
32   while( plugins != NULL )
33   {
34     tinyxml2::XMLElement* plugin = plugins->FirstChildElement( "Plugin" );
35     while( plugin != NULL )
36     {
37       std::string name = plugin->Attribute( "Name" );
38       if( loaded_plugins.find( name ) == loaded_plugins.end( ) )
39       {
40         try
41         {
42           interface->LoadPlugin( name );
43         }
44         catch( std::exception& err )
45         {
46           plugins_errors += err.what( ) + std::string( "\n" );
47
48         } // yrt
49
50       } // fi
51       plugin = plugin->NextSiblingElement( "Plugin" );
52
53     } // elihw
54     plugins = plugins->NextSiblingElement( "Plugins" );
55
56   } // elihw
57   if( plugins_errors != "" )
58   {
59     delete doc;
60     throw std::runtime_error(
61       std::string( "cpPlugins::Workspace: " ) + plugins_errors
62       );
63
64   } // fi
65
66   // Read filters
67   std::stringstream err;
68   tinyxml2::XMLElement* filter = root->FirstChildElement( "Filter" );
69   while( filter != NULL )
70   {
71     const char* category_value = filter->Attribute( "Category" );
72     const char* class_value = filter->Attribute( "Class" );
73     const char* name_value = filter->Attribute( "Name" );
74     float viewX = float( 0 ), viewY = float( 0 );
75     filter->QueryFloatAttribute( "ViewX", &viewX );
76     filter->QueryFloatAttribute( "ViewY", &viewY );
77     int explicit_execution = 0;
78     filter->QueryIntAttribute(
79       "ExplicitExecution", &explicit_execution
80       );
81     if( class_value != NULL && name_value != NULL )
82     {
83       auto new_filter =
84         this->CreateFilter( category_value, class_value, name_value );
85       if( new_filter != NULL )
86       {
87         new_filter->SetViewCoords( viewX, viewY );
88         new_filter->SetExplicitExecution( explicit_execution == 1 );
89         new_filter->GetParameters( )->FromXML( filter );
90       }
91       else
92         err
93           << "No valid class \"" << class_value << "\" with name \""
94           << name_value << "\"" << std::endl;
95     }
96     else
97       err << "Incomplete data." << std::endl;
98     filter = filter->NextSiblingElement( "Filter" );
99
100   } // elihw
101
102   // Read connections
103   tinyxml2::XMLElement* connection = root->FirstChildElement( "Connection" );
104   while( connection != NULL )
105   {
106     tinyxml2::XMLElement* orig = connection->FirstChildElement( "Origin" );
107     tinyxml2::XMLElement* dest = connection->FirstChildElement( "Destination" );
108     if( orig != NULL && dest != NULL )
109     {
110       const char* orig_filter = orig->Attribute( "Filter" );
111       const char* dest_filter = dest->Attribute( "Filter" );
112       const char* orig_name = orig->Attribute( "Name" );
113       const char* dest_name = dest->Attribute( "Name" );
114       if(
115         orig_filter != NULL && dest_filter != NULL &&
116         orig_name != NULL && dest_name != NULL
117         )
118       {
119         try
120         {
121           this->Connect( orig_filter, dest_filter, orig_name, dest_name );
122         }
123         catch( std::exception& exc )
124         {
125           err << exc.what( ) << std::endl;
126
127         } // yrt
128
129       } // fi
130
131     } // fi
132     connection = connection->NextSiblingElement( "Connection" );
133
134   } // elihw
135
136   // Read exposed inputs
137   tinyxml2::XMLElement* port = root->FirstChildElement( "ExposedInput" );
138   while( port != NULL )
139   {
140     this->ExposeInput(
141       port->Attribute( "Name" ),
142       port->Attribute( "Filter" ),
143       port->Attribute( "Input" )
144       );
145     port = port->NextSiblingElement( "ExposedInput" );
146
147   } // elihw
148
149   // Read exposed outputs
150   port = root->FirstChildElement( "ExposedOutput" );
151   while( port != NULL )
152   {
153     this->ExposeOutput(
154       port->Attribute( "Name" ),
155       port->Attribute( "Filter" ),
156       port->Attribute( "Output" )
157       );
158     port = port->NextSiblingElement( "ExposedOutput" );
159
160   } // elihw
161
162   // Throw errors
163   std::string err_str = err.str( );
164   if( err_str != "" )
165   {
166     delete doc;
167     throw std::runtime_error(
168       std::string( "cpPlugins::Workspace " ) + err_str
169       );
170
171   } // fi
172
173   // Finish
174   delete doc;
175 }
176
177 // -------------------------------------------------------------------------
178 void cpPlugins::Interface::Workspace::
179 Save( const std::string& fname ) const
180 {
181   tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument( );
182   tinyxml2::XMLElement* root = doc->NewElement( "cpPlugins_Workspace" );
183   std::set< std::string > used_plugins;
184
185   // Save filter data
186   for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
187   {
188     auto filter = i->second;
189     used_plugins.insert( filter->GetPluginName( ) );
190     tinyxml2::XMLElement* e = doc->NewElement( "Filter" );
191     e->SetAttribute( "Category", filter->GetClassCategory( ) );
192     e->SetAttribute( "Class", filter->GetClassName( ) );
193     e->SetAttribute( "Name", filter->GetName( ) );
194     e->SetAttribute( "ViewX", filter->GetViewX( ) );
195     e->SetAttribute( "ViewY", filter->GetViewY( ) );
196     e->SetAttribute(
197       "ExplicitExecution", ( filter->GetExplicitExecution( ) )? 1: 0
198       );
199
200     auto params = filter->GetParameters( );
201     params->ToXML( doc, e );
202     root->LinkEndChild( e );
203
204   } // rof
205
206   // Save used plugins
207   tinyxml2::XMLElement* plugins = doc->NewElement( "Plugins" );
208   for( auto pIt = used_plugins.begin( ); pIt != used_plugins.end( ); ++pIt )
209   {
210     tinyxml2::XMLElement* e = doc->NewElement( "Plugin" );
211     e->SetAttribute( "Name", pIt->c_str( ) );
212     plugins->LinkEndChild( e );
213
214   } // rof
215   root->LinkEndChild( plugins );
216
217   // Save connections
218   for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
219   {
220     auto orig = i->second;
221     auto outputs = orig->GetOutputsNames( );
222     if( outputs.size( ) == 0 )
223       continue;
224     for( auto j = this->m_Filters.begin( ); j != this->m_Filters.end( ); ++j )
225     {
226       if( i == j )
227         continue;
228       auto dest = j->second;
229       auto inputs = dest->GetInputsNames( );
230       if( inputs.size( ) == 0 )
231         continue;
232
233       for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
234       {
235         auto od = orig->GetOutput( *oIt );
236         for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
237         {
238           auto id = dest->GetInput( *iIt );
239           if( od != NULL && od == id )
240           {
241             tinyxml2::XMLElement* e_conn = doc->NewElement( "Connection" );
242             tinyxml2::XMLElement* e_orig = doc->NewElement( "Origin" );
243             tinyxml2::XMLElement* e_dest = doc->NewElement( "Destination" );
244             e_orig->SetAttribute( "Filter", orig->GetName( ) );
245             e_orig->SetAttribute( "Name", oIt->c_str( ) );
246             e_dest->SetAttribute( "Filter", dest->GetName( ) );
247             e_dest->SetAttribute( "Name", iIt->c_str( ) );
248             e_conn->LinkEndChild( e_orig );
249             e_conn->LinkEndChild( e_dest );
250             root->LinkEndChild( e_conn );
251
252           } // fi
253
254         } // rof
255
256       } // rof
257       
258     } // rof
259
260   } // rof
261
262
263   // Save exposed ports
264   auto eipIt = this->m_ExposedInputs.begin( );
265   for( ; eipIt != this->m_ExposedInputs.end( ); ++eipIt )
266   {
267     tinyxml2::XMLElement* port = doc->NewElement( "ExposedInput" );
268     port->SetAttribute( "Name", eipIt->first.c_str( ) );
269     port->SetAttribute( "Filter", eipIt->second.first.c_str( ) );
270     port->SetAttribute( "Input", eipIt->second.second.c_str( ) );
271     root->LinkEndChild( port );
272
273   } // rof
274
275   auto eopIt = this->m_ExposedOutputs.begin( );
276   for( ; eopIt != this->m_ExposedOutputs.end( ); ++eopIt )
277   {
278     tinyxml2::XMLElement* port = doc->NewElement( "ExposedOutput" );
279     port->SetAttribute( "Name", eopIt->first.c_str( ) );
280     port->SetAttribute( "Filter", eopIt->second.first.c_str( ) );
281     port->SetAttribute( "Output", eopIt->second.second.c_str( ) );
282     root->LinkEndChild( port );
283
284   } // rof
285
286   // Physical write and return
287   doc->LinkEndChild( root );
288   auto error = doc->SaveFile( fname.c_str( ) );
289   delete doc;
290   if( error != tinyxml2::XML_SUCCESS )
291   {
292     std::string m;
293     switch( error )
294     {
295     case tinyxml2::XML_NO_ATTRIBUTE:
296       m = "No attribute."; break;
297     case tinyxml2::XML_WRONG_ATTRIBUTE_TYPE:
298       m = "Wrong attribute."; break;
299     case tinyxml2::XML_ERROR_FILE_NOT_FOUND:
300       m = "File not found."; break;
301     case tinyxml2::XML_ERROR_FILE_COULD_NOT_BE_OPENED:
302       m = "File not opened."; break;
303     case tinyxml2::XML_ERROR_FILE_READ_ERROR:
304       m = "File not read."; break;
305     case tinyxml2::XML_ERROR_ELEMENT_MISMATCH:
306       m = "Element mismatch."; break;
307     case tinyxml2::XML_ERROR_PARSING_ELEMENT:
308       m = "Parsing element."; break;
309     case tinyxml2::XML_ERROR_PARSING_ATTRIBUTE:
310       m = "Parsing attribute."; break;
311     case tinyxml2::XML_ERROR_IDENTIFYING_TAG:
312       m = "Tag Id."; break;
313     case tinyxml2::XML_ERROR_PARSING_TEXT:
314       m = "Parsing text."; break;
315     case tinyxml2::XML_ERROR_PARSING_CDATA:
316       m = "Parsing cdata."; break;
317     case tinyxml2::XML_ERROR_PARSING_COMMENT:
318       m = "Parsing comment."; break;
319     case tinyxml2::XML_ERROR_PARSING_DECLARATION:
320       m = "Parsing declaration."; break;
321     case tinyxml2::XML_ERROR_PARSING_UNKNOWN:
322       m = "Parsing unknown."; break;
323     case tinyxml2::XML_ERROR_EMPTY_DOCUMENT:
324       m = "Empty document."; break;
325     case tinyxml2::XML_ERROR_MISMATCHED_ELEMENT:
326       m = "Mismatched element."; break;
327     case tinyxml2::XML_ERROR_PARSING:
328       m = "Parsing."; break;
329     case tinyxml2::XML_CAN_NOT_CONVERT_TEXT:
330       m = "Cannot convert."; break;
331     case tinyxml2::XML_NO_TEXT_NODE:
332       m = "No text."; break;
333     default:
334       m = "Unknown error."; break;
335     } // hctiws
336     throw std::runtime_error(
337       std::string( "cpPlugins::Workspace: Error while saving \"" ) +
338       fname + std::string( "\": " ) + m
339       );
340
341   } // fi
342 }
343
344 // eof - $RCSfile$