17 #include <Poco/NumberParser.h> 18 #include <Poco/NumberFormatter.h> 19 #include <Poco/DOM/NodeList.h> 20 #include <Poco/XML/NamePool.h> 21 #include <Poco/DOM/NamedNodeMap.h> 22 #include <Poco/DOM/Text.h> 23 #include <Poco/DOM/Attr.h> 24 #include <Poco/DOM/DOMWriter.h> 25 #include <Poco/DOM/DOMParser.h> 26 #include <Poco/DOM/DOMBuilder.h> 27 #include <Poco/SAX/InputSource.h> 28 #include <Poco/SAX/SAXException.h> 35 Poco::XML::Element* elem) :
36 factory(document), element(elem)
58 Poco::Exception exception(
"");
62 Poco::XML::DOMParser parser;
63 Poco::XML::InputSource source(reader);
65 source.setSystemId(baseDir.toStdString());
66 Poco::XML::Document* doc = parser.parse(&source);
68 Poco::XML::Element* elem = doc->documentElement();
76 catch (Poco::XML::SAXParseException& e)
78 errorMessage = QString(
"Could not parse content of XML file: ") + QString::fromStdString(e.displayText());
81 QString problemText = QString::fromStdString(exception.message());
82 if (problemText.isEmpty())
84 problemText = errorMessage.isEmpty() ?
"Could not find root element node of XML file." : errorMessage;
86 throw WorkbenchException(problemText);
95 auto doc =
new Poco::XML::Document();
96 Poco::XML::Element* elem = doc->createElement(type.toStdString());
97 doc->appendChild(elem)->release();
110 Poco::XML::Element* child = factory->createElement(type.toStdString());
111 element->appendChild(child)->release();
117 const QString& type,
const QString&
id)
119 Poco::XML::Element* child = factory->createElement(type.toStdString());
120 child->setAttribute(
TAG_ID.toStdString(),
id.toStdString());
121 element->appendChild(child)->release();
130 Poco::XML::Element* newElement =
131 dynamic_cast<Poco::XML::Element*
> (factory->importNode(elem,
true));
132 element->appendChild(newElement)->release();
142 Poco::XML::Element* child = element->getChildElement(type.toStdString());
153 const QString& type)
const 155 QList<IMemento::Pointer> mementos;
156 Poco::XML::NodeList* elementList = element->getElementsByTagName(type.toStdString());
157 for (
unsigned long i = 0; i < elementList->length(); i++)
159 Poco::XML::Element* elem =
160 dynamic_cast<Poco::XML::Element*
> (elementList->item(i));
164 elementList->release();
171 if (!element->hasAttribute(key.toStdString()))
return false;
173 const std::string& attr = element->getAttribute(key.toStdString());
177 value = Poco::NumberParser::parseFloat(attr);
178 }
catch (
const Poco::SyntaxException& )
180 std::string _qnan = Poco::NumberFormatter::format(std::numeric_limits<double>::quiet_NaN());
183 value = std::numeric_limits<double>::quiet_NaN();
187 std::string _inf = Poco::NumberFormatter::format(std::numeric_limits<double>::infinity());
190 value = std::numeric_limits<double>::infinity();
195 +
" value: " + QString::fromStdString(attr));
204 return QString::fromStdString(element->nodeName());
210 return QString::fromStdString(element->getAttribute(
TAG_ID.toStdString()));
215 if (!element->hasAttribute(key.toStdString()))
return false;
217 const std::string& attr = element->getAttribute(key.toStdString());
221 value = Poco::NumberParser::parse(attr);
223 catch (
const Poco::SyntaxException& )
226 +
" value: " + QString::fromStdString(attr));
235 const std::string& attr = element->getAttribute(key.toStdString());
238 else if (attr ==
"true")
252 QString v = QString::fromStdString(element->getAttribute(key.toStdString()));
262 Poco::XML::Text* textNode = GetTextNode();
264 if (textNode !=
nullptr)
266 return QString::fromStdString(textNode->getData());
274 QList<QString> values;
275 Poco::XML::NamedNodeMap* nnMap = element->attributes();
277 values.reserve(nnMap->length());
279 for (
unsigned long i = 0; i < nnMap->length(); i++)
281 values[i] = QString::fromStdString(nnMap->item(i)->nodeName());
287 Poco::XML::Text* berry::XMLMemento::GetTextNode()
const 291 Poco::XML::NodeList* nodes = element->childNodes();
293 unsigned long size = nodes->length();
299 for (
unsigned long index = 0; index < size; index++)
301 if (nodes->item(index)->nodeType() == Poco::XML::Node::TEXT_NODE)
303 return dynamic_cast<Poco::XML::Text*
> (nodes->item(index));
312 void berry::XMLMemento::PutElement(Poco::XML::Element* element,
bool copyText)
314 Poco::XML::NamedNodeMap* nodeMap = element->attributes();
315 unsigned long size = nodeMap->length();
317 for (
unsigned long index = 0; index < size; index++)
319 Poco::XML::Node* node = nodeMap->item(index);
320 Poco::XML::Attr* attr =
dynamic_cast<Poco::XML::Attr*
> (node);
321 PutString(QString::fromStdString(attr->nodeName()),
322 QString::fromStdString(attr->nodeValue()));
326 bool needToCopyText = copyText;
327 Poco::XML::Node* child = element->firstChild();
330 unsigned short nodeType = child->nodeType();
334 case Poco::XML::Node::ELEMENT_NODE:
336 Poco::XML::Element* elem =
dynamic_cast<Poco::XML::Element*
> (child);
339 child->PutElement(elem,
true);
342 case Poco::XML::Node::TEXT_NODE:
345 Poco::XML::Text* text =
dynamic_cast<Poco::XML::Text*
> (child);
346 PutTextData(QString::fromStdString(text->getData()));
347 needToCopyText =
false;
355 child = child->nextSibling();
361 std::string xmlValue = Poco::NumberFormatter::format(value);
362 element->setAttribute(key.toStdString(), xmlValue);
367 std::string xmlValue = Poco::NumberFormatter::format(value);
368 element->setAttribute(key.toStdString(), xmlValue);
379 const QString& value)
381 element->setAttribute(key.toStdString(), value.toStdString());
391 element->setAttribute(key.toStdString(),
"true");
395 element->setAttribute(key.toStdString(),
"false");
401 Poco::XML::Text* textNode = GetTextNode();
402 if (textNode ==
nullptr)
404 textNode = factory->createTextNode(data.toStdString());
405 element->insertBefore(textNode, element->firstChild())->release();
409 textNode->setData(data.toStdString());
417 Poco::XML::DOMWriter out;
419 out.writeNode(writer, factory);
void PutString(const QString &key, const QString &value) override
QList< QString > GetAttributeKeys() const override
bool GetInteger(const QString &key, int &value) const override
void PutBoolean(const QString &key, bool value) override
std::istream XMLByteInputStream
static void Log(const QString &message)
IMemento::Pointer CopyChild(IMemento::Pointer child)
QList< IMemento::Pointer > GetChildren(const QString &type) const override
SmartPointer< Other > Cast() const
QString GetTextData() const override
void PutInteger(const QString &key, int value) override
bool GetFloat(const QString &key, double &value) const override
QString GetType() const override
static const QString TAG_ID
std::ostream XMLByteOutputStream
const QString EMPTY_STRING
bool GetString(const QString &key, QString &value) const override
static XMLMemento::Pointer CreateReadRoot(berry::XMLMemento::XMLByteInputStream &reader)
void PutTextData(const QString &data) override
IMemento::Pointer CreateChild(const QString &type) override
IMemento::Pointer GetChild(const QString &type) const override
void PutFloat(const QString &key, double value) override
static XMLMemento::Pointer CreateWriteRoot(const QString &type)
QString GetID() const override
virtual Poco::XML::Element * GetElement() const
void Save(XMLByteOutputStream &writer)
bool GetBoolean(const QString &key, bool &value) const override
XMLMemento(Poco::XML::Document *document, Poco::XML::Element *elem)
void PutMemento(IMemento::Pointer memento) override