fw4spl
Convert.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwRuntime/Convert.hpp"
8 
9 #include "fwRuntime/Bundle.hpp"
10 #include "fwRuntime/ExtensionPoint.hpp"
11 #include "fwRuntime/io/BundleDescriptorReader.hpp"
12 #include "fwRuntime/Runtime.hpp"
13 
14 #include <boost/property_tree/ptree.hpp>
15 #include <boost/property_tree/xml_parser.hpp>
16 
17 #include <libxml/parser.h>
18 
19 #include <set>
20 
21 namespace fwRuntime
22 {
23 
24 //------------------------------------------------------------------------------
25 
26 Convert::Convert()
27 {
28 }
29 
30 //------------------------------------------------------------------------------
31 
33 {
34 }
35 
36 //------------------------------------------------------------------------------
37 
38 void Convert::fromConfigurationElementToXml( std::shared_ptr< ::fwRuntime::ConfigurationElement > _cfgElement,
39  xmlNodePtr _node)
40 {
41  //NAME
42  xmlNodePtr pNode = xmlNewNode( NULL, xmlCharStrdup( _cfgElement->getName().c_str() ) );
43  xmlAddChild(_node, pNode );
44 
45  std::string nodeValue = _cfgElement->getValue();
46  if(!nodeValue.empty())
47  {
48  xmlNodeSetContent(pNode, reinterpret_cast<const xmlChar*>(nodeValue.c_str()));
49  }
50 
51  std::map<std::string, std::string> attr_cfe = _cfgElement->getAttributes();
52 
53  for ( std::map<std::string, std::string>::iterator iter_attr_cfe = attr_cfe.begin();
54  iter_attr_cfe != attr_cfe.end();
55  ++iter_attr_cfe)
56  {
57  //ATTRIBUTES + VALUES
58  xmlSetProp(pNode, xmlCharStrdup((iter_attr_cfe->first).c_str()),
59  xmlCharStrdup((iter_attr_cfe->second).c_str()) );
60  if ((iter_attr_cfe->first) == std::string("class"))
61  {
62  xmlSetProp(pNode, xmlCharStrdup((iter_attr_cfe->first).c_str()),
63  xmlCharStrdup((iter_attr_cfe->second).c_str()) );
64  }
65  }
66 
67  SLM_ASSERT( "ConfigurationElement should not have children("
68  << _cfgElement->size() <<") and a value ("
69  << nodeValue <<") at the same time.",
70  !(!nodeValue.empty() && _cfgElement->size()) );
71 
72  for ( std::vector< ::fwRuntime::ConfigurationElement::sptr >::iterator iter_cfeC = _cfgElement->begin();
73  iter_cfeC != _cfgElement->end();
74  ++iter_cfeC )
75  {
76  fromConfigurationElementToXml( (*iter_cfeC), pNode);
77  }
78 
79 }
80 
81 //------------------------------------------------------------------------------
82 
84 {
85  xmlNodePtr node_root = xmlNewNode( NULL, xmlCharStrdup( BUNDLE_RC_PREFIX ) );
86  std::set< std::shared_ptr< ::fwRuntime::Bundle > > ::iterator iter_bundles;
88 
89  xmlNodePtr activated_Node = xmlNewNode( NULL, xmlCharStrdup( "Activated" ) );
90  xmlAddChild(node_root, activated_Node );
91 
92  xmlNodePtr inactivated_Node = xmlNewNode( NULL, xmlCharStrdup( "Inactivated" ) );
93  xmlAddChild(node_root, inactivated_Node );
94 
95  bool enable_Value = false; // the 'do while' loop stop if enable_Value==false.
96  do
97  {
98  enable_Value = !enable_Value;
99  for (iter_bundles = tmp_runtime->bundlesBegin();
100  iter_bundles != tmp_runtime->bundlesEnd();
101  ++iter_bundles)
102  {
103  //BUNDLE
104 
105  xmlNodePtr bundleNode = xmlNewNode( NULL, xmlCharStrdup( (*iter_bundles)->getIdentifier().c_str() ) );
106  if (enable_Value)
107  {
108  xmlAddChild(activated_Node, bundleNode );
109  }
110  else
111  {
112  xmlAddChild(inactivated_Node, bundleNode );
113  }
114 
115  //EXTENSIONS POINTS
116  xmlNodePtr extensionPoint_activated_list_Node = xmlNewNode( NULL, xmlCharStrdup( "Extensions_Points" ) );
117  xmlAddChild(bundleNode, extensionPoint_activated_list_Node );
118 
119  for ( std::set< std::shared_ptr< ::fwRuntime::ExtensionPoint > >::const_iterator iter_extensionPoints =
120  (*iter_bundles)->extensionPointsBegin();
121  iter_extensionPoints != (*iter_bundles)->extensionPointsEnd();
122  ++iter_extensionPoints)
123  {
124  //EXTENSIONS POINTS
125  std::string str = "Identifier : "+(*iter_extensionPoints)->getIdentifier();
126  //-----DEBUG------
127  xmlNodePtr extensionPointsNode = xmlNewNode( NULL, xmlCharStrdup( str.c_str() ) );
128  //xmlNodePtr extensionPointsNode = xmlNewNode( NULL, xmlCharStrdup(
129  // (str.substr(str.find_last_of("::")+1)).c_str() ) ) ;
130  //-----DEBUG------
131  if (((*iter_extensionPoints)->isEnable()) == enable_Value)
132  {
133  xmlAddChild(extensionPoint_activated_list_Node, extensionPointsNode );
134  }
135 
136  std::vector< ::fwRuntime::ConfigurationElement::sptr > AllConfigElement =
137  ((*iter_extensionPoints)->getAllConfigurationElements());
138  for (std::vector< ::fwRuntime::ConfigurationElement::sptr >::const_iterator iter_config_elem =
139  AllConfigElement.begin();
140  iter_config_elem != AllConfigElement.end();
141  ++iter_config_elem)
142  {
143  //CONFIGURATIONS ELEMENTS
144  Convert::fromConfigurationElementToXml( (*iter_config_elem), extensionPointsNode);
145  }
146  }//end extensionsPoints parsing
147 
148  //Extensions
149  xmlNodePtr extension_activated_list_Node = xmlNewNode( NULL, xmlCharStrdup( "Extensions" ) );
150  xmlAddChild(bundleNode, extension_activated_list_Node );
151 
152  for ( std::set< std::shared_ptr< ::fwRuntime::Extension > >::const_iterator iter_extension =
153  (*iter_bundles)->extensionsBegin();
154  iter_extension != (*iter_bundles)->extensionsEnd();
155  ++iter_extension)
156  {
157  if (((*iter_extension)->isEnable()) == enable_Value)
158  {
159  std::string str = (*iter_extension)->getPoint();
160  xmlNodePtr extensionNode = xmlNewNode( NULL, xmlCharStrdup( (str.c_str() ) ) );
161 
162  // Adds node if not exist
163  xmlNodePtr node;
164  bool found_node = false;
165  for ( node = extension_activated_list_Node->children;
166  node;
167  node = node->next)
168  {
169  if ( (node->type == XML_ELEMENT_NODE) &&
170  !(xmlStrcmp(node->name, xmlCharStrdup( (str.c_str() ) ) ) ) )
171  {
172  extensionNode = node;
173  found_node = true;
174  break;
175  }
176  }
177 
178  if (!found_node)
179  {
180  xmlAddChild(extension_activated_list_Node, extensionNode );
181  }
182  //end adds node
183 
184  for (std::vector< ::fwRuntime::ConfigurationElement::sptr >::iterator iter_cfe_extension =
185  (*iter_extension)->begin();
186  iter_cfe_extension != (*iter_extension)->end();
187  ++iter_cfe_extension )
188  {
189  Convert::fromConfigurationElementToXml( (*iter_cfe_extension), extensionNode);
190  }
191  }
192  }//end Extensions parsing
193 
194  //cleaning : delete empty node (or if contain empty node)
195  // xmlKeepBlanksDefault(0) don't work; only for TEXT NODES
196  // xmlIsBlankNode too, work only for Text-Nodes
197  if (!(extensionPoint_activated_list_Node->children))
198  {
199  xmlUnlinkNode(extensionPoint_activated_list_Node);
200  xmlFreeNode(extensionPoint_activated_list_Node);
201  }
202 
203  if (!(extension_activated_list_Node->children))
204  {
205  xmlUnlinkNode(extension_activated_list_Node);
206  xmlFreeNode(extension_activated_list_Node);
207  }
208 
209  if (!(bundleNode->children))
210  {
211  xmlUnlinkNode(bundleNode);
212  xmlFreeNode(bundleNode);
213  }
214  //end cleaning
215  }//end bundles iterator
216  }
217  while ( enable_Value );
218 
219  return node_root;
220 }//runningBundlesToXml
221 
222 //------------------------------------------------------------------------------
223 
224 xmlNodePtr Convert::toXml( ::fwRuntime::ConfigurationElement::sptr _cfgElement)
225 {
226  xmlNodePtr tmp = xmlNewNode( NULL, xmlCharStrdup( "Configurations_Elements" ) );
227  ::fwRuntime::Convert::fromConfigurationElementToXml( _cfgElement, tmp);
228  return tmp;
229 }
230 
231 //------------------------------------------------------------------------------
232 
233 std::string Convert::toXmlString( ::fwRuntime::ConfigurationElement::sptr _cfgElement)
234 {
235  xmlNodePtr node = toXml( _cfgElement );
236  xmlBufferPtr buffer = xmlBufferCreate();
237  xmlNodeDump( buffer, node->doc, xmlFirstElementChild(node), 0, 1 );
238 
239  std::string result = reinterpret_cast<const char*>(buffer->content);
240 
241  xmlFreeNode( node );
242  xmlBufferFree( buffer );
243  return result;
244 }
245 
246 //------------------------------------------------------------------------------
247 
248 ::boost::property_tree::ptree Convert::toPropertyTree( ::fwRuntime::ConfigurationElement::csptr _cfgElement)
249 {
250  ::boost::property_tree::ptree pt;
251  ::boost::property_tree::ptree ptAttr;
252 
253  std::string propertyName = _cfgElement->getName();
254  std::string propertyValue = _cfgElement->getValue();
255 
256  typedef std::map<std::string, std::string> AttributeMatType;
257 
258  if(!propertyValue.empty())
259  {
260  pt.put(propertyName, propertyValue);
261  }
262 
263  AttributeMatType attr = _cfgElement->getAttributes();
264 
265  for ( AttributeMatType::iterator iter = attr.begin(); iter != attr.end(); ++iter)
266  {
267  ptAttr.put(iter->first, iter->second);
268  }
269 
270  if(!ptAttr.empty())
271  {
272  pt.put_child(propertyName + ".<xmlattr>", ptAttr);
273  }
274 
275  for ( auto iterElement : _cfgElement->getElements() )
276  {
277  const std::string childName = iterElement->getName();
278  ::boost::property_tree::ptree ptChild;
279 
280  ptChild = toPropertyTree(iterElement);
281 
282  boost::optional< ::boost::property_tree::ptree& > child = ptChild.get_child_optional(childName);
283 
284  if(child)
285  {
286  ptChild = *child;
287  }
288 
289  pt.add_child( propertyName + "." + childName, ptChild );
290  }
291  return pt;
292 }
293 
294 //------------------------------------------------------------------------------
295 
296 ::fwRuntime::ConfigurationElement::sptr Convert::fromPropertyTree( ::boost::property_tree::ptree pt )
297 {
298  std::stringstream sstr;
299  ::boost::property_tree::write_xml(sstr, pt);
300 
301  std::string xml = sstr.str();
302 
303  xmlDocPtr doc = xmlParseMemory(xml.c_str(), static_cast<int>(xml.size()));
304 
305  if (doc == NULL)
306  {
307  return ::fwRuntime::ConfigurationElement::sptr();
308  }
309 
310  xmlNodePtr root = xmlDocGetRootElement(doc);
311 
312  ::fwRuntime::ConfigurationElement::sptr ce;
314 
315  xmlFreeDoc(doc);
316 
317  return ce;
318 }
319 
320 //------------------------------------------------------------------------------
321 
322 }//namespace fwRuntime
static FWRUNTIME_API Runtime * getDefault()
Retrieves the default runtime instance.
Definition: Runtime.cpp:286
Defines the runtime class.
Definition: Runtime.hpp:37
#define SPTR(_cls_)
FWRUNTIME_API BundleIterator bundlesBegin()
Retrieves the iterator on the begining of the bundle collection.
Definition: Runtime.cpp:133
static FWRUNTIME_API std::shared_ptr< ConfigurationElement > processConfigurationElement(xmlNodePtr node, const std::shared_ptr< Bundle > bundle)
Processes a configuration element XML node.
FWRUNTIME_API BundleIterator bundlesEnd()
Retrieves the iterator on the end of the bundle collection.
Definition: Runtime.cpp:140
virtual FWRUNTIME_API ~Convert()
Destructor : does nothing.
Definition: Convert.cpp:32
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
static FWRUNTIME_API xmlNodePtr toXml(std::shared_ptr< ::fwRuntime::ConfigurationElement > _cfgElement)
Build an xmlNodePtr from a ConfigurationElement.
Definition: Convert.cpp:224
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
FWRUNTIME_API static std::string toXmlString(::fwRuntime::ConfigurationElement::sptr _cfgElement)
Build an std::string from a ConfigurationElement.
Definition: Convert.cpp:233
Defines the bundle class.
Definition: Bundle.hpp:45
static FWRUNTIME_API xmlNodePtr runningBundlesToXml()
Build an xmlNodePtr with all running Bundles.
Definition: Convert.cpp:83