mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-29 12:45:22 +01:00
made XMLConfiguration fully writable
This commit is contained in:
parent
0b6fd120c5
commit
98eb0eb532
@ -1,7 +1,7 @@
|
||||
//
|
||||
// XMLConfiguration.h
|
||||
//
|
||||
// $Id: //poco/svn/Util/include/Poco/Util/XMLConfiguration.h#1 $
|
||||
// $Id: //poco/Main/Util/include/Poco/Util/XMLConfiguration.h#4 $
|
||||
//
|
||||
// Library: Util
|
||||
// Package: Configuration
|
||||
@ -9,7 +9,7 @@
|
||||
//
|
||||
// Definition of the XMLConfiguration class.
|
||||
//
|
||||
// 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
|
||||
@ -44,9 +44,9 @@
|
||||
#include "Poco/Util/MapConfiguration.h"
|
||||
#include "Poco/DOM/Document.h"
|
||||
#include "Poco/DOM/AutoPtr.h"
|
||||
#include "Poco/DOM/DOMWriter.h"
|
||||
#include "Poco/SAX/InputSource.h"
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@ -87,10 +87,6 @@ class Util_API XMLConfiguration: public AbstractConfiguration
|
||||
///
|
||||
/// Enumerating attributes is not supported.
|
||||
/// Calling keys("prop3.prop4") will return an empty range.
|
||||
///
|
||||
/// Setting properties is supported, with the restriction that only
|
||||
/// the value of existing properties can be changed.
|
||||
/// There is currently no way to programmatically add properties.
|
||||
{
|
||||
public:
|
||||
XMLConfiguration();
|
||||
@ -133,14 +129,30 @@ public:
|
||||
void load(const Poco::XML::Node* pNode);
|
||||
/// Loads the XML document containing the configuration data
|
||||
/// from the given XML node.
|
||||
|
||||
void loadEmpty(const std::string& rootElementName);
|
||||
/// Loads an empty XML document containing only the
|
||||
/// root element with the given name.
|
||||
|
||||
void save(const std::string& path) const;
|
||||
/// Writes the XML document containing the configuration data
|
||||
/// to the file given by path.
|
||||
|
||||
void save(std::ostream& ostr);
|
||||
void save(std::ostream& str) const;
|
||||
/// Writes the XML document containing the configuration data
|
||||
/// to the given stream.
|
||||
|
||||
void save(const std::string& path);
|
||||
|
||||
void save(Poco::XML::DOMWriter& writer, const std::string& path) const;
|
||||
/// Writes the XML document containing the configuration data
|
||||
/// to the given file.
|
||||
/// to the file given by path, using the given DOMWriter.
|
||||
///
|
||||
/// This can be used to use a DOMWriter with custom options.
|
||||
|
||||
void save(Poco::XML::DOMWriter& writer, std::ostream& str) const;
|
||||
/// Writes the XML document containing the configuration data
|
||||
/// to the given stream.
|
||||
///
|
||||
/// This can be used to use a DOMWriter with custom options.
|
||||
|
||||
protected:
|
||||
bool getRaw(const std::string& key, std::string& value) const;
|
||||
@ -150,10 +162,10 @@ protected:
|
||||
|
||||
private:
|
||||
const Poco::XML::Node* findNode(const std::string& key) const;
|
||||
static const Poco::XML::Node* findNode(std::string::const_iterator& it, const std::string::const_iterator& end, const Poco::XML::Node* pNode);
|
||||
static const Poco::XML::Node* findElement(const std::string& name, const Poco::XML::Node* pNode);
|
||||
static const Poco::XML::Node* findElement(int index, const Poco::XML::Node* pNode);
|
||||
static const Poco::XML::Node* findAttribute(const std::string& name, const Poco::XML::Node* pNode);
|
||||
static Poco::XML::Node* findNode(std::string::const_iterator& it, const std::string::const_iterator& end, Poco::XML::Node* pNode, bool create = false);
|
||||
static Poco::XML::Node* findElement(const std::string& name, Poco::XML::Node* pNode, bool create);
|
||||
static Poco::XML::Node* findElement(int index, Poco::XML::Node* pNode, bool create);
|
||||
static Poco::XML::Node* findAttribute(const std::string& name, Poco::XML::Node* pNode, bool create);
|
||||
|
||||
Poco::XML::AutoPtr<Poco::XML::Node> _pRoot;
|
||||
Poco::XML::AutoPtr<Poco::XML::Document> _pDocument;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
//
|
||||
// XMLConfigurationTest.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Util/testsuite/src/XMLConfigurationTest.cpp#5 $
|
||||
// $Id: //poco/Main/Util/testsuite/src/XMLConfigurationTest.cpp#7 $
|
||||
//
|
||||
// Copyright (c) 2004-200, 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
|
||||
@ -95,36 +95,6 @@ void XMLConfigurationTest::testLoad()
|
||||
assert (keys.size() == 2);
|
||||
assert (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
|
||||
assert (std::find(keys.begin(), keys.end(), "prop4[1]") != keys.end());
|
||||
|
||||
pConf->setString("prop1", "value1_changed");
|
||||
assert (pConf->getString("prop1") == "value1_changed");
|
||||
|
||||
pConf->setString("prop2", "value2_changed");
|
||||
assert (pConf->getString("prop2") == "value2_changed");
|
||||
|
||||
pConf->setString("prop3.prop4[@attr]", "value3_changed");
|
||||
assert (pConf->getString("prop3.prop4[@attr]") == "value3_changed");
|
||||
|
||||
pConf->setString("prop3.prop4[1][@attr]", "value4_changed");
|
||||
assert (pConf->getString("prop3.prop4[1][@attr]") == "value4_changed");
|
||||
|
||||
pConf->setString("prop5", "value5_changed");
|
||||
assert (pConf->getString("prop5") == "value5_changed");
|
||||
|
||||
pConf->setString("prop5[0]", "value5_changed");
|
||||
assert (pConf->getString("prop5[0]") == "value5_changed");
|
||||
|
||||
pConf->setString("prop5[1]", "value5_changed");
|
||||
assert (pConf->getString("prop5[1]") == "value5_changed");
|
||||
|
||||
try
|
||||
{
|
||||
pConf->setString("foo", "bar");
|
||||
fail("node not found - must throw");
|
||||
}
|
||||
catch (NotFoundException&)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
@ -137,6 +107,89 @@ void XMLConfigurationTest::testLoad()
|
||||
}
|
||||
|
||||
|
||||
void XMLConfigurationTest::testSave()
|
||||
{
|
||||
AutoPtr<XMLConfiguration> pConf = new XMLConfiguration;
|
||||
pConf->loadEmpty("config");
|
||||
|
||||
std::ostringstream ostr;
|
||||
pConf->save(ostr);
|
||||
std::string s(ostr.str());
|
||||
assert (s == "<config/>\n");
|
||||
|
||||
pConf->setString("prop1", "value1");
|
||||
assert (pConf->getString("prop1") == "value1");
|
||||
|
||||
pConf->setString("prop2", "value2");
|
||||
assert (pConf->getString("prop2") == "value2");
|
||||
|
||||
pConf->setString("prop3.prop4[@attr]", "value3");
|
||||
assert (pConf->getString("prop3.prop4[@attr]") == "value3");
|
||||
|
||||
pConf->setString("prop3.prop4[1][@attr]", "value4");
|
||||
assert (pConf->getString("prop3.prop4[1][@attr]") == "value4");
|
||||
|
||||
pConf->setString("prop5", "value5a");
|
||||
assert (pConf->getString("prop5") == "value5a");
|
||||
|
||||
pConf->setString("prop5[0]", "value5");
|
||||
assert (pConf->getString("prop5[0]") == "value5");
|
||||
assert (pConf->getString("prop5") == "value5");
|
||||
|
||||
pConf->setString("prop5[1]", "value6");
|
||||
assert (pConf->getString("prop5[1]") == "value6");
|
||||
|
||||
try
|
||||
{
|
||||
pConf->setString("prop5[3]", "value7");
|
||||
fail("bad index - must throw");
|
||||
}
|
||||
catch (Poco::InvalidArgumentException&)
|
||||
{
|
||||
}
|
||||
|
||||
std::ostringstream ostr2;
|
||||
pConf->save(ostr2);
|
||||
s = ostr2.str();
|
||||
|
||||
assert (s ==
|
||||
"<config>\n"
|
||||
"\t<prop1>value1</prop1>\n"
|
||||
"\t<prop2>value2</prop2>\n"
|
||||
"\t<prop3>\n"
|
||||
"\t\t<prop4 attr=\"value3\"/>\n"
|
||||
"\t\t<prop4 attr=\"value4\"/>\n"
|
||||
"\t</prop3>\n"
|
||||
"\t<prop5>value5</prop5>\n"
|
||||
"\t<prop5>value6</prop5>\n"
|
||||
"</config>\n");
|
||||
|
||||
pConf->setString("prop1", "value11");
|
||||
assert (pConf->getString("prop1") == "value11");
|
||||
|
||||
pConf->setString("prop2", "value21");
|
||||
assert (pConf->getString("prop2") == "value21");
|
||||
|
||||
pConf->setString("prop3.prop4[1][@attr]", "value41");
|
||||
assert (pConf->getString("prop3.prop4[1][@attr]") == "value41");
|
||||
|
||||
std::ostringstream ostr3;
|
||||
pConf->save(ostr3);
|
||||
s = ostr3.str();
|
||||
assert (s ==
|
||||
"<config>\n"
|
||||
"\t<prop1>value11</prop1>\n"
|
||||
"\t<prop2>value21</prop2>\n"
|
||||
"\t<prop3>\n"
|
||||
"\t\t<prop4 attr=\"value3\"/>\n"
|
||||
"\t\t<prop4 attr=\"value41\"/>\n"
|
||||
"\t</prop3>\n"
|
||||
"\t<prop5>value5</prop5>\n"
|
||||
"\t<prop5>value6</prop5>\n"
|
||||
"</config>\n");
|
||||
}
|
||||
|
||||
|
||||
void XMLConfigurationTest::setUp()
|
||||
{
|
||||
}
|
||||
@ -152,6 +205,7 @@ CppUnit::Test* XMLConfigurationTest::suite()
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("XMLConfigurationTest");
|
||||
|
||||
CppUnit_addTest(pSuite, XMLConfigurationTest, testLoad);
|
||||
CppUnit_addTest(pSuite, XMLConfigurationTest, testSave);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// XMLConfigurationTest.h
|
||||
//
|
||||
// $Id: //poco/svn/Util/testsuite/src/XMLConfigurationTest.h#1 $
|
||||
// $Id: //poco/Main/Util/testsuite/src/XMLConfigurationTest.h#5 $
|
||||
//
|
||||
// Definition of the XMLConfigurationTest class.
|
||||
//
|
||||
@ -47,6 +47,7 @@ public:
|
||||
~XMLConfigurationTest();
|
||||
|
||||
void testLoad();
|
||||
void testSave();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
Loading…
x
Reference in New Issue
Block a user