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