fw4spl
fwRuntime/src/helper.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
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/helper.hpp"
8 
9 #include "fwRuntime/Convert.hpp"
10 
11 #include <fwCore/base.hpp>
12 
13 #include <iostream>
14 
15 namespace fwRuntime
16 {
17 
18 //------------------------------------------------------------------------------
19 
20 std::pair< bool, std::string > validateConfigurationElement( std::shared_ptr< ::fwRuntime::io::Validator > _validator,
21  ::fwRuntime::ConfigurationElement::sptr _element )
22 {
23  SLM_ASSERT("_validator not instanced", _validator);
24  SLM_ASSERT("_element not instanced", _element);
25 
26  xmlNodePtr _elementNodePtr = xmlNewNode( NULL, xmlCharStrdup( _element->getName().c_str() ) );
27  ::fwRuntime::ConfigurationElement2XML( _element, _elementNodePtr );
28  xmlDocPtr xmlDoc = xmlNewDoc(BAD_CAST "1.0");
29  xmlNodePtr xmlNode = xmlCopyNode(_elementNodePtr, 1);
30  xmlDocSetRootElement(xmlDoc, xmlNode);
31 
32  _validator->clearErrorLog();
33 
34  std::pair< bool, std::string > validationResult;
35  if( !(_validator->validate( xmlNode ) == true) )
36  {
37  validationResult.first = false;
38  validationResult.second = _validator->getErrorLog();
39  }
40  else
41  {
42  validationResult.first = true;
43  }
44 
45  xmlFreeDoc( xmlDoc );
46  return validationResult;
47 }
48 
49 //------------------------------------------------------------------------------
50 
51 void ConfigurationElement2XML(::fwRuntime::ConfigurationElement::sptr _cfgElement, xmlNodePtr pNode)
52 {
53  //ATTRIBUTES + VALUES
54  std::map<std::string, std::string> attr = _cfgElement->getAttributes();
55  for ( std::map<std::string, std::string>::iterator iter_attr_cfe = attr.begin(); iter_attr_cfe != attr.end();
56  ++iter_attr_cfe)
57  {
58  xmlSetProp(pNode, xmlCharStrdup((iter_attr_cfe->first).c_str()),
59  xmlCharStrdup((iter_attr_cfe->second).c_str()) );
60  }
61  //ELEMENTS
62  for(::fwRuntime::ConfigurationElement::sptr elt : _cfgElement->getElements())
63  {
64  xmlNodePtr child = xmlNewNode( NULL, xmlCharStrdup( elt->getName().c_str() ) );
65 
66  xmlAddChild(pNode, child);
67  // If configuration element is a XML_TEXT_NODE : WARNING : even whitespace (non XML_TEXT_NODE) have been
68  // considered as valid XML_TEXT_NODE by BundleDescriptorReader!!!!
69  if( !elt->getValue().empty() )
70  {
71  xmlNodeSetContent(child, xmlCharStrdup( elt->getValue().c_str() ));
72  }
73  // If configuration element is a XML_ELEMENT_NODE
74  else
75  {
76  ConfigurationElement2XML(elt, child);
77  }
78  }
79 }
80 
81 //------------------------------------------------------------------------------
82 
83 ConfigurationElement::sptr getCfgAsAnExtension( ConfigurationElement::sptr config, std::string extension_pt )
84 {
85  ConfigurationElement::sptr resultConfig;
86  if( config->hasAttribute("config") )
87  {
88  std::string cfgContribution = config->getExistingAttributeValue("config");
89  std::vector< ConfigurationElement::sptr > cfgs = ::fwRuntime::getAllCfgForPoint( extension_pt );
90  OSLM_FATAL_IF( "No configuration contribution found for extension point " << extension_pt, cfgs.empty());
91 
92  // Search for all matching contributions
93  std::vector< ConfigurationElement::sptr > matchingCfg;
94  for(ConfigurationElement::sptr elt : cfgs)
95  {
96  if( cfgContribution == elt->getExistingAttributeValue("id") )
97  {
98  matchingCfg.push_back( elt );
99  }
100  }
101 
102  // If no contribution found
103  OSLM_FATAL_IF( "No contribution matching the requested requirement (" << cfgContribution
104  << " for extension point " << extension_pt << " )",
105  matchingCfg.empty());
106 
107  // Normal case : only one matching contribution has been found: matchingCfg.size == 1
108  resultConfig = *matchingCfg.begin();
109 
110  // If several matching contributions
111  OSLM_WARN_IF("Several contribution identified by " << cfgContribution << "( for cfg extension point " << extension_pt << " )"
112  << " has been found : the first one is returned",
113  (matchingCfg.size() > 1));
114  }
115  SLM_WARN_IF("Configuration has no config attribute", !config->hasAttribute("config"));
116  return resultConfig;
117 }
118 
119 //------------------------------------------------------------------------------
120 
121 std::vector< ConfigurationElement::sptr > getAllCfgForPoint( std::string _extension_pt )
122 {
123  using ::fwRuntime::ConfigurationElement;
124 
125  typedef std::vector< ConfigurationElement::sptr > ElementContainer;
126  typedef std::back_insert_iterator< ElementContainer > Inserter;
127 
128  ElementContainer renderElements;
129  Inserter renderInserter(renderElements);
130 
131  ::fwRuntime::getAllConfigurationElementsForPoint(_extension_pt, renderInserter);
132  return renderElements;
133 }
134 
135 //------------------------------------------------------------------------------
136 
137 std::vector< std::string > getAllIdsForPoint( std::string _extension_pt )
138 {
139  std::vector<std::string > ids;
140 
141  using ::fwRuntime::ConfigurationElement;
142  typedef std::vector< ConfigurationElement::sptr > ElementContainer;
143  typedef std::back_insert_iterator< ElementContainer > Inserter;
144 
145  // Collects all contributed actions
146  ElementContainer elements;
147  Inserter inserter(elements);
148  ::fwRuntime::getAllConfigurationElementsForPoint( _extension_pt, inserter);
149 
150  // Creates all contributed action instances.
151  for(::fwRuntime::ConfigurationElement::sptr elt : elements)
152  {
153  ids.push_back(elt->getAttributeValue("id"));
154  }
155  return ids;
156 }
157 
158 //------------------------------------------------------------------------------
159 
160 std::string getInfoForPoint( std::string _extension_pt )
161 {
162  std::string info = "";
163  if(::fwRuntime::findExtensionPoint( _extension_pt ) )
164  {
165  using ::fwRuntime::ConfigurationElement;
166  typedef std::vector< ConfigurationElement::sptr > ElementContainer;
167  typedef std::back_insert_iterator< ElementContainer > Inserter;
168 
169  // Collects all contributed actions
170  ElementContainer elements;
171  Inserter inserter(elements);
172  ::fwRuntime::getAllConfigurationElementsForPoint( _extension_pt, inserter);
173 
174  // Creates all contributed action instances.
175  for(::fwRuntime::ConfigurationElement::sptr elt : elements)
176  {
177  if( elt->getName() == "info" && elt->hasAttribute("text") )
178  {
179  info = elt->getAttributeValue("text");
180  break;
181  }
182  }
183  }
184  return info;
185 }
186 
187 //------------------------------------------------------------------------------
188 
189 std::map< std::string,
190  ConfigurationElement::sptr > getAllIdAndConfigurationElementsForPoint( std::string _extension_pt )
191 {
192  std::map<std::string, ConfigurationElement::sptr > cfgElementMap;
193 
194  using ::fwRuntime::ConfigurationElement;
195  typedef std::vector< ConfigurationElement::sptr > ElementContainer;
196  typedef std::back_insert_iterator< ElementContainer > Inserter;
197 
198  // Collects all contributed actions
199  ElementContainer elements;
200  Inserter inserter(elements);
201  ::fwRuntime::getAllConfigurationElementsForPoint( _extension_pt, inserter);
202 
203  // Creates all contributed action instances.
204  for(::fwRuntime::ConfigurationElement::sptr elt : elements)
205  {
206  cfgElementMap[elt->getAttributeValue("id")] = elt;
207  }
208  return cfgElementMap;
209 }
210 
211 //------------------------------------------------------------------------------
212 
213 std::vector< std::shared_ptr< ::fwRuntime::Extension > > getAllExtensionsForPoint(std::string extension_pt)
214 {
215  typedef std::vector< std::shared_ptr< Extension > > ExtensionContainer;
216  typedef std::back_insert_iterator< ExtensionContainer > Inserter;
217 
218  ExtensionContainer extElements;
219  Inserter extInserter(extElements);
220 
221  ::fwRuntime::getAllExtensionsForPoint( extension_pt, extInserter );
222  return extElements;
223 }
224 }
FWRUNTIME_API ConfigurationElement::sptr getCfgAsAnExtension(ConfigurationElement::sptr _config, std::string _extension_pt)
Returns the configuration element embedding the configuration to be considered for initializing an ob...
FWRUNTIME_API std::vector< ConfigurationElement::sptr > getAllCfgForPoint(std::string _extension_pt)
Returns configuration elements extending the _extension_pt extension point.
FWRUNTIME_API std::string getInfoForPoint(std::string _extension_pt)
Get information for the point.
FWRUNTIME_API std::vector< std::shared_ptr< ::fwRuntime::Extension > > getAllExtensionsForPoint(std::string extension_pt)
Returns extensions extending the _extension_pt extension point.
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
FWRUNTIME_API std::shared_ptr< ExtensionPoint > findExtensionPoint(const std::string &identifier)
Retrieves the extension point having the specified identifier.
Definition: operations.cpp:83
FWRUNTIME_API std::pair< bool, std::string > validateConfigurationElement(std::shared_ptr< ::fwRuntime::io::Validator > _validator,::fwRuntime::ConfigurationElement::sptr _element)
Check configuration element (its XML representation) with respect to the validator (referencing the x...
FWRUNTIME_API std::map< std::string, ConfigurationElement::sptr > getAllIdAndConfigurationElementsForPoint(std::string _extension_pt)
Returns contribution ids and configuration elements related to a given extension point.
#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 void ConfigurationElement2XML(::fwRuntime::ConfigurationElement::sptr _cfgElement, xmlNodePtr pNode)
Update pNode content according to _cfgElement.
#define OSLM_WARN_IF(message, cond)
Definition: spyLog.hpp:267
#define OSLM_FATAL_IF(message, cond)
Definition: spyLog.hpp:289
FWRUNTIME_API std::vector< std::string > getAllIdsForPoint(std::string _extension_pt)
Returns contribution ids to a given extension point.
#define SLM_WARN_IF(message, cond)
Definition: spyLog.hpp:265