fw4spl
Extension.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
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 #if defined(__GNUC__)
8 #pragma GCC visibility push(default)
9 #endif
10 #include <iostream>
11 #include <sstream>
12 #include <libxml/xmlstring.h>
13 #if defined(__GNUC__)
14 #pragma GCC visibility pop
15 #endif
16 
17 #include <fwCore/base.hpp>
18 
19 #include "fwRuntime/Bundle.hpp"
20 #include "fwRuntime/ExtensionPoint.hpp"
21 #include "fwRuntime/Runtime.hpp"
22 #include "fwRuntime/io/Validator.hpp"
23 #include "fwRuntime/Extension.hpp"
24 #include "fwRuntime/helper.hpp"
25 
26 namespace fwRuntime
27 {
28 
29 //------------------------------------------------------------------------------
30 
31 Extension::Extension( std::shared_ptr< Bundle > bundle, const std::string & id, const std::string & point,
32  xmlNodePtr xmlNode )
33  : BundleElement ( bundle ),
34  m_id ( id ),
35  m_point ( point ),
36  m_xmlDoc ( xmlNewDoc(BAD_CAST "1.0") ),
37  m_xmlNode ( xmlCopyNode(xmlNode, 1) ),
38  m_validity ( UnknownValidity )
39 {
40  xmlDocSetRootElement(m_xmlDoc, m_xmlNode);
41 }
42 
43 //------------------------------------------------------------------------------
44 
46 {
47  xmlFreeDoc( m_xmlDoc );
48 }
49 
50 //------------------------------------------------------------------------------
51 
52 const std::string& Extension::getIdentifier() const
53 {
54  return m_id;
55 }
56 
57 //------------------------------------------------------------------------------
58 
59 const std::string& Extension::getPoint() const
60 {
61  return m_point;
62 }
63 
64 //------------------------------------------------------------------------------
65 
66 xmlNodePtr Extension::getXmlNode() const
67 {
68  return m_xmlNode;
69 }
70 
71 //------------------------------------------------------------------------------
72 
74 {
75  return m_validity;
76 }
77 
78 //------------------------------------------------------------------------------
79 
81 {
82  // Skips the validation if already done.
83  if( m_validity != UnknownValidity )
84  {
85  return m_validity;
86  }
87 
88  // Retrieves the extension point.
89  Runtime * rntm( Runtime::getDefault() );
90  std::shared_ptr< ExtensionPoint > point( rntm->findExtensionPoint(m_point) );
91 
92  // Checks that the point exists.
93  if( !point )
94  {
95  throw RuntimeException(m_point + " : invalid point reference.");
96  }
97 
98  // Validates the extension.
99  std::shared_ptr< io::Validator > validator( point->getExtensionValidator() );
100  OSLM_ASSERT("The validator creation failed for the point "<<point->getIdentifier(), validator );
101 
102  // Check extension XML Node <extension id="xxx" implements="yyy" >...</extension>
103  validator->clearErrorLog();
104  if( validator->validate( m_xmlNode ) == true )
105  {
106  m_validity = Valid;
107  }
108  else
109  {
110  m_validity = Invalid;
111  const std::string identifier = m_id.empty() ? "anonymous" : m_id;
112  OSLM_ERROR(
113  "In bundle " << getBundle()->getIdentifier() << ". " << identifier
114  << ": invalid extension XML element node does not respect schema. Verification error log is : "
115  << std::endl << validator->getErrorLog() );
116  }
117 
118  return m_validity;
119 }
120 
121 //------------------------------------------------------------------------------
122 
123 void Extension::operator=(const Extension&) noexcept
124 {
125 }
126 
127 //------------------------------------------------------------------------------
128 
129 } // namespace fwRuntime
static FWRUNTIME_API Runtime * getDefault()
Retrieves the default runtime instance.
Definition: Runtime.cpp:286
Defines the runtime class.
Definition: Runtime.hpp:37
The extension passed the validation.
Definition: Extension.hpp:41
#define OSLM_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:310
Implements the base class for all element managed by a bundle.
Defines the extension class.
Definition: Extension.hpp:30
Defines the runtime exception class.
Extension(std::shared_ptr< Bundle > bundle, const std::string &id, const std::string &point, const xmlNodePtr xmlNode)
Constructor.
Definition: Extension.cpp:31
Validity
Defines the validity states of an extension.
Definition: Extension.hpp:38
The extension failed the validation.
Definition: Extension.hpp:42
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
FWRUNTIME_API std::shared_ptr< ExtensionPoint > findExtensionPoint(const std::string &identifier) const
Retrieves the extension point instance matching the specified identifier.
Definition: Runtime.cpp:313
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
FWRUNTIME_API const std::string & getIdentifier() const
Retrieves the extension identifier.
Definition: Extension.cpp:52
FWRUNTIME_API std::shared_ptr< Bundle > getBundle() const
Retrieves the bundle that owns the extension.
FWRUNTIME_API Validity getValidity() const
Retrieves the validity of the extension.
Definition: Extension.cpp:73
FWRUNTIME_API Validity validate()
Validates the extension.
Definition: Extension.cpp:80
FWRUNTIME_API const std::string & getPoint() const
Retrieves the extension point identifier.
Definition: Extension.cpp:59
FWRUNTIME_API xmlNodePtr getXmlNode() const
Retrieves the xml node that represents the extension.
Definition: Extension.cpp:66
The extension has not been validated.
Definition: Extension.hpp:40
~Extension()
Destructor.
Definition: Extension.cpp:45