mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-26 18:11:29 +02:00
XMLConfiguration now supports modifying properties (contributed by Tom Tan)
This commit is contained in:
parent
78349bba1f
commit
f716fdc5d6
@ -9,7 +9,7 @@
|
|||||||
//
|
//
|
||||||
// Definition of the XMLConfiguration class.
|
// Definition of the XMLConfiguration class.
|
||||||
//
|
//
|
||||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
|
||||||
// and Contributors.
|
// and Contributors.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person or organization
|
// Permission is hereby granted, free of charge, to any person or organization
|
||||||
@ -46,6 +46,7 @@
|
|||||||
#include "Poco/DOM/AutoPtr.h"
|
#include "Poco/DOM/AutoPtr.h"
|
||||||
#include "Poco/SAX/InputSource.h"
|
#include "Poco/SAX/InputSource.h"
|
||||||
#include <istream>
|
#include <istream>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
|
|
||||||
namespace Poco {
|
namespace Poco {
|
||||||
@ -87,8 +88,9 @@ class Util_API XMLConfiguration: public AbstractConfiguration
|
|||||||
/// Enumerating attributes is not supported.
|
/// Enumerating attributes is not supported.
|
||||||
/// Calling keys("prop3.prop4") will return an empty range.
|
/// Calling keys("prop3.prop4") will return an empty range.
|
||||||
///
|
///
|
||||||
/// Setting properties is not supported. An attempt to set a property results
|
/// Setting properties is supported, with the restriction that only
|
||||||
/// in a NotImplementedException being thrown.
|
/// the value of existing properties can be changed.
|
||||||
|
/// There is currently no way to programmatically add properties.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
XMLConfiguration();
|
XMLConfiguration();
|
||||||
@ -132,6 +134,14 @@ public:
|
|||||||
/// Loads the XML document containing the configuration data
|
/// Loads the XML document containing the configuration data
|
||||||
/// from the given XML node.
|
/// from the given XML node.
|
||||||
|
|
||||||
|
void save(std::ostream& ostr);
|
||||||
|
/// Writes the XML document containing the configuration data
|
||||||
|
/// to the given stream.
|
||||||
|
|
||||||
|
void save(const std::string& path);
|
||||||
|
/// Writes the XML document containing the configuration data
|
||||||
|
/// to the given file.
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool getRaw(const std::string& key, std::string& value) const;
|
bool getRaw(const std::string& key, std::string& value) const;
|
||||||
void setRaw(const std::string& key, const std::string& value);
|
void setRaw(const std::string& key, const std::string& value);
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
//
|
//
|
||||||
// XMLConfiguration.cpp
|
// XMLConfiguration.cpp
|
||||||
//
|
//
|
||||||
// $Id: //poco/svn/Util/src/XMLConfiguration.cpp#1 $
|
// $Id: //poco/1.3/Util/src/XMLConfiguration.cpp#1 $
|
||||||
//
|
//
|
||||||
// Library: Util
|
// Library: Util
|
||||||
// Package: Configuration
|
// Package: Configuration
|
||||||
// Module: XMLConfiguration
|
// Module: XMLConfiguration
|
||||||
//
|
//
|
||||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
|
||||||
// and Contributors.
|
// and Contributors.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person or organization
|
// Permission is hereby granted, free of charge, to any person or organization
|
||||||
@ -39,6 +39,10 @@
|
|||||||
#include "Poco/DOM/DOMParser.h"
|
#include "Poco/DOM/DOMParser.h"
|
||||||
#include "Poco/DOM/Element.h"
|
#include "Poco/DOM/Element.h"
|
||||||
#include "Poco/DOM/Attr.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"
|
#include "Poco/Exception.h"
|
||||||
#include "Poco/NumberParser.h"
|
#include "Poco/NumberParser.h"
|
||||||
#include "Poco/NumberFormatter.h"
|
#include "Poco/NumberFormatter.h"
|
||||||
@ -140,6 +144,24 @@ void XMLConfiguration::load(const Poco::XML::Node* pNode)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void XMLConfiguration::save(const std::string& path)
|
||||||
|
{
|
||||||
|
Poco::XML::DOMWriter writer;
|
||||||
|
writer.setNewLine("\n");
|
||||||
|
writer.setOptions(Poco::XML::XMLWriter::PRETTY_PRINT);
|
||||||
|
writer.writeNode(path, _pDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void XMLConfiguration::save(std::ostream& ostr)
|
||||||
|
{
|
||||||
|
Poco::XML::DOMWriter writer;
|
||||||
|
writer.setNewLine("\n");
|
||||||
|
writer.setOptions(Poco::XML::XMLWriter::PRETTY_PRINT);
|
||||||
|
writer.writeNode(ostr, _pDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool XMLConfiguration::getRaw(const std::string& key, std::string& value) const
|
bool XMLConfiguration::getRaw(const std::string& key, std::string& value) const
|
||||||
{
|
{
|
||||||
const Poco::XML::Node* pNode = findNode(key);
|
const Poco::XML::Node* pNode = findNode(key);
|
||||||
@ -154,7 +176,28 @@ bool XMLConfiguration::getRaw(const std::string& key, std::string& value) const
|
|||||||
|
|
||||||
void XMLConfiguration::setRaw(const std::string& key, const std::string& value)
|
void XMLConfiguration::setRaw(const std::string& key, const std::string& value)
|
||||||
{
|
{
|
||||||
throw Poco::NotImplementedException("Setting a property in an XMLConfiguration");
|
Poco::XML::Node* pNode = const_cast<Poco::XML::Node*>(findNode(key));
|
||||||
|
|
||||||
|
if (pNode)
|
||||||
|
{
|
||||||
|
unsigned short nodeType = pNode->nodeType();
|
||||||
|
if (Poco::XML::Node::ATTRIBUTE_NODE == nodeType)
|
||||||
|
{
|
||||||
|
pNode->setNodeValue(value);
|
||||||
|
}
|
||||||
|
else if (Poco::XML::Node::ELEMENT_NODE == nodeType)
|
||||||
|
{
|
||||||
|
Poco::XML::Node* pChildNode = pNode->firstChild();
|
||||||
|
if (pChildNode)
|
||||||
|
{
|
||||||
|
if (Poco::XML::Node::TEXT_NODE == pChildNode->nodeType())
|
||||||
|
{
|
||||||
|
pChildNode->setNodeValue(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else throw NotFoundException("Node not found in XMLConfiguration", key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
//
|
//
|
||||||
// XMLConfigurationTest.cpp
|
// XMLConfigurationTest.cpp
|
||||||
//
|
//
|
||||||
// $Id: //poco/svn/Util/testsuite/src/XMLConfigurationTest.cpp#1 $
|
// $Id: //poco/Main/Util/testsuite/src/XMLConfigurationTest.cpp#5 $
|
||||||
//
|
//
|
||||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
// Copyright (c) 2004-200, Applied Informatics Software Engineering GmbH.
|
||||||
// and Contributors.
|
// and Contributors.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person or organization
|
// Permission is hereby granted, free of charge, to any person or organization
|
||||||
@ -95,13 +95,34 @@ void XMLConfigurationTest::testLoad()
|
|||||||
assert (keys.size() == 2);
|
assert (keys.size() == 2);
|
||||||
assert (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
|
assert (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
|
||||||
assert (std::find(keys.begin(), keys.end(), "prop4[1]") != 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
|
try
|
||||||
{
|
{
|
||||||
pConf->setString("foo", "bar");
|
pConf->setString("foo", "bar");
|
||||||
fail("Not supported - must throw");
|
fail("node not found - must throw");
|
||||||
}
|
}
|
||||||
catch (NotImplementedException&)
|
catch (NotFoundException&)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user