XMLConfigurator.cc 2.91 KB
/**
 * 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 */