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 ...@@ -52,6 +52,9 @@ namespace uniset
/*! конфигурирование xml-файлом ( предпочтительный способ ) */ /*! конфигурирование xml-файлом ( предпочтительный способ ) */
Configuration( int argc, const char* const* argv, const std::string& xmlfile = "" ); 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-файлом */ /*! конфигурирование xml-файлом */
Configuration( int argc, const char* const* argv, std::shared_ptr<ObjectIndex> oind, const std::string& xmlfile = "" ); Configuration( int argc, const char* const* argv, std::shared_ptr<ObjectIndex> oind, const std::string& xmlfile = "" );
...@@ -251,6 +254,7 @@ namespace uniset ...@@ -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, 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 } // end of uniset namespace
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
......
...@@ -139,6 +139,9 @@ namespace uniset ...@@ -139,6 +139,9 @@ namespace uniset
std::string getFileName() const noexcept; std::string getFileName() const noexcept;
void createFromText( const std::string& text );
// Создать новый XML-документ // Создать новый XML-документ
void newDoc( const std::string& root_node, const std::string& xml_ver = "1.0"); void newDoc( const std::string& root_node, const std::string& xml_ver = "1.0");
......
...@@ -104,6 +104,7 @@ namespace uniset ...@@ -104,6 +104,7 @@ namespace uniset
Configuration::Configuration(): Configuration::Configuration():
oind(NULL), oind(NULL),
unixml(nullptr),
// _argc(0), // _argc(0),
// _argv(nullptr), // _argv(nullptr),
NSName("NameService"), NSName("NameService"),
...@@ -165,6 +166,19 @@ namespace uniset ...@@ -165,6 +166,19 @@ namespace uniset
initConfiguration(argc, argv); initConfiguration(argc, argv);
} }
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------
Configuration::Configuration( int argc, const char* const* argv, std::shared_ptr<UniXML> xml ):
oind(nullptr),
unixml(xml),
NSName("NameService"),
repeatCount(2), repeatTimeout(100),
localDBServer(uniset::DefaultObjectId),
localNode(uniset::DefaultObjectId),
localNodeName(""),
fileConfName(xml->getFileName())
{
initConfiguration(argc, argv);
}
// ---------------------------------------------------------------------------------
Configuration::Configuration( int argc, const char* const* argv, const string& fileConf, Configuration::Configuration( int argc, const char* const* argv, const string& fileConf,
uniset::ObjectInfo* omap ): uniset::ObjectInfo* omap ):
oind(NULL), oind(NULL),
...@@ -1053,7 +1067,7 @@ namespace uniset ...@@ -1053,7 +1067,7 @@ namespace uniset
} }
else if( verb_level == _argv[i] ) else if( verb_level == _argv[i] )
{ {
deb->verbose(uniset::uni_atoi(_argv[i+1])); deb->verbose(uniset::uni_atoi(_argv[i + 1]));
} }
} }
...@@ -1472,7 +1486,22 @@ namespace uniset ...@@ -1472,7 +1486,22 @@ namespace uniset
return uniset::uconf; return uniset::uconf;
} }
// -------------------------------------------------------------------------
std::shared_ptr<Configuration> uniset_init( int argc, const char* const* argv, std::shared_ptr<UniXML> xml )
{
if( uniset::uconf )
{
cerr << "Reusable call uniset_init... ignore.." << endl;
return uniset::uconf;
}
// atexit( UniSetActivator::normalexit );
// set_terminate( UniSetActivator::normalterminate ); // ловушка для неизвестных исключений
uniset::uconf = make_shared<Configuration>(argc, argv, xml);
return uniset::uconf;
}
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
......
...@@ -49,7 +49,7 @@ UniXML::UniXML(const string& fname): ...@@ -49,7 +49,7 @@ UniXML::UniXML(const string& fname):
{ {
open(filename); open(filename);
} }
// -----------------------------------------------------------------------------
UniXML::UniXML() UniXML::UniXML()
{ {
} }
...@@ -64,7 +64,7 @@ string UniXML::getFileName() const noexcept ...@@ -64,7 +64,7 @@ string UniXML::getFileName() const noexcept
return filename; 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 не удален из памяти assert(doc == nullptr); // предыдущий doc не удален из памяти
...@@ -107,6 +107,33 @@ UniXML::iterator UniXML::end() noexcept ...@@ -107,6 +107,33 @@ UniXML::iterator UniXML::end() noexcept
return iterator(NULL); 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 ) void UniXML::open( const string& _filename )
{ {
assert( doc == nullptr ); // предыдущий doc не удален из памяти assert( doc == nullptr ); // предыдущий doc не удален из памяти
......
...@@ -210,3 +210,30 @@ TEST_CASE("UniXML::iterator::getPropList", "[unixml][iterator-proplist][basic]" ...@@ -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