Commit 573defc9 authored by Pavel Vainerman's avatar Pavel Vainerman Committed by Pavel Vainerman

[conf]: supported external xml for configuration, supported create xml from text

parent 2455feab
......@@ -52,6 +52,9 @@ namespace uniset
/*! конфигурирование xml-файлом ( предпочтительный способ ) */
Configuration( int argc, const char* const* argv, const std::string& xmlfile = "" );
/*! конфигурирование внешним xml */
Configuration( int argc, const char* const* argv, std::shared_ptr<UniXML> xml );
/*! конфигурирование xml-файлом */
Configuration( int argc, const char* const* argv, std::shared_ptr<ObjectIndex> oind, const std::string& xmlfile = "" );
......@@ -251,6 +254,7 @@ namespace uniset
/*! инициализация "глобальной" конфигурации */
std::shared_ptr<Configuration> uniset_init( int argc, const char* const* argv, const std::string& xmlfile = "configure.xml" );
std::shared_ptr<Configuration> uniset_init( int argc, const char* const* argv, std::shared_ptr<UniXML> xml );
// --------------------------------------------------------------------------
} // end of uniset namespace
// --------------------------------------------------------------------------
......
......@@ -139,6 +139,9 @@ namespace uniset
std::string getFileName() const noexcept;
void createFromText( const std::string& text );
// Создать новый XML-документ
void newDoc( const std::string& root_node, const std::string& xml_ver = "1.0");
......
......@@ -49,7 +49,7 @@ UniXML::UniXML(const string& fname):
{
open(filename);
}
// -----------------------------------------------------------------------------
UniXML::UniXML()
{
}
......@@ -64,7 +64,7 @@ string UniXML::getFileName() const noexcept
return filename;
}
// -----------------------------------------------------------------------------
void UniXML::newDoc(const string& root_node, const string& xml_ver)
void UniXML::newDoc( const string& root_node, const string& xml_ver )
{
assert(doc == nullptr); // предыдущий doc не удален из памяти
......@@ -107,6 +107,33 @@ UniXML::iterator UniXML::end() noexcept
return iterator(NULL);
}
// -----------------------------------------------------------------------------
static void suppress(void *ctx, const char *msg, ...)
{
}
// -----------------------------------------------------------------------------
void UniXML::createFromText( const std::string& text )
{
assert(doc == nullptr); // предыдущий doc не удален из памяти
// hide console errors
// xmlSetGenericErrorFunc(NULL,suppress);
xmlKeepBlanksDefault(0);
// Can read files in any encoding, recode to UTF-8 internally
xmlDoc* d = xmlParseDoc((const xmlChar*)text.c_str());
if( d == NULL )
throw SystemError("UniXML(createFromText): parse error");
doc = std::unique_ptr<xmlDoc, UniXMLDocDeleter>(d);
// Support for XInclude (see eterbug #6304)
// main tag must to have follow property: xmlns:xi="http://www.w3.org/2001/XInclude"
//For include: <xi:include href="test2.xml"/>
xmlXIncludeProcess(doc.get());
}
// -----------------------------------------------------------------------------
void UniXML::open( const string& _filename )
{
assert( doc == nullptr ); // предыдущий doc не удален из памяти
......
......@@ -210,3 +210,30 @@ TEST_CASE("UniXML::iterator::getPropList", "[unixml][iterator-proplist][basic]"
}
}
// -----------------------------------------------------------------------------
TEST_CASE("UniXML createFromText", "[unixml][createFromText]" )
{
UniXML uxml;
CHECK_FALSE( uxml.isOpen() );
const string text = \
"<?xml version=\"1.0\" encoding=\"utf-8\"?> \
<TestConf xmlns:xi=\"http://www.w3.org/2001/XInclude\"> \
<UserData/> \
<UniSet> \
</UniSet> \
</TestConf>";
REQUIRE_NOTHROW(uxml.createFromText(text));
CHECK( uxml.isOpen() );
xmlNode* cnode = uxml.findNode(uxml.getFirstNode(), "UniSet");
CHECK( cnode != NULL );
uxml.close();
CHECK_FALSE( uxml.isOpen() );
const string badtext = "<?xml version=";
REQUIRE_THROWS_AS(uxml.createFromText(badtext), uniset::SystemError& );
}
// -----------------------------------------------------------------------------
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment