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
berryExpressionConverter.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 BlueBerry Platform
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 
18 
19 #include "berryPlatformException.h"
21 #include "berryIExtension.h"
22 #include "berryIContributor.h"
23 
24 #include "berryCoreException.h"
25 #include "berryElementHandler.h"
26 #include "berryExpression.h"
27 #include "berryStatus.h"
28 
29 #include "internal/berryExpressionPlugin.h"
30 #include "internal/berryCompositeExpression.h"
31 
32 #include "Poco/DOM/Node.h"
33 #include "Poco/DOM/Element.h"
34 
35 
36 namespace berry {
37 
38 ExpressionConverter* ExpressionConverter::INSTANCE = nullptr;
39 
40 ExpressionConverter*
42 {
43  if (INSTANCE) return INSTANCE;
44 
45  QList<ElementHandler::Pointer> handlers;
46  handlers.push_back(ElementHandler::GetDefault());
47  INSTANCE = new ExpressionConverter(handlers);
48 
49  return INSTANCE;
50 }
51 
52 ExpressionConverter::ExpressionConverter(const QList<ElementHandler::Pointer>& handlers)
53 {
54  fHandlers = handlers;
55 }
56 
59 {
60  for (int i = 0; i < fHandlers.size(); i++) {
61  ElementHandler::Pointer handler = fHandlers[i];
62  Expression::Pointer result = handler->Create(this, root);
63  if (!result.IsNull())
64  return result;
65  }
66  return Expression::Pointer();
67 }
68 
70 ExpressionConverter::Perform(Poco::XML::Element* root)
71 {
72  for (int i = 0; i < fHandlers.size(); i++) {
73  ElementHandler::Pointer handler = fHandlers[i];
74  Expression::Pointer result = handler->Create(this, root);
75  if (!result.IsNull())
76  return result;
77  }
78  return Expression::Pointer();
79 }
80 
81 void
83  const CompositeExpression::Pointer& result)
84 {
85  QList<IConfigurationElement::Pointer> children(element->GetChildren());
86 
87  QList<IConfigurationElement::Pointer>::iterator iter;
88  for (iter = children.begin(); iter != children.end(); ++iter)
89  {
90  Expression::Pointer child = this->Perform(*iter);
91  if (child.IsNull())
92  {
93  IStatus::Pointer status(new Status(IStatus::ERROR_TYPE, ExpressionPlugin::GetPluginId(),
94  IStatus::ERROR_TYPE, QString("Unknown expression element ") + GetDebugPath(*iter),
96  throw CoreException(status);
97  }
98 
99  result->Add(child);
100  }
101 }
102 
103 QString
105 {
106  QString buf;
107  buf.append(configurationElement->GetName());
108  Object::Pointer parent= configurationElement->GetParent();
109  while (parent)
110  {
112  {
113  buf.append(" > ");
114  buf.append(parent2->GetName());
115  QString id= parent2->GetAttribute("id");
116  if (!id.isEmpty())
117  {
118  buf.append(" (id=").append(id).append(')');
119  }
120  parent= parent2->GetParent();
121  }
122  else if (IExtension::Pointer parent2 = parent.Cast<IExtension>())
123  {
124  buf.append(" : ");
125  buf.append(parent2->GetExtensionPointUniqueIdentifier());
126  buf.append(" @ ");
127  buf.append(parent2->GetContributor()->GetName());
128  parent = nullptr;
129  }
130  else
131  {
132  parent = nullptr;
133  }
134  }
135  return buf;
136 }
137 
138 void
139 ExpressionConverter::ProcessChildren(Poco::XML::Element* element,
140  const CompositeExpression::Pointer& result)
141 {
142  Poco::XML::Node* child = element->firstChild();
143  while (child != nullptr) {
144  if (child->nodeType() == Poco::XML::Node::ELEMENT_NODE) {
145  Poco::XML::Element* elem = static_cast<Poco::XML::Element*>(child);
146  Expression::Pointer exp = this->Perform(elem);
147  if (exp.IsNull())
148  {
149  IStatus::Pointer status(new Status(IStatus::ERROR_TYPE, ExpressionPlugin::GetPluginId(),
150  IStatus::ERROR_TYPE, QString("Unknown expression element ")
151  + QString::fromStdString(elem->nodeName()), BERRY_STATUS_LOC));
152 
153  throw CoreException(status);
154  }
155  result->Add(exp);
156  }
157  child = child->nextSibling();
158  }
159 }
160 
161 } // namespace berry
static MsgHandler handler
Definition: usUtils.cpp:261
itk::SmartPointer< Self > Pointer
void ProcessChildren(const SmartPointer< IConfigurationElement > &element, const SmartPointer< CompositeExpression > &result)
static ExpressionConverter * GetDefault()
SmartPointer< Expression > Perform(const SmartPointer< IConfigurationElement > &root)
QString GetDebugPath(const SmartPointer< IConfigurationElement > &configurationElement)
#define BERRY_STATUS_LOC
Definition: berryStatus.h:257
berry::SmartPointer< Self > Pointer
Definition: berryObject.h:88
ExpressionConverter(const QList< SmartPointer< ElementHandler > > &handlers)
static ElementHandler::Pointer GetDefault()
SmartPointer< Other > Cast() const