]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/WorkspaceIO.cxx
XML IO added. Workspace singleton added to simplify pipeline definition and execution.
[cpPlugins.git] / lib / cpPlugins / Interface / WorkspaceIO.cxx
1 #include <cpPlugins/Interface/Workspace.h>
2 #include <third_party/tinyxml/tinyxml.h>
3
4 // -------------------------------------------------------------------------
5 std::string cpPlugins::Interface::Workspace::
6 LoadWorkspace( const std::string& fname )
7 {
8   TiXmlDocument* doc = new TiXmlDocument( fname.c_str( ) );
9   doc->LoadFile( );
10   TiXmlElement* root = doc->RootElement( );
11   if( std::string( root->Value( ) ) != "cpPlugins_Workspace" )
12     return( "cpPlugins::Interface::Workspace: No valid workspace" );
13   std::stringstream err;
14
15   // Read plugins files
16   TiXmlElement* plugins = root->FirstChildElement( "plugins" );
17   while( plugins != NULL )
18   {
19     const char* value = plugins->Attribute( "filename" );
20     if( value != NULL )
21     {
22       if( !( this->LoadPlugins( value ) ) )
23         err << "no valid plugins file \"" << value << "\"" << std::endl;
24
25     } // fi
26     plugins = plugins->NextSiblingElement( "plugins" );
27
28   } // elihw
29
30   // Read plugins paths
31   plugins = root->FirstChildElement( "plugins_dir" );
32   while( plugins != NULL )
33   {
34     const char* value = plugins->Attribute( "path" );
35     if( value != NULL )
36     {
37       int recursive;
38       if( plugins->QueryIntAttribute( "recursive", &recursive ) != TIXML_SUCCESS )
39         recursive = 0;
40       if( !( this->LoadPluginsPath( value, recursive == 1 ) ) )
41         err << "No valid plugins path \"" << value << "\"" << std::endl;
42
43     } // fi
44     plugins = plugins->NextSiblingElement( "plugins_dir" );
45
46   } // elihw
47
48   // Read filters
49   TiXmlElement* filter = root->FirstChildElement( "filter" );
50   while( filter != NULL )
51   {
52     const char* class_value = filter->Attribute( "class" );
53     const char* name_value = filter->Attribute( "name" );
54     if( class_value != NULL && name_value != NULL )
55     {
56       if( this->CreateFilter( class_value, name_value ) )
57       {
58         // Read parameters
59         TParameters* parameters = this->GetParameters( name_value );
60         TiXmlElement* param = filter->FirstChildElement( "parameter" );
61         while( param != NULL )
62         {
63           const char* param_name = param->Attribute( "name" );
64           const char* param_type = param->Attribute( "type" );
65           if( param_name != NULL && param_type != NULL )
66           {
67             std::string param_type_str( param_type );
68             const char* value = param->Attribute( "value" );
69             if( value != NULL )
70             {
71               std::istringstream value_str( value );
72               if( param_type_str == "String" )
73                 parameters->SetString( param_name, value );
74               else if( param_type_str == "Bool" )
75                 parameters->SetBool( param_name, value[ 0 ] != '0' );
76               else if( param_type_str == "Int" )
77               {
78                 TParameters::TInt v;
79                 value_str >> v;
80                 parameters->SetInt( param_name, v );
81               }
82               else if( param_type_str == "Uint" )
83               {
84                 TParameters::TUint v;
85                 value_str >> v;
86                 parameters->SetUint( param_name, v );
87               }
88               else if( param_type_str == "Real" )
89               {
90                 TParameters::TReal v;
91                 value_str >> v;
92                 parameters->SetReal( param_name, v );
93               }
94               /* TODO
95                  else if( param_type_str == "Index" )
96                  else if( param_type_str == "Point" )
97                  else if( param_type_str == "Vector" )
98               */
99             }
100             else
101             {
102               if( param_type_str == "StringList" )
103               {
104                 TiXmlElement* item = param->FirstChildElement( "item" );
105                 while( item != NULL )
106                 {
107                   value = item->Attribute( "value" );
108                   if( value != NULL )
109                     parameters->AddToStringList( param_name, value );
110                   item = item->NextSiblingElement( "item" );
111
112                 } // elihw
113               }
114               else if( param_type_str == "BoolList" )
115               {
116                 TiXmlElement* item = param->FirstChildElement( "item" );
117                 while( item != NULL )
118                 {
119                   value = item->Attribute( "value" );
120                   if( value != NULL )
121                     parameters->AddToBoolList( param_name, value[ 0 ] != '0' );
122                   item = item->NextSiblingElement( "item" );
123
124                 } // elihw
125               }
126               else if( param_type_str == "IntList" )
127               {
128                 TiXmlElement* item = param->FirstChildElement( "item" );
129                 while( item != NULL )
130                 {
131                   value = item->Attribute( "value" );
132                   if( value != NULL )
133                   {
134                     std::istringstream value_str( value );
135                     TParameters::TInt v;
136                     value_str >> v;
137                     parameters->AddToIntList( param_name, v );
138
139                   } // fi
140                   item = item->NextSiblingElement( "item" );
141
142                 } // elihw
143               }
144               else if( param_type_str == "UintList" )
145               {
146                 TiXmlElement* item = param->FirstChildElement( "item" );
147                 while( item != NULL )
148                 {
149                   value = item->Attribute( "value" );
150                   if( value != NULL )
151                   {
152                     std::istringstream value_str( value );
153                     TParameters::TUint v;
154                     value_str >> v;
155                     parameters->AddToUintList( param_name, v );
156
157                   } // fi
158                   item = item->NextSiblingElement( "item" );
159
160                 } // elihw
161               }
162               else if( param_type_str == "RealList" )
163               {
164                 TiXmlElement* item = param->FirstChildElement( "item" );
165                 while( item != NULL )
166                 {
167                   value = item->Attribute( "value" );
168                   if( value != NULL )
169                   {
170                     std::istringstream value_str( value );
171                     TParameters::TReal v;
172                     value_str >> v;
173                     parameters->AddToRealList( param_name, v );
174
175                   } // fi
176                   item = item->NextSiblingElement( "item" );
177
178                 } // elihw
179               }
180               /* TODO
181                  else if( param_type_str == "IndexList" )
182                  else if( param_type_str == "PointList" )
183                  else if( param_type_str == "VectorList" )
184                  else if( param_type_str == "Choices" );
185               */
186             } // fi
187
188           } // fi
189           param = param->NextSiblingElement( "parameter" );
190
191         } // elihw
192       }
193       else
194         err
195           << "No valid class \"" << class_value << "\" with name \""
196           << name_value << "\"" << std::endl;
197     }
198     else
199       err << "Incomplete data." << std::endl;
200     filter = filter->NextSiblingElement( "filter" );
201
202   } // elihw
203
204   // Read filters
205   TiXmlElement* connection = root->FirstChildElement( "connection" );
206   while( connection != NULL )
207   {
208     TiXmlElement* orig = connection->FirstChildElement( "origin" );
209     TiXmlElement* dest = connection->FirstChildElement( "destination" );
210     if( orig != NULL && dest != NULL )
211     {
212       const char* orig_filter = orig->Attribute( "filter" );
213       const char* dest_filter = dest->Attribute( "filter" );
214       const char* orig_name = orig->Attribute( "name" );
215       const char* dest_name = dest->Attribute( "name" );
216       if(
217         orig_filter != NULL && dest_filter != NULL &&
218         orig_name != NULL && dest_name != NULL
219         )
220         this->Connect( orig_filter, dest_filter, orig_name, dest_name );
221
222     } // fi
223     connection = connection->NextSiblingElement( "connection" );
224
225   } // elihw
226
227   // Finish and return
228   delete doc;
229   return( err.str( ) );
230 }
231
232 // -------------------------------------------------------------------------
233 std::string cpPlugins::Interface::Workspace::
234 SaveWorkspace( const std::string& fname ) const
235 {
236   return( "" );
237 }
238
239 // eof - $RCSfile$