Blame view

src/XMLParameterConfigurator/XMLParameterConfigurator.cc 3.24 KB
fbe3c2bb   Benjamin Renard   First commit
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
/**
 * XMLParameterConfigurator.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 "NodeCfg.hh"
#include "Constant.hh"

#include "ParamNode.hh"
#include "XMLParameterConfigurator.hh"
#include "Config.hh"

using namespace log4cxx;
using namespace AMDA::XMLConfigurator;

// Global variable

namespace AMDA
{
  namespace XMLParameterConfigurator
  {
    log4cxx::LoggerPtr gLogger = log4cxx::Logger::getLogger("AMDA-Kernel.XMLParameterConfigurator");

    XMLParameterConfigurator::XMLParameterConfigurator(const char* pXSDFile) : XMLConfigurator(pXSDFile, true)
    {
      LOG4CXX_DEBUG(gLogger, "XMLParameterConfigurator Constructor")
		getXmlConfiguratorMap()["param"] = RootNodeCfgSPtr(new ParamNode);
    }

    XMLParameterConfigurator::~XMLParameterConfigurator()
    {
    }

ec71f3db   brenard   Return a null poi...
53
bool XMLParameterConfigurator::proceed(const char* filename,
48d596b2   brenard   Do not log an err...
54
		const AMDA::Parameters::CfgContext& pCtx, bool optionalFile) {
fbe3c2bb   Benjamin Renard   First commit
55
56
57
58
59
60
61
	xmlDocPtr lDoc = NULL;
	xmlNodePtr lRoot = NULL;
	std::string configurationFileName = this->getParamPath() + "/" + filename
			+ ".xml";
	LOG4CXX_DEBUG(gLogger, "Filename to parse: " << configurationFileName)
	try {
		if (access(configurationFileName.c_str(), F_OK) != 0) {
48d596b2   brenard   Do not log an err...
62
63
			if (optionalFile) {
				LOG4CXX_WARN(gLogger, "Cannot find file: " << configurationFileName)
ec71f3db   brenard   Return a null poi...
64
				return false;
48d596b2   brenard   Do not log an err...
65
			}
fbe3c2bb   Benjamin Renard   First commit
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
			ERROR_EXCEPTION( ERROR_FILE_NOT_EXIST << configurationFileName);
		}

		if (!(lDoc = xmlParseFile(configurationFileName.c_str()))) {
			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);
		}
		AMDA::Parameters::CfgContext lContext(pCtx);
		lContext.push<std::string *>(&configurationFileName);

		// 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)) {
			cur->ns = lNsXML;
			ERROR_EXCEPTION(ERROR_DOC_NOT_VALID << " XSD file: " << _XSDFile);
		}
		cur->ns = lNsXML;

		RootNodeCfgMap::iterator it = getXmlConfiguratorMap().find(
				std::string((const char*) lRoot->name));
		if (it != getXmlConfiguratorMap().end()) {
			it->second->proceed(lRoot, lContext);
		} else {
			ERROR_EXCEPTION( ERROR_ROOT_NODE_NOT_SUPPORTED << lRoot->name);
		}
	} catch (...) {
		if (lDoc) {	xmlFreeDoc(lDoc); }
		throw;
	}
	if (lDoc) {	xmlFreeDoc(lDoc); }
ec71f3db   brenard   Return a null poi...
119
	return true;
fbe3c2bb   Benjamin Renard   First commit
120
121
122
123
}

   } /* namespace  XMLParameterConfigurator*/
} /* namespace AMDA */