Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
6  Division of Medical and Biological Informatics.
7  All rights reserved.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without
10  even the implied warranty of MERCHANTABILITY or FITNESS FOR
11  A PARTICULAR PURPOSE.
12 
13  See LICENSE.txt or http://www.mitk.org for details.
14 
15  ===================================================================*/
16 
17 #include "mitkXML2EventParser.h"
18 
19 #include "mitkEventFactory.h"
20 #include "mitkInteractionEvent.h"
23 #include "mitkInternalEvent.h"
24 
25 // VTK
26 #include <vtkXMLDataElement.h>
27 
28 // us
29 #include "usGetModuleContext.h"
30 #include "usModule.h"
31 #include "usModuleResource.h"
32 #include "usModuleResourceStream.h"
33 
34 namespace mitk
35 {
36  void mitk::XML2EventParser::StartElement(const char *elementName, const char **atts)
37  {
38  std::string name(elementName);
39 
41  {
42  //
43  }
45  {
46  std::string eventClass = ReadXMLStringAttribute(InteractionEventConst::xmlParameterEventClass(), atts);
47  // New list in which all parameters are stored that are given within the <input/> tag
48  m_EventPropertyList = PropertyList::New();
49  m_EventPropertyList->SetStringProperty(InteractionEventConst::xmlParameterEventClass().c_str(),
50  eventClass.c_str());
51  // MITK_INFO << "event class " << eventClass;
52  }
53  else if (name == InteractionEventConst::xmlTagAttribute())
54  {
55  // Attributes that describe an Input Event, such as which MouseButton triggered the event,or which modifier keys
56  // are
57  // pressed
58  std::string name = ReadXMLStringAttribute(InteractionEventConst::xmlParameterName(), atts);
59  std::string value = ReadXMLStringAttribute(InteractionEventConst::xmlParameterValue(), atts);
60  // MITK_INFO << "tag attr " << value;
61  m_EventPropertyList->SetStringProperty(name.c_str(), value.c_str());
62  }
63  }
64 
65  void mitk::XML2EventParser::EndElement(const char *elementName)
66  {
67  std::string name(elementName);
68  // At end of input section, all necessary infos are collected to created an interaction event.
70  {
71  InteractionEvent::Pointer event = EventFactory::CreateEvent(m_EventPropertyList);
72  if (event.IsNotNull())
73  {
74  m_InteractionList.push_back(event);
75  }
76  else
77  {
78  MITK_WARN << "EventConfig: Unknown Event-Type in config. Entry skipped: " << name;
79  }
80  }
81  }
82 
83  std::string mitk::XML2EventParser::ReadXMLStringAttribute(const std::string &name, const char **atts)
84  {
85  if (atts)
86  {
87  const char **attsIter = atts;
88 
89  while (*attsIter)
90  {
91  if (name == *attsIter)
92  {
93  attsIter++;
94  return *attsIter;
95  }
96  attsIter += 2;
97  }
98  }
99  return std::string();
100  }
101 
102  bool mitk::XML2EventParser::ReadXMLBooleanAttribute(const std::string &name, const char **atts)
103  {
104  std::string s = ReadXMLStringAttribute(name, atts);
105  std::transform(s.begin(), s.end(), s.begin(), ::toupper);
106 
107  return s == "TRUE";
108  }
109 
110  mitk::XML2EventParser::XML2EventParser(const std::string &filename, const us::Module *module)
111  {
112  if (module == NULL)
113  {
114  module = us::GetModuleContext()->GetModule();
115  }
116  us::ModuleResource resource = module->GetResource("Interactions/" + filename);
117  if (!resource.IsValid())
118  {
119  MITK_ERROR << "Resource not valid. State machine pattern in module " << module->GetName()
120  << " not found: /Interactions/" << filename;
121  return;
122  }
123 
124  us::ModuleResourceStream stream(resource);
125  this->SetStream(&stream);
126  bool success = this->Parse();
127  if (!success)
128  MITK_ERROR << "Error occured during parsing of EventXML File.";
129  }
130 
131  mitk::XML2EventParser::XML2EventParser(std::istream &inputStream)
132  {
133  this->SetStream(&inputStream);
134  bool success = this->Parse();
135  if (!success)
136  MITK_ERROR << "Error occured during parsing of EventXML File.";
137  }
138 }
static const std::string xmlParameterName()
static Pointer New()
std::string GetName() const
Definition: usModule.cpp:221
static const std::string xmlParameterValue()
#define MITK_ERROR
Definition: mitkLogMacros.h:24
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:23
Module * GetModule() const
static const std::string filename
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.
bool ReadXMLBooleanAttribute(const std::string &name, const char **atts)
XML2EventParser(const std::string &filename, const us::Module *module=NULL)
Construct an InteractionEventList object based on a XML configuration file.
ModuleResource GetResource(const std::string &path) const
Definition: usModule.cpp:267
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
static const std::string xmlTagAttribute()