Medical Imaging Interaction Toolkit  2018.4.99-6ca56567
Medical Imaging Interaction Toolkit
mitkXML2EventParser.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkXML2EventParser.h"
14 
15 #include "mitkEventFactory.h"
16 #include "mitkInteractionEvent.h"
19 #include "mitkInternalEvent.h"
20 
21 // VTK
22 #include <vtkXMLDataElement.h>
23 
24 // us
25 #include "usGetModuleContext.h"
26 #include "usModule.h"
27 #include "usModuleResource.h"
28 #include "usModuleResourceStream.h"
29 
30 namespace mitk
31 {
32  void mitk::XML2EventParser::StartElement(const char *elementName, const char **atts)
33  {
34  std::string name(elementName);
35 
37  {
38  //
39  }
41  {
42  std::string eventClass = ReadXMLStringAttribute(InteractionEventConst::xmlParameterEventClass(), atts);
43  // New list in which all parameters are stored that are given within the <input/> tag
44  m_EventPropertyList = PropertyList::New();
45  m_EventPropertyList->SetStringProperty(InteractionEventConst::xmlParameterEventClass().c_str(),
46  eventClass.c_str());
47  // MITK_INFO << "event class " << eventClass;
48  }
49  else if (name == InteractionEventConst::xmlTagAttribute())
50  {
51  // Attributes that describe an Input Event, such as which MouseButton triggered the event,or which modifier keys
52  // are
53  // pressed
54  std::string name = ReadXMLStringAttribute(InteractionEventConst::xmlParameterName(), atts);
55  std::string value = ReadXMLStringAttribute(InteractionEventConst::xmlParameterValue(), atts);
56  // MITK_INFO << "tag attr " << value;
57  m_EventPropertyList->SetStringProperty(name.c_str(), value.c_str());
58  }
59  }
60 
61  void mitk::XML2EventParser::EndElement(const char *elementName)
62  {
63  std::string name(elementName);
64  // At end of input section, all necessary infos are collected to created an interaction event.
66  {
67  InteractionEvent::Pointer event = EventFactory::CreateEvent(m_EventPropertyList);
68  if (event.IsNotNull())
69  {
70  m_InteractionList.push_back(event);
71  }
72  else
73  {
74  MITK_WARN << "EventConfig: Unknown Event-Type in config. Entry skipped: " << name;
75  }
76  }
77  }
78 
79  std::string mitk::XML2EventParser::ReadXMLStringAttribute(const std::string &name, const char **atts)
80  {
81  if (atts)
82  {
83  const char **attsIter = atts;
84 
85  while (*attsIter)
86  {
87  if (name == *attsIter)
88  {
89  attsIter++;
90  return *attsIter;
91  }
92  attsIter += 2;
93  }
94  }
95  return std::string();
96  }
97 
98  bool mitk::XML2EventParser::ReadXMLBooleanAttribute(const std::string &name, const char **atts)
99  {
100  std::string s = ReadXMLStringAttribute(name, atts);
101  std::transform(s.begin(), s.end(), s.begin(), ::toupper);
102 
103  return s == "TRUE";
104  }
105 
106  mitk::XML2EventParser::XML2EventParser(const std::string &filename, const us::Module *module)
107  {
108  if (module == nullptr)
109  {
110  module = us::GetModuleContext()->GetModule();
111  }
112  us::ModuleResource resource = module->GetResource("Interactions/" + filename);
113  if (!resource.IsValid())
114  {
115  MITK_ERROR << "Resource not valid. State machine pattern in module " << module->GetName()
116  << " not found: /Interactions/" << filename;
117  return;
118  }
119 
120  us::ModuleResourceStream stream(resource);
121  this->SetStream(&stream);
122  bool success = this->Parse();
123  if (!success)
124  MITK_ERROR << "Error occured during parsing of EventXML File.";
125  }
126 
127  mitk::XML2EventParser::XML2EventParser(std::istream &inputStream)
128  {
129  this->SetStream(&inputStream);
130  bool success = this->Parse();
131  if (!success)
132  MITK_ERROR << "Error occured during parsing of EventXML File.";
133  }
134 }
static const std::string xmlParameterName()
static Pointer New()
static const std::string xmlParameterValue()
#define MITK_ERROR
Definition: mitkLogMacros.h:20
static const std::string xmlTagConfigRoot()
DataCollection - Class to facilitate loading/accessing structured data.
static const std::string xmlParameterEventClass()
static InteractionEvent::Pointer CreateEvent(PropertyList::Pointer eventDescription)
std::string ReadXMLStringAttribute(const std::string &name, const char **atts)
#define MITK_WARN
Definition: mitkLogMacros.h:19
Module * GetModule() const
ModuleResource GetResource(const std::string &path) const
Definition: usModule.cpp:267
void EndElement(const char *elementName) override
Derived from XMLReader.
static const std::string xmlTagEventVariant()
void StartElement(const char *elementName, const char **atts) override
Derived from XMLReader.
std::string GetName() const
Definition: usModule.cpp:221
bool ReadXMLBooleanAttribute(const std::string &name, const char **atts)
XML2EventParser(const std::string &filename, const us::Module *module=nullptr)
Construct an InteractionEventList object based on a XML configuration file.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
static const std::string xmlTagAttribute()