Mantid
Loading...
Searching...
No Matches
ComponentParser.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9using namespace Mantid::Kernel;
10
11namespace Mantid::Geometry {
12
15 if (!m_current.empty())
16 return m_current[0];
17 else
18 return nullptr;
19}
20
21void ComponentParser::characters(const Poco::XML::XMLChar ch[], int start, int length) {
22 m_innerText = std::string(ch + start, length);
23}
24
25//----------------------------------------------------------------------------------------------
27void ComponentParser::startElement(const Poco::XML::XMLString & /*uri*/, const Poco::XML::XMLString &localName,
28 const Poco::XML::XMLString & /*qname*/, const Poco::XML::Attributes &attr) {
29 // Find the parent of this new component.
30 Component *current = nullptr;
31 if (!m_current.empty())
32 current = m_current.back();
33
34 // for (int i=0; i<attr.getLength(); i++) std::cout << i << " : "<<
35 // attr.getQName(i) << "," << attr.getLocalName(i) << '\n';
36
37 // Find the name in the attributes
38 std::string name = attr.getValue("", "name");
39
40 Component *newComp = nullptr;
41 if (localName == "Component")
42 newComp = new Component(name, current);
43 else {
44 // throw std::runtime_error("ComponentParser:: unexpected XML tag '" +
45 // localName + "'.");
46 }
47
48 // A new component was created
49 if (newComp) {
50 m_current.emplace_back(newComp);
51 // Read the attributes into the new component
52 newComp->readXMLAttributes(attr);
53 }
54}
55
56//----------------------------------------------------------------------------------------------
58void ComponentParser::endElement(const Poco::XML::XMLString & /*uri*/, const Poco::XML::XMLString &localName,
59 const Poco::XML::XMLString & /*qname*/) {
60 Component *current = nullptr;
61 if (!m_current.empty())
62 current = m_current.back();
63
64 if (!current) {
65 throw std::runtime_error("Failed to find last component");
66 }
67
68 if (localName == "pos") {
69 V3D pos;
71 // std::cout << "found pos " << pos << '\n';
72 current->setPos(pos);
73 } else if (localName == "rot") {
74 Quat rot;
76 // std::cout << "found rot " << rot << '\n';
77 current->setRot(rot);
78 }
79}
80
81} // namespace Mantid::Geometry
std::vector< Component * > m_current
The components currently being built up.
void startElement(const Poco::XML::XMLString &, const Poco::XML::XMLString &localName, const Poco::XML::XMLString &, const Poco::XML::Attributes &attr) override
Signals start of element.
void endElement(const Poco::XML::XMLString &, const Poco::XML::XMLString &localName, const Poco::XML::XMLString &) override
Signals end of element.
void characters(const Poco::XML::XMLChar[], int, int) override
Component is a wrapper for a Component which can modify some of its parameters, e....
Definition: Component.h:41
void setRot(const Kernel::Quat &) override
Set the orientation Kernel::Quaternion relative to parent (if present)
Definition: Component.cpp:226
virtual void readXMLAttributes(const Poco::XML::Attributes &attr)
Reads the XML attributes from Poco XML parser.
Definition: Component.cpp:535
void setPos(double, double, double) override
Set the IComponent position, x, y, z respective to parent (if present)
Definition: Component.cpp:204
Class for quaternions.
Definition: Quat.h:39
void fromString(const std::string &str)
Sets the Quat using a string.
Definition: Quat.cpp:665
Class for 3D vectors.
Definition: V3D.h:34
void fromString(const std::string &str)
Sets the vector using a string.
Definition: V3D.cpp:348