/* * * ProLinga-Validate * * Copyright (C) 2002-2008 Xobas Software. * All rights reserved. * * This file is part of ProLinga-Validate. * * ProLinga-Validate is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ProLinga-Validate is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ProLinga-Validate. If not, see . * * More information is available at the following addresses: * * Website : http://www.prolinga.org * * Email : prolinga-list@prolinga.org * * */ #include "ValCommon.h" #include #include #include #include "ValConfig.hpp" #include "ValMain.hpp" ConfigVal::ConfigVal(const char *configFileName) { xmlDocPtr docConf; xmlNodePtr curConf; xmlXPathContextPtr ctxConf; xmlXPathObjectPtr resConf; char productPath[255], configValue[255]; /* Init */ startPtr = new ConfigValSetting; /* Parsing file */ docConf = xmlParseFile(configFileName); if (docConf == NULL) { fprintf(stderr, "Error parsing config file. \n"); xmlFreeDoc(docConf); return; } curConf = xmlDocGetRootElement(docConf); if (curConf == NULL) { fprintf(stderr, "Empty document. \n"); xmlFreeDoc(docConf); return; } if (xmlStrcmp(curConf->name, (const xmlChar *)"ProLinga")) { fprintf(stderr, "Invalid Config File"); xmlFreeDoc(docConf); return; } /* Init Xpath */ xmlXPathInit(); /* Create XPath environment */ ctxConf = xmlXPathNewContext(docConf); /* Create config path Client */ strcpy(productPath, "/ProLinga/Configuration/Service[@Name='Validate']/Client/"); /* Get Client host name/ip */ strcpy(configValue, productPath); strcat(configValue, "ClientHost"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putStringValue(&startPtr, "ClientHost", (char *)xmlXPathCastToString(resConf)); /* Get Client port number */ strcpy(configValue, productPath); strcat(configValue, "ClientPort"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putNumberValue(&startPtr, "ClientPort", (int)xmlXPathCastToNumber(resConf)); /* Get Client Compression Level */ strcpy(configValue, productPath); strcat(configValue, "ClientCompressionLevel"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putNumberValue(&startPtr, "ClientCompressionLevel", (int)xmlXPathCastToNumber(resConf)); /* Create config path Server */ strcpy(productPath, "/ProLinga/Configuration/Service[@Name='Validate']/Server/"); /* Get Server host name/ip */ strcpy(configValue, productPath); strcat(configValue, "ServerHost"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putStringValue(&startPtr, "ServerHost", (char *)xmlXPathCastToString(resConf)); /* Get Server port number */ strcpy(configValue, productPath); strcat(configValue, "ServerPort"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putNumberValue(&startPtr, "ServerPort", (int)xmlXPathCastToNumber(resConf)); /* Number Of Threads */ strcpy(configValue, productPath); strcat(configValue, "NumberOfThreads"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putNumberValue(&startPtr, "NumberOfThreads", (int)xmlXPathCastToNumber(resConf)); /* Get Log file */ strcpy(configValue, productPath); strcat(configValue, "LogFile"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putStringValue(&startPtr, "LogFile", (char *)xmlXPathCastToString(resConf)); /* Get Log Level */ strcpy(configValue, productPath); strcat(configValue, "LogLevel"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putNumberValue(&startPtr, "LogLevel", (int)xmlXPathCastToNumber(resConf)); /* Get Server Compression Level */ strcpy(configValue, productPath); strcat(configValue, "ServerCompressionLevel"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putNumberValue(&startPtr, "ServerCompressionLevel", (int)xmlXPathCastToNumber(resConf)); /* Get Language Validation File */ strcpy(configValue, productPath); strcat(configValue, "LanguageValidationFile"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putStringValue(&startPtr, "LanguageValidationFile", (char *)xmlXPathCastToString(resConf)); /* Get Repository host name/ip */ strcpy(configValue, productPath); strcat(configValue, "RepositoryHost"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putStringValue(&startPtr, "RepositoryHost", (char *)xmlXPathCastToString(resConf)); /* Get Repository port number */ strcpy(configValue, productPath); strcat(configValue, "RepositoryPort"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putNumberValue(&startPtr, "RepositoryPort", (int)xmlXPathCastToNumber(resConf)); /* Get Repository Compression setting */ strcpy(configValue, productPath); strcat(configValue, "RepositoryCompressionLevel"); resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf); if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0) startPtr->putNumberValue(&startPtr, "RepositoryCompressionLevel", (int)xmlXPathCastToNumber(resConf)); /* Clean up */ xmlXPathFreeContext(ctxConf); xmlFreeDoc(docConf); } ConfigVal::~ConfigVal() { if (startPtr != NULL) startPtr->deleteList(&startPtr); } char *ConfigVal::getStringValue(const char *name, const char *defValue) { return startPtr->getStringValue(startPtr, name, defValue); } int ConfigVal::getNumberValue(const char *name, const int defValue) { return startPtr->getNumberValue(startPtr, name, defValue); } void ConfigVal::putStringValue(const char *name, const char *value) { startPtr->putStringValue(&startPtr, name, value); } void ConfigVal::putNumberValue(const char *name, const int value) { startPtr->putNumberValue(&startPtr, name, value); } void ConfigVal::writeConfig(const char *logFileName) const { FILE *log; time_t t; time(&t); /* Open log file */ log = fopen(startPtr->getStringValue(startPtr, "LogFile", DEF_LOG_FILE), "a"); /* Write settings */ fprintf(log,"-----------------------------------------------------------------\n"); fprintf(log, "Configuration ProLinga-Validate Server - %s \n", ctime(&t)); fprintf(log, "Log File : %s\n", startPtr->getStringValue(startPtr, "LogFile", DEF_LOG_FILE)); fprintf(log, "Log Level : %d\n", startPtr->getNumberValue(startPtr, "LogLevel", DEF_LOG_LEVEL)); fprintf(log, "Server Host : %s\n", startPtr->getStringValue(startPtr, "ServerHost", DEF_SERVER_HOST)); fprintf(log, "Server Port : %d\n", startPtr->getNumberValue(startPtr, "ServerPort", DEF_SERVER_PORT)); fprintf(log, "Number of Threads : %d\n", startPtr->getNumberValue(startPtr, "NumberOfThreads", DEF_THREADS)); fprintf(log, "Language Validation File : %s\n", startPtr->getStringValue(startPtr, "LanguageValidationFile", DEF_SYNTAX_FILE)); fprintf(log, "Repository Host : %s\n", startPtr->getStringValue(startPtr, "RepositoryHost", DEF_REPOSITORY_HOST)); fprintf(log, "Repository Port : %d\n", startPtr->getNumberValue(startPtr, "RepositoryPort", DEF_REPOSITORY_PORT)); fprintf(log, "Repository Compression : %d\n", startPtr->getNumberValue (startPtr, "RepositoryCompressionLevel", DEF_REPOSITORY_COMP_LEVEL)); fprintf(log,"-----------------------------------------------------------------\n"); /*Close log file */ fclose(log); } void ConfigVal::printList(void) const { startPtr->printList(startPtr); } ConfigValSetting::ConfigValSetting() { strcpy(confsetName, ""); confsetStringValue = NULL; confsetNumberValue = 0; nextPtr = NULL; } ConfigValSetting::~ConfigValSetting() { /* Nothing ? */ } char *ConfigValSetting::getStringValue(ConfigValSettingPtr confsetPtr, const char *name, const char *defValue) { ConfigValSettingPtr curPtr; /* Get value from memory */ curPtr = confsetPtr; while (curPtr != NULL) { if (strcmp(curPtr->confsetName, name) == 0) return curPtr->confsetStringValue; curPtr = curPtr->nextPtr; } return (char *)defValue; } int ConfigValSetting::getNumberValue(ConfigValSettingPtr confsetPtr, const char *name, const int defValue) { ConfigValSettingPtr curPtr; /* Get value from memory */ curPtr = confsetPtr; while (curPtr != NULL) { if (strcmp(curPtr->confsetName, name) == 0) return curPtr->confsetNumberValue; curPtr = curPtr->nextPtr; } return defValue; } void ConfigValSetting::putStringValue(ConfigValSettingPtr *confsetPtr, const char *name, const char *value) { ConfigValSettingPtr curPtr, newPtr; /* First check if already in list (update) */ curPtr = *confsetPtr; while (curPtr != NULL) { if (strcmp(curPtr->confsetName, name) == 0) { if (curPtr->confsetStringValue != NULL) delete curPtr->confsetStringValue; curPtr->confsetStringValue = new char[(strlen(value)+1)]; strcpy(curPtr->confsetStringValue, value); return; } curPtr = curPtr->nextPtr; } /* Create new Config instance */ newPtr = new ConfigValSetting; strcpy(newPtr->confsetName, name); newPtr->confsetStringValue = new char[(strlen(value)+1)]; strcpy(newPtr->confsetStringValue, value); /* Prepend in list */ newPtr->nextPtr = *confsetPtr; *confsetPtr = newPtr; } void ConfigValSetting::putNumberValue(ConfigValSettingPtr *confsetPtr, const char *name, const int value) { ConfigValSettingPtr curPtr, newPtr; /* First check if already in list (update) */ curPtr = *confsetPtr; while (curPtr != NULL) { if (strcmp(curPtr->confsetName, name) == 0) { curPtr->confsetNumberValue = value; return; } curPtr = curPtr->nextPtr; } /* Create new Config instance */ newPtr = new ConfigValSetting; strcpy(newPtr->confsetName, name); newPtr->confsetNumberValue = value; /* Prepend in list */ newPtr->nextPtr = *confsetPtr; *confsetPtr = newPtr; } void ConfigValSetting::deleteList(ConfigValSettingPtr *confsetPtr) { ConfigValSettingPtr tmpPtr; while (*confsetPtr != NULL) { tmpPtr = *confsetPtr; *confsetPtr = (*confsetPtr)->nextPtr; delete tmpPtr; } } void ConfigValSetting::printList(ConfigValSettingPtr confsetPtr) const { ConfigValSettingPtr curPtr; curPtr = confsetPtr; while (curPtr != NULL) { printf("Config Setting Name %s\n", curPtr->confsetName); curPtr = curPtr->nextPtr; } }