7 #include "fwRuntime/Convert.hpp" 9 #include "fwRuntime/Bundle.hpp" 10 #include "fwRuntime/ExtensionPoint.hpp" 11 #include "fwRuntime/io/BundleDescriptorReader.hpp" 12 #include "fwRuntime/Runtime.hpp" 14 #include <boost/property_tree/ptree.hpp> 15 #include <boost/property_tree/xml_parser.hpp> 17 #include <libxml/parser.h> 38 void Convert::fromConfigurationElementToXml( std::shared_ptr< ::fwRuntime::ConfigurationElement > _cfgElement,
42 xmlNodePtr pNode = xmlNewNode( NULL, xmlCharStrdup( _cfgElement->getName().c_str() ) );
43 xmlAddChild(_node, pNode );
45 std::string nodeValue = _cfgElement->getValue();
46 if(!nodeValue.empty())
48 xmlNodeSetContent(pNode, reinterpret_cast<const xmlChar*>(nodeValue.c_str()));
51 std::map<std::string, std::string> attr_cfe = _cfgElement->getAttributes();
53 for ( std::map<std::string, std::string>::iterator iter_attr_cfe = attr_cfe.begin();
54 iter_attr_cfe != attr_cfe.end();
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"))
62 xmlSetProp(pNode, xmlCharStrdup((iter_attr_cfe->first).c_str()),
63 xmlCharStrdup((iter_attr_cfe->second).c_str()) );
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()) );
72 for ( std::vector< ::fwRuntime::ConfigurationElement::sptr >::iterator iter_cfeC = _cfgElement->begin();
73 iter_cfeC != _cfgElement->end();
76 fromConfigurationElementToXml( (*iter_cfeC), pNode);
85 xmlNodePtr node_root = xmlNewNode( NULL, xmlCharStrdup( BUNDLE_RC_PREFIX ) );
86 std::set< std::shared_ptr< ::fwRuntime::Bundle > > ::iterator iter_bundles;
89 xmlNodePtr activated_Node = xmlNewNode( NULL, xmlCharStrdup(
"Activated" ) );
90 xmlAddChild(node_root, activated_Node );
92 xmlNodePtr inactivated_Node = xmlNewNode( NULL, xmlCharStrdup(
"Inactivated" ) );
93 xmlAddChild(node_root, inactivated_Node );
95 bool enable_Value =
false;
98 enable_Value = !enable_Value;
105 xmlNodePtr bundleNode = xmlNewNode( NULL, xmlCharStrdup( (*iter_bundles)->getIdentifier().c_str() ) );
108 xmlAddChild(activated_Node, bundleNode );
112 xmlAddChild(inactivated_Node, bundleNode );
116 xmlNodePtr extensionPoint_activated_list_Node = xmlNewNode( NULL, xmlCharStrdup(
"Extensions_Points" ) );
117 xmlAddChild(bundleNode, extensionPoint_activated_list_Node );
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)
125 std::string str =
"Identifier : "+(*iter_extensionPoints)->getIdentifier();
127 xmlNodePtr extensionPointsNode = xmlNewNode( NULL, xmlCharStrdup( str.c_str() ) );
131 if (((*iter_extensionPoints)->isEnable()) == enable_Value)
133 xmlAddChild(extensionPoint_activated_list_Node, extensionPointsNode );
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();
144 Convert::fromConfigurationElementToXml( (*iter_config_elem), extensionPointsNode);
149 xmlNodePtr extension_activated_list_Node = xmlNewNode( NULL, xmlCharStrdup(
"Extensions" ) );
150 xmlAddChild(bundleNode, extension_activated_list_Node );
152 for ( std::set< std::shared_ptr< ::fwRuntime::Extension > >::const_iterator iter_extension =
153 (*iter_bundles)->extensionsBegin();
154 iter_extension != (*iter_bundles)->extensionsEnd();
157 if (((*iter_extension)->isEnable()) == enable_Value)
159 std::string str = (*iter_extension)->getPoint();
160 xmlNodePtr extensionNode = xmlNewNode( NULL, xmlCharStrdup( (str.c_str() ) ) );
164 bool found_node =
false;
165 for ( node = extension_activated_list_Node->children;
169 if ( (node->type == XML_ELEMENT_NODE) &&
170 !(xmlStrcmp(node->name, xmlCharStrdup( (str.c_str() ) ) ) ) )
172 extensionNode = node;
180 xmlAddChild(extension_activated_list_Node, extensionNode );
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 )
189 Convert::fromConfigurationElementToXml( (*iter_cfe_extension), extensionNode);
197 if (!(extensionPoint_activated_list_Node->children))
199 xmlUnlinkNode(extensionPoint_activated_list_Node);
200 xmlFreeNode(extensionPoint_activated_list_Node);
203 if (!(extension_activated_list_Node->children))
205 xmlUnlinkNode(extension_activated_list_Node);
206 xmlFreeNode(extension_activated_list_Node);
209 if (!(bundleNode->children))
211 xmlUnlinkNode(bundleNode);
212 xmlFreeNode(bundleNode);
217 while ( enable_Value );
226 xmlNodePtr tmp = xmlNewNode( NULL, xmlCharStrdup(
"Configurations_Elements" ) );
227 ::fwRuntime::Convert::fromConfigurationElementToXml( _cfgElement, tmp);
235 xmlNodePtr node =
toXml( _cfgElement );
236 xmlBufferPtr buffer = xmlBufferCreate();
237 xmlNodeDump( buffer, node->doc, xmlFirstElementChild(node), 0, 1 );
239 std::string result =
reinterpret_cast<const char*
>(buffer->content);
242 xmlBufferFree( buffer );
248 ::boost::property_tree::ptree Convert::toPropertyTree( ::fwRuntime::ConfigurationElement::csptr _cfgElement)
250 ::boost::property_tree::ptree pt;
251 ::boost::property_tree::ptree ptAttr;
253 std::string propertyName = _cfgElement->getName();
254 std::string propertyValue = _cfgElement->getValue();
256 typedef std::map<std::string, std::string> AttributeMatType;
258 if(!propertyValue.empty())
260 pt.put(propertyName, propertyValue);
263 AttributeMatType attr = _cfgElement->getAttributes();
265 for ( AttributeMatType::iterator iter = attr.begin(); iter != attr.end(); ++iter)
267 ptAttr.put(iter->first, iter->second);
272 pt.put_child(propertyName +
".<xmlattr>", ptAttr);
275 for (
auto iterElement : _cfgElement->getElements() )
277 const std::string childName = iterElement->getName();
278 ::boost::property_tree::ptree ptChild;
280 ptChild = toPropertyTree(iterElement);
282 boost::optional< ::boost::property_tree::ptree& > child = ptChild.get_child_optional(childName);
289 pt.add_child( propertyName +
"." + childName, ptChild );
296 ::fwRuntime::ConfigurationElement::sptr Convert::fromPropertyTree( ::boost::property_tree::ptree pt )
298 std::stringstream sstr;
299 ::boost::property_tree::write_xml(sstr, pt);
301 std::string xml = sstr.str();
303 xmlDocPtr doc = xmlParseMemory(xml.c_str(),
static_cast<int>(xml.size()));
307 return ::fwRuntime::ConfigurationElement::sptr();
310 xmlNodePtr root = xmlDocGetRootElement(doc);
312 ::fwRuntime::ConfigurationElement::sptr ce;
static FWRUNTIME_API Runtime * getDefault()
Retrieves the default runtime instance.
Defines the runtime class.
FWRUNTIME_API BundleIterator bundlesBegin()
Retrieves the iterator on the begining of the bundle collection.
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.
virtual FWRUNTIME_API ~Convert()
Destructor : does nothing.
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.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
FWRUNTIME_API static std::string toXmlString(::fwRuntime::ConfigurationElement::sptr _cfgElement)
Build an std::string from a ConfigurationElement.
Defines the bundle class.
static FWRUNTIME_API xmlNodePtr runningBundlesToXml()
Build an xmlNodePtr with all running Bundles.