1 #include <cpPlugins/Interface/Workspace.h>
2 #include <tinyxml2/tinyxml2.h>
4 // -------------------------------------------------------------------------
5 void cpPlugins::Interface::Workspace::
6 Load( const std::string& fname )
9 TInterface::Pointer interface = TInterface::New( );
12 tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument( );
13 doc->LoadFile( fname.c_str( ) );
14 tinyxml2::XMLElement* root = doc->RootElement( );
18 throw std::runtime_error( "cpPlugins::Workspace: No valid file" );
21 if( std::string( root->Value( ) ) != "cpPlugins_Workspace" )
24 throw std::runtime_error( "cpPlugins::Workspace: Not a valid workspace" );
29 auto loaded_plugins = interface->GetPlugins( );
30 tinyxml2::XMLElement* plugins = root->FirstChildElement( "Plugins" );
31 std::string plugins_errors = "";
32 while( plugins != NULL )
34 tinyxml2::XMLElement* plugin = plugins->FirstChildElement( "Plugin" );
35 while( plugin != NULL )
37 std::string name = plugin->Attribute( "Name" );
38 if( loaded_plugins.find( name ) == loaded_plugins.end( ) )
42 interface->LoadPlugin( name );
44 catch( std::exception& err )
46 plugins_errors += err.what( ) + std::string( "\n" );
51 plugin = plugin->NextSiblingElement( "Plugin" );
54 plugins = plugins->NextSiblingElement( "Plugins" );
57 if( plugins_errors != "" )
60 throw std::runtime_error(
61 std::string( "cpPlugins::Workspace: " ) + plugins_errors
67 std::stringstream err;
68 tinyxml2::XMLElement* filter = root->FirstChildElement( "Filter" );
69 while( filter != NULL )
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
81 if( class_value != NULL && name_value != NULL )
84 this->CreateFilter( category_value, class_value, name_value );
85 if( new_filter != NULL )
87 new_filter->SetViewCoords( viewX, viewY );
88 new_filter->SetExplicitExecution( explicit_execution == 1 );
89 new_filter->GetParameters( )->FromXML( filter );
93 << "No valid class \"" << class_value << "\" with name \""
94 << name_value << "\"" << std::endl;
97 err << "Incomplete data." << std::endl;
98 filter = filter->NextSiblingElement( "Filter" );
103 tinyxml2::XMLElement* connection = root->FirstChildElement( "Connection" );
104 while( connection != NULL )
106 tinyxml2::XMLElement* orig = connection->FirstChildElement( "Origin" );
107 tinyxml2::XMLElement* dest = connection->FirstChildElement( "Destination" );
108 if( orig != NULL && dest != NULL )
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" );
115 orig_filter != NULL && dest_filter != NULL &&
116 orig_name != NULL && dest_name != NULL
121 this->Connect( orig_filter, dest_filter, orig_name, dest_name );
123 catch( std::exception& exc )
125 err << exc.what( ) << std::endl;
132 connection = connection->NextSiblingElement( "Connection" );
136 // Read exposed inputs
137 tinyxml2::XMLElement* port = root->FirstChildElement( "ExposedInput" );
138 while( port != NULL )
141 port->Attribute( "Name" ),
142 port->Attribute( "Filter" ),
143 port->Attribute( "Input" )
145 port = port->NextSiblingElement( "ExposedInput" );
149 // Read exposed outputs
150 port = root->FirstChildElement( "ExposedOutput" );
151 while( port != NULL )
154 port->Attribute( "Name" ),
155 port->Attribute( "Filter" ),
156 port->Attribute( "Output" )
158 port = port->NextSiblingElement( "ExposedOutput" );
163 std::string err_str = err.str( );
167 throw std::runtime_error(
168 std::string( "cpPlugins::Workspace " ) + err_str
177 // -------------------------------------------------------------------------
178 void cpPlugins::Interface::Workspace::
179 Save( const std::string& fname ) const
181 tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument( );
182 tinyxml2::XMLElement* root = doc->NewElement( "cpPlugins_Workspace" );
183 std::set< std::string > used_plugins;
186 for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
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( ) );
197 "ExplicitExecution", ( filter->GetExplicitExecution( ) )? 1: 0
200 auto params = filter->GetParameters( );
201 params->ToXML( doc, e );
202 root->LinkEndChild( e );
207 tinyxml2::XMLElement* plugins = doc->NewElement( "Plugins" );
208 for( auto pIt = used_plugins.begin( ); pIt != used_plugins.end( ); ++pIt )
210 tinyxml2::XMLElement* e = doc->NewElement( "Plugin" );
211 e->SetAttribute( "Name", pIt->c_str( ) );
212 plugins->LinkEndChild( e );
215 root->LinkEndChild( plugins );
218 for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
220 auto orig = i->second;
221 auto outputs = orig->GetOutputsNames( );
222 if( outputs.size( ) == 0 )
224 for( auto j = this->m_Filters.begin( ); j != this->m_Filters.end( ); ++j )
228 auto dest = j->second;
229 auto inputs = dest->GetInputsNames( );
230 if( inputs.size( ) == 0 )
233 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
235 auto od = orig->GetOutput( *oIt );
236 for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt )
238 unsigned int nInputs = dest->GetInputSize( *iIt );
239 for( unsigned int k = 0; k < nInputs; ++k )
241 auto id = dest->GetInput( *iIt, k );
242 if( od != NULL && od == id )
244 tinyxml2::XMLElement* e_conn = doc->NewElement( "Connection" );
245 tinyxml2::XMLElement* e_orig = doc->NewElement( "Origin" );
246 tinyxml2::XMLElement* e_dest = doc->NewElement( "Destination" );
247 e_orig->SetAttribute( "Filter", orig->GetName( ) );
248 e_orig->SetAttribute( "Name", oIt->c_str( ) );
249 e_dest->SetAttribute( "Filter", dest->GetName( ) );
250 e_dest->SetAttribute( "Name", iIt->c_str( ) );
251 e_conn->LinkEndChild( e_orig );
252 e_conn->LinkEndChild( e_dest );
253 root->LinkEndChild( e_conn );
268 // Save exposed ports
269 auto eipIt = this->m_ExposedInputs.begin( );
270 for( ; eipIt != this->m_ExposedInputs.end( ); ++eipIt )
272 tinyxml2::XMLElement* port = doc->NewElement( "ExposedInput" );
273 port->SetAttribute( "Name", eipIt->first.c_str( ) );
274 port->SetAttribute( "Filter", eipIt->second.first.c_str( ) );
275 port->SetAttribute( "Input", eipIt->second.second.c_str( ) );
276 root->LinkEndChild( port );
280 auto eopIt = this->m_ExposedOutputs.begin( );
281 for( ; eopIt != this->m_ExposedOutputs.end( ); ++eopIt )
283 tinyxml2::XMLElement* port = doc->NewElement( "ExposedOutput" );
284 port->SetAttribute( "Name", eopIt->first.c_str( ) );
285 port->SetAttribute( "Filter", eopIt->second.first.c_str( ) );
286 port->SetAttribute( "Output", eopIt->second.second.c_str( ) );
287 root->LinkEndChild( port );
291 // Physical write and return
292 doc->LinkEndChild( root );
293 auto error = doc->SaveFile( fname.c_str( ) );
295 if( error != tinyxml2::XML_SUCCESS )
300 case tinyxml2::XML_NO_ATTRIBUTE:
301 m = "No attribute."; break;
302 case tinyxml2::XML_WRONG_ATTRIBUTE_TYPE:
303 m = "Wrong attribute."; break;
304 case tinyxml2::XML_ERROR_FILE_NOT_FOUND:
305 m = "File not found."; break;
306 case tinyxml2::XML_ERROR_FILE_COULD_NOT_BE_OPENED:
307 m = "File not opened."; break;
308 case tinyxml2::XML_ERROR_FILE_READ_ERROR:
309 m = "File not read."; break;
310 case tinyxml2::XML_ERROR_ELEMENT_MISMATCH:
311 m = "Element mismatch."; break;
312 case tinyxml2::XML_ERROR_PARSING_ELEMENT:
313 m = "Parsing element."; break;
314 case tinyxml2::XML_ERROR_PARSING_ATTRIBUTE:
315 m = "Parsing attribute."; break;
316 case tinyxml2::XML_ERROR_IDENTIFYING_TAG:
317 m = "Tag Id."; break;
318 case tinyxml2::XML_ERROR_PARSING_TEXT:
319 m = "Parsing text."; break;
320 case tinyxml2::XML_ERROR_PARSING_CDATA:
321 m = "Parsing cdata."; break;
322 case tinyxml2::XML_ERROR_PARSING_COMMENT:
323 m = "Parsing comment."; break;
324 case tinyxml2::XML_ERROR_PARSING_DECLARATION:
325 m = "Parsing declaration."; break;
326 case tinyxml2::XML_ERROR_PARSING_UNKNOWN:
327 m = "Parsing unknown."; break;
328 case tinyxml2::XML_ERROR_EMPTY_DOCUMENT:
329 m = "Empty document."; break;
330 case tinyxml2::XML_ERROR_MISMATCHED_ELEMENT:
331 m = "Mismatched element."; break;
332 case tinyxml2::XML_ERROR_PARSING:
333 m = "Parsing."; break;
334 case tinyxml2::XML_CAN_NOT_CONVERT_TEXT:
335 m = "Cannot convert."; break;
336 case tinyxml2::XML_NO_TEXT_NODE:
337 m = "No text."; break;
339 m = "Unknown error."; break;
341 throw std::runtime_error(
342 std::string( "cpPlugins::Workspace: Error while saving \"" ) +
343 fname + std::string( "\": " ) + m