/
ComponentParser.cpp
91 lines (76 loc) · 2.76 KB
/
ComponentParser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "MantidGeometry/ComponentParser.h"
using namespace Mantid::Kernel;
namespace Mantid {
namespace Geometry {
//----------------------------------------------------------------------------------------------
/** Constructor
*/
ComponentParser::ComponentParser() { m_current.clear(); }
//----------------------------------------------------------------------------------------------
/** Destructor
*/
ComponentParser::~ComponentParser() {}
/** @return the top-level component created */
Component *ComponentParser::getComponent() {
if (m_current.size() > 0)
return m_current[0];
else
return NULL;
}
void ComponentParser::characters(const Poco::XML::XMLChar ch[], int start,
int length) {
m_innerText = std::string(ch + start, length);
}
//----------------------------------------------------------------------------------------------
/// Signals start of element
void ComponentParser::startElement(const Poco::XML::XMLString &,
const Poco::XML::XMLString &localName,
const Poco::XML::XMLString &,
const Poco::XML::Attributes &attr) {
// Find the parent of this new component.
Component *current = NULL;
if (!m_current.empty())
current = m_current.back();
// for (int i=0; i<attr.getLength(); i++) std::cout << i << " : "<<
// attr.getQName(i) << "," << attr.getLocalName(i) << std::endl;
// Find the name in the attributes
std::string name = attr.getValue("", "name");
Component *newComp = NULL;
if (localName == "Component")
newComp = new Component(name, current);
else {
// throw std::runtime_error("ComponentParser:: unexpected XML tag '" +
// localName + "'.");
}
// A new component was created
if (newComp) {
m_current.push_back(newComp);
// Read the attributes into the new component
newComp->readXMLAttributes(attr);
}
}
//----------------------------------------------------------------------------------------------
/// Signals end of element
void ComponentParser::endElement(const Poco::XML::XMLString &,
const Poco::XML::XMLString &localName,
const Poco::XML::XMLString &) {
Component *current = NULL;
if (!m_current.empty())
current = m_current.back();
if (!current) {
throw std::runtime_error("Failed to find last component");
}
if (localName == "pos") {
V3D pos;
pos.fromString(m_innerText);
// std::cout << "found pos " << pos << std::endl;
current->setPos(pos);
} else if (localName == "rot") {
Quat rot;
rot.fromString(m_innerText);
// std::cout << "found rot " << rot << std::endl;
current->setRot(rot);
}
}
} // namespace Mantid
} // namespace Geometry