made XMLConfiguration fully writable

This commit is contained in:
Guenter Obiltschnig
2009-04-08 13:08:34 +00:00
parent 0b6fd120c5
commit 98eb0eb532
4 changed files with 186 additions and 71 deletions

View File

@@ -1,13 +1,13 @@
//
// XMLConfiguration.cpp
//
// $Id: //poco/1.3/Util/src/XMLConfiguration.cpp#1 $
// $Id: //poco/Main/Util/src/XMLConfiguration.cpp#8 $
//
// Library: Util
// Package: Configuration
// Module: XMLConfiguration
//
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
@@ -39,8 +39,6 @@
#include "Poco/DOM/DOMParser.h"
#include "Poco/DOM/Element.h"
#include "Poco/DOM/Attr.h"
#include "Poco/DOM/DOMWriter.h"
#include "Poco/DOM/DOMException.h"
#include "Poco/DOM/Text.h"
#include "Poco/XML/XMLWriter.h"
#include "Poco/Exception.h"
@@ -144,7 +142,15 @@ void XMLConfiguration::load(const Poco::XML::Node* pNode)
}
void XMLConfiguration::save(const std::string& path)
void XMLConfiguration::loadEmpty(const std::string& rootElementName)
{
_pDocument = new Poco::XML::Document;
_pRoot = _pDocument->createElement(rootElementName);
_pDocument->appendChild(_pRoot);
}
void XMLConfiguration::save(const std::string& path) const
{
Poco::XML::DOMWriter writer;
writer.setNewLine("\n");
@@ -153,7 +159,7 @@ void XMLConfiguration::save(const std::string& path)
}
void XMLConfiguration::save(std::ostream& ostr)
void XMLConfiguration::save(std::ostream& ostr) const
{
Poco::XML::DOMWriter writer;
writer.setNewLine("\n");
@@ -162,6 +168,18 @@ void XMLConfiguration::save(std::ostream& ostr)
}
void XMLConfiguration::save(Poco::XML::DOMWriter& writer, const std::string& path) const
{
writer.writeNode(path, _pDocument);
}
void XMLConfiguration::save(Poco::XML::DOMWriter& writer, std::ostream& ostr) const
{
writer.writeNode(ostr, _pDocument);
}
bool XMLConfiguration::getRaw(const std::string& key, std::string& value) const
{
const Poco::XML::Node* pNode = findNode(key);
@@ -176,8 +194,8 @@ bool XMLConfiguration::getRaw(const std::string& key, std::string& value) const
void XMLConfiguration::setRaw(const std::string& key, const std::string& value)
{
Poco::XML::Node* pNode = const_cast<Poco::XML::Node*>(findNode(key));
std::string::const_iterator it = key.begin();
Poco::XML::Node* pNode = findNode(it, key.end(), _pRoot, true);
if (pNode)
{
unsigned short nodeType = pNode->nodeType();
@@ -195,6 +213,11 @@ void XMLConfiguration::setRaw(const std::string& key, const std::string& value)
pChildNode->setNodeValue(value);
}
}
else
{
Poco::AutoPtr<Poco::XML::Node> pText = _pDocument->createTextNode(value);
pNode->appendChild(pText);
}
}
}
else throw NotFoundException("Node not found in XMLConfiguration", key);
@@ -231,11 +254,12 @@ void XMLConfiguration::enumerate(const std::string& key, Keys& range) const
const Poco::XML::Node* XMLConfiguration::findNode(const std::string& key) const
{
std::string::const_iterator it = key.begin();
return findNode(it, key.end(), _pRoot);
Poco::XML::Node* pRoot = const_cast<Poco::XML::Node*>(_pRoot.get());
return findNode(it, key.end(), pRoot);
}
const Poco::XML::Node* XMLConfiguration::findNode(std::string::const_iterator& it, const std::string::const_iterator& end, const Poco::XML::Node* pNode)
Poco::XML::Node* XMLConfiguration::findNode(std::string::const_iterator& it, const std::string::const_iterator& end, Poco::XML::Node* pNode, bool create)
{
if (pNode && it != end)
{
@@ -248,14 +272,14 @@ const Poco::XML::Node* XMLConfiguration::findNode(std::string::const_iterator& i
std::string attr;
while (it != end && *it != ']') attr += *it++;
if (it != end) ++it;
return findAttribute(attr, pNode);
return findAttribute(attr, pNode, create);
}
else
{
std::string index;
while (it != end && *it != ']') index += *it++;
if (it != end) ++it;
return findNode(it, end, findElement(Poco::NumberParser::parse(index), pNode));
return findNode(it, end, findElement(Poco::NumberParser::parse(index), pNode, create), create);
}
}
else
@@ -263,29 +287,35 @@ const Poco::XML::Node* XMLConfiguration::findNode(std::string::const_iterator& i
while (it != end && *it == '.') ++it;
std::string key;
while (it != end && *it != '.' && *it != '[') key += *it++;
return findNode(it, end, findElement(key, pNode));
return findNode(it, end, findElement(key, pNode, create), create);
}
}
else return pNode;
}
const Poco::XML::Node* XMLConfiguration::findElement(const std::string& name, const Poco::XML::Node* pNode)
Poco::XML::Node* XMLConfiguration::findElement(const std::string& name, Poco::XML::Node* pNode, bool create)
{
const Poco::XML::Node* pChild = pNode->firstChild();
Poco::XML::Node* pChild = pNode->firstChild();
while (pChild)
{
if (pChild->nodeType() == Poco::XML::Node::ELEMENT_NODE && pChild->nodeName() == name)
return pChild;
pChild = pChild->nextSibling();
}
return 0;
if (create)
{
Poco::AutoPtr<Poco::XML::Element> pElem = pNode->ownerDocument()->createElement(name);
pNode->appendChild(pElem);
return pElem;
}
else return 0;
}
const Poco::XML::Node* XMLConfiguration::findElement(int index, const Poco::XML::Node* pNode)
Poco::XML::Node* XMLConfiguration::findElement(int index, Poco::XML::Node* pNode, bool create)
{
const Poco::XML::Node* pRefNode = pNode;
Poco::XML::Node* pRefNode = pNode;
if (index > 0)
{
pNode = pNode->nextSibling();
@@ -298,17 +328,35 @@ const Poco::XML::Node* XMLConfiguration::findElement(int index, const Poco::XML:
pNode = pNode->nextSibling();
}
}
if (!pNode && create)
{
if (index == 1)
{
Poco::AutoPtr<Poco::XML::Element> pElem = pRefNode->ownerDocument()->createElement(pRefNode->nodeName());
pRefNode->parentNode()->appendChild(pElem);
return pElem;
}
else throw Poco::InvalidArgumentException("Element index out of range.");
}
return pNode;
}
const Poco::XML::Node* XMLConfiguration::findAttribute(const std::string& name, const Poco::XML::Node* pNode)
Poco::XML::Node* XMLConfiguration::findAttribute(const std::string& name, Poco::XML::Node* pNode, bool create)
{
const Poco::XML::Element* pElem = dynamic_cast<const Poco::XML::Element*>(pNode);
Poco::XML::Node* pResult(0);
Poco::XML::Element* pElem = dynamic_cast<Poco::XML::Element*>(pNode);
if (pElem)
return pElem->getAttributeNode(name);
else
return 0;
{
pResult = pElem->getAttributeNode(name);
if (!pResult && create)
{
Poco::AutoPtr<Poco::XML::Attr> pAttr = pNode->ownerDocument()->createAttribute(name);
pElem->setAttributeNode(pAttr);
return pAttr;
}
}
return pResult;
}