XMLConfigurator.cc
2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* XMLConfigurator.cc
*
* Created on: 17 oct. 2012
* Author: AKKA IS
*/
#include <iostream>
#include <sstream>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
#include "log4cxx/logger.h"
// XML include
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xmlstring.h>
#include <libxml/xmlschemas.h>
#include "Config.hh"
#include "NodeCfg.hh"
#include "XMLConfigurator.hh"
#include "Constant.hh"
using namespace log4cxx;
// Global variable
namespace AMDA
{
namespace XMLConfigurator
{
XMLConfigurator::XMLConfigurator(const char* pXSDFile, bool isUsedXmlId) : AMDA::Parameters::FileConfigurator(),
_isUsedXmlId(isUsedXmlId)
{
_XSDFile = new char[strlen(pXSDFile)+1];
strcpy(_XSDFile,pXSDFile);
}
XMLConfigurator::~XMLConfigurator()
{
delete [] _XSDFile;
}
bool XMLConfigurator::proceed(const char* pFilename, const AMDA::Parameters::CfgContext& pCtx, bool optionalFile = false) {
xmlDocPtr lDoc = NULL;
xmlNodePtr lRoot = NULL;
LOG4CXX_DEBUG(gLogger, "Filename to parse: " << pFilename)
try {
if (access(pFilename, F_OK) != 0) {
if (optionalFile) {
LOG4CXX_WARN(gLogger, "Cannot find file: " << pFilename)
return false;
}
ERROR_EXCEPTION( ERROR_FILE_NOT_EXIST << pFilename);
}
if (!(lDoc = xmlParseFile(pFilename))) {
ERROR_EXCEPTION( ERROR_BAD_XML_FORMAT);
}
// const char* lParamerterXSD = "DataBaseParameters/xsd/parameter.xsd";
// if ( ! is_valid( lDoc, lParamerterXSD)) {
// ERROR_EXCEPTION( ERROR_DOC_NOT_VALID << " XSD file: " << lParamerterXSD);
// }
if (!(lRoot = xmlDocGetRootElement(lDoc))) {
ERROR_EXCEPTION( ERROR_EMPTY_DOCUMENT);
}
if (_isUsedXmlId)
{
// XML_XML_ID case
xmlNs* lNsXML = NULL;
xmlAttrPtr cur = lRoot->properties;
while (cur && !lNsXML) {
if ((xmlStrEqual(cur->name, (const xmlChar *) "id") == 1) && cur->ns
&& (xmlStrEqual(cur->ns->prefix, (const xmlChar *) "xml")
== 1)) {
lNsXML = cur->ns;
cur->ns = NULL;
} else {
cur = cur->next;
}
}
if (!lNsXML) {
ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << lRoot->name << "@" << XML_XML_ID)
}
}
if (NodeCfg::getIsValid(lRoot, _XSDFile) != 1) {
ERROR_EXCEPTION(ERROR_DOC_NOT_VALID << " XSD file: " << _XSDFile);
}
RootNodeCfgMap::iterator it = _xmlConfiguratorMap.find( std::string((const char*) lRoot->name));
if (it != _xmlConfiguratorMap.end()) {
it->second->proceed(lRoot, pCtx);
} else {
ERROR_EXCEPTION( ERROR_ROOT_NODE_NOT_SUPPORTED << lRoot->name);
}
} catch (...) {
if (lDoc) { xmlFreeDoc(lDoc); }
throw;
}
if (lDoc) { xmlFreeDoc(lDoc); }
return true;
}
void XMLConfigurator::addNodeParser( const char* pXPath, NodeCfgSPtr pNode) {
NodeGrpCfg::addNodeParser( &getXmlConfiguratorMap(), pXPath, pNode);
}
} /* namespace XMLConfigurator*/
} /* namespace AMDA */